Running drush entity:updates claimed successful but not changing anything

Question: I am working on a project with custom entity type. My module works fine in my local environment. But when I deployed to my staging site, things are strange.
For instance, I tried to change my entity definition and then run the drush command:
drush entity:updates -y

The command gave me positive response:
The following updates are pending:

dharmasun_payment entity type :
The Field 1 field needs to be installed.
The Field 2 field needs to be installed.

 // Do you wish to run all pending updates?: yes.

 Finished performing updates

Except the database was not changed at all. So when I re-run the command, it gives me the exact same output.
The staging site might have some different module installed than my development environment. But I cannot make breaking change on the staging platform (or my workmates would be mad).
My questions:

How do I begin to debug this? Is there anyway I can get better log messages with the drush entity:update command?
Or is there any alternative way to update entity definition and make changes to database?

Thanks.
Answer: For deploying entity schema updates use an update hook and the entity definition update manager. See the release note of Drupal 8.7.0 and the change record Support for automatic entity updates has been removed.
drush entup is moving from core to contrib: https://www.drupal.org/project/devel_entity_updates

This project aims to restore this functionality as a developer-only
  tool. This module depends on Devel and is not meant to be enabled in
  production environments or relied upon in deployment workflows.

"Or is there any alternative way to update entity definition and make changes to database?"
Yes, there is. Don't use base fields other than for the very basic things that never change (if core makes changes necessary you'll probably find then examples how to write the update hook). Instead use configurable fields (bundle fields), so that you can deploy easily through configuration.
Comment: I tried the `hook_update_N` with `entity_definition_update_manager` approach. The update ran on a `drush updatedb` command, but the database table was not updated at all. No error. Any idea how I can debug this?


Product topology on a product space of normed spaces is normable iff the product is finite

