In [54]:
import pandas as pd
import numpy as np
import os
import pickle as pkl
from pprint import pprint
import re

Load pickle, extract events to pd.DataFrame


In [83]:
fn = 'DEBUG_1_2017-10-07_12.05.54_outputDict.pickle'
pp_num = fn.split('_')[1]
with open(fn, 'rb') as f:
     outDict = pkl.load(f)
events = outDict['eventArray']

# Also load prepared design
prepped_design = pd.read_csv('../designs/pp_%s/all_blocks/trials.csv' % str(pp_num).zfill(3))

In [84]:
dat = pd.DataFrame(columns=['trial_ID', 'trial_start_time', 'stimulus_onset_time'])
dat['trial_ID'] = prepped_design['trial_ID']
dat['block_trial_ID'] = prepped_design['block_trial_ID']

# Extract using regex
re1 = re.compile('trial (?P<trial_ID>[0-9]*) started at (?P<start_time>[0-9]+.[0-9]+)')
re2 = re.compile('trial (?P<trial_ID>[0-9]*) phase (?P<phase_num>[0-9]+) started at (?P<start_time>[0-9]+.[0-9]+)')

for trial_n, trial in enumerate(events):    
    for trial_event_n, trial_event in enumerate(trial):

        # Lists are events: keypressed, saccades, etc. Non-lists are trial timings
        if not isinstance(trial_event, list):
            
            # Check for trial start
            if re1.match(trial_event) is not None:
                d = re1.match(trial_event).groupdict()
                dat.loc[dat['trial_ID'] == int(d['trial_ID']), 'trial_start_time'] = float(d['start_time'])
            
            # Check for phase start
            elif re2.match(trial_event) is not None:
                d = re2.match(trial_event).groupdict()
                dat.loc[dat['trial_ID'] == int(d['trial_ID']), 'phase_' + d['phase_num'] + '_start'] = float(d['start_time'])

Restructure


In [85]:
# Drop all rows without a trial start time [instructions etc]
dat = dat.dropna(axis=0, subset=['trial_start_time']).reset_index(drop=True)

# The stimulus onset time is phase_4_start
dat['stimulus_onset_time'] = dat['phase_4_start'].copy()

# The cure onset time is phase_2_start
dat['cue_onset_time'] = dat['phase_2_start']

# The start of phase_0 is the recorded 'trial start time': the trial code starts, but waits for scanner pulse
dat['phase_0_start'] = dat['trial_start_time'].copy()

# The actual trial start time is the start time of phase 1: the pre-cue fixation cross is shown.
dat['trial_start_time'] = dat['phase_1_start']

# Get start time of first trial: this is the phase_1 start of the first trial
t0 = dat['phase_1_start'].min()
for col in dat.columns:
    if 'phase' in col or 'time' in col:
        dat[col] = dat[col] - t0

# Calculate actual durations of each phase: this is the start time of the 'next' phase minus the own start time
dat['phase_6'] = dat['phase_7_start'] - dat['phase_6_start']
dat['phase_5'] = dat['phase_6_start'] - dat['phase_5_start']
dat['phase_4'] = dat['phase_5_start'] - dat['phase_4_start']
dat['phase_3'] = dat['phase_4_start'] - dat['phase_3_start']
dat['phase_2'] = dat['phase_3_start'] - dat['phase_2_start']
dat['phase_1'] = dat['phase_2_start'] - dat['phase_1_start']
dat['phase_0'] = dat['phase_1_start'] - dat['phase_0_start']


dat[['phase_' + str(x) for x in range(0,7)] + ['trial_start_time', 'stimulus_onset_time']]


