-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmriResponseSummary_RH.py
1368 lines (1094 loc) · 59.6 KB
/
fmriResponseSummary_RH.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
### Script processes fMRI behavioral response files and produces event-by-event and summary files. Flexible to process many different types of behavioral tasks. Returns .csv event-by-event and summary and .mat files that can be used in SPM for single-subject analyses.
### Input can be a 1) single file, 2) MRI scanner name, 3) MRI scanner ID (incomplete IDs partially supported), 4) .txt file with individual elements on each line. Elements in 4) can be of any type 1)-3).
### Example commands
## python3 fmriResponseSummary_RH.py /rawdata/mr-rh/MRraw/prisma/p086_ln/faces2_p086_ln_1763.txt
## python3 fmriResponseSummary_RH.py faces2_p086_ln_1763.txt (expects file in current dir)
## python3 fmriResponseSummary_RH.py p123
## python3 fmriResponseSummary_RH.py p1 (will process all files for all subjects starting with "p1", e.g., p12, p100, p199, etc.)
## python3 fmriResponseSummary_RH.py prisma (will process all files for all prisma scan folders)
### Tasks supported
## Hariri Faces Task (20180109); updated 20220707
## Reward Task (20180109); updated 20220706
## Alcohol Task (20180109)
## PSAP (20220708)
## RLP (20220628)
## Breathing (20230117)
## Loss Aversion (20230122) - CBS Entrepreneurship
### Updates
## 20201111 - work with MR001 datas
### Patrick M. Fisher
# Load relevant libraries
from sys import argv
#from sys import exit
import re
import pandas as pd
import numpy as np
from os import listdir
from os import path
from io import open
import os
import io
import time
import scipy.io as sio
#import itertools
# Input from command line
script, input1 = argv
class Response():
def __init__(self):
""" Initialize variables """
self.input1 = input1
# Dictionary of methods for processing different input types
self.inputDict = {'processSingleFile': self.processSingleFile, 'processListFile': self.processListFile, 'processScanner': self.processScanner, 'processSubjID': self.processSubjID}
# Dictionary of methods for processing different task types
self.taskDict = {'VAS': self.evalAlcohol, 'Reward': self.evalReward, 'Faces': self.evalFaces, 'RLP': self.evalRLP, 'PSAP': self.evalPSAP, 'aarhus_music': self.evalAarhus, 'breath': self.evalBreath, 'LA_fMRI': self.evalLossAversion}
self.outputName = {'VAS': 'alcohol', 'Reward': 'reward', 'Faces': 'faces', 'RLP': 'rlp', 'PSAP': 'psap', 'aarhus_music': 'aarhus', 'breath': 'breath', 'LA_fMRI': 'lossAversion'}
# MRI scanner name scheme
self.scannerNames = {'p': 'prisma', 'v': 'verio', 'm': 'mmr', 'n': 'mr001'}
# If not looking in local directory, look here
self.top = '/rawdata/mr-rh/MRraw/'
# Regexp for expected behavioral task file name structure
# self.allMatchTypes = r'^(VAS).*(.txt)$|^(faces[0-9].*(.txt)$|^(reward).*(.txt)$)'
self.allMatchTypes = r'^(VAS).*(.txt)$|^(HaririFaces[0-9]_dansk).*(.txt)$|^(Hariri_Reward_TC_dansk2_noTrigger).*(.txt)$|^(RLP).*(.txt)$|^(Events).*(.txt)$|^(aarhus-music).*(.txt)$|^(breathing).*(.txt)$|^(LA_fMRI_FourRuns).*(.txt)$'
# Updated while processing inputs
self.currTaskType = ''
self.currSubjID = ''
self.currSourceFile = ''
self.currTime = time.strftime('%c')
self.outFileSuffix = None
# SPM related structure (.mat file output)
self.spmStruct = {'names': [], 'onsets': [], 'durations': []}
def mapInput(self, input1):
""" Map input to appropriate type """
fullFilePath = None
# Inputs starting with "/" assumed to be full path information
if re.search('^(/).*(.txt)$', input1):
fullFilePath = input1
if not path.isfile(fullFilePath):
raise ValueError('%s does not exist' % fullFilePath)
# Inputs with .txt suffix and without full path information assumed to be in current directory
elif re.search('(.txt)$', input1):
fullFilePath = '/'.join([os.getcwd(), input1])
if not path.isfile(fullFilePath):
raise ValueError('%s does not exist' % fullFilePath)
# If a filename specified as input1
if fullFilePath:
try:
with open(fullFilePath, 'r', encoding='utf-16') as f:
if f.readline().rstrip() == '*** Header Start ***':
return ('processSingleFile', fullFilePath)
else:
return ('processListFile', fullFilePath)
except UnicodeError:
try:
# PSAP file exception (BOM, not utf-16)
with open(fullFilePath, 'r') as f:
if re.search('(Time).*(Event).*(Number)', f.readline().rstrip()):
return ('processSingleFile', fullFilePath)
except UnicodeError:
return ('processListFile', fullFilePath)
# Check if input matches MRI scanner name
if input1 in [v for k,v in self.scannerNames.items()]:
return ('processScanner', input1)
# If input matches MRI scanner "prefix" (keys in self.scannerNames), assume it is a subject ID
if re.search(r'^[' + re.escape(''.join(self.scannerNames.keys())) + ']', input1):
return ('processSubjID', input1)
def processSingleFile(self, fName):
""" Process single .txt file input """
# Pass filename forward
return [fName]
def processSubjID(self, subjID):
""" Process subjID input """
# All .txt files matching criteria for fMRI behavioral files
allMatches = []
# Determine MRI scanner name
if re.search(r'^[' + re.escape(''.join(self.scannerNames.keys())) + ']', subjID):
scanner = self.scannerNames[subjID[0]]
else:
raise ValueError('No scanner match for %s ' % subjID)
scannerFolder = ''.join([self.top, scanner])
# Find all subject IDs that START with "subjID" (can be >1)
subjIDFolder = [idx for idx in listdir(scannerFolder) if idx.startswith(subjID)]
if len(subjIDFolder) == 0:
raise ValueError('No folder match found %s' % subjID)
fullFolderPath = [''.join([self.top, scanner, '/', idx]) for idx in subjIDFolder]
# If only one match found, ensure stored as list
if type(fullFolderPath) is str:
fullFolderPath = [fullFolderPath]
# Identify all .txt files matching criteria for fMRI behavioral files among matches to "subjID"
for folderName in fullFolderPath:
currFolderList = listdir(folderName)
currFolderMatches = ['/'.join([folderName, idx]) for idx in currFolderList if re.search(self.allMatchTypes, idx)]
allMatches += currFolderMatches
# Return list of .txt files for further processing
return allMatches
def processScanner(self, scanner):
""" Process scanner input """
# All .txt files matching criteria for fMRI behavioral files
allMatches = []
scannerFolder = ''.join([self.top, scanner])
# List of folders (presumably subject IDs) in scanner folder
foldersList = listdir(scannerFolder)
# Identify all .txt files matching criteria for fMRI behavioral files among subjects' folders
for folderName in foldersList:
tmpNameFull = '/'.join([scannerFolder, folderName])
# Make sure tmpNameFull is a folder
if os.path.isdir(tmpNameFull):
currFolderList = listdir(tmpNameFull)
currFolderMatches = ['/'.join([tmpNameFull, idx]) for idx in currFolderList if re.search(self.allMatchTypes, idx)]
allMatches += currFolderMatches
# Return list of .txt files for further processing
return allMatches
def processListFile(self, fName):
""" Process list (.txt file) input """
# List of inputs from .txt file input
idList = []
# All .txt files matching criteria for fMRI behavioral files
allMatches = []
with open(fName, 'r') as f:
for line in f:
l = line.rstrip()
idList.append(l)
# Map each line in list. Enables list to contain subject ID, MRI scanner name, or files
inputInfo = [self.mapInput(idx) for idx in idList]
for idx in inputInfo:
allMatches += self.inputDict[idx[0]](idx[1])
# Return list of .txt files for further processing
return allMatches
def taskIdentify(self, fName):
""" Identify task for given .txt file """
# Update attribute
self.currSourceFile = fName
fileParts = fName.split('/')
regExpStr = r'^[' + ''.join(self.scannerNames.keys()) + '][0-9][0-9][0-9]'
# Determine subject ID based on parts of file name
idMatches = [idx for idx in fileParts if re.search(regExpStr, idx)]
# Convert list to string and update attribute
if idMatches:
self.currSubjID = ''.join(idMatches)
else:
self.currSubjID = ''
# Extract two pieces of information from fMRI behavioral file
try:
with open(fName, 'r', encoding='utf-16') as f:
for line in f:
if line.startswith('Experiment: '):
l = line.rstrip()
l_split = l.split(' ')
self.currTaskType = ''.join([idx for idx in self.taskDict.keys() if idx in l_split[1]])
# If subject ID has not been determined, use DataFile.Basename from file
if self.currSubjID == '' and line.startswith('DataFile.Basename'):
l = line.rstrip()
l_split = l.split(' ')
self.currSubjID = l_split[1]
except UnicodeError:
try:
with open(fName, 'r') as f:
if re.search('(Time).*(Event).*(Number)', f.readline().rstrip()):
print('here i am')
self.currTaskType = 'PSAP'
except UnicodeError:
pass
# Return method for identified task type
return self.taskDict[self.currTaskType]
def printFile (self, fullFileName):
""" Print response file """
with io.open(fullFileName, 'r', encoding = 'utf-16') as f:
for line in f:
print(str(line.rstrip()).lstrip())
def evalLossAversion(self, fullFileName):
""" Evaluate faces behavioral response file """
# column names
colEvents = ['onset', 'duration', 'run_num', 'trial_num', 'stimulus_type', 'target_person', 'gamble_side', 'win_side',
'win_amount', 'loss_amount', 'response_time', 'response', 'iti']
# variables to keep as strings
keepString = {'Procedure: GambleG[L|R]W[L|R]': 'block_name',
'outperson': 'target_person'}
# variables to keep as numeric
keepNumeric = {'ITI': 'iti',
'WinAmount': 'win_amount',
'LossAmount': 'loss_amount',
'(Gamble).*(.OnsetTime)': 'gamble_on',
'(Fixation).*(.OnsetTime)': 'fix_on',
'(GamblePresent).*(RT:)': 'gamble_rt',
'(Fixation).*(RT:)': 'fix_rt',
'(GamblePresent).*(RESP)': 'gamble_resp',
'(Fixation).*(RESP)': 'fix_resp',
'^(SelfList[0-9]{1}|Charity[0-9]{1}List).Sample': 'trial_num_current',
'^(Level: 2)$': 'level'}
dfEvent = pd.DataFrame(columns=colEvents) # event dataframe
trialCounter = 0
runCounter = 0
practice = True
# Process fMRI behavioral file
with io.open(fullFileName, 'r', encoding='utf-16') as f:
for line in f:
l = str(line.rstrip()).lstrip()
# signals end of paradigm
if l == 'Level: 1':
f.close()
currTrial['level'] = 1
dfEvent, trialCounter = self.eventLossAversion(currTrial, dfEvent, trialCounter, runCounter)
dfEvent.replace(np.nan, 'n/a', inplace=True)
return {'event': dfEvent, 'summary': None}
# new trial identifier
# avoids evaluating practice trials
if practice is True and re.search('^(Procedure: GambleG[L|R]W[L|R])$', l):
practice = False
trialCounter += 1
runCounter += 1
currTrial = {'trial_num': trialCounter} # current trial information
if re.search('^(\*\*\* LogFrame Start \*\*\*)', l):
if practice:
continue
trialCounter += 1
dfEvent, trialCounter = self.eventLossAversion(currTrial, dfEvent, trialCounter, runCounter)
if trialCounter is 0:
practice = True
dfEvent = pd.DataFrame(columns=colEvents) # event dataframe
currTrial = {'trial_num': trialCounter}
if practice is True:
continue
# pull relevant field for string and numeric values of interest
strCheck = [v for k, v in keepString.items() if re.search(k, l)]
numCheck = [v for k, v in keepNumeric.items() if re.search(k, l)]
if strCheck:
l_split = l.split(': ')
if l_split[1] == 'For velgørenhed':
l_split[1] = 'For velgoerenhed'
currTrial[''.join(strCheck)] = l_split[1]
elif numCheck:
l_split = l.split(': ')
if len(l_split) == 2:
currTrial[''.join(numCheck)] = int(l_split[1])
else:
currTrial[''.join(numCheck)] = None
# Return event data frame (summary not written)
return {'event': dfEvent, 'summary': None}
def eventLossAversion(self, currTrial, dfEvent, trialCounter, runCounter):
""" Organize current trial and update dfEvent """
if all([k in ['trial_num', 'level'] for k in currTrial.keys()]):
dfEvent = dfEvent.assign(run_num=runCounter)
dfEvent['onset'] = (dfEvent['onset'] - dfEvent.iloc[0]['onset'])/1000
dfEvent['response_time'] = [i/1000 if isinstance(i,int) else 'n/a' for i in dfEvent['response_time']]
dfEvent['iti'] = [i / 1000 if isinstance(i, int) else 'n/a' for i in dfEvent['iti']]
dfEvent['duration'] = np.append(np.delete(np.array(dfEvent['onset'].diff()), 0), dfEvent['iti'].iloc[-1] / 1000)
self.outFileSuffix = 'run-' + str(runCounter)
dfEvent.replace(np.nan, 'n/a', inplace=True)
if currTrial['level'] is not 1:
self.writeRespCsv({'event': dfEvent, 'summary': None})
trialCounter = 0
return (dfEvent, trialCounter)
nrep = 2
if re.search('^(GambleGL)',currTrial['block_name']):
gamble_side = 'L'
elif re.search('^(GambleGR)',currTrial['block_name']):
gamble_side = 'R'
if re.search('^(GambleG[L|R]WL)',currTrial['block_name']):
win_side = 'L'
elif re.search('^(GambleG[L|R]WR)',currTrial['block_name']):
win_side = 'R'
if currTrial['gamble_resp'] is None:
currTrial['gamble_resp'] = 'n/a'
if currTrial['fix_resp'] is None:
currTrial['fix_resp'] = 'n/a'
if currTrial['gamble_rt'] is 0:
currTrial['gamble_rt'] = 'n/a'
if currTrial['fix_rt'] is 0:
currTrial['fix_rt'] = 'n/a'
# update currEvent
currEvent = {'onset': [currTrial['gamble_on'], currTrial['fix_on']], 'duration': np.repeat('n/a', nrep),
'run_num': np.repeat('n/a', nrep), 'trial_num': np.repeat(currTrial['trial_num_current'], nrep),
'stimulus_type': ['gamble', 'fixation'],
'target_person': np.repeat(currTrial['target_person'], nrep),
'gamble_side': [gamble_side, 'n/a'],
'win_side': [win_side, 'n/a'],
'win_amount': [currTrial['win_amount'], 'n/a'],
'loss_amount': [currTrial['loss_amount'], 'n/a'],
'response': [currTrial['gamble_resp'], currTrial['fix_resp']],
'response_time': [currTrial['gamble_rt'], currTrial['fix_rt']],
'iti': ['n/a', currTrial['iti']]}
# add currEvent to dfEvent
dfEvent = dfEvent.append(pd.DataFrame.from_dict(currEvent), ignore_index=True)
# return updated dfEvent
return (dfEvent, trialCounter)
def evalBreath(self, fullFileName):
""" Evaluate Breathing behavioral response file """
# column names
eventInfo = {'onset': [], 'duration': [], 'event_num': [], 'response': []}
# Process fMRI behavioral file
with io.open(fullFileName, 'r', encoding='utf-16') as f:
for line in f:
l = str(line.rstrip()).lstrip()
if re.search('KeyboardStimulus.OnsetTime',l):
l_split = l.split(': ')
startTime = int(l_split[1])
eventInfo['onset'].append(0)
eventInfo['event_num'].append(0)
eventInfo['response'].append('n/a')
elif re.search('KeyboardStimulus.OffsetTime',l):
l_split = l.split(': ')
endTime = int(l_split[1])
elif re.search('^(KeyPress[0-9]+RT)',l):
l_split = l.split(': ')
eventInfo['onset'].append((int(l_split[1]) - startTime)/1000)
eventInfo['event_num'].append(len(eventInfo['event_num']))
elif re.search('^(KeyPress[0-9]+RESP)',l):
l_split = l.split(': ')
eventInfo['response'].append(int(l_split[1]))
# calculate durations
durations = [np.round(eventInfo['onset'][i+1]-eventInfo['onset'][i],3) for i in range(len(eventInfo['onset'])-1)]
durations.append(np.round((endTime-startTime)/1000 - eventInfo['onset'][-1],3))
eventInfo['duration'] = durations
dfEvent = pd.DataFrame(columns=eventInfo.keys()) # event dataframe
dfEvent = dfEvent.append(pd.DataFrame.from_dict(eventInfo), ignore_index=True)
# Return event data frame (summary not written)
return {'event': dfEvent, 'summary': None}
def evalPSAP(self, fullFileName):
""" Evaluate PSAP behavioral response file """
colEvents = ['trial_num', 'option', 'onset', 'duration', 'provocation_time', 'presses']
dfEvent = pd.DataFrame(columns = colEvents) # event dataframe
trialCounter = 0
readLine = False
with io.open(fullFileName,'r') as f:
for line in f:
l = str(line.strip()).lstrip()
if re.search('Test',l):
readLine = True
continue
if readLine:
if re.search('Begin',l):
trialCounter += 1
currTrial = {'trial_num': trialCounter, 'provocation_time': 'n/a', 'presses': 'n/a'}
currTrial['onset'] = int(l.split(' ')[0])/1000
currTrial['option'] = l.split(' ')[-1]
elif re.search('End',l):
currTrial['duration'] = int(l.split(' ')[0])/1000 - currTrial['onset']
dfEvent = dfEvent.append(pd.DataFrame(currTrial, index = [0]), ignore_index = True)
elif re.search('Provocation',l):
currTrial['provocation_time'] = int(l.split(' ')[0])/1000
currTrial['presses'] = int(l.split(' ')[-1])
else:
pass
if 'duration' not in currTrial.keys():
currTrial['duration'] = 'n/a'
dfEvent = dfEvent.append(pd.DataFrame(currTrial, index = [0]), ignore_index = True)
if 'Opt1' in dfEvent['option']:
dfEvent.at[dfEvent['option']=='Opt1','option'] = 'monetary'
dfEvent.at[dfEvent['option']=='Opt2','option'] = 'stealing'
dfEvent.at[dfEvent['option']=='Opt3','option'] = 'protection'
else:
dfEvent.at[dfEvent['option']=='Opt2','option'] = 'monetary'
dfEvent.at[dfEvent['option']=='Opt3','option'] = 'stealing'
dfEvent.at[dfEvent['option']=='Opt4','option'] = 'protection'
print(dfEvent)
# Return event data frame (summary not written)
return {'event': dfEvent, 'summary': None}
def evalAarhus(self,fullFileName):
""" Evaluate Aarhus music behavioral response file """
# column names
colEvents = ['stimulus', 'onset', 'duration', 'file']
# variables to keep as strings
keepString = {'Procedure': 'music_file'}
# variables to keep as numeric
keepNumeric = {'^(FixOnly.OnsetTime)': 'fix_on', '^(FixMusic[1-4].OnsetTime)': 'music_on'}
paradigmInfo = {'music_file': [], 'fix_on': [], 'music_on': []}
# Process fMRI behavioral file
with io.open(fullFileName,'r', encoding='utf-16') as f:
for line in f:
l = str(line.rstrip()).lstrip()
# pull relevant field for string and numeric values of interest
strCheck = [v for k,v in keepString.items() if re.search(k,l)]
numCheck = [v for k,v in keepNumeric.items() if re.search(k,l)]
if strCheck:
paradigmInfo[''.join(strCheck)].append(l.split(': ')[1])
if numCheck:
paradigmInfo[''.join(numCheck)].append(int(l.split(': ')[1]))
# create variables for df
stimulus = ['fixation','music','fixation','music','fixation','music','fixation','music','fixation']
files = ['n/a',paradigmInfo['music_file'][0],'n/a',paradigmInfo['music_file'][1],'n/a',paradigmInfo['music_file'][2],'n/a',paradigmInfo['music_file'][3],'n/a']
allonsets = paradigmInfo['fix_on'] + paradigmInfo['music_on']
allonsets.sort()
onsets = [(i-allonsets[0])/1000 for i in allonsets]
durations = [onsets[i]-onsets[i-1] for i in range(1,len(onsets))]
durations.append(10)
currEvent = {'stimulus': stimulus, 'onset': onsets, 'duration': durations, 'file': files}
dfEvent = pd.DataFrame(columns = colEvents) # event dataframe
dfEvent = dfEvent.append(pd.DataFrame.from_dict(currEvent), ignore_index = True)
# Return event data frame (summary not written)
return {'event': dfEvent, 'summary': None}
def evalFaces(self, fullFileName):
""" Evaluate faces behavioral response file """
# list of stimuli
stimulus_list = ['Shapes', 'Fear', 'Neutral', 'Angry', 'Surprise']
# column names
colEvents = ['block_name', 'block_num', 'block_subtype', 'trial_num', 'stimulus_type', 'onset', 'duration', 'response_time', 'response', 'correct_response', 'stimulus_file']
# variables to keep as strings
keepString = {'TrialCondition': 'block_name', 'Procedure: (' + '|'.join(stimulus_list) + ')(BlockProc|FacesBlock)': 'block_name', 'Procedure: (' + '|'.join(stimulus_list) + ')(Trial|Faces)Proc': 'stimulus_type', 'Experiment': 'run_type', 'Stimulus': 'stimulus_file'}
# variables to keep as numeric
keepNumeric = {'(' + '|'.join(stimulus_list) + ')(TrialProbe|FacesProcProbe)(.OnsetTime)': 'stim_on', '^(' + '|'.join(stimulus_list) + ')(TrialProbe|FacesProcProbe)(.RT)': 'rt', '^(' + '|'.join(stimulus_list) + ')(TrialProbe|FacesProcProbe)(.RESP)': 'resp', '^(' + '|'.join(stimulus_list) + ')(TrialProbe|FacesProcProbe)(.CRESP)': 'cresp', '^(' + '|'.join(stimulus_list) + ')(TrialFixation|FacesProcFix)(.OnsetTime)': 'fix_on', '^(FacesRunBlockList)': 'block_num', '^(Match)(Shapes|Faces)(.OnsetTime)': 'onset'}
dfEvent = pd.DataFrame(columns = colEvents) # event dataframe
trialCounter = 0
currTrial = {'trial_num': trialCounter} # current trial information
# Process fMRI behavioral file
with io.open(fullFileName,'r', encoding='utf-16') as f:
for line in f:
l = str(line.rstrip()).lstrip()
# signals end of paradigm
if l=='Procedure: FacesRunProc':
f.close()
dfEvent = self.eventFaces({}, dfEvent, trialCounter)
dfEvent.replace(np.nan,'n/a',inplace=True)
return {'event': dfEvent, 'summary': None}
# new trial identifier
if re.search('^(\*\*\* LogFrame Start \*\*\*)',l):
trialCounter += 1
if trialCounter > 1:
dfEvent, trialCounter = self.eventFaces(currTrial, dfEvent, trialCounter)
currTrial = {'trial_num': trialCounter}
# pull relevant field for string and numeric values of interest
strCheck = [v for k,v in keepString.items() if re.search(k,l)]
numCheck = [v for k,v in keepNumeric.items() if re.search(k,l)]
if strCheck:
l_split = l.split(': ')
currTrial[''.join(strCheck)] = l_split[1]
elif numCheck:
l_split = l.split(': ')
if len(l_split) == 2:
currTrial[''.join(numCheck)] = int(l_split[1])
if ''.join(numCheck) == 'rt' and currTrial[''.join(numCheck)] == 0:
currTrial[''.join(numCheck)] = None
else:
currTrial[''.join(numCheck)] = None
# Return event data frame (summary not written)
return {'event': dfEvent, 'summary': None}
def eventFaces(self, currTrial, dfEvent, trialCounter):
""" Organize current trial and update dfEvent """
stimulus_list = ['Shapes', 'Fear', 'Neutral', 'Angry', 'Surprise']
# processing end of paradigm
if len(currTrial) == 0:
# convert onsets to seconds
dfEvent['onset'] = (dfEvent['onset']-dfEvent.loc[0,'onset'])/1000
# calculate durations
for elem in dfEvent.index:
if elem < len(dfEvent.index)-1:
dfEvent.loc[elem,'duration'] = dfEvent.loc[elem+1,'onset']-dfEvent.loc[elem,'onset']
else:
dfEvent.loc[elem,'duration'] = 2
# return updated dfEvent (trialCounter need not be returned)
return(dfEvent)
nrep = 2 # faces trials comprise four events (stimulus, fixation)
# "match shapes" and "match faces" events handled differently
if all([i in currTrial.keys() for i in ['trial_num', 'block_name', 'block_num', 'onset']]):
#currTrial['outcome'] = 'Instruction'
if re.search('Shapes', currTrial['block_name']):
currTrial['stimulus_type'] = 'MatchShapes'
else:
currTrial['stimulus_type'] = 'MatchFaces'
dfEvent = dfEvent.append(pd.DataFrame(currTrial, index = [0]), ignore_index = True)
dfEvent.loc[dfEvent['block_name'].isnull(), 'block_name'] = currTrial['block_name']
dfEvent.loc[dfEvent['block_num'].isnull(), 'block_num'] = currTrial['block_num']
dfEvent.loc[dfEvent.index[-1], 'trial_num'] = None
# update row order; guess number/press finger comes after block trials in paradigm file
currBlockIdx = dfEvent[dfEvent['block_num'] == currTrial['block_num']].index
prevBlockIdx = dfEvent[dfEvent['block_num'] != currTrial['block_num']].index
dfEvent.loc[currBlockIdx[-1],'block_name'] = 'Instruction'
dfEvent = dfEvent.reindex(list(prevBlockIdx) + [currBlockIdx[-1]] + list(currBlockIdx[:-1]))
dfEvent = dfEvent.reset_index(drop=True)
trialCounter -= 1
else:
currOnsets = [currTrial['stim_on'], currTrial['fix_on']]
currTrial['block_subtype'] = ''.join([idx for idx in stimulus_list if re.search(idx,currTrial['stimulus_type'])])
# update currEvent
currEvent = {'trial_num': np.repeat(currTrial['trial_num'],nrep), 'block_name': np.repeat(currTrial['block_name'],nrep), 'block_subtype': np.repeat(currTrial['block_subtype'],nrep), 'stimulus_type': ['stimulus', 'fixation'], 'onset': currOnsets, 'duration': np.repeat('n/a',nrep), 'response_time': [currTrial['rt'], 'n/a'], 'response': [currTrial['resp'], 'n/a'], 'correct_response': [currTrial['cresp'], 'n/a'], 'stimulus_file': currTrial['stimulus_file']}
# add currEvent to dfEvent
dfEvent = dfEvent.append(pd.DataFrame.from_dict(currEvent), ignore_index = True)
# return updated dfEvent
return(dfEvent, trialCounter)
def evalFaces_old(self, fullFileName):
""" Evaluate faces behavioral response file """
# Update attribute to incorporate version of faces task (i.e., 1,2,3, or 4)
self.outputName['Faces'] = ''.join(['faces', self.currSourceFile.split('HaririFaces')[1][0]])
colEvents = ['type', 'subtype', 'imgName', 'rt', 'resp', 'cresp', 'acc', 'omitted']
colSummary = ['RT_corrOnly', 'RT_all', 'accuracy', 'correct', 'incorrect', 'omitted']
rowSummary = ['shapes', 'fear', 'angry', 'neutral', 'surprise', 'faces']
dfEvent = pd.DataFrame(columns = colEvents)
dfSummary = pd.DataFrame(index = rowSummary, columns = colSummary)
# New trial information identifiers.
trialType = {'Procedure: ShapesTrialProc': 'shapes', 'Procedure: FearFacesProc': 'fear', 'Procedure: NeutralFacesProc': 'neutral', 'Procedure: AngryFacesProc': 'angry', 'Procedure: SurpriseFacesProc': 'surprise'}
# Process fMRI behavioral file
with io.open(fullFileName,'r', encoding='utf-16') as f:
for line in f:
l = str(line.rstrip()).lstrip()
# trial subtype
if l in trialType.keys():
dfEvent.loc[len(dfEvent)+1] = pd.Series({'subtype': trialType[l]})
# trial type
if re.search('^(TrialCondition)', l):
l_split = l.split(' ')
dfEvent.at[len(dfEvent), 'type'] = l_split[1]
# accuracy
if re.search('.*(Probe).*(ACC).*', l):
l_split = l.split(' ')
dfEvent.at[len(dfEvent), 'acc'] = int(l_split[1])
# reaction time
if re.search('.*(Probe).*(\.RT:).*', l):
l_split = l.split(' ')
if len(l_split) == 2:
dfEvent.at[len(dfEvent), 'rt'] = int(l_split[1])
dfEvent.at[len(dfEvent), 'omitted'] = False
else:
dfEvent.at[len(dfEvent), 'omitted'] = True
# actual response
if re.search('.*(Probe).*(\.RESP).*', l):
l_split = l.split(' ')
if len(l_split) == 2:
dfEvent.at[len(dfEvent), 'resp'] = int(l_split[1])
# correct response
if re.search('.*(Probe).*(\.CRESP).*', l):
l_split = l.split(' ')
dfEvent.at[len(dfEvent), 'cresp'] = int(l_split[1])
# stimulus (image file name)
if re.search('^(Stimulus)', l):
l_split = l.split(' ')
dfEvent.at[len(dfEvent), 'imgName'] = l_split[1]
# Compute summary measures
for k,v in trialType.items():
dfSummary.at[v, 'RT_corrOnly'] = np.mean(dfEvent.loc[(dfEvent['subtype'] == v) & dfEvent['acc'] == 1, 'rt'])
dfSummary.at[v, 'RT_all'] = np.mean(dfEvent.loc[dfEvent['subtype'] == v, 'rt'])
dfSummary.at[v, 'accuracy'] = np.mean(dfEvent.loc[dfEvent['subtype'] == v, 'acc'])
dfSummary.at[v, 'correct'] = np.sum(dfEvent.loc[dfEvent['subtype'] == v, 'acc'])
dfSummary.at[v, 'omitted'] = np.sum(dfEvent.loc[dfEvent['subtype'] == v, 'omitted'])
dfSummary.at[v, 'incorrect'] = np.sum(dfEvent['subtype'] == v) - dfSummary.at[v, 'correct'] - dfSummary.at[v, 'omitted']
dfSummary.at['faces', 'RT_corrOnly'] = np.mean(dfEvent.loc[(dfEvent['type'] == 'faces') & dfEvent['acc'] == 1, 'rt'])
dfSummary.at['faces', 'RT_all'] = np.mean(dfEvent.loc[dfEvent['type'] == 'faces', 'rt'])
dfSummary.at['faces', 'accuracy'] = np.mean(dfEvent.loc[dfEvent['type'] == 'faces', 'acc'])
dfSummary.at['faces', 'correct'] = np.sum(dfEvent.loc[dfEvent['type'] == 'faces', 'acc'])
dfSummary.at['faces', 'omitted'] = np.sum(dfEvent.loc[dfEvent['type'] == 'faces', 'omitted'])
dfSummary.at['faces', 'incorrect'] = np.sum(dfEvent['type'] == 'faces') - dfSummary.at['faces', 'correct'] - dfSummary.at['faces', 'omitted']
# Clean up fractions
dfSummary['RT_corrOnly'] = [float("{0:.3f}".format(idx)) for idx in dfSummary['RT_corrOnly']]
dfSummary['RT_all'] = [float("{0:.3f}".format(idx)) for idx in dfSummary['RT_all']]
dfSummary['accuracy'] = [float("{0:.3f}".format(idx)) for idx in dfSummary['accuracy']]
# Transpose to match how Peter reads it into database
dfSummary = dfSummary.transpose()
# Return even and summary data frames
return {'event': dfEvent, 'summary': dfSummary}
def evalReward(self,fullFileName):
""" Evaluate reward behavioral response file """
# column names
colEvents = ['block_name', 'block_num', 'trial_num', 'stimulus_type', 'onset', 'duration', 'response_time',
'response', 'high_num', 'low_num', 'num_shown', 'outcome', 'feedback']
# variables to keep as strings
keepString = {'TrialCondition': 'subtype', 'lowNum': 'low_num', 'highNum': 'high_num', '(Reward|Loss|Control)BlockProc': 'block_name'}
# variables to keep as numeric
keepNumeric = {'^(GamStim.OnsetTime|ControlStim.OnsetTime)': 'guess_on', '^(GamStim.RT|ControlStim.RT)': 'rt',
'^(GamStim.RESP|ControlStim.RESP)': 'resp',
'^(Feedback[RL]{1}.OnsetTime|ControlFeedbackStar.OnsetTime)': 'num_on',
'^(Feedback(Up|Down)Arrow.OnsetTime|ControlFeedbackCircle.OnsetTime)': 'feed_on',
'GuessingFixation.OnsetTime': 'fix_on', '^(GuessingRunBlockList)': 'block_num',
'^(GuessNumber.OnsetTime|PressButton.OnsetTime)': 'onset',
'^(GamStim.OnsetToOnsetTime|ControlStim.OnsetToOnsetTime)': 'onset2onset'}
# feedback dictionary
feedbackDict = {'Up': 'up_arrow', 'Down': 'down_arrow', 'Circle': 'circle'}
dfEvent = pd.DataFrame(columns = colEvents) # event dataframe
trialCounter = 0
currTrial = {'trial_num': trialCounter} # current trial information
allTrials = [] # work around for .txt file missing some onset times
# Process fMRI behavioral file
with io.open(fullFileName,'r', encoding='utf-16') as f:
for line in f:
l = str(line.rstrip()).lstrip()
# signals end of paradigm
if l=='Procedure: GuesingRunProc':
f.close()
if len(allTrials) > 0:
dfEvent = self.eventReward2(allTrials,dfEvent,trialCounter)
else:
dfEvent = self.eventReward({}, dfEvent, trialCounter)
dfEvent.replace(np.nan, 'n/a', inplace=True)
return {'event': dfEvent, 'summary': None}
# new trial identifier
if re.search('^(\*\*\* LogFrame Start \*\*\*)',l):
trialCounter += 1
if trialCounter > 1:
if any(['guess_on' and not 'num_on' in currTrial.keys(), 'block_name' and not 'onset' in currTrial.keys()]) and len(currTrial.keys())>1:
allTrials.append(currTrial)
else:
dfEvent, trialCounter = self.eventReward(currTrial, dfEvent, trialCounter)
currTrial = {'trial_num': trialCounter}
# pull relevant field for string and numeric values of interest
strCheck = [v for k,v in keepString.items() if re.search(k,l)]
numCheck = [v for k,v in keepNumeric.items() if re.search(k,l)]
if strCheck:
l_split = l.split(': ')
currTrial[''.join(strCheck)] = l_split[1]
if re.search('(Reward|Loss|Control)BlockProc',l_split[1]):
currTrial[''.join(strCheck)] = l_split[1].split('BlockProc')[0]
elif numCheck:
l_split = l.split(': ')
if len(l_split) == 2:
currTrial[''.join(numCheck)] = int(l_split[1])
if ''.join(numCheck) == 'rt' and currTrial[''.join(numCheck)] == 0:
currTrial[''.join(numCheck)] = None
if ''.join(numCheck) == 'feed_on':
currTrial['feedback'] = ''.join([v for k,v in feedbackDict.items() if re.search(k,l_split[0])])
else:
currTrial[''.join(numCheck)] = None
def eventReward(self, currTrial, dfEvent, trialCounter):
""" Organize current trial and update dfEvent """
# processing end of paradigm
if len(currTrial) == 0:
# convert onsets to seconds
dfEvent['onset'] = (dfEvent['onset']-dfEvent.loc[0,'onset'])/1000
# calculate durations
for elem in dfEvent.index:
if elem < len(dfEvent.index)-1:
dfEvent.loc[elem,'duration'] = dfEvent.loc[elem+1,'onset']-dfEvent.loc[elem,'onset']
else:
dfEvent.loc[elem,'duration'] = 3
# return updated dfEvent (trialCounter need not be returned)
return(dfEvent)
nrep = 4 # reward trials comprise four events (guess, number, feedback, fixation)
# "guess number" and "press finger" events handled differently
if all([i in currTrial.keys() for i in ['trial_num', 'block_name', 'block_num', 'onset']]):
if currTrial['block_name'] in ['Reward','Loss']:
currTrial['stimulus_type'] = 'GuessNumber'
elif currTrial['block_name'] == 'Control':
currTrial['stimulus_type'] = 'PressFinger'
dfEvent = dfEvent.append(pd.DataFrame(currTrial, index = [0]), ignore_index = True)
dfEvent.loc[dfEvent['block_name']=='n/a', 'block_name'] = currTrial['block_name']
#print(dfEvent)
#print(currTrial['block_num'])
dfEvent.loc[dfEvent['block_num'].isna(), 'block_num'] = currTrial['block_num']
print(dfEvent)
dfEvent.loc[dfEvent.index[-1], 'trial_num'] = 'n/a'
# update row order; guess number/press finger comes after block trials in paradigm file
currBlockIdx = dfEvent[dfEvent['block_num'] == currTrial['block_num']].index
prevBlockIdx = dfEvent[dfEvent['block_num'] != currTrial['block_num']].index
dfEvent.loc[currBlockIdx[-1],'block_name'] = 'Instruction'
#print(list(prevBlockIdx) + [currBlockIdx[-1]] + list(currBlockIdx[:-1]))
dfEvent = dfEvent.reindex(list(prevBlockIdx) + [currBlockIdx[-1]] + list(currBlockIdx[:-1]))
dfEvent = dfEvent.reset_index(drop=True)
trialCounter -= 1
else:
currOnsets = [currTrial['guess_on'], currTrial['num_on'], currTrial['feed_on'], currTrial['fix_on']]
if currTrial['subtype'] == 'Reward' and currTrial['resp']:
currTrial['num_shown'] = [currTrial['low_num'], currTrial['high_num']][currTrial['resp']==3]
elif currTrial['subtype'] == 'Loss' and currTrial['resp']:
currTrial['num_shown'] = [currTrial['low_num'], currTrial['high_num']][currTrial['resp']==2]
elif currTrial['subtype'] == 'Control':
currTrial['high_num'] = None
currTrial['low_num'] = None
if currTrial['resp']:
currTrial['num_shown'] = '*'
else:
currTrial['num_shown'] = '---'
currTrial['feedback'] = '---'
elif not currTrial['resp']:
currTrial['num_shown'] = '---'
currTrial['feedback'] = '---'
if currTrial['subtype'] in ['Reward','Loss']:
currTrial['trial_type'] = 'guess'
else:
currTrial['trial_type'] = 'press'
if currTrial['rt'] is not None:
currTrial['rt'] = currTrial['rt']/1000
# update currEvent
currEvent = {'trial_num': np.repeat(currTrial['trial_num'],nrep), 'block_name': np.repeat('n/a',nrep), 'stimulus_type': ['guess', 'number', 'feedback', 'fixation'], 'onset': currOnsets, 'duration': np.repeat('n/a',nrep), 'response_time': [currTrial['rt'], 'n/a', 'n/a', 'n/a'], 'response': [currTrial['resp'], 'n/a', 'n/a', 'n/a'], 'num_shown': ['n/a', currTrial['num_shown'], 'n/a', 'n/a'], 'high_num': ['n/a', currTrial['high_num'], 'n/a', 'n/a'], 'low_num': ['n/a', currTrial['low_num'], 'n/a', 'n/a'], 'outcome': ['n/a', 'n/a', currTrial['subtype'], 'n/a'], 'feedback': ['n/a', 'n/a', currTrial['feedback'], 'n/a']}
# add currEvent to dfEvent
dfEvent = dfEvent.append(pd.DataFrame.from_dict(currEvent), ignore_index = True)
# return updated dfEvent
return(dfEvent, trialCounter)
def eventReward2(self, allTrials, dfEvent, trialCounter):
""" Organizes trials with limited paradigm timing information """
""" MR001 CBS-entrepreneurship project e-prime behavioral .txt files contain fewer event time stamps """
#colEvents = ['onset', 'duration', 'block_name', 'block_num', 'trial_num', 'stimulus_type', 'response_time', 'response', 'high_num', 'low_num', 'num_shown', 'outcome', 'feedback']
#dfEvent = pd.DataFrame(columns=colEvents) # event dataframe
instr_dur = 3 # instructions duration fixed at 3 s
nrep = 4 # each trial has four parts
trialCounter = 0
blockCounter = 1
for i in range(len(allTrials) - 1):
if i == 0:
currBlock = {'onset': [0], 'duration': [instr_dur], 'block_name': ['Instruction'],
'block_num': [blockCounter], 'trial_num': ['n/a'], 'stimulus_type': ['n/a'],
'response_time': ['n/a'], 'response': ['n/a'], 'high_num': ['n/a'], 'low_num': ['n/a'],
'num_shown': ['n/a'], 'outcome': ['n/a'], 'feedback': ['n/a']}
overallStartTime = allTrials[i]['guess_on'] - instr_dur * 1000
if 'subtype' in allTrials[i].keys():
trialCounter += 1
currGuessOn = (allTrials[i]['guess_on'] - overallStartTime) / 1000
currGuessDur = (allTrials[i]['onset2onset']) / 1000
currNumbrOn = currGuessOn + (allTrials[i]['onset2onset'] / 1000)
currNumbrDur = 0.5
currFeedbOn = currNumbrOn + currNumbrDur
currFeedbDur = 0.5
currFixatOn = currFeedbOn + currFeedbDur
if 'subtype' in allTrials[i + 1].keys():
nextGuessOn = (allTrials[i + 1]['guess_on'] - overallStartTime) / 1000
currFixatDur = nextGuessOn - currGuessOn - currGuessDur - currNumbrDur - currFeedbDur
elif i + 2 < len(allTrials) - 1:
nextGuessOn = (allTrials[i + 2]['guess_on'] - overallStartTime) / 1000
currFixatDur = nextGuessOn - currGuessOn - currGuessDur - currNumbrDur - currFeedbDur - instr_dur
currBlock['onset'] = currBlock['onset'] + [currGuessOn, currNumbrOn, currFeedbOn, currFixatOn]
currBlock['duration'] = currBlock['duration'] + [currGuessDur, currNumbrDur, currFeedbDur, currFixatDur]
currBlock['block_name'] = currBlock['block_name'] + ['n/a'] * nrep
currBlock['block_num'] = currBlock['block_num'] + [blockCounter] * nrep
currBlock['trial_num'] = currBlock['trial_num'] + [trialCounter] * nrep
currBlock['stimulus_type'] = currBlock['stimulus_type'] + ['guess', 'number', 'feedback', 'fixation']
if allTrials[i]['rt'] is not None:
currBlock['response_time'] = currBlock['response_time'] + [allTrials[i]['rt'] / 1000] + ['n/a'] * (
nrep - 1)
currBlock['response'] = currBlock['response'] + [allTrials[i]['resp']] + ['n/a'] * (nrep - 1)
else:
currBlock['response_time'] = currBlock['response_time'] + ['n/a'] * nrep
currBlock['response'] = currBlock['response'] + ['n/a'] * nrep
if allTrials[i]['subtype'] in ['Reward', 'Loss']:
currBlock['high_num'] = currBlock['high_num'] + ['n/a'] + [allTrials[i]['high_num']] + ['n/a'] * 2
currBlock['low_num'] = currBlock['low_num'] + ['n/a'] + [allTrials[i]['low_num']] + ['n/a'] * 2
if allTrials[i]['subtype'] == 'Reward' and allTrials[i]['resp']:
num_shown = [allTrials[i]['low_num'], allTrials[i]['high_num']][allTrials[i]['resp'] == 3]
currBlock['num_shown'] = currBlock['num_shown'] + ['n/a'] + [num_shown] + ['n/a'] * 2
currBlock['outcome'] = currBlock['outcome'] + ['n/a'] * 2 + ['Reward'] + ['n/a']
currBlock['feedback'] = currBlock['feedback'] + ['n/a'] * 2 + ['up_arrow'] + ['n/a']
elif allTrials[i]['subtype'] == 'Loss' and allTrials[i]['resp']:
num_shown = [allTrials[i]['low_num'], allTrials[i]['high_num']][allTrials[i]['resp'] == 2]
currBlock['num_shown'] = currBlock['num_shown'] + ['n/a'] + [num_shown] + ['n/a'] * 2
currBlock['outcome'] = currBlock['outcome'] + ['n/a'] * 2 + ['Loss'] + ['n/a']
currBlock['feedback'] = currBlock['feedback'] + ['n/a'] * 2 + ['down_arrow'] + ['n/a']
else:
currBlock['num_shown'] = currBlock['num_shown'] + ['n/a'] + ['---'] + ['n/a'] * 2
currBlock['outcome'] = currBlock['outcome'] + ['n/a'] * 2 + ['---'] + ['n/a']
currBlock['feedback'] = currBlock['feedback'] + ['n/a'] * 2 + ['---'] + ['n/a']
elif allTrials[i]['subtype'] == 'Control':
currBlock['high_num'] = currBlock['high_num'] + ['n/a'] * nrep
currBlock['low_num'] = currBlock['low_num'] + ['n/a'] * nrep
if allTrials[i]['resp']:
currBlock['num_shown'] = currBlock['num_shown'] + ['n/a'] + ['*'] + ['n/a'] * 2
currBlock['outcome'] = currBlock['outcome'] + ['n/a'] * 2 + ['Control'] + ['n/a']
currBlock['feedback'] = currBlock['feedback'] + ['n/a'] * 2 + ['circle'] + ['n/a']
else:
currBlock['num_shown'] = currBlock['num_shown'] + ['n/a'] + ['---'] + ['n/a'] * 2
currBlock['outcome'] = currBlock['outcome'] + ['n/a'] * 2 + ['---'] + ['n/a']
currBlock['feedback'] = currBlock['feedback'] + ['n/a'] * 2 + ['---'] + ['n/a']
elif 'block_name' in allTrials[i].keys():
currBlock['block_name'] = list(
map(lambda x: x.replace('n/a', allTrials[i]['block_name']), currBlock['block_name']))
if allTrials[i]['block_name'] in ['Reward', 'Loss']:
currBlock['block_name'] = list(
map(lambda x: x.replace('n/a', 'GuessNumber'), currBlock['block_name']))
else:
currBlock['block_name'] = list(
map(lambda x: x.replace('n/a', 'PressButton'), currBlock['block_name']))
if i < len(allTrials) - 1:
currBlock['onset'].append(currBlock['onset'][-1] + currBlock['duration'][-1])
currBlock['duration'].append(instr_dur)
currBlock['block_name'].append('Instruction')
currBlock['block_num'].append(currBlock['block_num'][-1] + 1)
blockCounter += 1
currBlock['trial_num'].append('n/a')
currBlock['stimulus_type'].append('n/a')
currBlock['response_time'].append('n/a')
currBlock['response'].append('n/a')
currBlock['high_num'].append('n/a')
currBlock['low_num'].append('n/a')
currBlock['num_shown'].append('n/a')
currBlock['outcome'].append('n/a')
currBlock['feedback'].append('n/a')
dfEvent = pd.DataFrame(currBlock)
return(dfEvent)