-
Notifications
You must be signed in to change notification settings - Fork 1
/
bar2m_dcm.py
executable file
·360 lines (291 loc) · 11.2 KB
/
bar2m_dcm.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
#! /usr/bin/env python
## ---------------------------------------------------------------- ##
## BAR2M
## ---------------------------------------------------------------- ##
## A file that calculates the onset of experimental events (grouped
## by condition) in the BAR study. Event onsets and durations are
## written to text files specific for each experimental block
## ('session' in SPM lingo)
import sys, os
from operator import add
from math import sqrt
## ---------------------------------------------------------------- ##
## This is a list of contrasts vectors (calculated per session)
## ---------------------------------------------------------------- ##
## ---------------------------------------------------------------- ##
## This is a list of imaging-related variables
## ---------------------------------------------------------------- ##
TR = 2000.0
OFFSET = 2
BLOCK = 0
TRIAL = 0
PROBLEM_ONSET = 0
PROBLEM_RT = 0
CHOICE_ONSET = 0
CHOICE_RT = 0
CHOICE_ACC = 0
LOGIC = 0
RULES = 0
NUM_OF_RULES = 0
S_PROBLEM_ONSET = 0
S_PROBLEM_RT = 0
S_CHOICE_ONSET = 0
S_CHOICE_RT = 0
S_CHOICE_ACC = 0
S_LOGIC = 0
S_RULES = 0
S_NUM_OF_RULES = 0
LC_BLOCK = 0
LC_PROBLEM_ONSET = 0
LC_PROBLEM_RT = 0
LC_CHOICE_ONSET = 0
LC_CHOICE_RT = 0
LC_CHOICE_ACC = 0
LC_LOGIC = 0
LC_RULES = 0
LC_NUM_OF_RULES = 0
class Trial:
"""
An abstract class representing a BAR trail---two phases
(Problem, Choice), with associated Onsets and
Durations (ie. RTs), followed by randomly-varying Delays.
"""
LOGIC = {"Logic" : 2, "Non-Logic" : 1,};
RULES = {"Low" : 0, "High" : 2}
def __init__(self, tokens):
"""Initializes and catches eventual errors"""
self.ok = True
self.trial = -1
self.before = 0.0
try:
self.Create(tokens)
self.Initialize()
except ValueError as v:
sys.stderr.write("ValueError: %s; Trying Special Case\n" % (v))
# A value error might be a sign of "Special" problems.
try:
self.CreateSpecial(tokens)
self.Initialize()
except ValueError as v:
sys.stderr.write("ValueError: %s; Trying Last Chance\n" % (v))
self.ok = False
except IndexError:
sys.stderr.write("IndexError: %s\n" % tokens)
self.ok = False
def Initialize(self):
"""Sets the proper fields once the values have been read"""
self.acc = self.choiceAcc
self.blockBegin = 0
# Reset the durations of problems and choices
# that timed out
if self.problemRt == 0:
self.problemRt = 30000
if self.choiceRt == 0:
self.choiceRt = 4000
self.onsets = {'Problem' : self.problemOnset,
'Choice' : self.choiceOnset}
self.rts = {'Problem' : self.problemRt,
'Choice' : self.choiceRt}
def Create(self, tokens):
"""Performs the necessary initialization"""
self.block = int(tokens[BLOCK])
self.problemOnset = int(tokens[PROBLEM_ONSET])
self.problemRt = int(tokens[PROBLEM_RT])
self.choiceOnset = int(tokens[CHOICE_ONSET])
self.choiceRt = int(tokens[CHOICE_RT])
self.choiceAcc = int(tokens[CHOICE_ACC])
self.logic = tokens[LOGIC]
self.rules = tokens[RULES]
self.numrules = int(tokens[NUM_OF_RULES])
def CreateSpecial(self, tokens):
"""Performs the necessary initialization for the Special Problem"""
self.block = -1 # By default, the second block
self.problemOnset = int(tokens[S_PROBLEM_ONSET])
self.problemRt = int(tokens[S_PROBLEM_RT])
self.choiceOnset = int(tokens[S_CHOICE_ONSET])
self.choiceRt = int(tokens[S_CHOICE_RT])
self.choiceAcc = int(tokens[S_CHOICE_ACC])
self.logic = tokens[S_LOGIC]
self.rules = tokens[S_RULES]
self.numrules = int(tokens[S_NUM_OF_RULES])
def CreateLastChance(self, tokens):
"""
Performs initialization when the data table is corrupted.
"""
self.block = int(tokens[LC_BLOCK])
self.problemOnset = int(tokens[LC_PROBLEM_ONSET])
self.problemRt = int(tokens[LC_PROBLEM_RT])
self.choiceOnset = int(tokens[LC_CHOICE_ONSET])
self.choiceRt = int(tokens[LC_CHOICE_RT])
self.choiceAcc = int(tokens[LC_CHOICE_ACC])
self.logic = tokens[LC_LOGIC]
self.rules = tokens[LC_RULES]
self.numrules = int(tokens[LC_NUM_OF_RULES])
def Difficulty(self):
"""Calculates the problem difficulty as a number between 1 and 4"""
return Trial.LOGIC[self.logic] + Trial.RULES[self.rules]
def RelativeTime(self, val):
"Time since the beginning of the block"
return (float(val) - float(self.blockBegin))/1000.0
def __str__(self):
return "<BAR:%d/%d (%.2f), L:%s, R:%d>" % (self.block, self.trial, self.RelativeTime(self.problemOnset), self.logic, self.numrules)
def __repr__(self):
return self.__str__()
def Parse(filename):
"""Parses a Table-format logfile"""
global BLOCK
global TRIAL
global PROBLEM_ONSET
global PROBLEM_RT
global CHOICE_ONSET
global CHOICE_RT
global CHOICE_ACC
global LOGIC
global RULES
global NUM_OF_RULES
global S_PROBLEM_ONSET
global S_PROBLEM_RT
global S_CHOICE_ONSET
global S_CHOICE_RT
global S_CHOICE_ACC
global S_LOGIC
global S_RULES
global S_NUM_OF_RULES
global LC_BLOCK
global LC_PROBLEM_ONSET
global LC_PROBLEM_RT
global LC_CHOICE_ONSET
global LC_CHOICE_RT
global LC_CHOICE_ACC
global LC_LOGIC
global LC_RULES
global LC_NUM_OF_RULES
fin = open(filename, 'rU')
subject = filename.split('-')[1]
lines = fin.readlines()
tokens = [x.split('\t') for x in lines]
tokens = [[y.strip() for y in x] for x in tokens]
colNames = tokens[0]
rows = tokens[1:]
# --- Block info
try:
BLOCK = colNames.index("BlockNum")
#TRIAL = colNames.index("Trial")
except ValueError as e:
sys.stderr.write("Cannot find 'Block' info. Aborting\n")
sys.exit(0)
# --- Problem onset
try:
PROBLEM_ONSET = colNames.index("Problem.OnsetTime[Trial]")
except ValueError as e:
sys.stderr.write("Could not find 'Problem.OnsetTime' info at 'Trial' level\n")
PROBLEM_ONSET = colNames.index("Problem.OnsetTime")
# --- Problem RT
try:
PROBLEM_RT = colNames.index("Problem.RT[Trial]")
except ValueError as e:
sys.stderr.write("Could not find 'Problem.RT' info at 'Trial' level\n")
PROBLEM_RT = colNames.index("Problem.RT")
# --- Choice Onset
try:
CHOICE_ONSET = colNames.index("Choice.OnsetTime[Trial]")
except ValueError as e:
sys.stderr.write("Could not find 'Choice.Onset' info at 'Trial' level\n")
CHOICE_ONSET = colNames.index("Choice.OnsetTime")
# --- Choice RT
try:
CHOICE_RT = colNames.index("Choice.RT[Trial]")
except ValueError as e:
sys.stderr.write("Could not find 'Choice.RT' info at 'Trial' level\n")
CHOICE_RT = colNames.index("Choice.RT")
# --- Choice Accuracy
try:
CHOICE_ACC = colNames.index("Choice.ACC[Trial]")
except ValueError as e:
sys.stderr.write("Could not find 'Choice.ACC' info at 'Trial' level\n")
CHOICE_ACC = colNames.index("Choice.ACC")
# --- Logic
try:
LOGIC = colNames.index("Logic[Trial]")
except ValueError as e:
sys.stderr.write("Could not find 'Logic' info at 'Trial' level\n")
LOGIC = colNames.index("Logic")
# --- Rules
try:
RULES = colNames.index("Rules[Trial]")
except ValueError as e:
sys.stderr.write("Could not find 'Rules' info at 'Trial' level\n")
RULES = colNames.index("Rules")
# --- NumRules
try:
NUM_OF_RULES = colNames.index("NumRules[Trial]")
except ValueError as e:
sys.stderr.write("Could not find 'NumRules' info at 'Trial' level\n")
NUM_OF_RULES = colNames.index("NumRules")
try:
# Special marks for the "special" problem(#17)
S_PROBLEM_ONSET = colNames.index("Problem.OnsetTime[Block]")
S_PROBLEM_RT = colNames.index("Problem.RT[Block]")
S_CHOICE_ONSET = colNames.index("Choice.OnsetTime[Block]")
S_CHOICE_RT = colNames.index("Choice.RT[Block]")
S_CHOICE_ACC = colNames.index("Choice.ACC[Block]")
S_LOGIC = colNames.index("Logic[Block]")
S_RULES = colNames.index("Rules[Block]")
S_NUM_OF_RULES = colNames.index("NumRules[Block]")
except ValueError as e:
sys.stderr.write("Cannot find Special Problem, skipping...\n")
trials = [Trial(r) for r in rows]
trials = [t for t in trials if t.ok] # Excludes warmup trials
FIRST_TRIALS = []
previous = None
# Now we need to add the "Special" problem at the end of the
block_max = max(set([t.block for t in trials]))
for i in [t for t in trials if t.block == -1]:
i.block = block_max
for t in trials:
if previous == None or t.block != previous.block:
FIRST_TRIALS.append(t)
previous = t
print len(trials)
for f in FIRST_TRIALS:
subset = [t for t in trials if t.block == f.block]
for s in subset:
print f, s.problemOnset, f.problemOnset
s.blockBegin = f.problemOnset - (OFFSET * TR)
BLOCKS = set(t.block for t in trials)
BLOCKS = list(BLOCKS)
BLOCKS.sort()
print(FIRST_TRIALS)
print FIRST_TRIALS[1].problemOnset
fout = open("s%s_sessions_dcm.m" % subject, 'w')
I = 0 # Total of i counters
bf = file("blocks.txt", 'r')
blengths = [(int(x) - 1) * TR for x in bf.readlines()]
blengths = [0.0] + blengths[0:-1]
# Cumulative lengths
k = [sum(blengths[0:i+1]) for i in range(len(blengths))]
blengths = k
for d, b in zip(blengths, BLOCKS):
for t in [x for x in trials if x.block == b]:
t.blockBegin -= d
description = ""
i = 1
for phase in ['Problem', 'Choice']:
description += "names{%d}='%s';\n" % (i, phase)
onsets = "%s" % [round(a.RelativeTime(a.onsets[phase]), 0) \
for a in trials]
durations = "%s" % [a.rts[phase]/1000.0 \
for a in trials]
description += "onsets{%d}=%s;\n" % (i, onsets.replace(";", ""))
description += "durations{%d}=%s;\n" % (i, durations.replace(";", ""))
i += 1
fout.write("names=cell(1,2);\n")
fout.write("onsets=cell(1,2);\n")
fout.write("durations=cell(1,2);\n")
fout.write(description)
fout.write("save('sessions_dcm.mat', 'names', 'onsets', 'durations');\n")
fout.flush()
fout.close()
if __name__ == "__main__":
filename=sys.argv[1]
Parse(filename)