-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_surface_stat.py
executable file
·358 lines (291 loc) · 12.4 KB
/
script_surface_stat.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
"""
script to calibrate RFX/MFX statistic in the volume
Note
----
stat_calibration and mesh_processing_dev modules are not publicly available
Bertrand Thirion
"""
import os.path as op
import numpy as np
from configobj import ConfigObj
import nipy.neurospin.group.onesample as fos
import nipy.neurospin.graph.graph as fg
import mixed_effects_stat as mes
import mesh_processing as mep
from stat_calibration import *
from soma import aims
from nipy.neurospin.glm_files_layout import tio
################################################################
# User Parameters
################################################################
# b_cached:
# - False = compute and store area
# - True = use cached area
# - None = are = 1
b_cached = False
# whether to use smoothed data or not
b_smooth = False
################################################################
# first define paths etc.
################################################################
# list of subjects
subj = ['s12069', 's12300', 's12401', 's12431', 's12508', 's12532', 's12539', 's12562','s12590', 's12635', 's12636', 's12898', 's12081', 's12165', 's12207', 's12344', 's12352', 's12370', 's12381', 's12405', 's12414', 's12432']
nsubj = len(subj)
# paths to mesh
db_path = '/data/home/virgile/virgile_internship'
left_meshes = [op.join(db_path,"%s/surf/lh.r.aims.white.mesh") %s for s in subj]
right_meshes = [op.join(db_path,"%s/surf/rh.r.aims.white.mesh") %s for s in subj]
# output path
if b_smooth:
threshold_path = 'surface_threshold_smooth.con'
# output dir
swd = '/data/home/virgile/virgile_internship/group_analysis/smoothed_FWHM5'
else:
threshold_path = 'surface_threshold.con'
# output dir
swd = '/data/home/virgile/virgile_internship/group_analysis/smoothed_FWHM0'
################################################################
# Create a topological model from the mesh
################################################################
# buils graph from meshes
R = aims.Reader()
left_mesh = R.read(left_meshes[0])
lg = mep.mesh_to_graph(left_mesh)
right_mesh = R.read(right_meshes[0])
rg = mep.mesh_to_graph(right_mesh)
gg = fg.concatenate_graphs(lg,rg)
area = np.ones(gg.V)
if b_cached==True:
area = np.load('area.npz')
elif b_cached==False:
area = np.zeros(gg.V)
for s in range(nsubj):
area[:lg.V] += mep.node_area(R.read(left_meshes[s]))
area[lg.V:] += mep.node_area(R.read(right_meshes[s]))
area /= nsubj
area.dump('area.npz')
ijk = np.reshape(np.arange(gg.V), (gg.V, 1))
################################################################
# Load the effects and variance
################################################################
# list of contrasts
contrast = ['audio-video']
contrast_id = contrast
def load_textures(left_con_tex, right_con_tex, left_var_tex, right_var_tex):
"""
"""
nsubj = len(left_con_tex)
beta = []
varbeta = []
tiny = 1.e-15
for s in range(nsubj):
ltex = tio.Texture(left_con_tex[s]).read(left_con_tex[s]).data
rtex = tio.Texture(right_con_tex[s]).read(right_con_tex[s]).data
beta.append(np.hstack((ltex,rtex)))
ltex = tio.Texture(left_var_tex[s]).read(left_var_tex[s]).data
rtex = tio.Texture(right_var_tex[s]).read(right_var_tex[s]).data
varbeta.append(np.hstack((ltex,rtex)))
VarFunctional = np.array(varbeta).T
Functional = np.array(beta).T
Functional[np.isnan(Functional)] = 0
VarFunctional[np.isnan(VarFunctional)] = 0
VarFunctional = np.maximum(VarFunctional, tiny)
return Functional, VarFunctional
################################################################
# Compute the thresholds
################################################################
for cid in contrast_id:
print cid
# build textures paths
if b_smooth:
left_con_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM5/Contrast/left_%s_con.tex")
% (s,cid) for s in subj]
right_con_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM5/Contrast/right_%s_con.tex")
% (s,cid) for s in subj]
left_var_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM5/Contrast/left_%s_ResMS.tex")
% (s,cid) for s in subj]
right_var_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM5/Contrast/right_%s_ResMS.tex")
% (s,cid) for s in subj]
else:
left_con_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM0/Contrast/left_%s_con.tex")
% (s,cid) for s in subj]
right_con_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM0/Contrast/right_%s_con.tex")
% (s,cid) for s in subj]
left_var_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM0/Contrast/left_%s_ResMS.tex")
% (s,cid) for s in subj]
right_var_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM0/Contrast/right_%s_ResMS.tex")
% (s,cid) for s in subj]
# organize functional and variance functional data in an array structure
Functional, VarFunctional = load_textures(left_con_tex, right_con_tex, left_var_tex, right_var_tex)
zc_rfx = select_voxel_level_threshold(
Functional, VarFunctional, nbsamp=1024, pval=0.05, method='rfx',
corrected=True, group_size=nsubj)
print zc_rfx
zc_mfx = select_voxel_level_threshold(
Functional, VarFunctional, nbsamp=1024,pval=0.05, method='mfx',
corrected=True, group_size=nsubj)
print zc_mfx
z1_rfx = select_voxel_level_threshold(
Functional, VarFunctional, nbsamp=1024, pval=0.001, method='rfx',
corrected=False, group_size=nsubj)
print z1_rfx
z2_rfx = select_cluster_level_threshold(
Functional, VarFunctional, ijk, nbsamp=1024, pval=0.05, method='rfx',
threshold=z1_rfx, group_size=nsubj, graph=gg, volume=area)
print z2_rfx
z1_mfx = select_voxel_level_threshold(
Functional, VarFunctional, nbsamp=1024, pval=0.001, method='mfx',
corrected=False, group_size=nsubj)
print z1_mfx
z2_mfx = select_cluster_level_threshold(
Functional, VarFunctional, ijk, nbsamp=1024, pval=0.05, method='mfx',
threshold=z1_mfx, group_size=nsubj, graph=gg, volume=area)
print z2_mfx
# save the thresholds in a ConfigObj structure
thresholds={cid:{'zc_rfx':zc_rfx, 'zc_mfx':zc_mfx, 'z1_rfx':z1_rfx,\
'z2_rfx':z2_rfx, 'z1_mfx':z1_mfx, 'z2_mfx':z2_mfx}}
x = ConfigObj(threshold_path)
for k in thresholds.keys():
x[k] = thresholds[k]
x.write()
##########################################################################
# Do the group analysis
##########################################################################
for cid in contrast_id:
print cid
# build textures paths
if b_smooth:
left_con_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM5/Contrast/left_%s_con.tex")
% (s,cid) for s in subj]
right_con_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM5/Contrast/right_%s_con.tex")
% (s,cid) for s in subj]
left_var_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM5/Contrast/left_%s_ResMS.tex")
% (s,cid) for s in subj]
right_var_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM5/Contrast/right_%s_ResMS.tex")
% (s,cid) for s in subj]
else:
left_con_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM0/Contrast/left_%s_con.tex")
% (s,cid) for s in subj]
right_con_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM0/Contrast/right_%s_con.tex")
% (s,cid) for s in subj]
left_var_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM0/Contrast/left_%s_ResMS.tex")
% (s,cid) for s in subj]
right_var_tex = [
op.join(db_path,
"%s/fct/glm/smoothed_FWHM0/Contrast/right_%s_ResMS.tex")
% (s,cid) for s in subj]
Functional, VarFunctional = load_textures(left_con_tex, right_con_tex, left_var_tex, right_var_tex)
#------------------------------------------------------------------------
# load the thresholds
#------------------------------------------------------------------------
thresholds = ConfigObj(threshold_path)
zc_rfx = float(thresholds[cid]['zc_rfx'])
zc_mfx = float(thresholds[cid]['zc_mfx'])
z1_rfx = float(thresholds[cid]['z1_rfx'])
z1_mfx = float(thresholds[cid]['z1_mfx'])
z2_rfx = float(thresholds[cid]['z2_rfx'])
z2_mfx = float(thresholds[cid]['z2_mfx'])
#------------------------------------------------------------------------
# rfx
#------------------------------------------------------------------------
#y = fos.stat(Functional, id='student', axis=1)
y = mes.t_stat(Functional)
#------------------------------------------------------------------------
# voxel-level rfx
#------------------------------------------------------------------------
thy = y*(y>zc_rfx).astype(np.float)
tex_labels_name = op.join(swd,"left_vrfx_%s.tex"%cid)
textureW = tio.Texture(tex_labels_name, data = thy[:lg.V])
textureW.write(tex_labels_name)
tex_labels_name = op.join(swd,"right_vrfx_%s.tex"%cid)
textureW = tio.Texture(tex_labels_name, data = thy[lg.V:])
textureW.write(tex_labels_name)
print 'number of active nodes: left %d, right: %d' \
%(np.sum(thy[:lg.V]), np.sum(thy[lg.V:lg.V+rg.V]))
#------------------------------------------------------------------------
# cluster-level rfx
#------------------------------------------------------------------------
y = np.reshape(y,np.size(y))
y = y * (y>z1_rfx)
idx = np.nonzero(y)[0]
n1 = len(idx)
if n1>0:
sg = gg.subgraph(y>z1_rfx)
u = sg.cc()
larea = area[y>z1_rfx]
su = np.array([np.sum(larea[u==k]) for k in np.unique(u)])
y[idx[su[u]<=z2_rfx]] = 0
print "Number of clusters: %04d"% np.sum(su>z2_rfx)
tex_labels_name = op.join(swd,"left_crfx_%s.tex"%cid)
textureW = tio.Texture(tex_labels_name, data = y[:lg.V])
textureW.write(tex_labels_name)
tex_labels_name = op.join(swd,"right_crfx_%s.tex"%cid)
textureW = tio.Texture(tex_labels_name, data = y[lg.V:])
textureW.write(tex_labels_name)
#------------------------------------------------------------------------
# mfx
#------------------------------------------------------------------------
#y = fos.stat_mfx(Functional, VarFunctional, id='student_mfx', axis=1)
y = mes.mfx_t_stat(Functional, VarFunctional)
#------------------------------------------------------------------------
# voxel-level mfx
#------------------------------------------------------------------------
thy = y*(y>zc_mfx).astype(np.float)
tex_labels_name = op.join(swd,"left_vmfx_%s.tex"%cid)
textureW = tio.Texture(tex_labels_name, data = thy[:lg.V])
textureW.write(tex_labels_name)
tex_labels_name = op.join(swd,"right_vmfx_%s.tex"%cid)
textureW = tio.Texture(tex_labels_name, data = thy[lg.V:])
textureW.write(tex_labels_name)
print 'number of active nodes: left %d, right: %d' \
%(np.sum(thy[:lg.V]), np.sum(thy[lg.V:lg.V+rg.V]))
#------------------------------------------------------------------------
# cluster-level mfx
#------------------------------------------------------------------------
y = np.reshape(y,np.size(y))
y = y * (y>z1_mfx)
idx = np.nonzero(y)[0]
n1 = len(idx)
if n1>0:
sg = gg.subgraph(y>z1_mfx)
u = sg.cc()
larea = area[y>z1_mfx]
su = np.array([np.sum(larea[u==k]) for k in np.unique(u)])
y[idx[su[u]<=z2_mfx]] = 0
print "Number of clusters: %04d"% np.sum(su>z2_mfx)
tex_labels_name = op.join(swd,"left_cmfx_%s.tex"%cid)
textureW = tio.Texture(tex_labels_name, data = y[:lg.V])
textureW.write(tex_labels_name)
tex_labels_name = op.join(swd,"right_cmfx_%s.tex"%cid)
textureW = tio.Texture(tex_labels_name, data = y[lg.V:])
textureW.write(tex_labels_name)