Out[85]:
phase_0 phase_1 phase_2 phase_3 phase_4 phase_5 phase_6 trial_start_time stimulus_onset_time
0 0.718097 1.502530 1.001858 0.000059 0.601132 0.000060 0.000026 0.000000 2.504447
1 2.90371 0.016283 1.001752 0.000069 0.601117 0.000081 0.000026 6.011258 7.029362
2 2.98829 2.253873 1.001790 0.000066 0.601077 0.000059 0.000018 12.005993 15.261722
3 2.13088 0.005200 1.001953 0.000060 0.601120 0.000053 0.000021 17.995099 19.002312
4 2.98758 2.237399 1.001930 0.000059 0.601085 0.000066 0.000017 23.994981 27.234369
5 2.17003 0.016226 1.001893 0.000057 0.601037 0.000058 0.000022 30.006926 31.025102
6 2.98183 0.005726 1.001958 0.000058 0.601049 0.000058 0.000022 35.995304 37.003046
7 2.9872 0.017126 1.001887 0.000058 0.601060 0.000060 0.000022 41.995323 43.014394
8 1.96903 1.487282 1.001892 0.000056 0.000125 0.600924 0.000054 50.994977 53.484207
9 2.90842 2.249911 1.001929 0.000057 0.000126 0.600999 0.000048 56.995069 60.246966
10 2.14577 2.261045 1.001948 0.000057 0.000129 0.600987 0.000047 62.995265 66.258315
11 2.13434 0.018380 1.001878 0.000055 0.000119 0.600932 0.000047 68.995135 70.015448
12 3.00447 2.253773 1.001911 0.000079 0.000133 0.600894 0.000080 75.008301 78.264064
13 2.1364 0.750855 1.001904 0.000044 0.000109 0.601073 0.000081 81.002954 82.755757
14 3.00515 2.253822 1.001484 0.000061 0.000163 0.601121 0.000064 87.014609 90.269976
15 2.1223 0.731918 1.001889 0.000057 0.000118 0.600931 0.000057 92.994971 94.728835
16 2.00615 1.500217 1.001945 0.000059 0.601067 0.000059 0.000023 101.995094 104.497315
17 2.89531 0.008765 1.001878 0.000060 0.601123 0.000059 0.000023 107.995097 109.005800
18 2.98412 0.738218 1.001896 0.000059 0.601093 0.000049 0.000013 113.995001 115.735174
19 2.98972 0.766013 1.001879 0.000057 0.601123 0.000057 0.000022 119.995259 121.763208
20 2.99547 0.009365 1.001900 0.000057 0.601089 0.000058 0.000022 125.995155 127.006477
21 2.98371 0.003839 1.001987 0.000056 0.601102 0.000056 0.000022 131.995241 133.001123
22 2.98897 2.219532 1.001899 0.000057 0.601120 0.000058 0.000025 137.995158 141.216646
23 2.18634 0.750750 1.001943 0.000084 0.600929 0.000066 0.000035 144.005512 145.758289
24 1.98744 0.750811 1.002043 0.000061 0.000111 0.600916 0.000060 153.005766 154.758681
25 2.96631 2.258895 1.002230 0.000049 0.000114 0.600922 0.000054 158.995280 162.256454
26 2.13641 2.253797 1.001902 0.000044 0.000109 0.600971 0.000046 164.995288 168.251031
27 2.14161 0.745919 1.001859 0.000049 0.000112 0.600648 0.000046 170.995075 172.742902
28 2.98192 0.005711 1.001968 0.000057 0.000123 0.600942 0.000053 176.995159 178.002895
29 2.98732 1.503129 1.001885 0.000058 0.000119 0.601008 0.000056 182.995280 185.500352
... ... ... ... ... ... ... ... ... ...
360 2.9793 2.254266 1.001900 1.752893 1.503088 0.000059 0.500995 3128.996155 3134.005214
361 1.99446 0.016148 1.001912 3.255783 1.502839 0.000045 0.500943 3138.013771 3142.287614
362 2.72065 1.502315 1.001947 2.504758 1.502755 0.000062 0.500969 3147.014008 3152.023028
363 1.98238 2.253703 1.001900 3.255784 1.502890 0.000048 0.500903 3156.014465 3162.525852
364 0.466052 0.750881 1.001820 1.753225 1.502917 0.000081 0.500856 3164.997850 3168.503776
365 2.97997 0.016436 1.001886 1.753285 1.502830 0.000054 0.500856 3173.998195 3176.769802
366 2.97965 1.472658 1.001932 2.504516 1.502759 0.000063 0.500821 3182.994956 3187.974062
367 2.00775 0.003464 1.001754 1.753169 1.503012 0.000090 0.501101 3191.994933 3194.753320
368 2.96256 2.224700 1.001880 2.504701 1.502956 0.000050 0.500818 3200.995062 3206.726343
369 1.26285 2.241849 1.001870 1.753312 1.502895 0.000056 0.500841 3209.994967 3214.991998
370 2.00201 0.751378 1.001907 1.001861 1.502470 0.000067 0.500913 3218.999643 3221.754789
371 2.97159 0.751168 1.001778 1.753421 1.503165 0.000060 0.500851 3227.999879 3231.506246
372 2.97237 0.014695 1.001880 1.001887 1.502834 0.000059 0.500913 3237.002303 3239.020765
373 2.97376 0.723663 1.001886 1.752938 1.502869 0.000045 0.501160 3245.994964 3249.473451
374 2.97359 0.740261 1.001870 1.753607 1.502883 0.000057 0.500911 3254.995086 3258.490824
375 2.96323 1.502302 1.001899 2.504697 1.502879 0.000060 0.500818 3264.001841 3269.010739
376 1.98167 2.253681 1.001915 1.753085 1.503076 0.000061 0.500850 3273.002172 3278.010853
377 1.97833 2.227838 1.001957 1.753302 1.502816 0.000058 0.500684 3281.994961 3286.978058
378 2.00492 0.007037 1.001892 1.001880 1.502493 0.000076 0.500798 3290.995301 3293.006110
379 2.98703 0.016206 1.001957 1.001739 1.503278 0.000059 0.500845 3300.002819 3302.022721
380 2.97495 2.261938 1.001893 3.256191 1.502806 0.000062 0.500535 3308.995311 3315.515333
381 0.482689 2.254002 1.001764 1.753511 1.502884 0.000061 0.500839 3318.003503 3323.012780
382 1.97682 2.262613 1.001940 2.504768 1.503135 0.000065 0.500474 3326.995312 3332.764633
383 1.22513 2.262988 1.001874 2.504703 1.502709 0.000047 0.500793 3335.995312 3341.764877
384 1.22668 0.016250 1.002101 3.256180 1.502814 0.000058 0.500920 3345.004586 3349.279117
385 2.70642 0.009383 1.001898 1.753275 1.502966 0.000067 0.500746 3353.995322 3356.759878
386 2.99873 0.016011 1.001871 2.504864 1.502689 0.000070 0.500939 3363.005383 3366.528129
387 2.96916 0.010383 1.001862 3.256102 1.502929 0.000047 0.500626 3371.994967 3376.263314
388 2.72225 2.248197 1.001868 3.256187 1.502878 0.000062 0.500823 3380.995068 3387.501320
389 0.488126 1.496822 1.002110 1.753346 1.502817 0.000056 0.500889 3389.995139 3394.247417

