forked from celinede/scripts4DWI_dipy_brainhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_surf_stat_map_3.py
executable file
·408 lines (347 loc) · 15.4 KB
/
plot_surf_stat_map_3.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
def plot_surf_stat_map(coords, faces, stat_map=None,
elev=0, azim=0,
cmap='coolwarm',
threshold=None, bg_map=None,
bg_on_stat=False,
alpha='auto',
vmin=None, vmax=None,
cbar='sequential', # or'diverging'
symmetric_cbar="auto",
figsize=None,
labels=None, label_col=None, label_cpal=None,
mask=None, mask_lenient=None,
**kwargs):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns
# load mesh and derive axes limits
faces = np.array(faces, dtype=int)
limits = [coords.min(), coords.max()]
# set alpha if in auto mode
if alpha == 'auto':
if bg_map is None:
alpha = .5
else:
alpha = 1
# if cmap is given as string, translate to matplotlib cmap
if type(cmap) == str:
cmap = plt.cm.get_cmap(cmap)
# initiate figure and 3d axes
if figsize is not None:
fig = plt.figure(figsize=figsize)
else:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d', xlim=limits, ylim=limits)
ax.view_init(elev=elev, azim=azim)
ax.set_axis_off()
# plot mesh without data
p3dcollec = ax.plot_trisurf(coords[:, 1], coords[:, 0], coords[:, 2],
triangles=faces, linewidth=0.,
antialiased=False,
color='white')
# where mask is indices of nodes to include:
if mask is not None:
cmask = np.zeros(len(coords))
cmask[mask] = 1
cutoff = 2 # include triangles in cortex only if ALL nodes in mask
if mask_lenient: # include triangles in cortex if ANY are in mask
cutoff = 0
fmask = np.where(cmask[faces].sum(axis=1) > cutoff)[0]
# If depth_map and/or stat_map are provided, map these onto the surface
# set_facecolors function of Poly3DCollection is used as passing the
# facecolors argument to plot_trisurf does not seem to work
if bg_map is not None or stat_map is not None:
face_colors = np.ones((faces.shape[0], 4))
face_colors[:, :3] = .5*face_colors[:, :3]
if bg_map is not None:
bg_data = bg_map
if bg_data.shape[0] != coords.shape[0]:
raise ValueError('The bg_map does not have the same number '
'of vertices as the mesh.')
bg_faces = np.mean(bg_data[faces], axis=1)
bg_faces = bg_faces - bg_faces.min()
bg_faces = bg_faces / bg_faces.max()
face_colors = plt.cm.gray_r(bg_faces)
# modify alpha values of background
face_colors[:, 3] = alpha*face_colors[:, 3]
if stat_map is not None:
stat_map_data = stat_map
stat_map_faces = np.mean(stat_map_data[faces], axis=1)
if cbar is 'diverging':
print cbar
# Call _get_plot_stat_map_params to derive symmetric vmin and vmax
# And colorbar limits depending on symmetric_cbar settings
cbar_vmin, cbar_vmax, vmin, vmax = \
_get_plot_stat_map_params(stat_map_faces, vmax,
symmetric_cbar, kwargs)
if cbar is 'sequential':
if vmin is None:
vmin = stat_map_data.min()
if vmax is None:
vmax = stat_map_data.max()
if threshold is not None:
kept_indices = np.where(abs(stat_map_faces) >= threshold)[0]
stat_map_faces = stat_map_faces - vmin
stat_map_faces = stat_map_faces / (vmax-vmin)
if bg_on_stat:
face_colors[kept_indices] = cmap(stat_map_faces[kept_indices]) * face_colors[kept_indices]
else:
face_colors[kept_indices] = cmap(stat_map_faces[kept_indices])
else:
stat_map_faces = stat_map_faces - vmin
stat_map_faces = stat_map_faces / (vmax-vmin)
if bg_on_stat:
if mask is not None:
face_colors[fmask] = cmap(stat_map_faces)[fmask] * face_colors[fmask]
else:
face_colors = cmap(stat_map_faces) * face_colors
else:
if mask is not None:
face_colors[fmask] = cmap(stat_map_faces)[fmask]
else:
face_colors = cmap(stat_map_faces)
if labels is not None:
'''
labels requires a tuple of label/s, each a list/array of node indices
----------------------------------------------------------------------
color palette for labels
if label_cpal is None, outlines will be black
if it's a color palette name, a different color for each label will be generated
if it's a list of rgb or color names, these will be used
valid color names from http://xkcd.com/color/rgb/
'''
if label_cpal is not None:
if label_col is not None:
raise ValueError("Don't use label_cpal and label_col together.")
if type(label_cpal) == str:
cpal = sns.color_palette(label_cpal, len(labels))
if type(label_cpal) == list:
if len(label_cpal) < len(labels):
raise ValueError('There are not enough colors in the color list.')
try:
cpal = sns.color_palette(label_cpal)
except:
cpal = sns.xkcd_palette(label_cpal)
for n_label, label in enumerate(labels):
for n_face, face in enumerate(faces):
count = len(set(face).intersection(set(label)))
if (count > 0) & (count < 3):
if label_cpal is None:
if label_col is not None:
face_colors[n_face,0:3] = sns.xkcd_palette([label_col])[0]
else:
face_colors[n_face,0:3] = sns.xkcd_palette(["black"])[0]
else:
face_colors[n_face,0:3] = cpal[n_label]
p3dcollec.set_facecolors(face_colors)
return fig
def _get_plot_stat_map_params(stat_map_data, vmax, symmetric_cbar, kwargs,
force_min_stat_map_value=None):
import numpy as np
""" Internal function for setting value limits for plot_stat_map and
plot_glass_brain.
The limits for the colormap will always be set to range from -vmax to vmax.
The limits for the colorbar depend on the symmetric_cbar argument, please
refer to docstring of plot_stat_map.
"""
# make sure that the color range is symmetrical
if vmax is None or symmetric_cbar in ['auto', False]:
# Avoid dealing with masked_array:
if hasattr(stat_map_data, '_mask'):
stat_map_data = np.asarray(
stat_map_data[np.logical_not(stat_map_data._mask)])
stat_map_max = np.nanmax(stat_map_data)
if force_min_stat_map_value == None:
stat_map_min = np.nanmin(stat_map_data)
else:
stat_map_min = force_min_stat_map_value
if symmetric_cbar == 'auto':
symmetric_cbar = stat_map_min < 0 and stat_map_max > 0
if vmax is None:
vmax = max(-stat_map_min, stat_map_max)
if 'vmin' in kwargs:
raise ValueError('this function does not accept a "vmin" '
'argument, as it uses a symmetrical range '
'defined via the vmax argument. To threshold '
'the map, use the "threshold" argument')
vmin = -vmax
if not symmetric_cbar:
negative_range = stat_map_max <= 0
positive_range = stat_map_min >= 0
if positive_range:
cbar_vmin = 0
cbar_vmax = None
elif negative_range:
cbar_vmax = 0
cbar_vmin = None
else:
cbar_vmin = stat_map_min
cbar_vmax = stat_map_max
else:
cbar_vmin, cbar_vmax = None, None
return cbar_vmin, cbar_vmax, vmin, vmax
def plot_surf_label(coords, faces,
labels=None,
elev=0, azim=0,
cpal='bright',
threshold=None,
bg_map=None,
bg_on_labels=False,
alpha='auto',
figsize=None,
**kwargs):
'''
- labels requires a tuple of label/s, each a list/array of node indices
- cpal takes either the name of a seaborn color palette or matplotlib color map,
or a list of rgb values or color names from http://xkcd.com/color/rgb/
'''
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns
# load mesh and derive axes limits
faces = np.array(faces, dtype=int)
limits = [coords.min(), coords.max()]
# set alpha if in auto mode
if alpha == 'auto':
if bg_map is None:
alpha = .5
else:
alpha = 1
# if cap is given as string, translate to seaborn color palette
if type(cpal) == str:
cpal = sns.color_palette(cpal, len(labels))
if type(cpal) == list:
if len(cpal) < len(labels):
raise ValueError('There are not enough colors in the color list.')
try:
cpal = sns.color_palette(cpal)
except:
cpal = sns.xkcd_palette(cpal)
# initiate figure and 3d axes
if figsize is not None:
fig = plt.figure(figsize=figsize)
else:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d', xlim=limits, ylim=limits)
ax.view_init(elev=elev, azim=azim)
ax.set_axis_off()
# plot mesh without data
p3dcollec = ax.plot_trisurf(coords[:, 0], coords[:, 1], coords[:, 2],
triangles=faces, linewidth=0.,
antialiased=False,
color='white')
if bg_map is not None or labels is not None:
face_colors = np.ones((faces.shape[0], 4))
face_colors[:, :3] = .5*face_colors[:, :3]
if bg_map is not None:
bg_data = bg_map
if bg_data.shape[0] != coords.shape[0]:
raise ValueError('The bg_map does not have the same number '
'of vertices as the mesh.')
bg_faces = np.mean(bg_data[faces], axis=1)
bg_faces = bg_faces - bg_faces.min()
bg_faces = bg_faces / bg_faces.max()
face_colors = plt.cm.gray_r(bg_faces)
# modify alpha values of background
face_colors[:, 3] = alpha*face_colors[:, 3]
# color the labels, either overriding or overlaying bg_map
if labels is not None:
for n_label,label in enumerate(labels):
for n_face, face in enumerate(faces):
count = len(set(face).intersection(set(label)))
if count > 1:
if bg_on_labels:
face_colors[n_face,0:3] = cpal[n_label] * face_colors[n_face,0:3]
else:
face_colors[n_face,0:3] = cpal[n_label]
p3dcollec.set_facecolors(face_colors)
return fig
def crop_img(fig, margin=10):
# takes fig, returns image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import os
fig.savefig('./tempimage', bbox_inches='tight', orientation='landscape')
plt.close(fig)
img = mpimg.imread('./tempimage.png')
os.remove('./tempimage.png')
kept = {'rows':[], 'cols':[]}
for row in range(img.shape[0]):
if len(set(np.ndarray.flatten(img[row,:,:]))) > 1:
kept['rows'].append(row)
for col in range(img.shape[1]):
if len(set(np.ndarray.flatten(img[:,col,:]))) > 1:
kept['cols'].append(col)
if margin:
return img[min(kept['rows'])-margin:max(kept['rows'])+margin,
min(kept['cols'])-margin:max(kept['cols'])+margin]
else:
return img[kept['rows']][:,kept['cols']]
def create_fig(data=None, coords=None, faces=None, azims=None, elevs=None,
labels=None, label_col=None,
hemi=None, sulc=None, alpha='auto',
cmap='cubehelix', cpal='bright', cbar=False,
dmin=None, dmax=None,
mask=None):
import nibabel as nib, numpy as np
import matplotlib.pyplot as plt, matplotlib as mpl
from IPython.core.display import Image, display
import os
if sulc is None:
sulc_bool = False
else:
sulc_bool = True
# create images
imgs = []
for (azim, elev) in zip(azims, elevs):
#for elev in elevs:
if data is not None:
if dmin is None:
dmin = data[np.nonzero(data)].min()
if dmax is None:
dmax = data.max()
fig = plot_surf_stat_map(coords, faces, stat_map=data,
elev=elev, azim=azim,
cmap=cmap, cbar='sequential',
bg_map=sulc,bg_on_stat=sulc_bool,
vmin=dmin, vmax=dmax,
labels=labels, label_col=label_col,
alpha=alpha,
mask=mask, mask_lenient=False)
#label_cpal=cpal)
else:
fig = plot_surf_label(coords, faces,
labels=labels,
elev=elev, azim=azim,
bg_map=sulc,
cpal=cpal,
bg_on_labels=sulc_bool,
alpha=alpha)
# crop image
imgs.append((crop_img(fig, margin=15)),)
plt.close(fig)
# create figure with color bar
fig = plt.figure()
fig.set_size_inches(8, 4)
ax1 = plt.subplot2grid((4,60), (0,0), colspan = 26, rowspan =4)
plt.imshow(imgs[0])
ax1.set_axis_off()
ax2 = plt.subplot2grid((4,60), (0,28), colspan = 26, rowspan =4)
plt.imshow(imgs[1])
ax2.set_axis_off()
if cbar==True and data is not None:
cax = plt.subplot2grid((4,60), (1,59), colspan = 1, rowspan =2)
cmap = plt.cm.get_cmap(cmap)
norm = mpl.colors.Normalize(vmin=dmin, vmax=dmax)
cb = mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm)
cb.set_ticks([dmin, dmax])
return fig
#fig.savefig('./tempimage')
#plt.close(fig)
#display(Image(filename='./tempimage.png', width=800))
#os.remove('./tempimage.png')