Question: Suppose $(X_{i}, \Vert \cdot \Vert_i)_{i\in I}$ are all normed spaces over the same field $\Phi= \mathbb{R}, \mathbb{C}$ and suppose $X= \prod_{i \in I} X_i$ is the product space. I want to show that the product topology on $X$ is normable iff $I$ is finite. My work in the '$\Leftarrow$'-direction, is to show that the bases for the two topologies are equal and so therefore generate the same topology.
In the other direction however, I seem to get stuck, since I think the right way to go about it is to suppose that $I$ is infinite, but that the product topology on $X$ is normable and try to reach a contradiction, but I can't figure out where the contradiction is supposed to come from?
Answer: I think you can prove it by showing if the index is infinite, then any non empty open set is not bounded (for definition of "bounded" in this case see here
Answer: Suppose for example $\displaystyle  \mathbb R ^\infty = \prod_{i=1}^\infty \mathbb R$ is normable. Then there is an open neighborhood (the unit ball) $B$ of $\overline  0$ such that $\displaystyle \bigcap_{n=1}^\infty\frac{1}{n} B = \{\overline 0\}$. You should prove this if it is not obvious to you. But every neighborhood of the origin in $\mathbb R ^\infty$ contains some set $U = \displaystyle (-\varepsilon,\varepsilon)^N \times\prod_{i={N+1}}^\infty \mathbb R$ and we have $\displaystyle \bigcap_{n=1}^\infty\frac{1}{n} U = \{0\}^N \times\prod_{i={N+1}}^\infty \mathbb R \ne \{ \overline 0 \}$.


What does it mean to serialize/ deserialize a session?

Question: In reading about Rails, I encountered the phrase "rails will read the cookie and deserialize the session", but I don't know what the second part means.  I've googled and searched SO, and found explanations of how to do it.  For example,
# Serialize using Marshal.dump - Binary
serialized_object = Marshal.dump(hello)

# DeSerialize using Marshal.load
hello = Marshal.load(serialized_object)

But I haven't seen an explanation of what is being done or why you would want to do it.
Answer: From further reading, I've gathered that serialization and deserialization in general refer to the conversion of structured data into binary in order to be transmitted electronically (serialization), and undoing that transformation (deserialization).  So presumably serializing/ deserializing a session refers to the session or the aggregate of session data going through that process.


What is the process to get from learning an instrument to playing a song you actually want to play

Question: This question kind of falls under how does one study music, and how that translates to a place where one can than play a song they actually want to play.
I understand that practice makes perfect, but I have a hard time to remmain encouraged to continue studying when it seems I'm playing the same songs over and over again. Is rote repetition of a song really a good way to learn to make music on an instrument or just a good way to memorize a song?
How do I get from clumsily trying to find the notes on my instrument to actually playing a song I want to play? I can sort of see how repeat play of song can teach you where the note is, but is it like language, where you feel like you are saying nonsense for months until all of a sudden it "Clicks" and you can't really explain what is different, or is there a way to get to that point that may help me feel like progress is being made?
How does one measure said progress?
Comment: Get a teacher, and write down a list of songs you love that you would like to learn. The teacher will pick the best ones for your level of ability. A teacher will also increase the speed at which you progress.
Comment: There won't be a click point. You practice the fundamentals and read chord charts, notation or tabs for the song you like and play along to it. Learning any instrument is a never ending process and you can only get better as you learn more
Comment: Maybe this question is too broad. I think it strongly depends on the instrument you want to play (and on the "difficulty" of the song you want to play). But to give a small "guideline" I usually just try to play the song. When I spot difficulties, then I try to get the theory behind it and try to practise the correspondent technique
Comment: If you're playing rock or blues, etc. then learn basic theory, develop some chops, and get out there and **play** with others - **jam** and **experiment** in a context where you're not ashamed if something doesn't work right. Just do it - don't worry about particular songs - just jump in - that's how you learn to **play music.**
Comment: Probably the best thing to do is try to get explicit as you can about your goals. *pick up any guitar book and play any song*? *play with others, regardless of genre*? Gimme a lil help here :-)
Answer: Tim's pretty much got it there, but I just wanted to add there I don't really think there is just one way you should learn. I've been playing guitar for over 10 years now, and I've met all sorts of different musicians who've learnt in different ways.
It all comes down to personal preference, which can be daunting for a beginner because you don't know what you prefer! My opinion would be to find a good teacher, of whatever instrument you're playing, who you enjoy playing music with! Some people will tell you that you absolutely should learn to play by learning the grades of guitar/piano/whatever, and getting your grades 1 to 8 and the tests passed, however I learnt with a teacher who said no, let's not do that, let's just play the music you want to play and I'll teach you the techniques as when they're needed and you're capable.
It's a tough road, and trust me, there'll be times where you want to give up, but please, please keep at it, whatever way you end up learning, it'll be worth it in the long run!
Answer: To an extent, it will rely on many different factors. How musical the student is, what other musical experience he has, what instrument it is, whether he's learning from music are several of them.
Rather like a child reading from a book, a beginner can sound stilted, and not play fluently. If it's just one piece, the more times it's practised, the smoother it becomes. If that learner has some musicality anyway, this should come sooner.
The instrument will have some bearing for many reasons. A piano, for example, will play in tune on the right notes(!). Compare with a violin which will play 'in the cracks' and not sound conducive to being in tune. A guitar in tune will generally play notes in tune, which can be bent out of tune by beginners (and experienced players - that's another story!).
Learning from the music presents two tasks - reading the dots and finding them on the instrument.Simultaneously. So the process for a beginner is pretty tricky.
Once several notes are known, you can start to try to play tunes by ear. This will underline the fact that certain notes belong together - they're scales in keys.
Learning tunes is always the books' favourite. But once a tune is learned by heart, that's it. No - try playing it quickly, slowly, quietly, loudly, with another player, etc.
Rote repetition of a song isn't necessarily the best way to learn. On guitar, for instance, play the same tune in different keys, assuming there are no open strings.Once that tune is learned, move on. Visit it a week later - if you can't play it properly - you haven't learned it! Play without the dots. Lots of tunes in books are short, so in the early stages, several tunes will be tackled quickly. Probably better than trying to learn a two page song. That comes later.
The answers to this question are going to be diverse, I hope. This is a start.
Answer: I'm all for studying with teachers, but if you want to take ownership of your musicianship you should also spend some time in self-directed study.  Play along with the music you like to listen to on your iPod or whatever you use.  You will feel lost at first but keep at it.  Try different things and listen for whether it sounds good.  Study chord progressions.  Don't focus on mastering a song at a time but rather experiment with a playlist.  Repeat each tune 4 or 5 times and move on, you can revisit individual tunes the next day.  I find that having a big playlist and putting it on shuffle, playing along for an hour or so each day, works best.  After a few weeks of this you will start to develop a sense of how to play along without having music dictated to you.  Hope this helps - the important thing is to have fun with it.
Comment: I like this answer a lot - I was going to answer something similar. You need to mix it up and focus more on the big concepts in theory than on individual songs, and play along with and experiment with music you like. Using playlists with different genres, styles and favorite songs to play along with are a great way to learn how to play real music.
Answer: The important things about practising stuff is improving and learning.  One experiment worth trying is taking it down three notches: pick out some stuff you already mastered some time ago.  Now play it perfectly.  Once you play it perfectly, record it and listen.  Anything to improve left?
You'll find as you retry "really easy" stuff and/or listen to recordings of it that this leaves more of your attention for actually listening into what you do rather than spending all your focus on producing it.
It turns out that there is a lot more to do than hitting all of the right notes at approximately the right time.  Practising on the same stuff repeatedly will at some point of time free mental capacity for working on those aspects.
The problem is that to dial back the level at which you are playing requires reaching some reasonable level first.  And if you don't see the point on repeatedly working on the same stuff, chances are that you are not actually developing an idea what can be improved.
Most of the improvement you attain happens while you are practising rather than under supervision.  So the teacher does not really know in advance what might or might not make something "click" with you.
Comment: what are 'three notches'?
Answer: Not sure what instrument you are trying to learn but I am a big fan of learning to play an instrument by learning to play songs you personally like as opposed to learning "When The Saints Go Marching In" because it's in a lesson book.  Learning an instrument takes dedication and practice and it is easy to lose interest if you are only learning to play songs you have no interest in.  If you are taking lessons and your teacher is not open to teaching you how to play simple beginner arrangements of songs you want to play, you might want to consider a new teacher.  If you are trying to learn on your own, you might find useful videos on You Tube that teach you how to play particular songs.  Just go to You Tube and type {Name of Song} guitar (or piano or flute) lesson in the search bar.  You might have multiple versions to choose from so pick one that seems more on your level and if not that song, try another that you are interested in learning.  Or find someone who plays the instrument you are learning and ask them to create a simple arrangement for some songs you like and show you how to play them.  Good luck.
Comment: How did you know that I could play "When The Saints Go Marching In" on anything, even though I kind of hate this song?
Comment: @Mallow Seems to be a song taught in many beginner music instruction books.  The only time I ever hear it is when I'm in a music store and a student is playing it.


How can I split a midi file programatically?

Question: Im using the mido library on python to work with the midi file.
Given any midi file, I would like to output a midi file consisting only of the first 10 seconds of the input. I don't see any functions to do this in the library, so im trying to do this through going through each of the tracks.
Im finding a few things confusing. For example, below is what you get when you examine a single track in your midi object.
<meta message track_name name='Slow Strings' time=0>
program_change channel=0 program=49 time=0
program_change channel=0 program=49 time=0
control_change channel=0 control=7 value=71 time=0
control_change channel=0 control=10 value=75 time=0
note_on channel=0 note=55 velocity=89 time=15360
note_on channel=0 note=59 velocity=95 time=0
note_on channel=0 note=62 velocity=92 time=0
note_off channel=0 note=55 velocity=64 time=1324
note_off channel=0 note=62 velocity=64 time=12

So do I go through this list of messages in the track and count the time attribute until the running sum exceeds 10000 (milliseconds)? I don't think so, because looking at the piece on musescore, nothing significant haappens 15.36 seconds into the song.
I read here, that the times aren't in milliseconds, but delta times, which must be converted to milliseconds through PPQ (pulses per quater note). Is this correct, and if so is using a running sum of time the right approach to creating an output midi of time 10 seconds?
Comment: See: https://sites.uci.edu/camp2014/2014/05/19/timing-in-midi-files/
Comment: Does it have to be within python...? You could simply go to http://midi.mathewvp.com/midiTrim.htm upload the whole midi file, type 0 to 10 seconds and download the trimmed version again. This may be the fastest way, but you could also do this within any DAW if you want.
Answer: The times are not in milliseconds, they're in midi ticks which you can convert to seconds with the function tick2second(ticks, ticks_per_beat, tempo). Your code should look something like this:
from mido import MidiFile
from mido import tick2second

mid = MidiFile('input.mid')

for i, track in enumerate(mid.tracks):
    total_time = 0
    for msg in track:
        if msg.type == 'set_tempo':
            tempo = msg.tempo
        total_time  += tick2second(msg.time, mid.ticks_per_beat, tempo)
        print(msg, total_time) # or copy to output file
        if total_time > 10:
            break

Note: this assumes there are no tempo changes in the first 10 seconds of your input files. If there are your code will need to be more complex.
Answer: If you're OK with C# instead of Python, you can use DryWetMIDI library. The code to take first 10 seconds of a MIDI file is super easy:
var midiFile = MidiFile.Read("My MIDI file.mid");
var newMidiFile = midiFile.TakePart(new MetricTimeSpan(0, 0, 10));
newMidiFile.Write("10sec version.mid");


Rails Serialization - fast_jsonapi / active_model_serializers

Question: I am a bit lost getting on board fast_jsonapi / active_model_serializers to build an API.  I have the basics down but seem stuck on a custom solution.
I have this as a serializer:
class AreaSerializer
  include FastJsonapi::ObjectSerializer
  attributes :id, :name, :cost_center, :notes
  has_many :children
end

In my area model I have:
  has_many :children, -> { Area.where(ancestry: id) }

My controller looks like:
class Api::V1::AreasController < ApiController

  def index
    render json: AreaSerializer.new(Area.root).serialized_json
  end

end

Areas are nested in a hierarchy with the ancestry gem.  The output is:
{
"data": [{
    "id": "1",
    "type": "area",
    "attributes": {
        "id": 1,
        "name": "Calgary",
        "cost_center": "123456",
        "notes": ""
    },
    "relationships": {
        "children": {
            "data": [{
                "id": "3",
                "type": "child"
            }]
        }
    }
}, {
    "id": "2",
    "type": "area",
    "attributes": {
        "id": 2,
        "name": "Edmonton",
        "cost_center": "78946",
        "notes": ""
    },
    "relationships": {
        "children": {
            "data": []
        }
    }
}]

}
I am looking for an out put like this:
{
"data": [{
    "id": "1",
    "type": "area",
    "attributes": {
        "id": 1,
        "name": "Calgary",
        "cost_center": "123456",
        "notes": ""
    },
    "relationships": {
        "areas": {
            "data": [{
                "id": "3",
                "type": "area",
                "attributes": {
                    "id": 3,
                    "name": "Child Area",
                    "cost_center": "123456",
                    "notes": ""
                }
            }]
        }
    }
}, {
    "id": "2",
    "type": "area",
    "attributes": {
        "id": 2,
        "name": "Edmonton",
        "cost_center": "78946",
        "notes": ""
    }
}]

}
The idea being where the nested relationship shows the details etc.
Answer: I just started using fast_jsonapi in my rails project.
fast_jsonapi adheres to JSON:API spec which you can see here.
So, you will not be able to use the relationship helper functions (:has_many, :belongs_to) to achieve the output you want, which is nesting the attributes of area inside the relationships key.
"relationships": {
        "areas": {
            "data": [{
                "id": "3",
                "type": "area",
                "attributes": { // nesting attributes of area
                    "id": 3,
                    "name": "Child Area",
                    "cost_center": "123456",
                    "notes": ""
                }
            }]
        }
    }

JSON:API specifies that:

To reduce the number of HTTP requests, servers MAY allow responses
  that include related resources along with the requested primary
  resources. Such responses are called "compound documents".

So instead, you will have the attributes of area inside a key called included.
In order to get the included key in your response, you will need to provide an options hash in your controller to the serializer.
I don't quite understand your model Area relationships, but assume an Area has many SubArea.
class Api::V1::AreasController < ApiController

  def index
    // serializer options
    options = {
      include: [:sub_area],
      collection: true
    }
    render json: AreaSerializer.new(Area.root, options).serialized_json
  end

end

Answer: You Cannot use Association in fast_jsonapi . To Get a response in nested format . You need to add methods and need to create another serializer .
class AreaSerializer
  include FastJsonapi::ObjectSerializer

  set_type 'Area'

  attributes :id, :name, :cost_center, :notes

 attribute :childrens do |area, params|
     ChildrenSerializer.new(area.childrens, {params:
     params})
  end
end

class ChildrenSerilizer
   include FastJsonapi::ObjectSerializer
    set_type 'Area'
    attributes :id, :name ...
end

Answer: I started using the technique listed above but ended up forking and re-writing the jsonapi-serializer gem so it allows nesting (up to 4 levels deep) and does away with the concept of having relationships and attributes keys.  I was also frustrated that it only output ID and TYPE keys of which TYPE is redundant most of the time since the object typically stays the same class in an array as an example and people seldom use real polymorphism (although it still supports this and outputs ID/type for polymorphic relationships).
Probably the best part of my re-write is that it allows deterministic field select-ability anywhere within the json key tree via a new fields input.
https://github.com/rubesMN/jsonapi-serializer


Custom breadcrumb on user profile page

Question: I am using profile2 module for my User profiles.
Now breadcrumbs on user profile pages are:

Home > Username

I need breadcrumbs to be like this:

Home > Custom page > Custom profile2 field

How can I change it? Thanks for your replies.
Comment: What is going to be the url of "custom page" and "custom profile2 field" ? Is "custom page" a static url, or does it change per user?
Comment: Custom page url is View page. And custom profile2 field is without url (it would be the same like Page title, which I managed to change in template).
Comment: i don't really get it. what page/path are you visiting where you want to see the breadcrumb you describe?
Comment: I am on user profile... so path alias of users/id. Profile2 content is on user profile page.
Comment: ok, so what has profile2 to do with it?
Comment: There is a field from profile2. Do you know how to change breadcrumb of user profile page?
Comment: i am going to post an answer similar to http://drupal.stackexchange.com/questions/106048/listing-page-in-breadcrumb-of-single-item. But I would recommend that you flesh out the question a bit more, so that it is useful for other visitors coming from google.
Answer: You can use Path Breadcrumbs module. This module helps you to create breadcrumbs for any page with any selection rules and load any entity from the URL.
If this module is not satisfy your requirements, then visit Comparison of Breadcrumb Customizing modules to get more information.
Answer: In Drupal 7 you can use MYTHEME_breadcrumb function in template.php file, if you don't want to use any contributed modules.

    /**
     * Preprocess function to customizing the breadcrumbs
     * @param type $variables
     * @return string
     */
    function MYTHEME_breadcrumb($variables) {
        $breadcrumb = $variables['breadcrumb']; //get the breadcrumb
        if (!empty($breadcrumb)) {
            if (arg(0) == 'user' && is_numeric(arg(1))) {//Check your condition based on your requirement for user profile
                $breadcrumb[] = l('Custom page text', 'Custom page link');
                $breadcrumb[] = t('Custom profile2 field text');
                //$breadcrumb[] = l('Custom profile2 field text', 'Custom profile2 field link');//if you want link use this
            }
            $output .= '' . implode(' » ', $breadcrumb) . '';
            return $output;
        }
    }

Comment: Thanks, it workds. But how can I get the Custom profile2 field in this function?
Comment: You can use [profile2_load_by_user](http://drupalcontrib.org/api/drupal/contributions!profile2!profile2.module/function/profile2_load_by_user/7) API to get the custom Profile2 fields
Answer: Answer similar to https://drupal.stackexchange.com/a/106057/2974, using Crumbs.
After installing the module:

Go to admin/structure/crumbs/entity-parent/user, and specify the views page as the parent path for 'authenticated user' (this will cover other roles too, because every user is authenticated).
On admin/structure/menu, edit the main menu (or another menu), create an item that links to the views page, and give it a title like "List of users". (*)
On admin/structure/crumbs, make sure that the menu.link_title.main-menu and menu.hierarchy.main-menu are enabled. Or just enable menu.*.
(enabling means to move those lines up into the "Enabled" section with drag+drop)
To see what going on behind the scenes, you can visit admin/structure/crumbs/debug.

EDIT:
This might not get you the modified title for the last breadcrumb item.
If you want that, you can use hook_crumbs_plugins() or hook_menu_breadcrumb_alter() to change that part. Maybe this should be covered in a separate question, "How to to change the title of a breadcrumb item in Crumbs programmatically?".
Note:
It could be that in your particular case, Path breadcrumbs is the easier option, as it allows to do all via the UI. Crumbs might improve on that in the future, but for now it is what it is.
Comment: Hm, thanks, but I can't use Crumbs. It breaks all my settings from custom breadcrumbs and I can't make it work with Crumbs.
Comment: If you already use custom breadcrumbs, then I think it is easier to switch to path breadcrumbs from there, instead of switching to Crumbs.
Comment: But even if you don't end up using it, I am still interested in feedback about stuff that cannot be done with Crumbs :)
Comment: Crumbs look really interesting, but now I don't have time to learnt it :/. Custom breadcrumbs module doesn't support profile2.
Answer: You can use yourtheme_breadcrumb hook to change breadcrumb for specific use case. Look at https://api.drupal.org/api/drupal/includes!theme.inc/function/theme_breadcrumb/7

Custom breadcrumb on user profile page


SBS Server 2008 with periodic lsass.exe using 100% of 1 Core

Question: SBS Server 2008 with everything running on it (AD/PDC, SQL 2012, FileServer, RDP Gateway, WSUS, No Exchange though).
Every 30 minutes on the hour and half hour, lsass.exe uses 25% of the CPU (1 core at 100%). At this time it's reading/writing from ntds.dit, edb.log, edb.chk. This lasts for approx 2 minutes during which it affects the performance of the server (obviously).
Has anyone got any idea what is causing this?
I've already gone through Task Scheduler looking for a culprit and it's not there. Next step will be checking Group Policy running schedule, it's possible it's set to every 30 minutes, but lsass is not involved when I run gpupdate manually and gpupdate completes in 5 secs or so with no CPU use.
Any other ideas what to look for?
Comment: As an aside, SQL Server (Express) jumps to 25% CPU use for 2 seconds, twice during this lsass problem. This happens whether or not it's under significant load from users.
Answer: OK I figured it out.
lsass.exe is clearing the Locked Accounts and failed login history. For some reason this takes 1 entire CPU at 100% for almost a minute.
This is controlled by Group Policy in the Windows Security Settings under Account Lockout. I had it set to 30/30 and when I changed it to 45/45 it started happening on 45 mins past the hour. I now have it run once daily only.


Does race condition(lost update or write skew) happen in Django admin?

Question: In Django views, we can use select_for_update() to prevent race condition(lost update or write skew) so race condition doesn't happen in Django views with select_for_update(). *I used Django 3.2.16.
But, even though I googled, I couldn't find any information saying "in Django admin, race condition doesn't happen or select_for_update() is used to prevent race condition".
So, in Django admin, does race condition happen?

If yes, are there any ways to prevent race condition in Django admin?

If no, is select_for_update() or other way used to prevent race condition in Django admin? and can I see the code for me?
Answer: By default in Django Admin, lost update or write skew caused by race condition can happen because select_for_update() is not used. *My answer explains lost update and write skew.
So, I wrote the example code with select_for_update() to prevent lost update or write skew in Django Admin as shown below. *I used Django 3.2.16 and PostgreSQL:
<Lost update>
For example, you create store_product table with id, name and stock with models.py as shown below:
store_product table:

id
name
stock

1
Apple
10

2
Orange
20

# "store/models.py"

from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=30)
    stock = models.IntegerField()

Then, you need to override get_queryset() with select_for_update() in ProductAdmin(): as shown below:
# "store/admin.py"

from django.contrib import admin
from .models import Product

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):

    def get_queryset(self, request):
        qs = super().get_queryset(request)

        last_part_of_referer = request.META.get('HTTP_REFERER').split('/')[-2]
        last_part_of_uri = request.build_absolute_uri().split('/')[-2]

        if (last_part_of_referer == "change" and last_part_of_uri == "change"):
            qs = qs.select_for_update()

        return qs

