2012-11-14

Find median or any statistic parameter in loop by array key


Suppose you have multiple lists with the same length and key:

key----value1----value2
1          35           33
2          40           20
3          23           23
..
12        60           70


and you would like to calculate medians by the key -
result should look like:

key----median
1          34
2          30
3          23
..
12        65
Why?
If you have raw tabular data, of course, this can be done fast in any spreadsheet. but if your results are intermediate results generated in large script, then it would be good if they have a structure and in such case they can be put together and passed to function.

What was before?
Previously in my script I invented superduper basic simple data structure for my data - long term monthly mean pairs. The month number is a key and it has its corresponding calculated value. It is kinda pseudo 2D array, where each element is a small array containing respective key (month number) and value, altogether 12 elements.

Why again?
So, what happens if you run such long term monthly mean function in loop (each time changing something in previous calculations to get different long term monthly mean values, of course, otherwise there would have no meaning of it..)? You get multiple arrays with similar structure.
..
What can be done with such arrays? Well.. different things, of course, any statistics.. in my case, 50th percentile, called median (or vice versa). The next thing is practical - how to pass all these arrays to the specific function? Wrap them together.

How?
What is wrapping here? Superduper basic simple thing, actually - I created an "outer" array, I call it wrapper array, which I am appending with "long term monthly mean" pseudo 2D array and such an "outer array"
 is what I passed to the function.

What function does? Function takes apart such an "outer array" and reconfigures it by keys allowing to make a median calculation over the created reconfigured lists. Reconfigured array consists of number of keys of lists - list count is equal to key count - and each list consists of all values corresponding to the key. Then statistics can be done over each list and the result can passed to the function return (I prefer to return array with the similar structure like "long term monthly mean" structure - with pairs consisting of key and corresponding statistical value).

The full sample script can be seen here.

2012-11-12

another "long term monthly mean" script


I always try to simplify and optimize things when I write my scripts. When I firstly started to write some scripts which iterated over a dates, I didnt realize the importance of splitted datetime objects by its parts and I wrote these scripts complicated with different inner conditions and so on. This one is very basic and simple one - it takes apart the "datetime" and stores the value accordingly by the month in a 2D temporary array, where in the one dimension there are 12 months and in the another dimension - appended values. Then in another loop the values are averaged within each month and result is given as 2D array as a month-averaged value pair.

The function can be seen here - http://pastebin.com/YZEtkKh6

2012-09-24

24 min interval for Cron Job and cycling through file

 Simple approach, how to make cron job iterating over a list of a variables. Firstly, it is necessary store the list somewhere permanently. In simpliest case it can be text document and here the trick is to take only information from first line and then to put it on the end of file as a last element. array_push copies the first line of file $farray[0] as a last element of $farray, and then array_shift removes this first element.
$farray=file("file.txt");
$thisIsWhatINeed=$farray[0];
array_push($farray,$farray[0]);
array_shift($farray);
file_put_contents('file.txt', implode("",$farray));
I added a condition, because in my case there could be, that some part of cron job should not be executed at certain times. I have 54 runs per 24 hours; I calculated that cron job must run every 24 minutes. In 24 hours there are 1440 minutes; 54 runs could take maximum interval 26.666 minutes, but for convenience creating cron jobs (which is another aspect, written a little bit later) I set interval for 24 minutes between cron jobs and add 6 empty runs, which were taken in account using condition. Finally I had 60 runs for 24 hours, so i have 24 minutes as a equal interval between cron job runs.
And here comes another aspect - it is impossible to set intervals in cron, only set times. Thus i had to create 5 cron jobs with each defining (with 24 min step) when to start and run after 2 hours (because in 2 hours there can be 5x24 min intervals)

Two ways of defining upbeat bar length in Sibelius 7

Lately I had to give crash-course in music scores writing program Sibelus (version 7) and after that course I got telephone calls twice asking and precising some of very basic common tasks in the software. Despite I got the main problem - how to put an upbeat, I ended up struggling about how far and what exactly my student was doing working with Sibelius, because there are two ways how to do it.
Ok, so much for introduction, here go my solutions:

1) Easy way. Creating upbeat at quick start. When score writer has chosen what type of writer want to create, next thing is to choose different properties for scores, like time signature, tempo, key, etc. Under the Time signature setup there is option to create Pick-up bar with different length than it is defined by time signature. Easy as pie, but often forgettable.

2) Not so easy way. But, if score writer forgot to create upbeat at quick start, then it is still possible to make upbeat later. Here, writer have to select "Notations" tab to access "Time signature" dropdown selector. In dropdown selector there are common signatures, and under them there is button "More Options". And upbeat creation can be found there. Checkbox at "start with bar of length", choose length and press OK. After such manipulation mouse cursor becomes active and now it will create a bar with previously defined length in the place where score writer will click. So, because upbeat needs to be in the beginning of scores, score writer must click somewhere on the first bar or, better, on the key.


2012-08-13

Listen the difference

Try to find differences!
This is from movie Kick-Ass, 2010


And this is from one of my favorite horror movies - 28 weeks later, 2007

And this - this is The One which was in the beggining. 28 days later (2002

Between 28 days/months sequel and KickAss the answer is two note offset in main theme. Could it be called composer plagiated himself?