390 rows × 9 columns


In [94]:
dat['trial_start_time_block'] = dat['trial_start_time']
dat['stimulus_onset_time_block'] = dat['stimulus_onset_time']
dat['cue_onset_time_block'] = dat['cue_onset_time']

new_block_rows = np.where(dat['block_trial_ID']==0)
# Loop over blocks
for i in range(len(new_block_rows)):
    idx_start = new_block_rows[i]
    idx_end = new_block_rows[i+1] if i < (len(new_block_rows)-1) else dat.shape[0]
    print('Correcting row idx: %d-%d' % (idx_start, idx_end))
    
    t0_this_block = dat.loc[dat['trial_ID'] == idx_start, 'trial_start_time'].values[0]
    dat.loc[idx_start:(idx_end-1), 'trial_start_time_block'] = dat.loc[idx_start:(idx_end-1), 'trial_start_time_block'] - t0_this_block
    dat.loc[idx_start:(idx_end-1), 'cue_onset_time_block'] = dat.loc[idx_start:(idx_end-1), 'cue_onset_time_block'] - t0_this_block
    dat.loc[idx_start:(idx_end-1), 'stimulus_onset_time_block'] = dat.loc[idx_start:(idx_end-1), 'stimulus_onset_time_block'] - t0_this_block


Selecting idx: 0-8
Selecting idx: 8-16
Selecting idx: 16-24
Selecting idx: 24-32
Selecting idx: 32-40
Selecting idx: 40-48
Selecting idx: 48-127
Selecting idx: 127-206
Selecting idx: 206-298
Selecting idx: 298-390