Then, if you change(update) product as shown below:

SELECT FOR UPDATE and UPDATE queries are run in transaction according to the PostgreSQL query logs as shown below. *You can check how to log PostgreSQL queries:

And, if you don't override get_queryset() in ProductAdmin(): as shown below:
# "store/admin.py"

from django.contrib import admin
from .models import Product

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    pass

SELECT and UPDATE queries are run as shown below:

<Write skew>
For example, you create store_doctor table with id, name and on_call with models.py as shown below:
store_doctor table:

id
name
on_call

1
John
True

2
Lisa
True

# "store/models.py"

from django.db import models

class Doctor(models.Model):
    name = models.CharField(max_length=30)
    on_call = models.BooleanField()

Then, you need to override response_change() with select_for_update() and save_model() in DoctorAdmin(): as shown below. *At least one doctor must be on call:
# "store/admin.py"

from django.contrib import admin
from .models import Doctor
from django.db import connection

@admin.register(Doctor)
class DoctorAdmin(admin.ModelAdmin):

    def response_change(self, request, obj):
        qs = super().get_queryset(request).select_for_update().filter(on_call=True)
        obj_length = len(qs)

        if obj_length == 0:
            obj.on_call = True
        obj.save()

        return super().response_change(request, obj)

    def save_model(self, request, obj, form, change):
        last_part_of_path = request.path.split('/')[-2]

        if last_part_of_path == "add":
            obj.save()

Then, if you change(update) doctor as shown below:

SELECT FOR UPDATE and UPDATE queries are run in transaction but I don't know how to remove the 1st SELECT query in light blue as shown below:

And, if you don't override response_change() and save_model() in DoctorAdmin(): as shown below:
# "store/admin.py"

from django.contrib import admin
from .models import Doctor

@admin.register(Doctor)
class DoctorAdmin(admin.ModelAdmin):
    pass

SELECT and UPDATE queries are run as shown below:

For example for write skew again, you create store_event table with id, name and user with models.py as shown below:
store_event table:

id
name
user

1
Make Sushi
John

2
Make Sushi
Tom

# "store/models.py"

from django.db import models

class Event(models.Model):
    name = models.CharField(max_length=30)
    user = models.CharField(max_length=30)

Then, you need to override response_add() with select_for_update() and save_model() in EventAdmin(): as shown below. *Only 3 users can join the event "Make Sushi":
# "store/admin.py"

from django.contrib import admin
from .models import Event
from django.db import connection

@admin.register(Event)
class EventAdmin(admin.ModelAdmin):

    def response_add(self, request, obj, post_url_continue=None):
        qs = super().get_queryset(request).select_for_update() \
                                          .filter(name="Make Sushi")
        obj_length = len(qs)

        if obj_length < 3:
            obj.save()

        return super().response_add(request, obj, post_url_continue)

    def save_model(self, request, obj, form, change):
        last_part_of_path = request.path.split('/')[-2]

        if last_part_of_path == "change":
            obj.save()

Then, if you add event as shown below:

SELECT FOR UPDATE and INSERT queries are run in transaction as shown below:

And, if you don't override response_add() and save_model() in EventAdmin(): as shown below:
# "store/admin.py"

from django.contrib import admin
from .models import Event

@admin.register(Event)
class EventAdmin(admin.ModelAdmin):
    pass

Only INSERT query is run as shown below:

You can also see my posts below about SELECT FOR UPDATE in Django:

How to run "SELECT FOR UPDATE" instead of "SELECT" when adding data in Django Admin?

How to run "SELECT FOR UPDATE" instead of "SELECT" when changing and deleting data in Django Admin?

How to run "SELECT FOR UPDATE" for the default "Delete selected" in Django Admin Actions?
Answer: Yes, a race condition can occur in Django admin if multiple users try to update the same object at the same time. This can result in lost updates or write skew, where the final value of the object is not what was intended. To avoid this, Django admin uses optimistic locking to ensure that updates are performed safely and consistently. This means that if two users try to update the same object simultaneously, one of the updates will be rejected and the user will need to retrieve the latest version of the object and try again.
Comment: I couldn't find such information `Django admin uses optimistic locking to ensure that updates are performed safely and consistently` in Django documentation. Are there such information in Django documentation?


wildcard ssl for subdomains

Question: I generate my ssl keys via openssl use this commands:
openssl req -passin pass:3a1b -new -key server.key -out server.csr;
cp server.key server.key.org;
openssl rsa -passin pass:3a1b -in server.key.org -out server.key;
openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt;

Also i add my localdomain as Common Name set to *.localdomain.sweb. but when user accept certification for domain www.localdomain.sweb domains another.localdomain.sweb also need to be accepted.
How can i set all sub domains as valid certificate when user accept main domain certificate.
Comment: I think they mean "when user accepts the self-signed certificate despite its being self-signed"
Comment: What do you mean by "*when user accept[s] certification for domain ... [another] domain also need[s] to be accepted*"?
Answer: The answer is simple: DO NOT USE SELF-SIGNED CERTIFICATES.
Your users' browser is (quite correctly) insisting that they accept an invalid (self-signed/unknown authority) certificate for each domain. This is because the browser has no way of knowing that the certificate they're accepting for www.localdomain.sweb is also valid for another.localdomain.sweb (because there's no valid signing authority attesting to the certificate).
If you acquire a proper certificate signed by a recognized certificate authority (or alternatively establish your own CA and distribute its public key to your clients as a recognized certificate authority) the browser will trust it for all domains in the wildcard, because it's properly signed, and therefore duly authorized (since presumably the CA performed some validation before issuing the certificate).

wildcard ssl for subdomains


What is the best way of presenting many Bar Chart plots

Question: I used R programming to produce separate 30 Bar Chart plots. The x-axis scale is the same for the 30 plots (11 bins), but y-axis is different.
I want to present these results in a limited work of pages.
Is there a way, for example, a plot or any ideas that could help me to present the results instead of those 30 plots.
Comment: Please explain what is plotted on the x-axis and what is plotted on the y-axis.  What role do "bins" have for plotting bar charts?  Indeed, could you state what you mean by a "bar chart"?  Are you using that as a synonym for "histogram" (which it is not)?
Comment: Can you post example data for people to work with? You can get really nice answers that way. You also need to provide some context, eg, what the variables mean & what you want to learn from them / show about them.
Answer: Make 30 different plots, assign them each to their own object. Then arrange them in a 6 by 5 grid using gridExtra::grid.arrange() or cowplot::plot_grid()
Comment: This would be a great answer if the question were posted on [SO], but here we are expecting answers that address the second part of the question: "any ideas that could help me to present the results *instead* of those 30 plots.".


How to calculate result from fields inserted?

Question: I'm new to Drupal. Can anyone advice me on what's the method of collecting data information from inserted results?
For example, suppose I have book content type, when I insert data into its fields I want a page to show results of the following:

How many book has been inserted? (eg. 5 books)
How many magazine? (eg. 2 magazine)
How many references?(eg. 1 reference)
How many novels? (eg. 2 novels)
How many guide book? (eg. 0 guide)

I've been trying to figure this out through and my last option which is to code the result myself(the hard way).
Comment: First you describe how to define magazine, references, novels, guide book in your book content type
Comment: it would be a field which will give an option of choosing the type of book the user is inserting.
Answer: Such page can be creating using various approaches, as further explained below.
Using Views and Views Calc
Use the Views module, together with the Views_Calc module. Here is a quote about Views_Calc (from its project page):

... adds simple calculations to a views table. Requires the Views module.
You can SUM, AVERAGE, COUNT, MIN, MAX, or AVG selected columns. A plug in theme makes the view look more spreadsheet-like (right justifies numeric fields, shades calculated columns and rows, and underlines calculated rows).
You can also create custom dynamic fields that can be used in any view using SQL snippets like CONCAT('field_first_name', ' ', 'field_last_name') or DATE_FORMAT(node.created, '%m/%%d/%Y'). The custom fields are set up in admin/settings/views_calc.

Refer to the question about How can I show average votes of fivestar nodes? for some screenprints about how to use this module.
Using Forena
If you'd want to "roll your own cookies" (i.e. some report that perfectly fits your custom requirements), then you might want to consider the Forena module. Here is an excerpt about that from its Community documentation:

Forena Reports is built of the idea of using SQL to get data out of a database and use XHTML and CSS to format it into web reports.
It is designed to leverage existing knowledge of HTML, CSS, SQL and JavaScript to help you create rich interactive web reports.

For an example of such custom report, created with Forena, start from the statistics (summary info) of the Watchdog Statistics, as in this Watchdog Statistics sample. Note the red numbers to the right of the table, which are actually hyperlinks that can be used as drill-down reports. I.e. if you click on any of them (like the "2" for "Page not found"), you get a details report about (in this case) the 2 specific records related to the "Page not found"). More details about this specific case can be found in comment #2 of issue 2337327. Obvious, I didn't really do any effort to beef up the look-and-feel of that report, like background, etc.
Disclosure: I'm a co-maintainer of Views_Calc and Forena,
I hope this does not violate the site's policy on self-promotion.
Comment: oh man, i've been spending a whole week on the net trying to figure out this through modules and stuff before my last option which was coding the result (the hard way)..i never came across this module from my research.. forena seems bit complex than views_calc from your explanation, i think this would do the job....and YES this is exactly what i was looking for! God bless u man! Thanks :D
Comment: oh my apology, i am new to this website and i was trying to upvote (which didnt work)but didnt notice the "accept answer", no questions so far. Thank you for the notes.
Comment: No need for apology, this website takes a little time to get used to all its features, rules, etc. If you have other questions: keep asking them (by creating new questions). The more you ask, the more ways there will be to receive "points" for asking, accepting answers, etc. And you only need 15 for being allowed to "upvote". Keep in mind: I did not invent those rules, I also find a few of them rather strange, but that's how things work around here ...
Comment: yeah, i can see that. im starting to get used to it.
Question, would these modules be used to labeled fieled? i mean, in my question, i wanted the result to be displaying the book types(which will be taken from taxonomy terms) like novel, magazine. thanks


