/
marie_ventral_mosaic.py
executable file
·200 lines (146 loc) · 5.25 KB
/
marie_ventral_mosaic.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
import os
from nibabel.freesurfer import read_geometry, read_morph_data
from nibabel.gifti import read as gifti_read
from mayavi import mlab
import numpy as np
import colorsys
from matplotlib import colors
basedir = ("/neurospin/unicog/protocols/IRMf/"
"mathematicians_Amalric_Dehaene2012/"
"Surface_analysis/mathematicians/")
subjects = ['aa130114',
'aa130169',
'al130244',
'bm120103',
'ce130459',
'cf120444',
'fm120345',
'hr120357',
'jc130030',
'jl120341',
'jl130200',
'kg120369',
'ld130145',
'lr120300',
'mp130263',
'mr120371',
'rm130241',
'mk130199',
'of140017']
contrasts = (['bodies-others',
'math-others',
'houses-others',
'words-others',
'numbers-others',
'tools-others',
'faces-others',
'checkers-others'] +
['bodies-checkers',
'maths-checkers',
'houses-checkers',
'words-checkers',
'numbers-checkers',
'tools-checkers',
'faces-checkers'])
colormaps = dict(
words=dict(name='orange', H=30),
houses=dict(name='jaune', H=60),
faces=dict(name='rouge', H=0),
math=dict(name='vert', H=120),
numbers=dict(name='bleu', H=240),
tools=dict(name='violet', H=270),
bodies=dict(name='rose', H=300),
checkers=dict(name='cyan', H=180)
)
def make_colormap(name, color_hue):
color = colorsys.hsv_to_rgb(color_hue, 1., 1.)
color4 = tuple(list(color) + [1.])
cmap = colors.LinearSegmentedColormap.from_list(
name, [(0., 0., 0., .9), color4]) # La 4e valeur: alpha
return cmap
for _, description in colormaps.items():
description['colormap'] = make_colormap(description['name'],
description['H'] / 360.)
def get_surface_file(subject, side, file_type="inflated"):
return os.path.join(basedir, subject, "t1", subject, "surf",
"%s.%s" % (side, file_type))
def get_geometry(subject, side, surface_type="inflated"):
mesh_file = get_surface_file(subject, side, surface_type)
vertices, triangles = read_geometry(mesh_file)
x, y, z = vertices.T
return x, y, z, triangles
def get_curvature_sign(subject, side):
curv_file = get_surface_file(subject, side, "curv")
curv = (read_morph_data(curv_file) < 0).astype(int)
return curv
def get_contrast_file(subject, contrast, side):
return os.path.join(basedir, subject, "fmri", "results",
"%s_z_map_%s.gii" % (contrast, side))
def get_contrast(subject, contrast, side):
contrast_file = get_contrast_file(subject, contrast, side)
gii = gifti_read(contrast_file)
contrast = gii.darrays[0].data
return contrast
def show_contrasts(subject, contrasts, side, threshold):
x, y, z, triangles = get_geometry(subject, side, "inflated") ## inflated or white
curv = get_curvature_sign(subject, side)
f = mlab.figure()
mlab.clf()
# anatomical mesh
mlab.triangular_mesh(x, y, z, triangles, transparent=False,
opacity=1., name=subject,
scalars=curv, colormap="bone", vmin=-1, vmax=2)
mlab.title(subject)
cmaps = [colormaps[c.split("-")[0]]['colormap'] for c in contrasts]
for contrast, colormap in zip(contrasts, cmaps):
# functional mesh
data = get_contrast(subject, contrast, side)
func_mesh = mlab.pipeline.triangular_mesh_source(x, y, z, triangles,
scalars=data)
# threshold
thresh = mlab.pipeline.threshold(func_mesh, low=threshold)
surf = mlab.pipeline.surface(thresh, colormap='hot', transparent=True,
opacity=.8) # diminuer pour avoir plus de transparence
lut = (np.array([colormap(v) for v in np.linspace(.25, 1., 256)]) * 255
).astype(int)
surf.module_manager.scalar_lut_manager.lut.table = lut
mlab.draw()
return f
def usage_message():
print "Subjects to be chosen from "
print "\n".join(subjects)
print "\nContrasts to be chosen from"
print "\n".join(contrasts)
if __name__ == "__main__":
from argparse import ArgumentParser
import sys
parser = ArgumentParser()
parser.add_argument("subject")
parser.add_argument("--contrasts", nargs="+")
parser.add_argument("--side", nargs="+")
parser.add_argument("--threshold")
try:
args = parser.parse_args()
except:
usage_message()
sys.exit()
if args.subject is None:
usage_message()
sys.exit()
if args.contrasts is None:
usage_message()
sys.exit()
for contrast in args.contrasts:
if contrast not in contrasts:
usage_message()
sys.exit()
if args.side is None:
side = ['lh', 'rh']
else:
side = args.side
if args.threshold is None:
threshold = 2.
else:
threshold = int(args.threshold)
for s in side:
mfig = show_contrasts(args.subject, args.contrasts, s, threshold)