/
avg_ROIs_definition.py
433 lines (415 loc) · 19.3 KB
/
avg_ROIs_definition.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
from jumeg.jumeg_preprocessing import get_files_from_list
import mne
import os
import numpy as np
from dirs_manage import reset_directory, set_directory
subjects_dir = os.environ['SUBJECTS_DIR']
def DICS_inverse(fn_epo, event_id=1,event='LLst', ctmin=0.05, ctmax=0.25, fmin=4, fmax=8,
min_subject='fsaverage'):
"""
Inverse evokes into source space using DICS method.
----------
fn_epo : epochs of raw data.
event_id: event id related with epochs.
ctmin: the min time for computing CSD
ctmax: the max time for computing CSD
fmin: min value of the interest frequency band
fmax: max value of the interest frequency band
min_subject: the subject for the common brain space.
save_forward: Whether save the forward solution or not.
"""
from mne import Epochs, pick_types
from mne.io import Raw
from mne.event import make_fixed_length_events
fnlist = get_files_from_list(fn_epo)
# loop across all filenames
for fname in fnlist:
meg_path = os.path.split(fname)[0]
name = os.path.basename(fname)
stc_name = name[:name.rfind('-epo.fif')]
subject = name.split('_')[0]
subject_path = subjects_dir + '/%s' %subject
min_dir = subjects_dir + '/%s' %min_subject
fn_trans = meg_path + '/%s-trans.fif' % subject
fn_src = subject_path + '/bem/%s-ico-5-src.fif' % subject
fn_bem = subject_path + '/bem/%s-5120-5120-5120-bem-sol.fif' % subject
# Make sure the target path is exist
stc_path = min_dir + '/DICS_ROIs/%s' % subject
set_directory(stc_path)
# Read the MNI source space
epochs = mne.read_epochs(fname)
tmin = epochs.times.min()
tmax = epochs.times.max()
fn_empty = meg_path + '/%s_empty,nr-raw.fif' % subject
raw_noise = Raw(fn_empty, preload=True)
epochs.info['bads'] = raw_noise.info['bads']
picks_noise = pick_types(raw_noise.info, meg='mag', exclude='bads')
events_noise = make_fixed_length_events(raw_noise, event_id, duration=1.)
epochs_noise = Epochs(raw_noise, events_noise, event_id, tmin,
tmax, proj=True, picks=picks_noise,
baseline=None, preload=True, reject=None)
# Make sure the number of noise epochs is the same as data epochs
epochs_noise = epochs_noise[:len(epochs.events)]
evoked = epochs.average()
forward = mne.make_forward_solution(epochs.info, trans=fn_trans,
src=fn_src, bem=fn_bem,
fname=None, meg=True, eeg=False,
mindist=5.0, n_jobs=2,
overwrite=True)
forward = mne.convert_forward_solution(forward, surf_ori=True)
from mne.time_frequency import compute_epochs_csd
from mne.beamformer import dics
data_csd = compute_epochs_csd(epochs, mode='multitaper', tmin=ctmin, tmax=ctmax,
fmin=fmin, fmax=fmax)
noise_csd = compute_epochs_csd(epochs_noise, mode='multitaper', tmin=ctmin, tmax=ctmax,
fmin=fmin, fmax=fmax)
stc = dics(evoked, forward, noise_csd, data_csd)
from mne import morph_data
stc_morph = morph_data(subject, min_subject, stc, grade=5, smooth=5)
stc_morph.save(stc_path + '/%s_%d_%d' % (event, fmin, fmax), ftype='stc')
def apply_inverse(fnepo, method='dSPM', event='LLst', min_subject='fsaverage', STC_US='ROI',
snr=3.0):
'''
Parameter
---------
fnepo: string or list
The epochs file with ECG, EOG and environmental noise free.
method: inverse method, 'MNE' or 'dSPM'
event: string
The event name related with epochs.
min_subject: string
The subject name as the common brain.
STC_US: string
The using of the inversion for further analysis.
'ROI' stands for ROIs definition, 'CAU' stands for causality analysis.
snr: signal to noise ratio for inverse solution.
'''
#Get the default subjects_dir
from mne.minimum_norm import (apply_inverse, apply_inverse_epochs)
fnlist = get_files_from_list(fnepo)
# loop across all filenames
for fname in fnlist:
fn_path = os.path.split(fname)[0]
name = os.path.basename(fname)
stc_name = name[:name.rfind('-epo.fif')]
subject = name.split('_')[0]
subject_path = subjects_dir + '/%s' %subject
min_dir = subjects_dir + '/%s' %min_subject
fn_trans = fn_path + '/%s-trans.fif' % subject
fn_cov = fn_path + '/%s_empty,nr-cov.fif' % subject
fn_src = subject_path + '/bem/%s-ico-4-src.fif' % subject
fn_bem = subject_path + '/bem/%s-5120-5120-5120-bem-sol.fif' % subject
snr = snr
lambda2 = 1.0 / snr ** 2
#noise_cov = mne.read_cov(fn_cov)
epochs = mne.read_epochs(fname)
noise_cov = mne.read_cov(fn_cov)
if STC_US == 'ROI':
# this path used for ROI definition
stc_path = min_dir + '/%s_ROIs/%s' %(method,subject)
#fn_cov = meg_path + '/%s_empty,fibp1-45,nr-cov.fif' % subject
evoked = epochs.average()
set_directory(stc_path)
noise_cov = mne.cov.regularize(noise_cov, evoked.info,
mag=0.05, grad=0.05, proj=True)
fwd_ev = mne.make_forward_solution(evoked.info, trans=fn_trans,
src=fn_src, bem=fn_bem,
fname=None, meg=True, eeg=False,
mindist=5.0, n_jobs=2,
overwrite=True)
fwd_ev = mne.convert_forward_solution(fwd_ev, surf_ori=True)
forward_meg_ev = mne.pick_types_forward(fwd_ev, meg=True, eeg=False)
inverse_operator_ev = mne.minimum_norm.make_inverse_operator(
evoked.info, forward_meg_ev, noise_cov,
loose=0.2, depth=0.8)
# Compute inverse solution
stc = apply_inverse(evoked, inverse_operator_ev, lambda2, method,
pick_ori=None)
# Morph STC
subject_id = min_subject
stc_morph = mne.morph_data(subject, subject_id, stc, grade=5, smooth=5)
stc_morph.save(stc_path + '/%s' % (stc_name), ftype='stc')
elif STC_US == 'CAU':
stcs_path = min_dir + '/stcs/%s/%s/' % (subject,event)
reset_directory(stcs_path)
noise_cov = mne.cov.regularize(noise_cov, epochs.info,
mag=0.05, grad=0.05, proj=True)
fwd = mne.make_forward_solution(epochs.info, trans=fn_trans,
src=fn_src, bem=fn_bem,
meg=True, eeg=False, mindist=5.0,
n_jobs=2, overwrite=True)
fwd = mne.convert_forward_solution(fwd, surf_ori=True)
forward_meg = mne.pick_types_forward(fwd, meg=True, eeg=False)
inverse_operator = mne.minimum_norm.make_inverse_operator(
epochs.info, forward_meg, noise_cov, loose=0.2,
depth=0.8)
# Compute inverse solution
stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2,
method=method, pick_ori='normal')
s = 0
while s < len(stcs):
stc_morph = mne.morph_data(
subject, min_subject, stcs[s], grade=5, smooth=5)
stc_morph.save(stcs_path + '/trial%s_fsaverage'
% (subject, str(s)), ftype='stc')
s = s + 1
def apply_stcs(method='dSPM', event='LLst'):
import glob, os
from scipy.signal import detrend
from scipy.stats.mstats import zscore
fn_list = glob.glob(subjects_dir+'/fsaverage/%s_ROIs/*/*,evtW_%s_bc-lh.stc' % (method, event))
stcs = []
for fname in fn_list:
stc = mne.read_source_estimate(fname)
#stc = stc.crop(tmin, tmax)
cal_data = stc.data
dt_data = detrend(cal_data, axis=-1)
zc_data = zscore(dt_data, axis=-1)
stc.data.setfield(zc_data, np.float32)
stcs.append(stc)
stcs = np.array(stcs)
stc_avg = np.sum(stcs, axis=0)/stcs.shape[0]
fn_avg = subjects_dir+'/fsaverage/%s_ROIs/%s' %(method,event)
stc_avg.save(fn_avg, ftype='stc')
def apply_rois(fn_stc, tmin, tmax, thr, min_subject='fsaverage'):
#fn_avg = subjects_dir+'/fsaverage/%s_ROIs/%s-lh.stc' %(method,evt_st)
stc_avg = mne.read_source_estimate(fn_stc)
stc_avg = stc_avg.crop(tmin, tmax)
src_pow = np.sum(stc_avg.data ** 2, axis=1)
stc_avg.data[src_pow < np.percentile(src_pow, thr)] = 0.
fn_src = subjects_dir+'/%s/bem/fsaverage-ico-5-src.fif' %min_subject
src_inv = mne.read_source_spaces(fn_src)
func_labels_lh, func_labels_rh = mne.stc_to_label(
stc_avg, src=src_inv, smooth=True,
subjects_dir=subjects_dir,
connected=True)
# Left hemisphere definition
i = 0
labels_path = fn_stc[:fn_stc.rfind('-')] + '/ini'
reset_directory(labels_path)
while i < len(func_labels_lh):
func_label = func_labels_lh[i]
func_label.save(labels_path + '/ROI_%d' %(i))
i = i + 1
# right hemisphere definition
j = 0
while j < len(func_labels_rh):
func_label = func_labels_rh[j]
func_label.save(labels_path + '/ROI_%d' %(j))
j = j + 1
def _cluster_rois(sel_path, label_list, stc, src, min_dist, weight, mni_subject='fsaverage'):
"""
subfunctions of merge_ROIs
----------
mer_path: str
The directory for storing merged ROIs.
label_list: list
Labels to be merged
"""
class_list = []
class_list.append(label_list[0])
for test_fn in label_list[1:]:
test_label = mne.read_label(test_fn)
i = 0
belong = False
while (i < len(class_list)) and (belong is False):
class_label = mne.read_label(class_list[i])
if test_label.hemi != class_label.hemi:
i = i + 1
continue
else:
class_pca = stc.extract_label_time_course(class_label, src, mode='pca_flip')
test_pca = stc.extract_label_time_course(test_label, src, mode='pca_flip')
class_pow = np.sum(class_pca ** 2)
test_pow = np.sum(test_pca ** 2)
max_pca = class_pca
exch = False
if class_pow < test_pow:
max_pca = test_pca
exch = True
nearby = False
class_stc = stc.in_label(class_label)
test_stc = stc.in_label(test_label)
if class_label.hemi == 'lh':
class_vtx, _ = class_stc.get_peak(hemi='lh')
test_vtx, _ = test_stc.get_peak(hemi='lh')
class_mni = mne.vertex_to_mni(class_vtx, 0, mni_subject)[0]
test_mni = mne.vertex_to_mni(test_vtx, 0, mni_subject)[0]
elif class_label.hemi == 'rh':
class_vtx, _ = class_stc.get_peak(hemi='rh')
test_vtx, _ = test_stc.get_peak(hemi='rh')
class_mni = mne.vertex_to_mni(class_vtx, 1, mni_subject)[0]
test_mni = mne.vertex_to_mni(test_vtx, 1, mni_subject)[0]
if np.linalg.norm(class_mni - test_mni) < min_dist:
if exch == True:
os.remove(class_list[i])
class_list[i] = test_fn
elif exch == False:
os.remove(test_fn)
nearby = True
belong = True
if nearby == False:
thre = max_pca.std() * weight
diff = np.abs(np.linalg.norm(class_pca) - np.linalg.norm(test_pca))
if diff < thre:
if exch == True:
os.remove(class_list[i])
class_list[i] = test_fn
elif exch == False:
os.remove(test_fn)
belong = True
i = i + 1
if belong is False:
class_list.append(test_fn)
return len(class_list)
def sele_rois(fn_stc_list, fn_src, min_dist, weight, tmin=0.1, tmax=0.5):
"""
select ROIs, so that the overlapped lables merged into one.
If 'group' is False, ROIs from all the events are merged and
saved in the folder 'ROIs' under the 'labels_path'.
If 'group' is True, ROIs from all the subjects are merged and
saved in the folder 'merged' under the 'labels_path'.
----------
labels_path: the total path of all the ROIs' folders.
group: if 'group' is False, merge ROIs from different events within one
subject, if 'group' is True, merge ROIs across subjects.
evelist: events name of all subfolders
"""
fn_stc_list = get_files_from_list(fn_stc_list)
# loop across all filenames
for fn_stc in fn_stc_list:
import glob, shutil
labels_path = fn_stc[:fn_stc.rfind('-')]
source_path = labels_path + '/ini/'
sel_path = labels_path + '/ROIs/'
reset_directory(sel_path)
for filename in glob.glob(os.path.join(source_path, '*.*')):
shutil.copy(filename, sel_path)
reducer = True
stc = mne.read_source_estimate(fn_stc)
stc = stc.crop(tmin, tmax)
src = mne.read_source_spaces(fn_src)
while reducer:
list_dirs = os.walk(sel_path)
label_list = ['']
for root, dirs, files in list_dirs:
for f in files:
label_fname = os.path.join(root, f)
label_list.append(label_fname)
label_list = label_list[1:]
len_class = _cluster_rois(sel_path, label_list, stc, src, min_dist, weight)
if len_class == len(label_list):
reducer = False
def apply_stand(fn_stc, radius=5.0, min_subject='fsaverage', tmin=0.1, tmax=0.5):
"""
----------
fname: averaged STC of the trials.
radius: the radius of every ROI.
"""
fnlist = get_files_from_list(fn_stc)
# loop across all filenames
for fn_stc in fnlist:
stc_path = fn_stc[:fn_stc.rfind('-')]
stc = mne.read_source_estimate(fn_stc, subject=min_subject)
stc = stc.crop(tmin, tmax)
#min_path = subjects_dir + '/%s' %min_subject
# extract the subject infromation from the file name
source_path = stc_path + '/ROIs/'
stan_path = stc_path + '/standard/'
reset_directory(stan_path)
list_dirs = os.walk(source_path)
for root, dirs, files in list_dirs:
for f in files:
label_fname = os.path.join(root, f)
label = mne.read_label(label_fname)
stc_label = stc.in_label(label)
src_pow = np.sum(stc_label.data ** 2, axis=1)
if label.hemi == 'lh':
# Get the max MNE value within each ROI
seed_vertno = stc_label.vertices[0][np.argmax(src_pow)]
func_label = mne.grow_labels(min_subject, seed_vertno,
extents=radius, hemis=0,
subjects_dir=subjects_dir,
n_jobs=1)
func_label = func_label[0]
func_label.save(stan_path + '%s' %f)
elif label.hemi == 'rh':
seed_vertno = stc_label.vertices[1][np.argmax(src_pow)]
func_label = mne.grow_labels(min_subject, seed_vertno,
extents=radius, hemis=1,
subjects_dir=subjects_dir,
n_jobs=1)
func_label = func_label[0]
func_label.save(stan_path + '%s' %f)
def _cluster1_rois(mer_path, label_list):
"""
subfunctions of merge_ROIs
----------
mer_path: str
The directory for storing merged ROIs.
label_list: list
Labels to be merged
"""
class_list = []
class_list.append(label_list[0])
for test_fn in label_list[1:]:
test_label = mne.read_label(test_fn)
i = 0
belong = False
while (i < len(class_list)) and (belong is False):
class_label = mne.read_label(class_list[i])
label_name = class_label.name
if test_label.hemi != class_label.hemi:
i = i + 1
continue
overlapped = len(np.intersect1d(test_label.vertices,
class_label.vertices))
if overlapped > 0:
com_label = test_label + class_label
pre_test = test_label.name.split('_')[0]
pre_class = class_label.name.split('_')[0]
#label_name = pre_class + '_%s-%s' %(pre_test,class_label.name.split('-')[-1])
if pre_test != pre_class:
pre_class += ',%s' % pre_test
pre_class = list(set(pre_class.split(',')))
new_pre = ''
for pre in pre_class[:-1]:
new_pre += '%s,' % pre
new_pre += pre_class[-1]
label_name = '%s' % (new_pre) + \
class_label.name.split('_')[-1]
os.remove(class_list[i])
os.remove(test_fn)
fn_newlabel = mer_path + '%s.label' %label_name
if os.path.isfile(fn_newlabel):
fn_newlabel = fn_newlabel[:fn_newlabel.rfind('_')] + '_new,%s' %fn_newlabel.split('_')[-1]
mne.write_label(fn_newlabel, com_label)
class_list[i] = fn_newlabel
belong = True
i = i + 1
if belong is False:
class_list.append(test_fn)
return len(class_list)
def apply_merge(labels_path, evt_list):
import glob, shutil
for evt in evt_list:
mer_path = labels_path + '%s/merged/' %evt[0]
reset_directory(mer_path)
source0_path = labels_path + '%s/standard/' %evt[0]
source1_path = labels_path + '%s/standard/' %evt[1]
source = glob.glob(os.path.join(source0_path, '*.*'))
source = source + glob.glob(os.path.join(source1_path, '*.*'))
for filename in source:
shutil.copy(filename, mer_path)
reducer = True
while reducer:
list_dirs = os.walk(mer_path)
label_list = ['']
for root, dirs, files in list_dirs:
for f in files:
label_fname = os.path.join(root, f)
label_list.append(label_fname)
label_list = label_list[1:]
len_class = _cluster1_rois(mer_path, label_list)
if len_class == len(label_list):
reducer = False