-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_flame_model_files.py
415 lines (264 loc) · 11.1 KB
/
create_flame_model_files.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
def create_dummy_string(length):
ppstring = ""
for i in range(0, length):
ppstring += '\t' + '%1.5e' %(1.0)
ppstring += '\n'
return ppstring
def write_mat_file(design_matrix, output_dir, model_name, \
depatsified_EV_names, current_output=None):
import os
import numpy as np
dimx = None
dimy = None
if len(design_matrix.shape) == 1:
dimy = 1
dimx = design_matrix.shape[0]
else:
dimx, dimy = design_matrix.shape
ppstring = '/PPheights'
for i in range(0, dimy):
ppstring += '\t' + '%1.5e' %(1.0)
ppstring += '\n'
filename = model_name + ".mat"
out_file = os.path.join(output_dir, filename)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
with open(out_file, 'wt') as f:
print >>f, '/NumWaves\t%d' %dimy
print >>f, '/NumPoints\t%d' %dimx
print >>f, ppstring
# print labels for the columns - mainly for double-checking your model
col_string = '\n'
for col in depatsified_EV_names:
col_string = col_string + col + '\t'
print >>f, col_string, '\n'
print >>f, '/Matrix'
np.savetxt(f, design_matrix, fmt='%1.5e', delimiter='\t')
return out_file
def create_grp_file(design_matrix, grp_file_vector, output_dir, model_name):
import os
import numpy as np
dimx = None
dimy = None
if len(design_matrix.shape) == 1:
dimy = 1
dimx = design_matrix.shape[0]
else:
dimx, dimy = design_matrix.shape
filename = "grouping.grp"
out_file = os.path.join(output_dir, model_name + ".grp")
with open(out_file, "wt") as f:
print >>f, '/NumWaves\t1'
print >>f, '/NumPoints\t%d\n' %dimx
print >>f, '/Matrix'
np.savetxt(f, grp_file_vector, fmt='%d', delimiter='\t')
return out_file
def create_con_file(con_dict, col_names, model_name, current_output, out_dir):
import os
out_file = os.path.join(out_dir, model_name) + ".con"
with open(out_file,'w+') as f:
# write header
num = 1
for key in con_dict:
f.write("/ContrastName%s\t%s\n" %(num,key))
num += 1
f.write("/NumWaves\t%d\n" %len(con_dict[key]))
f.write("/NumContrasts\t%d\n" %len(con_dict))
f.write("/PPheights%s" %create_dummy_string(len(con_dict[key])))
f.write("/RequiredEffect%s" %create_dummy_string(len(con_dict[key])))
f.write("\n\n")
# print labels for the columns - mainly for double-checking your
# model
col_string = '\n'
for col in col_names:
col_string = col_string + col + '\t'
print >>f, col_string, '\n'
# write data
f.write("/Matrix\n")
for key in con_dict:
for v in con_dict[key]:
f.write("%1.5e\t" %v)
f.write("\n")
return out_file
def create_fts_file(ftest_list, con_dict, model_name, current_output, \
out_dir):
import os
import numpy as np
try:
print "\nFound f-tests in your model, writing f-tests file " \
"(.fts)..\n"
out_file = os.path.join(out_dir, model_name + '.fts')
with open(out_file, 'w') as f:
print >>f, '/NumWaves\t', len(con_dict)
print >>f, '/NumContrasts\t', len(ftest_list)
# process each f-test
ftst = []
for ftest_string in ftest_list:
ftest_vector = []
cons_in_ftest = ftest_string.split(",")
for con in con_dict.keys():
if con in cons_in_ftest:
ftest_vector.append(1)
else:
ftest_vector.append(0)
ftst.append(ftest_vector)
fts_n = np.array(ftst)
# print labels for the columns - mainly for double-checking your
# model
col_string = '\n'
for con in con_dict.keys():
col_string = col_string + con + '\t'
print >>f, col_string, '\n'
print >>f, '/Matrix'
for i in range(fts_n.shape[0]):
print >>f, ' '.join(fts_n[i].astype('str'))
except Exception as e:
filepath = os.path.join(out_dir, "model_files", current_output, \
model_name + '.fts')
errmsg = "\n\n[!] CPAC says: Could not create .fts file for " \
"FLAMEO or write it to disk.\nAttempted filepath: %s\n" \
"Error details: %s\n\n" % (filepath, e)
raise Exception(errmsg)
return out_file
def create_con_ftst_file(con_file, model_name, current_output, output_dir, \
column_names, coding_scheme, group_sep):
"""
Create the contrasts and fts file
"""
import os
import numpy as np
with open(con_file,"r") as f:
evs = f.readline()
evs = evs.rstrip('\r\n').split(',')
count_ftests = 0
# remove "Contrasts" label and replace it with "Intercept"
evs[0] = "Intercept"
fTest = False
for ev in evs:
if "f_test" in ev:
count_ftests += 1
if count_ftests > 0:
fTest = True
try:
data = np.genfromtxt(con_file, names=True, delimiter=',', dtype=None)
except:
print "Error: Could not successfully read in contrast file: ",con_file
raise Exception
lst = data.tolist()
ftst = []
fts_columns = []
contrasts = []
contrast_names = []
length = None
length = len(list(lst[0]))
# lst = list of tuples, "tp"
# tp = tuple in the format (contrast_name, 0, 0, 0, 0, ...)
# with the zeroes being the vector of contrasts for that contrast
for tp in lst:
contrast_names.append(tp[0])
# create a list of integers that is the vector for the contrast
# ex. [0, 1, 1, 0, ..]
con_vector = list(tp)[1:(length-count_ftests)]
fts_vector = list(tp)[(length-count_ftests):length]
fts_columns.append(fts_vector)
# add Intercept column
if group_sep == False:
if coding_scheme == "Treatment":
con_vector.insert(0, 0)
elif coding_scheme == "Sum":
con_vector.insert(0, 1)
contrasts.append(con_vector)
# contrast_names = list of the names of the contrasts (not regressors)
# contrasts = list of lists with the contrast vectors
num_EVs_in_con_file = len(contrasts[0])
contrasts = np.array(contrasts, dtype=np.float16)
fts_columns = np.array(fts_columns)
# if there are f-tests, create the array for them
if fTest:
if len(contrast_names) < 2:
errmsg = "\n\n[!] CPAC says: Not enough contrasts for running " \
"f-tests.\nTip: Do you have only one contrast in your " \
"contrasts file? f-tests require more than one contrast.\n"\
"Either remove the f-tests or include more contrasts.\n\n"
raise Exception(errmsg)
fts_n = fts_columns.T
if len(column_names) != (num_EVs_in_con_file):
err_string = "\n\n[!] CPAC says: The number of EVs in your model " \
"design matrix (found in the %s.mat file) does not " \
"match the number of EVs (columns) in your custom " \
"contrasts matrix CSV file.\n\nCustom contrasts matrix "\
"file: %s\n\nNumber of EVs in design matrix: %d\n" \
"Number of EVs in contrasts file: %d\n\nThe column " \
"labels in the design matrix should match those in " \
"your contrasts .CSV file.\nColumn labels in design " \
"matrix:\n%s" % (model_name, con_file, \
len(column_names), num_EVs_in_con_file, \
str(column_names))
raise Exception(err_string)
for design_mat_col, con_csv_col in zip(column_names, evs[1:]):
if con_csv_col not in design_mat_col:
errmsg = "\n\n[!] CPAC says: The names of the EVs in your " \
"custom contrasts .csv file do not match the names or " \
"order of the EVs in the design matrix. Please make " \
"sure these are consistent.\nDesign matrix EV columns: "\
"%s\nYour contrasts matrix columns: %s\n\n" \
% (column_names, evs[1:])
raise Exception(errmsg)
out_file = os.path.join(output_dir, model_name + '.con')
with open(out_file,"wt") as f:
idx = 1
pp_str = '/PPheights'
re_str = '/RequiredEffect'
for name in contrast_names:
print >>f, '/ContrastName%d' %idx, '\t', name
pp_str += '\t%1.5e' %(1)
re_str += '\t%1.5e' %(1)
idx += 1
print >>f, '/NumWaves\t', (contrasts.shape)[1]
print >>f, '/NumContrasts\t', (contrasts.shape)[0]
print >>f, pp_str
print >>f, re_str + '\n'
# print labels for the columns - mainly for double-checking your model
col_string = '\n'
for ev in evs:
col_string = col_string + ev + '\t'
print >>f, col_string, '\n'
print >>f, '/Matrix'
np.savetxt(f, contrasts, fmt='%1.5e', delimiter='\t')
ftest_out_file = None
if fTest:
print "\nFound f-tests in your model, writing f-tests file (.fts)..\n"
ftest_out_file = os.path.join(output_dir, model_name + '.fts')
with open(ftest_out_file,"wt") as f:
print >>f, '/NumWaves\t', (contrasts.shape)[0]
print >>f, '/NumContrasts\t', count_ftests
# print labels for the columns - mainly for double-checking your
# model
col_string = '\n'
for con in contrast_names:
col_string = col_string + con + '\t'
print >>f, col_string, '\n'
print >>f, '/Matrix'
for i in range(fts_n.shape[0]):
print >>f, ' '.join(fts_n[i].astype('str'))
return out_file, ftest_out_file
def create_flame_model_files(design_matrix, col_names, contrasts_dict, \
custom_contrasts_csv, ftest_list, group_sep, grouping_vector, \
coding_scheme, model_name, output_measure, output_dir):
mat_file = write_mat_file(design_matrix, output_dir, model_name, \
col_names, output_measure)
grp_file = create_grp_file(design_matrix, grouping_vector, output_dir, \
model_name)
if contrasts_dict:
con_file = create_con_file(contrasts_dict, col_names, model_name, \
output_measure, output_dir)
if len(ftest_list) > 0:
fts_file = create_fts_file(ftest_list, contrasts_dict, \
model_name, output_measure, output_dir)
else:
fts_file = None
elif custom_contrasts_csv:
con_file, fts_file = create_con_ftst_file(custom_contrasts_csv, \
model_name, output_measure, output_dir, col_names, coding_scheme,\
group_sep)
return mat_file, grp_file, con_file, fts_file