How to calculate this mass.

Question: Determinate the mass of portion of spherical surface given by the equation $x^2+y^2+z^2=2a^2$ who is inside of the cylindrical surface given by the equation $(x^2+y^2)=2a^2(x^2-y^2)$, knowing that the density at the point $P(x,y,z)$ is given by $\rho(x,y,z) = k(x^2+y^2+z^2)$.
I don't know how to find the integration limits, because the graphic is so strange :( If anyone could help me I would really appreciate it.
Answer: As you are interested, we are on the way of finding the proper limits. First of all, note that there is a nice symmetric throughout the equations so, I restrict myself to consider the $x>0,y>0,z>0$. If you have a nice imagine, you'll find out that we should multiply the result by $8$. Working on $$(x^2+y^2)=2a^2(x^2-y^2)$$ gives us $$y/x=\sqrt{\frac{2a^2-1}{2a^2+1}}=k$$ so we found a nice range for $\theta$: $$\theta|_0^{\arctan{k}}$$ Other limits are easy to find. Indeed, $$\rho|_0^{\sqrt{2}a},~~\phi|_0^{\pi}$$
Comment: @Babak I shall! You too, my friend!
Comment: @amWhy: Thanks. Have a shining day Amy. :-)

How to calculate this mass.


Is it recommended to practice sight reading by "feeling out the music" instead of counting

Question: I've been learning how to sight read the traditional way, by counting. Unfortunately, if there's ever a rest that less than a eighth rest, I automatically get thrown off, mostly because I'm only counting the parts that I play in, not the parts that I don't. However, I've been doing this thing lately where I just kind memorize the rhythm patterns, and rely on intuition rather than actually counting (for example, taking in a breath whenever I come across an eighth note rest) My question is, is this a good approach or should I still just try counting? I've had more success with intuition than straight up counting, but that could just be because I'm missing something.
Thanks in advance,
Wow, I didn't expect to get this kind or response, its also a bit embarrassing because of the stuff some people pointed out, so yeah, its obvious I still have a long way to go.
I've learned quite a bit from reading through the responses, and I'm going to try to improve my practicing from now on. Thanks everyone, and sorry for any confusion.
Comment: "... I automatically get thrown off, mostly because ..." Old joke: "Doctor, it hurts when I do this!". "Well, stop doing it then!" If you don't count correctly, then obviously you will be thrown off. So what's the real question? If it's "is counting correctly better than just guessing", the answer is pretty obvious...
Comment: You may be interested in this video about how classical musicians tend to take an analytical approach (counting and "reacting") whereas jazz and other non-classical musicians "feel" rhythm: https://youtu.be/rEbUNDW9bDA
Comment: I'm not sure what you mean by "I'm only counting the parts that I play in." It sounds like most people here are interpreting that to mean that you're not counting rests at all, but if that were the case it seems you'd be more confused by *long* rests, not short ones. Also, failing to count rests seems like a pretty basic mistake, and one that you'd correct once you realize you're doing it (no offense if that's what you're actually doing--it is also a pretty *common* mistake).
Comment: Not enough for an answer, but I have had several situations in my upper level college choir (Emmy Award winning), where every person in the choir assumed the rhythm when practicing and had to fix it later in group practice. It took weeks to fix what would have taken a week or less to learn properly. But since everyone had used their intuition, they got the wrong rhythm stuck in their head. Moral: Always count, no matter how good you get, when you are sight reading anything. Some composers will do things that you would do differently, and suddenly your intuition is just wrong.
Comment: Have you tried the standard "One-and,"  "One-Eee-and" , "One-ee-and-ahh"  methods for tracking subdivided beats?
Comment: Addressing your parenthetical comment: *" (for example, taking in a breath whenever I come across an eighth note rest)"* -- This is a VERY bad habit to get into. It will invariably make you late at faster tempos. Instead, learn to make the rest part of the rhythm pattern, just like a note of the same length, but one that doesn't make any sound.
Answer: Yes, with reservations.
If I encounter a bar with four crotchets, I don't count them. I know what it's going to sound like; I don't need to go 'one, two, three, four'.
If I encounter a bar with a dotted quaver, a semiquaver tied to a quaver, and a quaver, I don't count that either. It's a more complicated rhythm, but I've played it so many times I know how it sounds. Counting distracts me from more important things like tone and inflection.
So, if you know what a rhythm sounds like, you don't need to count it. It's kind of like a scale. If you see a series of notes that's part of a scale in a piece of music, you probably aren't going to read every individual note. You'll know what to play. You can do the same thing with rhythm.
Now for the reservation. You do still need to be able to count, and to know when you don't know what something sounds like. If I put my arranger's hat on, I can tell you that it's quite annoying when someone plays rhythms incorrectly because they think they know what it sounds like, and don't actually read what I wrote.
So, in summary, feeling rhythms (rather than counting) is natural, and can be desired. But be careful to feel the actual rhythm, not the one you think is there.