In [101]:
compare_cols = ['trial_start_time_block', 'cue_onset_time_block', 'stimulus_onset_time_block']
dat[compare_cols] - prepped_design[compare_cols]


Out[101]:
trial_start_time_block cue_onset_time_block stimulus_onset_time_block
0 0.000000 0.002530 0.004447
1 0.011258 0.027541 0.029362
2 0.005993 0.009866 0.011722
3 -0.004901 0.000299 0.002312
4 -0.005019 -0.017620 -0.015631
5 0.006926 0.023152 0.025102
6 -0.004696 0.001030 0.003046
7 -0.004677 0.012449 0.014394
8 0.000000 -0.012718 -0.010770
9 0.000092 0.000003 0.001989
10 0.000288 0.011333 0.013338
11 0.000158 0.018538 0.020471
12 0.013324 0.017097 0.019087
13 0.007977 0.008832 0.010780
14 0.019632 0.023454 0.024999
15 -0.000006 -0.018088 -0.016142
16 0.000000 0.000217 0.002221
17 0.000003 0.008768 0.010706
18 -0.000093 -0.011875 -0.009920
19 0.000165 0.016178 0.018114
20 0.000061 0.009426 0.011383
21 0.000147 0.003986 0.006029
22 0.000064 -0.030404 -0.028448
23 0.010418 0.011168 0.013195
24 0.000000 0.000811 0.002915
25 -0.010486 -0.001591 0.000688
26 -0.010478 -0.006681 -0.004735
27 -0.010691 -0.014772 -0.012864
28 -0.010607 -0.004896 -0.002871
29 -0.010486 -0.007357 -0.005414
... ... ... ...
360 0.001091 0.005357 0.010150
361 0.018707 0.034855 0.042550
362 0.018944 0.021259 0.027964
363 0.019401 0.023104 0.030788
364 0.002786 0.003667 0.008712
365 0.003131 0.019567 0.024738
366 -0.000108 -0.027450 -0.021002
367 -0.000131 0.003333 0.008256
368 -0.000002 -0.025302 -0.018721
369 -0.000097 -0.008248 -0.003066
370 0.004579 0.005957 0.009725
371 0.004815 0.005983 0.011182
372 0.007239 0.021934 0.025701
373 -0.000100 -0.026437 -0.021613
374 0.000022 -0.009717 -0.004240
375 0.006777 0.009079 0.015675
376 0.007108 0.010789 0.015789
377 -0.000103 -0.022265 -0.017006
378 0.000237 0.007274 0.011046
379 0.007755 0.023961 0.027657
380 0.000247 0.012185 0.020269
381 0.008439 0.012441 0.017716
382 0.000248 0.012861 0.019569
383 0.000248 0.013236 0.019813
384 0.009522 0.025772 0.034053
385 0.000258 0.009641 0.014814
386 0.010319 0.026330 0.033065
387 -0.000097 0.010286 0.018250
388 0.000004 -0.001799 0.006256
389 0.000075 -0.003103 0.002353

390 rows × 3 columns