-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_design_matrix.py
395 lines (336 loc) · 15.3 KB
/
generate_design_matrix.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
# -*- coding: utf-8 -*-
"""
function for the
A script that extracts the conditions from eprime csv file,
and saves them into an SPM multiconditions file (.mat).
It saves the corresponding movement regressors from movement_files/
into a (.mat) file in movement_files
The conditions are :
*
- Anticip_hit_largewin
- Anticip_hit_smallwin
- Anticip_hit_nowin
- Anticip_missed_largewin
- Anticip_missed_smallwin
- Anticip_missed_nowin
- Anticip_noresp
*
- Feedback_hit_largewin
- Feedback_hit_smallwin
- Feedback_hit_nowin
- Feedback_missed_largewin
- Feedback_missed_smallwin
- Feedback_missed_nowin
- Feedback_noresp
*
- Press_left
- Press_right
"""
import os, glob
from collections import OrderedDict
import numpy as np
from scipy import io
import matplotlib.pyplot as plt
import pandas as pd
import xlsxwriter
from nipy.modalities.fmri import design_matrix
from nipy.modalities.fmri.experimental_paradigm import BlockParadigm
BASE_DIR = os.path.join('eprime_files_caiman', 'csv')
DST_BASE_DIR = os.path.join('eprime_files_caiman', 'mat')
N_SCANS = 289
TR = 2400.
START_DELAY = 6000.
TASK_DURATION = {'anticip': 4., 'feedback': 1.45}
ANTICIP_DESIGN = True
def check_subject_eprime(eprime_file, mapping):
"""A temporary function that checks if the eprime id
has an existing corresponding subject
"""
eprime_nb = eprime_file.split('/')[-1].split('.')[0].rsplit('-')[-2]
res = mapping[mapping['eprime'] == int(eprime_nb)]['subject'].values
return res, eprime_nb
def generate_multiconditions_excel(output_file, conditions, onset, condition):
"""Generate a *.xlsx file which contains the conditions
"""
work = xlsxwriter.Workbook(output_file + '.xlsx')
wsheet = work.add_worksheet('Conditions')
col = 0
for key in conditions.keys():
wsheet.write(0, col, key)
row = 1
for item in conditions[key]:
wsheet.write(row, col, item)
row += 1
col += 1
wsheet2 = work.add_worksheet('Timeline')
order = onset.argsort()
wsheet2.write(0, 0, 'onset')
wsheet2.write(0, 1, 'condition')
row = 1
for i in order:
wsheet2.write(row, 0, onset[i])
wsheet2.write(row, 1, condition[i])
row += 1
work.close()
def generate_multiregressors_mat(output_file, regressors):
"""Generate a *.mat file that contains the regressors
"""
io.savemat(output_file, {'R': regressors})
def generate_multiconditions_mat(output_file, conditions, ddurations):
"""Generate a *.mat file that contains the names, the onsets
and the durations, according to SPM's mutiple coditions file
"""
names = np.zeros((len(conditions),), dtype=np.object)
onsets = np.zeros((len(conditions),), dtype=np.object)
durations = np.zeros((len(conditions),), dtype=np.object)
for i in np.arange(0, len(conditions)):
names[i] = conditions.keys()[i]
onsets[i] = conditions[names[i]]
durations[i] = ddurations[names[i]]
if len(onsets[i])==0:
durations[i] = 0.
onsets[i] = [3600.]
print output_file
io.savemat(output_file, {'names' : names,
'onsets' : onsets,
'durations' : durations})
return names, onsets, durations
def compute_mid_conditions(filename):
df = pd.read_csv(filename)
# Extract hits, misses and noresps
# hits
hit = np.zeros(len(df))
h_idx = df[df['PictureTarget.RESP'].notnull()]['TrialList']
hit[h_idx.values - 1] = 1
# noresps
noresp = np.zeros(len(df))
n_idx = df[df['PictureTarget.RESP'].isnull()]['TrialList']
noresp[n_idx.values - 1] = 1
# misses
miss = np.zeros(len(df))
m_idx = df[df['PictureTarget.RESP'].isnull()]['TrialList']
miss[m_idx.values - 1] = 1
# Extract bigwins, smallwins and nowins
# big wins
largewin = np.zeros(len(df))
lw_idx = df[df['prize']==10]['TrialList']
largewin[lw_idx.values - 1] = 1
# small wins
smallwin = np.zeros(len(df))
sw_idx = df[df['prize']==2]['TrialList']
smallwin[sw_idx.values - 1] = 1
# no wins
nowin = np.zeros(len(df))
nw_idx = df[df['prize']==0]['TrialList']
nowin[nw_idx.values - 1] = 1
# Extract press left (5), press right (4)
# press left
pleft = np.zeros(len(df))
pl_idx = df[df['PictureTarget.RESP'] == 5]['TrialList']
pleft[pl_idx.values - 1] = 1
# press right
pright = np.zeros(len(df))
pr_idx = df[df['PictureTarget.RESP'] == 4]['TrialList']
pright[pr_idx.values - 1] = 1
# Extract times
first_onset = df['PicturePrime.OnsetTime'][0]
anticip_start_time = (df['PicturePrime.OnsetTime'] - first_onset + START_DELAY - 2 * TR)/1000.
response_time = (df['PictureTarget.RTTime'] - first_onset + START_DELAY - 2 * TR)/1000.
feedback_start_time = (df['PictureTarget.OnsetTime'] + df['Target_time'] - first_onset + START_DELAY - 2 * TR)/1000.
# Compute conditions
cond = pd.DataFrame({'response_time': response_time,
'anticip_start_time': anticip_start_time,
'feedback_start_time': feedback_start_time})
# Anticipation
anticip_hit_largewin = cond[(hit==1) & (largewin==1)]['anticip_start_time'].values
anticip_hit_smallwin = cond[(hit==1) & (smallwin==1)]['anticip_start_time'].values
anticip_hit_nowin = cond[(hit==1) & (nowin==1)]['anticip_start_time'].values
anticip_hit = np.hstack((anticip_hit_largewin,
anticip_hit_smallwin, anticip_hit_nowin))
anticip_hit_modgain = np.hstack([[3.]*len(anticip_hit_largewin),
[2.]*len(anticip_hit_smallwin),
[1.]*len(anticip_hit_nowin)])
anticip_missed_largewin = cond[(miss==1) & (largewin==1)]['anticip_start_time'].values
anticip_missed_smallwin = cond[(miss==1) & (smallwin==1)]['anticip_start_time'].values
anticip_missed_nowin = cond[(miss==1) & (nowin==1)]['anticip_start_time'].values
anticip_missed = np.hstack((anticip_missed_largewin,
anticip_missed_smallwin, anticip_missed_nowin))
anticip_missed_modgain = np.hstack([[3.]*len(anticip_missed_largewin),
[2.]*len(anticip_missed_smallwin),
[1.]*len(anticip_missed_nowin)])
anticip_noresp = cond[(noresp==1)]['anticip_start_time'].values
# Feedback
feedback_hit_largewin = cond[(hit==1) & (largewin==1)]['feedback_start_time'].values
feedback_hit_smallwin = cond[(hit==1) & (smallwin==1)]['feedback_start_time'].values
feedback_hit_nowin = cond[(hit==1) & (nowin==1)]['feedback_start_time'].values
feedback_hit = np.hstack((feedback_hit_largewin,
feedback_hit_smallwin, feedback_hit_nowin))
feedback_hit_modgain = np.hstack([[3.]*len(feedback_hit_largewin),
[2.]*len(feedback_hit_smallwin),
[1.]*len(feedback_hit_nowin)])
feedback_missed_largewin = cond[(miss==1) & (largewin==1)]['feedback_start_time'].values
feedback_missed_smallwin = cond[(miss==1) & (smallwin==1)]['feedback_start_time'].values
feedback_missed_nowin = cond[(miss==1) & (nowin==1)]['feedback_start_time'].values
feedback_missed = np.hstack((feedback_missed_largewin,
feedback_missed_smallwin, feedback_missed_nowin))
feedback_missed_modgain = np.hstack([[3.]*len(feedback_missed_largewin),
[2.]*len(feedback_missed_smallwin),
[1.]*len(feedback_missed_nowin)])
feedback_noresp = cond[(noresp==1)]['feedback_start_time'].values
# Response
press_left = cond[(pleft==1)]['response_time'].values
press_right = cond[(pright==1)]['response_time'].values
# namelist
namelist = ['anticip_hit', 'anticip_missed', 'anticip_noresp',
'feedback_hit', 'feedback_missed', 'feedback_noresp',
'press_left', 'press_right']
modulationnamelist = ['anticip_hit_modgain', 'anticip_missed_modgain',
'feedback_hit_modgain', 'feedback_missed_modgain']
conditions = OrderedDict()
"""
#XXX As the missed case is often missing, we won't use it at this time
#XXX missed case has been corrected
conditions = {'anticip_hit_largewin' : anticip_hit_largewin,
'anticip_hit_smallwin' : anticip_hit_smallwin,
'anticip_hit_nowin' : anticip_hit_nowin,
'anticip_missed_largewin' : anticip_missed_largewin,
'anticip_missed_smallwin' : anticip_missed_smallwin,
'anticip_missed_nowin' : anticip_missed_nowin,
'anticip_noresp' : anticip_noresp,
'feedback_hit_largewin' : feedback_hit_largewin,
'feedback_hit_smallwin' : feedback_hit_smallwin,
'feedback_hit_nowin' : feedback_hit_nowin,
'feedback_missed_largewin' : feedback_missed_largewin,
'feedback_missed_smallwin' : feedback_missed_smallwin,
'feedback_missed_nowin' : feedback_missed_nowin,
'feedback_noresp' : feedback_noresp,
'press_left' : press_left,
'press_right' : press_right}
"""
if ANTICIP_DESIGN:
conditions['anticip_hit_largewin'] = anticip_hit_largewin
conditions['anticip_hit_smallwin'] = anticip_hit_smallwin
conditions['anticip_hit_nowin'] = anticip_hit_nowin
conditions['anticip_missed_largewin'] = anticip_missed_largewin
conditions['anticip_missed_smallwin'] = anticip_missed_smallwin
conditions['anticip_missed_nowin'] = anticip_missed_nowin
#conditions['anticip_noresp'] = anticip_noresp
else:
conditions['feedback_hit_largewin'] = feedback_hit_largewin
conditions['feedback_hit_smallwin'] = feedback_hit_smallwin
conditions['feedback_hit_nowin'] = feedback_hit_nowin
conditions['feedback_missed_largewin'] = feedback_missed_largewin
conditions['feedback_missed_smallwin'] = feedback_missed_smallwin
conditions['feedback_missed_nowin'] = feedback_missed_nowin
#conditions['feedback_noresp'] = feedback_noresp
conditions['press_left'] = press_left
conditions['press_right'] = press_right
durations = OrderedDict()
for k in conditions.keys():
if 'feedback' in k:
durations[k] = TASK_DURATION['feedback']
elif 'anticip' in k:
durations[k] = TASK_DURATION['anticip']
else:
durations[k] = 0.
return conditions, durations
##############################################################################
##############################################################################
##############################################################################
##############################################################################
##############################################################################
# Load eprime csv file
file_list = sorted(glob.glob(os.path.join(BASE_DIR, 'c_*.csv')))
for f in file_list:
# Compute conditions
conditions, durations = compute_mid_conditions(f)
# Load regressors if they exist
mapping = pd.read_csv(os.path.join('eprime_files_caiman', 'mapping.csv'),
names=['eprime','subject'])
subject_id, eprime_id = check_subject_eprime(f, mapping)
subject_id = subject_id.astype(np.int)
if len(subject_id)>0:
print subject_id[0]
filepath = os.path.join('movement_files_caiman',
''.join(['S',str(int(subject_id[0])), '_reg.csv']))
if os.path.isfile(filepath):
reg = pd.read_csv(filepath)
regressors = reg.values[:,1:]
# Create paradigms
empty_conditions = []
pseudo_empty_conditions = []
condition = []
onset = []
duration = []
for c in conditions:
if len(conditions[c]) < 1:
empty_conditions.append(c)
elif len(conditions[c]) < 2:
pseudo_empty_conditions.append(c)
condition += [c]*len(conditions[c])
onset = np.hstack([onset, conditions[c]])
duration += [durations[c]]*len(conditions[c])
paradigm = BlockParadigm(con_id=condition,
onset=onset,
duration=duration)
frametimes = np.linspace(0, (N_SCANS-1)*TR/1000., num=N_SCANS)
design_mat = design_matrix.make_dmtx(frametimes, paradigm,
hrf_model='Canonical with derivative',
add_regs=regressors,
drift_model='Cosine',
hfcut=128)
#
# # Fill empty conditions
# for c in empty_conditions:
# if c == 'anticip_missed_largewin':
# ind = 6
# elif c == 'feedback_missed_largewin':
# ind = 18
# elif c == 'anticip_missed_smallwin':
# ind = 10
# elif c == 'feedback_missed_smallwin':
# ind = 20
# elif c == 'anticip_missed_nowin':
# ind = 8
# elif c == 'feedback_missed_nowin':
# ind = 22
# design_mat.matrix = np.insert(design_mat.matrix,
# ind, 1, axis=1)
# design_mat.matrix = np.insert(design_mat.matrix,
# ind+1, 1, axis=1)
# design_mat.names.insert(ind, c)
# design_mat.names.insert(ind+1, c + '_derivative')
#
#
# # Fill pseudo empty conditions
# print empty_conditions
# print pseudo_empty_conditions
# for c in pseudo_empty_conditions:
# if c == 'anticip_missed_largewin':
# ind = 6
# elif c == 'feedback_missed_largewin':
# ind = 18
# elif c == 'anticip_missed_smallwin':
# ind = 10
# elif c == 'feedback_missed_smallwin':
# ind = 20
# elif c == 'anticip_missed_nowin':
# ind = 8
# elif c == 'feedback_missed_nowin':
# ind = 22
# design_mat.matrix[:, ind] = np.ones(N_SCANS)
# design_mat.matrix[:, ind+1] = np.ones(N_SCANS)
# Reorder conditions
for i in np.arange(2,21,6):
for j in range(2):
tmp = design_mat.matrix[:,i+j].copy()
design_mat.matrix[:,i+j] = design_mat.matrix[:,i+j+2]
design_mat.matrix[:,i+j+2] = tmp
design_mat.names[i+j], design_mat.names[i+j+2] = design_mat.names[i+j+2], design_mat.names[i+j]
design_mat.write_csv(os.path.join('design_matrices',
str(subject_id[0]) + '.csv'))
fig_title = 'S'+ str(subject_id[0])
design_mat.show()
plt.title(fig_title)
plt.set_cmap('gray')
plt.savefig(os.path.join('figures', fig_title+'.png'))