Also, as an aside, counting rhythms properly requires you to count the rests as well. If you don't, you'll get it wrong.
Comment: There can be found specific exercises for developing "rhythm vocabulary", where you learn to recognize the rhythm pattern much like recognizing a word rather than individual letters.  There is a rhythm vocabulary workbook that I often give to students to help them with sight reading.  It does require that you count the exercises first until you  begin to recognize the patterns.
Comment: this is a good answer, but it's worth noting that it sounds like OP doesn't actually know how to count rhythms.  As you mentioned, "only counting the parts that I play in" doesn't really make any sense
Answer: It's like everything else we learn and do in life!
Start by counting. I've played with top musos, and each time a new piece comes along the watchwords are 'Get your head down and count like hell'. So even they rely on this!
Eventually, we reach a point where we've met pretty well all of the more usual rhythmic patterns, and recognise them, maybe not conciously, but more automatically. It's like when we see 'wheelbarrow' we remember the shape of the word so don't have to spell it out each time.
On to what I think is your main tripping up point. Rests make no noise! So, there's nothing to count! Well, make something! My old violin teacher, bless him (Cyril Perfect, Christine McVie's dad,Fleetwood Mac, fwiw...) would often sniff to 'play' a rest in a piece. Trouble was, it sometimes followed him into performances...  But, it worked as there was always something tangible wherever there was a rest. I tend to nod the head, shrug the shoulders, or suchlike. But do something.
Answer: Some people block when they try to execute a rhythmically rich passage while counting out the beats and the sub-beats.  You may be one of those people.  If you are then you may want to learn rhythmic solfege.
When you have a tricky passage use a metronome for the rhythmic pulse and chant or sing the rhythmic solfege for that passage until you can hear it in your head without chanting.  Then play the passage, still using the metronome.  Record your chanting of the solfege and your playing of the passage to ensure that you are playing it correctly.
There are several systems of rhythmic solfege but one that seems to be popular right now is Takidimi although the Kodaly method is often used as well.
Comment: [related question](http://music.stackexchange.com/q/9530/1344)
Answer: Count. Practice counting. Eventually (not too long) counting becomes second nature and needs no conscious effort.
Comment: Is 45 years "not too long"?  I've publicly performed everything from the Goldberg Variations to Michael Finnissy, but last week when I took a stab at some easy-peasy Sweelinck, my rhythms were a train wreck.  *Never* stop practicing counting.
Answer: The basic instance where counting makes sense when sight-reading is with long notes or pauses in orchestral contexts.  Solo pieces tend not to remain quiet for such long durations.  I rarely count anything.
Here is an example where this actually caused some consternation for me: this is a multitrack recording where I put a third voice (1, 2, 4 are in the "live" take) in after the fact.  Only when doing that I discovered that the last note before the second repeat was two beats instead of four.  All the previous versions were solo takes and this problem was inconspicuous at least to me.  The moment I tried to do this in "orchestral context", it blew up around my ears.
Took several tries until I could rush through the third voice here similarly without it becoming too obvious.
Even when playing stuff solo, listeners acquainted to a piece will get thrown off when stuff like this happens, and it also disturbs the overall structure of the piece for people new to it.
So you want to avoid mishaps and take whatever measures you think appropriate to keep them from happening.  The more acquainted you are to score reading, the less your sightreading skills will rely on means like counting.  And the fuller the score is and the more parallel elements are structuring the music, the less likely blunders like two missing beats are likely to happen.  And often the music makes enough "sense" of its own that it is easy to "autocorrect" mistakes.
The last time I remember that being completely useless and merciless counting being necessary was in chamber sonates by Corelli because here the leading voices in a number of movements were wrong when you fell into the trap of synchronizing them: (I think some Corrente was particularly tricky): basically they needed to keep their melodic structures offset by some changing distance.
Putting the voices together was really a counting orgy.  You couldn't "intuit" your way through that one at all.  Not on sight.
Answer: What is "intuition"?  Maybe you just chose a poor word, but that sounds like guessing.
You're correct that you don't want to expend the brainpower to actively count every single note.  You want to learn to recognize chunks of music, especially regarding rhythms within a beat.  Practice reading various subdivisions of a beat until the patterns become automatic.  There are lots of books with rhythm practice exercises.
Comment: It doesn't sound like guessing to me. My understanding is that he's referring to being able to recognize shapes and play them accordingly. When I type `:-)` you recognize it as a face *intuitively...* (even though it hardly shares any resemblance to an actual human face) Similarly, a dotted eighth and a sixteenth has a pretty "intuitive" look/feel. I would differentiate between intuition and guessing.
Comment: @GeneralNuisance - might be true. But it took me ages to work out that :-) was supposed to be a smiley face. But sideways.
Comment: @Tim Well, yes, I had to have it explained to me a few times over before it stuck. My point with this is instant recognition and processing. An even better example is the English language, which has more sight-words than almost any other language.
Answer: Not instead of counting, no, but it shouldn't be a question of either one or the other; You should almost certainly be trying to 'feel' the music for the sake of giving a performance that sounds like the right feel; Likewise, if you really don't know a tune, you're also going to need to be reading the music.
In this case, it sounds like your musical feel is acting like a useful check on your reading - but the thing to do is probably to is to work on the identified reading weakness.
Answer: Every system of rhythmic instruction comes down to two basic things:  some form of counting system, and some form of "learned intuition" or "muscle memory".
When we first start to learn rhythm, we might be banging a stick on a block or clapping our hands...1...2...3...4.  After a while, our brain starts to remember the pattern and we don't really need to count anymore.  But when we encounter a new rhythm, we don't have any "stored" memory of that new pattern.  As you get better, you'll build up a "library" of stored patterns, so that you can just glance at a few bars of rhythm and you'll be able to spit it out without so much as a thought.  It becomes a series of stored patterns in your brain that no longer require conscious thought.  (think - do you have to consciously measure the speed and distance and timing to get a spoon to your mouth? - when you were a baby, you did, but now it's automatic). But it takes time!
So your question comes down more to:  what do you do RIGHT NOW that might help you on your rhythmic journey.  A few people have pointed to subdivision.  That's super important and a MUST if you want to understand what's happening in between notes.  Subdivision is simply tapping or thinking about each note in double time or quadruple time to help keep your notes even or accurate.  You can use numbers or sounds when counting.  "ta" for quarter notes, "ti ti" for eights and "tiri tiri" for sixteenth notes can be helpful for some. You can clap your hands on the quarter notes and say the subdivision.  So you might be clapping 1,   2,   3,   4,   but say Ti ti, ti ti, ti ti, ti ti along with each clap or tap.  Saying "one AND two AND three AND four AND" (where and represents the other half of the quarter note) is a good way of subdividing with just your voice.  You can help yourself of course by clapping the quarters.  It might feel like patting your head and rubbing your tummy at first, but this isn't a "child's game".  Anyone of any skill level can benefit by practicing and changing up how they learn and rehearse rhythm.
An important thing to improve rhythm is to simply practice...with a metronome.  As a start, try putting the metronome (or metronome app) to a reasonable tempo - say 60 beats per minute (60bpm).  Clap along with the pulse or click.  Then clap double-time (two claps per click).  Then return to clapping with the click.  Now switch it up.  Say "ta" with each click, repeat for twenty to thirty clicks, followed by double-time "ta" with each click. (to change how you store the pattern - clapping and speaking are two different ways of storing the information - related - but still different!)  If you're an instrumentalist, then try the same thing on the instrument.  Now combine two ways of representing the pulse...clap and say "ta" or play and tap your foot or hand (along with the metronome).  Then go into subdivision on one of your patterns, the clap or the voice - then switch it up.
Brain studies have shown that pattern retention is significantly improved when a "guide pattern" is used (the metronome) (, as opposed to just doing it yourself from the page. (Neuropsychology: Lewis et al, 2004)  You can move from that to triplets or to quadruplets and to dotted rhythms.  You can find many examples online - a quick search shows some free materials at practicesightreading.com.  You shouldn't need to buy anything....pick up an old hymn book at a flea market and practice singing or clapping the rhythms (with a metronome!)
A very important thing to practice is consistent tempo (which is why we use the metronome)  This is overlooked by so many people and one of the reasons why rhythm can be sloppy.  We tend to sing or play in OUR version of the pulse (or tempo) instead of THE ACTUAL tempo.  This can lead to rushing and a poorly learned rhythmic selection because it fails as soon as someone else is "steering the ship" - or changing the pulse when they deliberately speed up or slow down (read: conductor or drummer).  Practicing with that metronome eliminates your need to track tempo as well and you can focus on getting the relative timing accurate.
One more thing that often gets overlooked:  rhythm and tempo are very 'internal' things to us.  But we are required to synchronize those pulses with other players, pianist, conductor,etc.  The biggest way we synchronize as classical musicians is with the conductor or principal player (baton waving or head nodding+breathing together, respectively).  These things are often visual cues....but we've been practicing our rhythms with auditory cues!! (Click click click....)  So....incorporate into your practice some use of the metronome with a flashing lightand try and stay synchronized.  This will significantly solidify your pattern learning.
Jazz musicians may "feel" the music but they all still count.  They might just be counting groupings of learned patterns now instead of individual beats - and all in the subconscious.
Practice practice practice (and occasionally turn off the metronome, too, because you can't take that up on stage with you).
Comment: Let me quickly address "feeling out the music" - when anyone does that successfully - it's because they already have a library of learned and automatically recognized patterns - which they might not be consciously aware of (maybe they danced to music a lot as a baby/child and internalized the rhythms of that music).  Practicing and identifying these and new patterns through counting and subdivision will help identify which patterns are already known and create new ones.  If you don't know why something works, but you want to get better, it's good to figure out what you actually know!
Answer: no.
reading rhythm is more important than reading pitch.  (which is also pretty dang important.)  just ask somebody who plays drums.
don't guess at music.  play it correctly.  after that, play around with it (improvise)
Comment: But most drums don't play pitch, so if you ask them, of course they'll say rhythm is more important. Not that I don't agree with you per se, but be careful with that.
Answer: A little trick I like explain to my students - there are 2 ways to count rhythms.

Divisive: eg 4 beats in a bar of 4/4. 16th notes become 1e&a, 2e&a, etc.
Additive: Find the smallest rhythmic unit, make that the "beat", and work out the other rhythmic values as multiples of that. This is a good way of working things out (provided there aren't any divisive rhythmic devices like triplets).

I like to teach "Additive" counting to beginners as it's easier for most people to add beats together than to imagine what 1/2 or 1/4 of a beat sounds like. Take a rhythm like this:

Here's how you can work it out:

Find the smallest rhythmic unit. In this case, it is 16th notes (semiquavers).
Work out the other rhythms as multiples of the smallest rhythmic unit. For the example below, these are:

16th note = 1 beat
8th note = 2 beats
Dotted 8th note = 3 beats
Quarter note = 4 beats
Dotted quarter note = 6 beats
Half note = 8 beats

Now write out the beat lengths for each note - for the example above, you'll get:

3 1 2 2 | 3 1 2 | 4 2 | 2 2 4 3 1 | 8 4 |
2 1 1 4 2 2 3 1 | 2 1 1 1 1 8 (6+2 tied notes) 2 2 1 1 4 |

Count aloud with a slow metronome from 1 to (beat length) for each note, and clap on the 1s. So for the example:

1 2 3, 1, 1 2, 1 2 | 1 2 3, 1, 1 2 | 1 2 3 4, 1 2 | (etc)

Now, you can work out just about any rhythm. Notice that you can figure out the rhythm without needing to know about time signatures using this method (the 2/4, 6/8, 3/4, etc - don't need to worry about those). That's a big plus for beginners - they've got enough things to think about already!
Answer: I've been playing piano, organ, accordion, and other keyboard instruments for over 50 years, and I've never counted.  I get the rhythm right, usually.  Is that intuition?  I don't think so.  I think it's just recognizing rhythm patterns and knowing how they'll sound.
Recently I took up banjo, then mountain dulcimer, hammer dulcimer, and autoharp.  I still don't count.  At this point I think it's safe to say I never will.
I know a lot of other people who do count.  They aren't any better, or any worse, at playing correctly than I am.  There's more than one road to Oz.
Comment: So, if you've *never* counted, how did you manage to get them right all those years ago? Intuition depends on experience; is not really innate in most people. Serious question!
Comment: A statement concerning the different musical styles would have been more helpful than a list of instruments. If the *Oz* area is small enough...
Comment: It could be that their teacher clapped the rhythm for them - that's what my dad did for me before I was old enough to understand fractions.
Comment: @Tim - I learned to read music before I learned to read English.  At age 5 when I started lessons my teacher played my piece for me and I emulated.  Very quickly I learned to associate the rhythm patterns with the sounds they made, and didn't need her to play them for me anymore.  She didn't teach me to count, and so I didn't.
Comment: @guidot - Sorry if my writing style offends you.  That's just how I write.  I was trying to make the point that my lack of counting embraced a wide variety of instruments, even if they do all fall into only a couple of genres.
Comment: @L3B: No offense taken and I'm unaware what lead to that assumption .


Valores de Barras de Cores no Matplotlib

Question: Eu tenho um código com 3 variáveis: a1, a2 e dc1.A variável dc1 é o valor absoluto da diferença de a2 e a1. Eu estou gerando um gráfico na matplotlib com os valores da variável a1 e gostaria as cores da 'colorbar' fossem definidos pelos valores da variável dc1. Mas não estou conseguindo fazer isso.
Abaixo segue o meu código.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

a1 = np.array([1.5,2.5,2.0,5.5,7.0])

a2 = np.array([5.0,7.5,1.0,8,9])

dc1 = np.abs(a2-a1)

idx=np.arange(len(a1))

plt.scatter(idx,a1,s=dc1*100, c=dc1, cmap="BuPu",
            edgecolors="black", linewidth=0.90)
plt.title(" MLP Keras")
plt.xlabel('Amostras\n')
plt.ylabel('Discrepâncias(m)')
plt.colorbar(dc1)
plt.grid(True)
plt.show()

Grato pela atenção
Answer: Segundo a documentação, em inglês, o plt.colorbar utiliza um matplotlib.ScalarMappable como argumento. Esse ScalarMappable tem informação suficiente acerca do gradiente e dos valores associados a ele e pode ser construído manualmente através de mpl.cm.ScalarMappable(args).
No seu código você não precisa fazer isso, quando passa um valor para o cmap no plt.scatter a colorbar já é preparada automaticamente. Para exibi-la a única coisa que precisa ser feita é chamar plt.colorbar() (sem argumentos).
a única modificação necessária no seu código é
plt.colorbar()

e o resultado fica


Tagging separate files

Question: I wonder if, and how, the following would be possible to tag each uploaded file with one or more tags and show these files filtered on their tag(s).
I know it's possible to do this for nodes but have not found a way to tag single files.
The purpose of this would be the creation of an image bank in which the administrator can upload images or files (pdf, zip) and define each image/file as being, for example, 300dpi, print, high resolution, color, black & white, etc.
Comment: Hello Blix, and welcome on _Drupal Answers_. To which version of Drupal are you interested?
Comment: @kiamlaluno: Ideally I would like to use Drupal 7. But Drupal 6 is also an option.
Answer: In both Drupal 6 and Drupal 7 files are not used independently. Most often, fields are used through a file field, on either a node (D6 or D&) or an entity (D7).
To do the above, I would suggest using a file field on something, that holds all the info about the file. You could add things like title, teaser  text, description in addition to tagging. For Drupal 6 the only option you have is to use nodes, for Drupal 7 you could use nodes or create your own entity in a custom module. The easiest thing will be to use nodes, as you wont need to create a lot of functions for editing, loading, rending... the entity. Nodes are probably also a good match in this case since the files are content on your site.
Another plus doing this, is that you would be able to use the Views module, and create different galleries or list of image files.
Comment: I am trying to achieve the same. I guess we might have to add file field to entity type if own entity type is considered. Can you please explain how to use nodes to achieve this?

Tagging separate files


Cliff's delta in python?

Question: I want to calculate Cliff's delta in Python.
After searching on Bing, and stats.stackexchange.com, I am astonished that there is no such library implemented in Python.
I only found the package that is implemented in R.
I'm wondering if there is something, or it is a must that developers have to use R to calculate  Cliff's delta or implement it by themselves in Python?
Comment: Have you seen this repository already? https://github.com/neilernst/cliffsDelta
Comment: For what do you want to calculate Cliff's delta? Two sets of observations, or for two distributions (using formulas rather than data)?
Comment: Did you encounter the Wikipedia page [about cliff's delta](https://en.wikipedia.org/wiki/Effect_size#Effect_size_for_ordinal_data) which tells that you can just as well compute the Mann–Whitney U statistic? (or do you want to do more with it)
Answer: I find it useful to search github when I want to find a particular function that isn't in the usual places I'd look:
https://github.com/search?q=cliff+delta
This result looks like your best bet
https://github.com/neilernst/cliffsDelta

This is code to calculate the Cliff's Delta effect size metric, which strangely is not in NumPy/SciPy.stats.

The above code has now been converted into a package which can be found here
https://pypi.org/project/cliffs-delta/
Comment: Thanks! I test it and it works! Thanks again for suggesting to me that I should search on Github.

Cliff's delta in python?


Volume swept by a polygon when it "slides" along a given curve

Question: Given a parametric curve $\mathbf{r}\left(s\right)$, where the parameter $s$ is the length of the curve, lets define a plane $\Pi$ perpendicular to the curve at a specific $s=s_1$. Lets also define a polygon $\mathcal{P} \in \Pi$ of $n$ given vertices (or more generally, a closed area $A \in \Pi$)
Now, lets "slide" the plane $\Pi$ along the curve to a new position $s=s_2$. My question is: How can I calculate the volume swept by the polygon $\mathcal{P}$ as function of $s_1$, $s_2$, and $\mathbf{r}\left(s\right)$?
Thanks in advance
(This question is related with the one that I posted yesterday: http://goo.gl/iH7YJu)
Comment: You may also need to specify how the plane $\Pi$ rotates (or what it means that it doesn't rotate) around the curve as it slides
Comment: Yep, let's say it does not rotate
Answer: Lets assume that $\mathbf{r}\left(s\right)$ is "sufficiently smooth" in $s\in[s_1, s_2]$ and its curvature $\kappa \left(s\right)$ is never zero in $s\in[s_1, s_2]$. If $\mathcal{V}$, the solid generated by the motion of the polygon $\mathcal{P}$ along the curve with the frame $\left\{\mathbf{n}(s),\mathbf{b}(s)\right\}$, is not self-intersecting, then the volume of $\mathcal{V}$ is given by:
\begin{equation}
V = AL + M_Y \int_{s_1}^{s_2} \kappa \left(s\right) \mathrm{d}s
\end{equation}
where $A$ is the area of the polygon $\mathcal{P}$, $L$ is the length of the curve between $s_1$ and $s_2$, and $M_Y$ is the moment of $\mathcal{P}$ about the initial binormal vector $\mathbf{b}(s_1)$
Comment: @AnthonyCarapetis, In reference to the paper you sent me (http://www.jstor.org/stable/2316426), at pag.360 the properties of periodicity and smoothness of the curve C_P are used in order to simplify eq.21: seven equations are then obtained (eq.23-29). In my particular case, C_P is not smooth: it is actually a poligon, so it is not differenciable in the vertices. However, I think all these simplifications can be still done. Does the absence of smoothness affects to any of the simplifications done in eq.23-29?

Thanks you very much in advance
Comment: Since polygons are continuous and piecewise differentiable, there should be no issue - everything should still work if you interpret the derivatives as weak derivatives.
Answer: I assume, that sliding means that every point of a polygon has fixed coordinates in a Frenet frame. Let the original polygon (at $s=s_1$) has an area $A$. The volume can be computed as $A$ times the length of a curve from $s_1$ to $s_2$. Since $s$ is arclength parameter then it is $A(s_2-s_1)$.
Comment: You need a "balancing" condition on the polygon for this to be true - its centroid must lie in the binormal direction. See e.g. http://www.jstor.org/stable/2316426
Comment: @user3473823: You need to specify the manner in which you're transporting the vertices. Saying they "don't rotate within the plane" doesn't exactly make sense, since the normal planes at different points on the curve are different. The most natural way to transport is to rotate the polygon along with the frame {N,B}, which is the scheme that gives equation 32.
Comment: This is not correct in general. It is only correct in linear translation and/or specific arrangements
Comment: this is the most general setup where it holds, unless I misunderstood what you think by "sliding".
Comment: It is by no means  a simple issue, one of the big ambiguities being self-intersections ! You should have a Google search with the keywords "tube" (this is the most usual name for a generalized cylinder) and "volume".
Comment: @JeanMarie I agree, here we assume a lot of simplifications for sure, no self intersections, everything is smooth etc.
Comment: @Anth I don't see the point where that condition should be used, Can you provide some details. I  will see the reference later
Comment: @user26977: think about the revolution of a disc about a circle to get a solid torus. If you offset the disc towards the centre of the circle, you create a smaller torus, but the cross-sectional area is not changed. For this case of a horizontal circle, the condition is that the centroid of the region/polygon should lie on the same vertical line as the curve.
Comment: @AnthonyCarapetis I am studying the reference you sent me, and I have a question about it. If I am moving the vertices of the polygon as said in http://goo.gl/iH7YJu , Can I assume that the moving domain is rotating with the binormal vector? That would allow me to use the simpler equation (32) instead of the general one (eq. 44).
Comment: The OP said the polygon doesn't rotate, so you can't get a torus by sweeping a disc.
Comment: The polygon does not rotate within the plane $\Pi$ (i.e., if you look at the plane $\Pi$ as it moves, the polygon always is in the same position). But of course it spatially rotates: $\mathbf{r}\left(s\right)$ is a generic curve
Comment: @AnthonyCarapetis -- my mistake. I misread the question, and the OP's comment led to further confusion.
Comment: Right, I am going to write the answer. Thank you very much!


DB and modules in folder that doesn't exist

Question: Ok, so I ran into an issue today that completely baffles me...
I have a staging server that I have about 30 sites on. Most are WordPress and about 5 are Drupal. Yesterday I worked on a Drupal site that stores modules in contrib and custom directories. The other Drupal installs use the default setup for modules (no contrib or custom folders). Everything worked for all sites yesterday with no issues.
I came into my office today and noticed that 2 of the Drupal sites are all messed up.
When I visit Site1 I get an error page stating:
Notice: Undefined index: olympusat in _drupal_maintenance_theme() (line 74 of /var/www/vhosts/parables.stage/public_html/includes/theme.maintenance.inc).
Notice: Trying to get property of non-object in _drupal_theme_initialize() (line 145 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'd_parables.languages' doesn't exist: SELECT * FROM {languages} ORDER BY weight ASC, name ASC; Array ( ) in language_list() (line 2651 of /var/www/vhosts/parables.stage/public_html/includes/bootstrap.inc).
Notice: Trying to get property of non-object in _theme_build_registry() (line 710 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Trying to get property of non-object in _theme_build_registry() (line 710 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Trying to get property of non-object in _theme_build_registry() (line 710 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Undefined index: olympusat in theme_get_setting() (line 1414 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Trying to get property of non-object in theme_get_setting() (line 1451 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Trying to get property of non-object in theme_get_setting() (line 1461 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Undefined index: olympusat in template_preprocess_maintenance_page() (line 2702 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Trying to get property of non-object in template_preprocess_maintenance_page() (line 2702 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Warning: array_keys() expects parameter 1 to be array, null given in template_preprocess_maintenance_page() (line 2705 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Warning: Invalid argument supplied for foreach() in template_preprocess_maintenance_page() (line 2705 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).

So first thing I did was try to flush the cache... output is below.
[root@StagingServer public_html]# drush cc all
Drush command terminated abnormally due to an unrecoverable error.   [error]
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'd_parables.languages' doesn't exist: SELECT * FROM {languages} ORDER BY weight ASC, name ASC; Array
(
)
 in language_list() (line 2651 of /var/www/vhosts/parables.stage/public_html/includes/bootstrap.inc).

So I decided the DB was somehow corrupted. I dumped a fresh version of the DB from PRODuction and tried to visit the page. Same error as above... So I ran drush cc all again and I got the exact same error...
Confirmed I was in the correct folder and still the same issues.
Ok, I'll just try to fix Site2 first then...
Site2 just has a blank page. Checking the error log shows:
PHP Fatal error:  require_once(): Failed opening required '/var/www/vhosts/olympusat.stage/public_html/sites/all/modules/contrib/entity/includes/entity.inc' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/vhosts/olympusat.stage/public_html/includes/bootstrap.inc on line 3146

The problem with this error is that there is no contribfolder for modules. This is setup on Site1 only. SO next I wanted to confirm that each site was pointing to the correct DB. I verified both settings.php files were correct... they are.
I am lost on what to do next... Site2 is using a default setup and all modules are in the sites/all/modules directory. I cannot understand why it's looking in the contrib folder for a module. The module does exist in the modules directory... Not really sure how to fix this issue.
So I decided to check permissions and ownership for the sites in question. Everything seems correct...
Any suggestions or insight on this is appreciated.
Thanks
More Info
So this is what is happening after I use registry_rebuild (drush rr)
Site1:
Command Line:
cd /var/www/vhosts/parables.stage/public_html
cd sites/all/modules/contrib/registry_rebuild
php registry_rebuild.php

ouput:
DRUPAL_ROOT is /var/www/vhosts/parables.stage/public_html.

Bootstrapping to DRUPAL_BOOTSTRAP_SESSION

Bootstrap caches have been cleared in DRUPAL_BOOTSTRAP_SESSION

Doing registry_rebuild() in DRUPAL_BOOTSTRAP_SESSION

Bootstrapping to DRUPAL_BOOTSTRAP_FULL

Rebuilding registry via registry_rebuild_cc_all in DRUPAL_BOOTSTRAP_FULL

All caches have been cleared with registry_rebuild_cc_all.

There were 711 files in the registry before and 711 files now.

All caches have been cleared with registry_rebuild_cc_all.

If you don't see any crazy fatal errors, your registry has been rebuilt.

Site works as expected.
Site2
Command Line:
cd /var/www/vhosts/olympusat.stage/public_html
cd sites/all/modules/registry_rebuild
php registry_rebuild.php

ouput:
DRUPAL_ROOT is /var/www/vhosts/olympusat.stage/public_html.

Bootstrapping to DRUPAL_BOOTSTRAP_SESSION

Bootstrap caches have been cleared in DRUPAL_BOOTSTRAP_SESSION

Doing registry_rebuild() in DRUPAL_BOOTSTRAP_SESSION

Bootstrapping to DRUPAL_BOOTSTRAP_FULL

Rebuilding registry via registry_rebuild_cc_all in DRUPAL_BOOTSTRAP_FULL

All caches have been cleared with registry_rebuild_cc_all.

There were 734 files in the registry before and 734 files now.

All caches have been cleared with registry_rebuild_cc_all.

If you don't see any crazy fatal errors, your registry has been rebuilt.

Site works as expected.
Now when I check Site2 (parables.stage)
I see the error:
Fatal error: require_once(): Failed opening required '/var/www/vhosts/parables.stage/public_html/sites/all/modules/entity/includes/entity.inc' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/vhosts/parables.stage/public_html/includes/bootstrap.inc on line 3062

Notice there is no contirb folder in the path as this site is setup to use...
So I clear the cache:
cd /var/www/vhosts/olympusat.stage/public_html
drush cc all

Now I get the error:
Notice: Undefined index: olympusat in _drupal_maintenance_theme() (line 74 of /var/www/vhosts/parables.stage/public_html/includes/theme.maintenance.inc).
Notice: Trying to get property of non-object in _drupal_theme_initialize() (line 145 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'd_parables.languages' doesn't exist: SELECT * FROM {languages} ORDER BY weight ASC, name ASC; Array ( ) in language_list() (line 2651 of /var/www/vhosts/parables.stage/public_html/includes/bootstrap.inc).
Notice: Trying to get property of non-object in _theme_build_registry() (line 710 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Trying to get property of non-object in _theme_build_registry() (line 710 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Trying to get property of non-object in _theme_build_registry() (line 710 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Undefined index: olympusat in theme_get_setting() (line 1414 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Trying to get property of non-object in theme_get_setting() (line 1451 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Trying to get property of non-object in theme_get_setting() (line 1461 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Undefined index: olympusat in template_preprocess_maintenance_page() (line 2702 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Notice: Trying to get property of non-object in template_preprocess_maintenance_page() (line 2702 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Warning: array_keys() expects parameter 1 to be array, null given in template_preprocess_maintenance_page() (line 2705 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).
Warning: Invalid argument supplied for foreach() in template_preprocess_maintenance_page() (line 2705 of /var/www/vhosts/parables.stage/public_html/includes/theme.inc).

Also, the title says Olympusat Media... not parables... So I start this loop over...
I'm deciding to delete these sites and set them back up... Hopefully this will cause this to go away.
Comment: Sounds like you need Registry Rebuild https://www.drupal.org/project/registry_rebuild
Comment: Also - double check you are not sharing a DB between two sites.
Comment: you can also try to clear the caches manually, truncating cache* tables
Comment: Thanks. I manually installed registry_rebuild and ran it. It fixes the sites but only 1 will work properly. In other words, if I rr on Site1 it's fine. Then I rr on Site2 and it's fine but Site1 is now down... very weird. I am using Drush as root and cd into each site's directory... must be a setup issue. Each site is using it's own DB.
Answer: It sounds like a module might have been removed from the /modules directory without being properly uninstalled.
A registry rebuild usually fixes this type of issue. If you're using Drush, make sure you have the registry rebuild extension downloaded:
drush dl registry_rebuild -y

Once this is done, go ahead and run the following command from the Drupal webroot:
drush rr

As I said, that should hopefully do the trick.
Comment: This does semi work. It's very weird. When I fix the sites, it takes one or the other down in the process. Looking more into the setup of the sites. This may be the accepted answer once I figure out the last issue.
Comment: Hmm, do the other sites share the same database? If so then I guess that could be the issue.
Comment: Nope, they have separate folders and DB's. Settings files have been confirmed. Very weird.
Comment: Very weird indeed. Are all core theme includes all standard and not 'hacked'?
Comment: All standard. No hacking other than custom modules but these are in a custom folder for Site1. Site2 uses only drupal modules but there is no contrib folder as this was not setup to use that structure and nothing was modified. It seems that there may be an issue with Drush. I am cd into each site's public_html directory and running `drush rr`but it still keeps messing up each other. Weird thing is no other sites are affected.
Comment: You might need to run drush at a higher bootstrap level than that. If you can run 'drush st' from sites/default and correct info is returned without errors, then run 'drush rr' from there.
Comment: Updated original post to reflect my attempts to resolve this


How to make clusters (consisting of demands) equal to the load of a truck?

Question: I am working on a routing problem where I have thousands of points (places) with individual demands (in Weight and Volume).
So far I have created 5 clusters based on their location. Now I need to further divide the clusters individually and create clusters as such one truck can cater to a single cluster.
Example: Suppose there are three points that are quite close in the cluster and the cumulative load is 200 kg and 700 cubic ft, which is equal to a truckload, so I will map them together in a cluster and one truck will be assigned to it.
Can they be modeled using traditional clustering methods (like KMeans or Hierarchical clustering) or I have to use some other techniques like a decision tree to segregate the points further?
Also, I am using R for my analysis.
Any help would mean a lot!
Answer: It took some time but finally, I solved this problem after adapting a two-step approach:
1st Step:
Sort the points in clockwise (or anticlockwise) order.
For that I took the help from here:
https://stackoverflow.com/questions/67497664/how-to-sort-points-in-clockwise-order-in-r-with-respect-to-the-centre
2nd Step
Now create the function for clustering. In the function first I sorted the rows with increasing angles. Then I created a while loop that will check the demand of each point and tally it against the full capacity. If the demand is within the full capacity then it will be assigned to this cluster, if it exceeds the capacity then it will be assigned to the next one.
Finally, the function will return a list with the clusters.
The sample while loop if anyone needs it for the reference.
NOTE: 170 is the truck capacity.
<code>while (i <= length(df$angle)){
  d <- d+ df$demand[i]
  if(d<=170){
    cluster_list[i] <- cluster_number
    i<- i+1
  }
  else{
    cluster_number <- cluster_number+1
    d <- 0
    i<-i
  }
}

</code>
Answer: This is an optimization problem (specifically, an allocation problem). I'd try to model this problem in MS Excel and use the Solver add-in.
Comment: Thanks! I finally solved it with a different approach: https://stats.stackexchange.com/a/524145/320869


Summation in a Network using identity activation function

Question: I am currently experimenting with a network with one input-layer, one hidden layer, and one output-layer.
I am using the identity-function as the activation-function.
During the forward-pass, i began computing the outputs of the $j$th neuron in a layer as follows:
$$ f \left( \frac{ \sum_{i=1}^n{p_iw_{ji}}}{n} \right) $$
Since $f$ is the identity-function this reduces to:
$$ \frac{ \sum_{i=1}^n{p_iw_{ji}}}{n} $$
where $p_i$ is the $i$th input from the previous layer, and $w_{ji}$ is the weight from the $i$th input to the $j$th neuron in the current layer. $n$ is the number of inputs.
But i notice that the more neurons i add, the smaller my output-values become (since the weights and initial values are in the range $[0, 1]$, the resulting values become smaller with each multiplication.
So i tried taking only the sum, without dividing over the number of inputs:
$$ \sum_{i=1}^n{p_iw_{ji}} $$
But, unsurprisingly, the resulting values explode with a growing number of neurons.
I know that there are activation-functions that deal with this problem, and keep the values within a certain range, but my question is, whether there are practical ways of keeping the values in a neural net from either becoming very large, or very small, when using a linear activation function.
Ideally, the solution would be one that would not do harm to the network should the activation-function later be swapped.
Comment: Are you aware that with identity activation adding more layers to network has same effect as widening single-layer network?
Comment: @Thanks for the hint, intuitively this makes sense, i got to do the math on it to see for myself if it really is equivalent.
Answer: This problem can be avoided by initializing weights adequately.
In general, when using activation-functions that don't clamp the values into a range, the initial weights should be scaled inversely proportional to the number of weights.
For example, when using random values for initial weights,having obtained a random value $r$, a weight could be initialized as $w = \frac{r}{\sqrt{n}}$ where $n$ is the number of weights in the given layer.
This way, resulting outputs will likely neither explode nor vanish.
Furthermore, a lower learning-rate may avoid weights being adjusted too much, which otherwise might lead to higher errors in subsequent iterations, and thus, even higher subsequent errors, producing an effect similar to a feedback-loop.


Check Previous Values in Numpy Array

Question: I'm trying to check the previous 90 values in an array (or upto the start of the array) to determine if a value already exists.
So far my code just checks the previous value:
overt = np.array([])

if frame ==0:
    overt = np.append(overt,lachange)

else:
    pframe = frame - 1
    plachange = overt[pframe]
    if lachange ==0:
        overt = np.append(overt,lachange)
    elif lachange ==1:
        if plachange == lachange:
           overt = np.append(overt,0)
           lachange = 0
        else:
            overt = np.append(overt,lachange)

The value is 1 if the vehicle has changed lane and 0 if it hasn't but if the value is 1 multiple times in a 3 second period I want only the first one to be recorded and the following values to be 0.
Comment: Can you provide an example input array and the desired output?
Comment: you should use overt as a list instead of an array. An array is built with the specific assumption that you aren't going to be changing its size. Since you are going to be append to it on each iteration, having overt be an array actually makes the code run slower
Answer: As I understand it, what you want to do is:
append 1 to overt if lachange is 1, and the last 90 values are all non-1
append 0 to over otherwise
def add_change(lachange, overt):
    if (lachange == 1) and (not (1 in overt[-90:])):
        overt.append(1)
    else
        overt.append(0)

overt[-90:] grabs the last 90 values in overt. If there are less than 90 values, it grabs everything (or nothing if overt is empty).
1 in overt[-90:] returns True if there is a 1 in it (there has been a lane change) and False otherwise. Use not to flip the values
this checks that if there is a lane change (lachange==1) and there wasn't one in the last 90 items. If so, add 1 to it. Else, add 0 to it
Code written assumes overt is a list instead of a numpy array. If it was an array, you would have to change the append statements, and return overt at the end

Check Previous Values in Numpy Array


How to temporarily sort a file from longest to shortest line, before restoring it back to its original order?

Question: The answers in Sorting lines from longest to shortest provide various ways to sort a file's lines from longest to shortest.
I need to temporarily sort a file from longest to shortest, to give some time for a BASH script to perform some operations to edit various content, but then to restore the file to its original order after the BASH script has finished.
How can I first sort a file from longest to shortest, but then be able to restore the order later?
Comment: Make a backup file, copy it back after some time?
Comment: How big is the file going to be?  If it is not very large you could read the lines from the file into a `hash` and write your own custom sort subroutines and apply them to your `hash`.
Answer: Done by enhancing your linked answer by these steps:

Prepend a length and line number to the front of each line, sort by length, cut length (just like in linked answer)
perl -ne 'print length($_)." $. $_"' file.txt | sort -r -n | cut -d ' ' -f 2- > newfile.txt

Do your bash translation (ignoring first number on each line)
If for some reason you can't do your amorphous translation with the number prefixes, then split the numbers into a separate file and merge them back in afterwords.
Sort by line number and then cut off line number to restore file to previous state.
sort -n newfile.txt | cut -d ' ' -f 2- > file.txt

Answer: Something like this to store the original line order in a separate file might be what you need:
awk -v OFS='\t' '{print length($0), NR, $0}' infile |
sort -k1rn -k2n |
tee order.txt |
cut -f3- > sorted.txt

do stuff with sorted.txt then

cut -f2 order.txt |
paste - sorted.txt |
sort -n |
cut -f2- > outfile

You don't say what you want done with lines that are the same length as each other but the above will preserve the order from the original file in that case. If that's not what you want, play with the sort -rn commands, modifying the -ks as necessary.


Looking for a reference of quasi inverse in rings

Question: This question is related to exercise 1.51 from Rotman's "Introduction to the Theory of Groups".
down vote
favorite
2
My question is related to exercise 1.51 from Rotman's "Introduction to the Theory of Groups".
An element a
in a ring $R$ (with unit element $1$) has a left quasi-inverse if there exists an element $b\in R$ such that $a+b−ba=0$. I want to show that if every element in $R$ has a left quasi-inverse except $1$, then $R\setminus{1}$ is a group under the operation $a∗b=a+b−ba$.
If you take a ring and change its multiplication by the new, this new structure has anything interesting?
Is some reference papers or books about this operation?
Answer: First I'll answer your question about the exercise (showing that $(R\setminus \{1\},*)$ is a group).
Let's start by checking that $*$ is associative on all of $R$:
\begin{align*}
(a * b)*c &= (a+b-ba)*c\\
&= (a+b-ba)+c-c(a+b-ba)\\
&= a+b+c-ba-ca-cb+cba\\
&= a+(b+c-cb)-(b+c-cb)a\\
&= a*(b+c-cb)\\
&= a*(b*c).
\end{align*}
Now observe that for any $a\in R$:
\begin{align*}
 1*a &= 1+a-a1 = 1\\
 a*0 &= a+0-0a = a\\
 0*a &= 0+a-a0 = a
\end{align*}
And if $b$ is a left quasi-inverse of $a$, then $a*b = 0$.
Now we'd like to show that $R\setminus \{1\}$ is closed under $*$. Let $a,b\in R\setminus \{1\}$, and suppose for contradiction that $a*b = 1$. Since $b\neq 1$, $b$ has a left quasi-inverse $c$, so
\begin{align*}
(a*b)*c &= 1*c\\
a*(b*c) &= 1\\
a*0 &= 1\\
a &= 1
\end{align*}
which is a contradiction.
So now we have an associate operation $*$ on $R\setminus \{1\}$ which has an identity element $0$, and every element has a right-inverse (for all $a$ there exists $b$ such that $a*b = 0$). So you can apply the fact that a monoid with right-inverses is a group. (Proof: Let $b$ be the right-inverse of $a$, and let $c$ be the right-inverse of $b$. Then $(a*b)*c = 0*c$, so $a*(b*c) = c$, so $a = c$, so $b*a = 0$, and $b$ is the inverse of $a$).

Ok, having solved the exercise, some comments:
An element with a left quasi-inverse is called left quasiregular. For a reference, you could start with Wikipedia and follow the references there.
An element $x$ is left quasiregular if and only if $(1-x)$ has a left inverse. Indeed, if $y(1-x) = 1$, then the left quasi-inverse of $x$ is $z = -xy$. We have $$x+z-zx = x-xy+xyx = x(1-y+yx) = x(1-y(1-x)) = x(1-1) = 0.$$
Conversely, if $x+z-zx = 0$, then $y=(1-z)$ is the left inverse of $(1-x)$. We have $$y(1-x) = (1-z)(1-x) = 1-x-z+zx = 1-(x+z-zx) = 1.$$
Suppose $R$ has the property that every element of $R\setminus \{1\}$ is left quasiregular, as in the exercise. Then for all $x\neq 1$, $(1-x)$ has a left inverse. Thus every nonzero element of $R$ has a left inverse. This means that $R\setminus \{0\}$ is a monoid with left-inverses, and hence a group (by the dual of the fact noted above). So every element of $R\setminus \{1\}$ is left quasiregular if and only if $R$ is a division ring.


SQLite SELECT MAX() query returns null to cursor?

Question: I have a SQLite Table called "TBL_EVO" where I want to get the biggest value from column "DATE_MODIFIED". With following query I got the result in SQLite Browser like below.
SELECT MAX(DATE_MODIFIED) FROM 'TBL_EVO';

When I run the query from code from my application I got a nullpointerexception. Database is absolutly the same like in the SQLBrowser.
So DATE_MODIFIED exists with its values.
It crashes at cursor.moveToFirst();.
Even when I surround it with if(cursor.moveToFirst())
When I do this the code jumps to else.
What am I doing wrong?
MainActivity.java
MySQLiteHelper db = new MySQLiteHelper(this);
String strDateTime = db.strSelectMaxDate(TableNames[intTableIDStart]);
Log.v("TBL_EVO_DATE_TIME: ", "-> "+strDateTime);

MySQLiteHelper.java - strSelectMaxDate
public String strSelectMaxDate(String strTable){
    String strValueFromDB = "";
    String queryMaxDate = "SELECT MAX(DATE_MODIFIED) FROM '" + strTable+"'";
    Log.v("TBL_EVO_DATE_TIME_QUERY", queryMaxDate);
    try{
        db = this.getReadableDatabase();
        cursor = db.rawQuery(queryMaxDate, null);
        cursor.moveToFirst();
        if(cursor == null){
            strValueFromDB = "cursorisnull";
            Log.v("TBL_EVO_DATE_TIME_VAL1", strValueFromDB);
        }else {
            strValueFromDB = cursor.getString(0);
            Log.v("TBL_EVO_DATE_TIME_VAL2", cursor.getString(0));
        }
    }catch(Exception e){
        Log.v("TBL_EVO_DATE_TIME_ERROR", "-> "+e);
        strValueFromDB = "error: "+e;
    }finally{
        cursor.close();
        db.close();
    }
    return strValueFromDB;
}

Logcat
09-24 15:24:23.207 21900-21917/com.spicysoftware.goexpert V/TBL_EVO_DATE_TIME_QUERY: SELECT MAX(DATE_MODIFIED) FROM 'TBL_EVO'
09-24 15:24:23.216 21900-21917/com.spicysoftware.goexpert V/TBL_EVO_DATE_TIME_ERROR: -> java.lang.NullPointerException: println needs a message
09-24 15:24:23.216 21900-21917/com.spicysoftware.goexpert V/TBL_EVO_DATE_TIME:: -> error: java.lang.NullPointerException: println needs a message

Comment: `println needs a message` is definitely an interesting error, suggests that something's going wrong with the internals. When you `catch` your exceptions, can you also call `e.printStackTrace()` to show exactly where the exception is thrown?
Comment: i think cursor.getString(0) return null , am i right?
Comment: could you please add the version of android phone tested on?
Comment: `What am I doing wrong?` You're enclosing the table name in ***string delimiters***, here: `... FROM 'TBL_EVO'`.
Comment: @AnisBENNSIR It has been testet on an Android 7.0 Emulator.
Comment: @MSeiz5, could you please add Table creation statement, and confirm that all raw have DATE_MODIFIED  not null! in fact, i think that the max will always return a raw, it will be null if the group contains a null value...
https://www.sqlite.org/lang_aggfunc.html
Answer: Replace your queryMaxDate with:
String queryMaxDate = "SELECT MAX(DATE_MODIFIED) FROM " + strTable;

Comment: Thanks for your suggestion. But I got still the same error.
Comment: pls do e.printStackTrace(); inside your catch statement then post the log.
Answer: I solved the problem.
Problem was, that I did a new SQLConnection in an asynctask.
I had to open a new connection in Asynctask.