Note that there are some explanatory texts on larger screens.

plurals
  1. POAligning data table created from perl hash
    primarykey
    data
    text
    <p>I'm trying to write a script to process output from behavioral testing equipment. I need to have all data aligned by timestamp in the resulting CSV file. Here's the catch: the start time differs between test runs (it's close, but not exact - can be off by a few seconds to several minutes). I can get the output I want, and I think I have a good idea as to how I can align all variables, but don't know how to implement it.</p> <p>All data is in a hash with two levels ( %hash{id}{vars} ) with all variables stored as a number to keep things simple (variable names are read from an array on printout). Once all data has been scraped from the input files, the script walks through the hash and prints out data as follows:</p> <pre><code>Variable 1 ID #1 data1 data2 data3... ID #2 data1 data2 data3... ... Variable 2 ... </code></pre> <p>and so on.</p> <p>These are 24 h recordings. The last datapoint (var=20) for all subjects is light: data reads either "ON" or "OFF" for day and night. The best method of alignment I can see is to use the light OFF marker to align data.</p> <p>My thinking is as follows:<br> 1. Find first position for each ID for which var '20' = 'OFF' and record position<br> 2. Figure out which ID has the greatest position for OFF (ie, the one that started recording earliest)<br> 3. Add empty value pairs to every other subject until OFF position is the same for all. </p> <p>For example, if data is recorded once per minute and one subject has an OFF time that is 5 minutes later than all others, add 5 empty data points to all other subjects to align the data.</p> <p>This would have to be done for all datapoints for each subject, not just the lights on/off measure.</p> <p>Would this approach work? And if so, how could I implement this?</p> <p>**Note that I need to be able to package this as a standalone script to run on multiple computers, so I can't count on perl modules that aren't installed by default.</p> <p>--edit per request: example. Input data looks like this (it's a CSV file)</p> <pre><code>ID, TIME, DATA1, DATA2, DATA3, [...] , LIGHT Subj1, 10:00:00, data1, data2, data3, [...] , ON Subj1, 10:00:30, data1, data2, data3, [...] , ON Subj1, 10:01:00, data1, data2, data3, [...] , OFF Subj1, 10:01:00, data1, data2, data3, [...] , OFF </code></pre> <p>For another subject, data might look like this:</p> <pre><code>ID, TIME, DATA1, DATA2, DATA3, [...] , LIGHT Subj2, 09:59:27, data1, data2, data3, [...] , ON Subj2, 09:59:57, data1, data2, data3, [...] , ON Subj2, 10:00:27, data1, data2, data3, [...] , ON Subj2, 10:00:57, data1, data2, data3, [...] , OFF Subj2, 10:01:27, data1, data2, data3, [...] , OFF </code></pre> <p>Script takes each line from all files and adds them to a hash keyed by ID, with one level for each data column keyed by column number. For these two files hash would look like this:</p> <pre><code>$VAR1 = { 'Subj1' =&gt; { '1' =&gt; [ data1 data1 ... ] '2' =&gt; [ data2 data2 ... ] ... '20' =&gt; [ ON ON ... } 'Subj1' =&gt; { '1' =&gt; [ data1 data1 ... ] '2' =&gt; [ data2 data2 ... ] ... '20' =&gt; [ ON ON ... } }; </code></pre> <p>Data is output with a foreach loop:</p> <pre><code>foreach my $k (sort {$a cmp $b} keys %data) { print OUT $k, "\,"; foreach my $d ( @{ $data{$k}{$i} } ) { print OUT $d, "\,"; } print OUT "\n"; } </code></pre> <p>Output looks like this:</p> <pre><code>TIME Subj1, 10:00:00, 10:00:30, 10:01:00, 10:01:30, Subj2, 09:59:27, 09:59:57, 10:00:27, 10:00:57, 10:01:27, DATA1 Subj1, data1, data1, data1, data1, data1, Subj2, data2, data2, data2, data2, data2, data2, [ ... all other data ... ] LIGHT Subj1, ON, ON, OFF, OFF, Subj2, ON, ON, ON, OFF, OFF, </code></pre> <p>What I need to do is align all data by the ON/OFF columns in LIGHT, by adding empty values like so:</p> <pre><code>TIME Subj1, , 10:00:00, 10:00:30, 10:01:00, 10:01:30, Subj2, 09:59:27, 09:59:57, 10:00:27, 10:00:57, 10:01:27, DATA1 Subj1, , data1, data1, data1, data1, data1, Subj2, data2, data2, data2, data2, data2, data2, [ ... all other data ... ] LIGHT Subj1, , ON, ON, OFF, OFF, Subj2, ON, ON, ON, OFF, OFF, </code></pre> <p>Trying to figure out how best to do this. Sorry this is long...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload