-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
376 lines (307 loc) · 12.8 KB
/
utils.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
import numpy as np
import mne
from config import fname, event_id
import matplotlib.pyplot as plt
# from config import roinames_time
from matplotlib.colors import ListedColormap,LinearSegmentedColormap
from functools import partial
import types
import matplotlib.pyplot as plt
from matplotlib.cm import get_cmap
from mne.viz import Brain
from mne.viz.topo import _iter_topography
import numpy as np
from scipy.spatial import distance
import matplotlib as mpl
def create_grow_ROIs(stcs, roinames_time, extents=20):
func_rois = []
for roiname in roinames_time:
if roiname == 'STC':
labels_name = ['superiortemporal-lh',
'bankssts-lh', 'transversetemporal-lh']
elif roiname == 'OTC':
labels_name = ['fusiform-lh']
elif roiname == 'precentral':
labels_name = ['precentral-lh']
elif roiname == 'IFG':
labels_name = ['parsorbitalis-lh', 'parstriangularis-lh', 'parsopercularis-lh',
'lateralorbitofrontal-lh', 'frontalpole-lh']
tmin, tmax = roinames_time[roiname][:-1]
stc = stcs[roinames_time[roiname][-1]]-stcs[0]
peak_vertex, peak_time = stc.get_peak(
hemi='lh', tmin=tmin, tmax=tmax, mode='pos') # vertex ID
rough_labels = mne.grow_labels(
'fsaverage', [peak_vertex], extents, hemis=0, names=roiname)
brain=stc.plot(initial_time=peak_time,hemi='lh',)
# #%%
brain.add_foci(peak_vertex, coords_as_verts=True, hemi="lh", color="k")
# # We add a title as well, stating the amplitude at this time and location
# brain.add_text(0.1, 0.9, "Peak coordinate", "title", font_size=14)
# # rough_labels=mne.label.select_sources(SUBJECT,'lh',peak_vertex, extents,subjects_dir=fname.mri_subjects_dir, name=roiname)
# brain.add_label(rough_labels[0])
func_rois.append(rough_labels[0])
return func_rois
def create_grow_ROIs1(stcs, roinames_time=['STC1','STC2','precentral','OT'], extents=15,plot=False):
labels = mne.read_labels_from_annot('fsaverage', 'aparc.a2009s', 'lh',
)
func_rois = []
stc = stcs[-1]-stcs[0]
for roiname in roinames_time:
print(roiname)
if roiname == 'STC1':
labels_name = ['G_temp_sup-G_T_transv-lh',
'S_temporal_transverse-lh',]
extents=20
elif roiname == 'STC2':
labels_name = ['S_temporal_sup-lh',]
extents=15
elif roiname == 'precentral':
labels_name = ['G_precentral-lh','S_central-lh','S_precentral-inf-part-lh']
extents=15
elif roiname == 'OT':
labels_name = ['S_oc-temp_lat-lh']
stc = stcs[-2]-stcs[0]
extents=15
print(labels_name)
for j in range(0, len(labels_name)):
print(j)
l = [label for label in labels if label.name ==
labels_name[j]][0]
if j == 0:
label = l
else:
label += l
# stc = np.mean(stcs)
stc_label = stc.in_label(label)
peak_vertex, peak_time = stc_label.get_peak(
hemi='lh',
tmin=0.3, tmax=1.1,
mode='pos') # vertex ID
rough_labels = mne.grow_labels(
'fsaverage', [peak_vertex], extents, hemis=0, names=roiname)
if plot:
brain = stc.plot(initial_time=peak_time, hemi='lh',)
#%%
brain.add_foci(peak_vertex,
coords_as_verts=True,
hemi="lh", color="k")
# # We add a title as well, stating the amplitude at this time and location
# brain.add_text(0.1, 0.9, "Peak coordinate", "title", font_size=14)
rough_labels = mne.grow_labels(
'fsaverage', [peak_vertex], extents, hemis=0, names=roiname)
brain.add_label(rough_labels[0], borders=True)
print('.')
func_rois.append(rough_labels[0])
return func_rois
def create_grow_ROIs2(stcs, roinames_time=['OTC','STC','precentral','OC'],
parc='aparc.a2009s_custom_gyrus_sulcus_1100mm2',
extents=30, plot=False):
labels = mne.read_labels_from_annot('fsaverage', parc, 'both',
)
func_rois = []
for roiname in roinames_time:
if roiname == 'STC':
stc = stcs[-1]-stcs[0]
labels_name = [
# 'G_temp_sup-Lateral+S_temporal_sup_sub1-lh',
# 'G_temp_sup-Lateral+S_temporal_sup_sub2-lh',
# 'G_temp_sup-Lateral+S_temporal_sup_sub3-lh',
'G_pariet_inf-Supramar+G_temp_sup-Plan_tempo+G_temp_sup-G_T_transv+S_temporal_transverse+Lat_Fis-post_sub1-lh',
# 'G_Ins_lg_and_S_cent_ins+G_temp_sup-Plan_polar+S_circular_insula_inf+G_insular_short+S_circular_insula_ant_sub2-lh',
]
elif roiname == 'OTC':
labels_name = ['G_oc-temp_lat-fusifor+S_oc-temp_lat-lh',
# 'G_and_S_occipital_inf+S_occipital_ant+S_collat_transv_post-lh'
]
stc = stcs[2]-stcs[0]
elif roiname == 'OC':
labels_name = [
'G_and_S_occipital_inf+S_occipital_ant+S_collat_transv_post-lh'
]
# extents=15
stc = stcs[2]-stcs[0]
elif roiname == 'precentral':
# extents=15
stc = stcs[-1]-stcs[0]
labels_name = [ 'G_precentral+S_central+S_precentral-inf-part+S_precentral-sup-part_sub2-lh',
'G_precentral+S_central+S_precentral-inf-part+S_precentral-sup-part_sub1-lh',]
# elif roiname == 'IFG':
# labels_name = ['parsorbitalis-lh', 'parstriangularis-lh', 'parsopercularis-lh',
# 'lateralorbitofrontal-lh', 'frontalpole-lh']
for j in np.arange(0, len(labels_name)):
l = [label for label in labels if label.name ==
labels_name[j]][0]
if j == 0:
label = l
else:
label += l
# tmin, tmax = roinames_time[roiname][:-1]
# stc = np.mean(stcs)
stc_label = stc.in_label(label)
peak_vertex, peak_time = stc_label.get_peak(
hemi='lh',
# vert_as_index=False,
tmin=0.3, tmax=1.1,
mode='pos') # vertex ID
# peak_vertex, _, peak_time=stc_label.center_of_mass(SUBJECT)
rough_labels = mne.grow_labels(
'fsaverage', [peak_vertex], extents, hemis=0, names=roiname)
if plot:
brain = stc.plot(initial_time=peak_time, hemi='lh',)
#%%
brain.add_foci(peak_vertex,
coords_as_verts=True,
hemi="lh", color="k")
# # We add a title as well, stating the amplitude at this time and location
# brain.add_text(0.1, 0.9, "Peak coordinate", "title", font_size=14)
brain.add_label(rough_labels[0], borders=True)
print('.')
func_rois.append(rough_labels[0])
return func_rois
def stc_baseline_correction(X, stc, tmin, tmax):
time_dim = len(stc.times)
# baseline_timepoints = X.times[np.where(X.times<0)]
# baseline_timepoints = X.times[np.where(X.times==tmin):np.where(X.times==tmax)]
# Convert tmin/tmax to sample indices
tmin, tmax = np.searchsorted(stc.times, [tmin, tmax])
baseline_timepoints = stc.times[tmin:tmax]
baseline_mean = X[:, tmin:tmax].mean(1)
baseline_mean_mat = np.repeat(baseline_mean.reshape([len(baseline_mean), 1]),
time_dim, axis=1)
corrected_stc = X - baseline_mean_mat
return corrected_stc
def mask_function(X, cut_off=None):
if cut_off is not None:
r, c = X.shape
for i in np.arange(0, r):
for j in np.arange(0, c):
if X[i, j] < cut_off:
X[i, j] = cut_off
return X
def select_rois(rois_id, parc='aparc.a2009s_custom_gyrus_sulcus_1100mm2',combines=[[0,1]]):
annotation = mne.read_labels_from_annot(
'fsaverage', parc=parc,)
rois = [label for label in annotation if 'Unknown' not in label.name]
labels=[]
if combines:
for ids in combines:
for j in np.arange(0, len(ids)):
if j == 0:
label = rois[rois_id[ids[j]]]
else:
label += rois[rois_id[ids[j]]]
labels.append(label)
com_ids=[rois_id[i] for i in sum(combines, [])]
left_ids=[i for i in rois_id if i not in com_ids]
labels.extend([rois[i] for i in left_ids])
return labels
def compare_p(pval):
"""
By: youj2
Parameters
----------
pval : TYPE
DESCRIPTION.
Returns
-------
p : TYPE
DESCRIPTION.
"""
if pval < 0.001:
p = 0.001
stars="***"
# elif pval < 0.005:
# p = 0.005
elif pval < 0.01:
p = 0.01
stars="**"
elif pval < 0.05:
p = 0.05
stars="*"
else:
p = 0.1
stars=''
return p, stars
# def discrete_cmap(N, base_cmap=None):
# """Create an N-bin discrete colormap from the specified input map"""
# # Note that if base_cmap is a string or None, you can simply do
# # return plt.cm.get_cmap(base_cmap, N)
# # The following works for string, None, or a colormap instance:
# base = plt.cm.get_cmap(base_cmap)
# color_list = base(np.linspace(0, 1, N))
# cmap_name = base.name + str(N)
# return ListedColormap(cmap_name, color_list, N)
def plot_roi_map(values, rois, subject, subjects_dir, cmap="plasma",hemi='both', alpha=1.0):
cmap = mpl.cm.viridis_r
cmap = [cmap(i) for i in np.linspace(0, 1, 4)]
dic={'0.1':0,'0.05':1,'0.01':2,'0.001':3,}
brain = Brain(
subject=subject, subjects_dir=subjects_dir, surf="inflated", hemi=hemi
)
labels_lh = np.zeros(len(brain.geo["lh"].coords), dtype=int)
labels_rh = np.zeros(len(brain.geo["rh"].coords), dtype=int)
ctab_lh = list()
ctab_rh = list()
for i, (roi, value) in enumerate(zip(rois, values), 1):
if roi.hemi == "lh":
labels = labels_lh
ctab = ctab_lh
else:
labels = labels_rh
ctab = ctab_rh
labels[roi.vertices] = i
ctab.append([int(x * 255) for x in cmap[dic[value]][:4]] + [i])
ctab_lh = np.array(ctab_lh)
ctab_rh = np.array(ctab_rh)
brain.add_annotation(
[(labels_lh, ctab_lh), (labels_rh, ctab_rh)], borders=False, alpha=alpha
)
return brain
# def create_functional_aparc_ROIs(stcs, thre=0.6):
# labels = mne.read_labels_from_annot('fsaverage', 'aparc', 'both',
# )
# func_rois = []
# for roiname in roinames_time:
# if roiname == 'STC':
# labels_name = ['superiortemporal-lh',
# 'bankssts-lh', 'transversetemporal-lh']
# elif roiname == 'OTC':
# labels_name = ['fusiform-lh']
# elif roiname == 'precentral':
# labels_name = ['precentral-lh']
# elif roiname == 'IFG':
# labels_name = ['parsorbitalis-lh', 'parstriangularis-lh', 'parsopercularis-lh',
# 'lateralorbitofrontal-lh', 'frontalpole-lh']
# tmin, tmax = roinames_time[roiname][:-1]
# # stc = stcs[roinames_time[roiname][-1]]-stcs[0]
# stc = np.mean(stcs)
# for j in np.arange(0, len(labels_name)):
# l = [label for label in labels if label.name ==
# labels_name[j]][0]
# if j == 0:
# label = l
# else:
# label += l
# stc_mean = stc.copy().crop(tmin, tmax).mean()
# stc_mean_label = stc_mean.in_label(label)
# data = np.abs(stc_mean_label.data)
# stc_mean_label.data[data < thre * np.max(data)] = 0.0
# func_labels, _ = mne.stc_to_label(
# stc_mean_label,
# src=mne.read_source_spaces(fname.fsaverage_src),
# smooth=True,
# connected=True,
# verbose="error",
# )
# func_rois.append(func_labels[0])
# return func_rois
if __name__ == "__main__":
# stcs=[]
# mne.set_config('SUBJECTS_DIR', fname.mri_subjects_dir)
# SUBJECT = 'fsaverage'
# for i, cat in enumerate(event_id):
# stc = mne.read_source_estimate(fname.ga_stc1(category=cat))
# stcs.append(stc)
# stc.subject=SUBJECT
# func_rois=create_grow_ROIs1(stcs,extents=15,plot=True)
labels=select_rois(rois_id=[34,32,50,62,])