-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_preprocess.py
218 lines (197 loc) · 8.82 KB
/
1_preprocess.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
import os
import os.path as op
import numpy as np
import nibabel as nib
import pandas as pd
import mne
import mne_bids
from utils import load_raw, reject_epochs, plot_evoked
from params import BIDS_ROOT as bids_root
from params import SUBJECTS as subjects
from params import TASK as task
from params import TEMPLATE as template
subjects_dir = op.join(bids_root, 'derivatives', 'freesurfer-7.3.2')
ieeg_dir = op.join(bids_root, 'derivatives', 'mne-ieeg')
path = mne_bids.BIDSPath(root=bids_root, task=task)
out_dir = op.join(bids_root, 'derivatives', 'analysis_data')
# %%
# Align CT, takes ~15 minutes per subject, no user input
for sub in subjects:
T1 = nib.load(op.join(subjects_dir, f'sub-{sub}', 'mri', 'T1.mgz'))
CT_orig = nib.load(op.join(bids_root, f'sub-{sub}', 'anat',
f'sub-{sub}_ct.nii.gz'))
reg_affine, _ = mne.transforms.compute_volume_registration(
CT_orig, T1, pipeline='rigids')
np.savez_compressed(op.join(
ieeg_dir, f'sub-{sub}', 'CT', 'reg_affine.npz'),
reg_affine=reg_affine)
CT_aligned = mne.transforms.apply_volume_registration(
CT_orig, T1, reg_affine)
nib.save(CT_aligned, op.join(
bids_root, 'derivatives', f'sub-{sub}', 'CT', 'CT_aligned.mgz'))
''' Fix surface RAS, should be scanner RAS
import numpy as np
for sub in subjects:
raw = load_raw(sub)
trans = mne.coreg.estimate_head_mri_t(
f'sub-{sub}', op.join(bids_root, 'freesurfer'))
info = mne.io.read_info(op.join(
bids_root, 'derivatives', f'sub-{sub}',
'ieeg', f'sub-{sub}_task-SlowFast_info.fif'))
montage = mne.channels.make_dig_montage(
dict(zip(info.ch_names, [ch['loc'][:3] for ch in info['chs']])),
coord_frame='head')
montage.apply_trans(trans)
mne_bids.convert_montage_to_ras(
montage, subject=f'sub-{sub}', subjects_dir=subjects_dir)
montage.rename_channels({ch: ch.replace('-20000', '').replace('-2000', '')
for ch in montage.ch_names})
raw.set_montage(montage)
mne_bids.dig._write_electrodes_tsv(raw, op.join(
bids_root, f'sub-{sub}', 'ieeg',
f'sub-{sub}_space-ACPC_electrodes.tsv'), 'ieeg', overwrite=True)
T1_mgz = nib.load(op.join(subjects_dir,
f'sub-{sub}', 'mri', 'T1.mgz'))
T1_nii = nib.load(op.join(bids_root, f'sub-{sub}', 'anat',
f'sub-{sub}_T1w.nii.gz'))
reg_affine = mne.transforms.compute_volume_registration(
T1_mgz, T1_nii, 'rigids')[0]
os.makedirs(op.join(ieeg_dir, f'sub-{sub}', 'MR', 'reg_affine.npz'),
exist_ok=True)
np.savez_compressed(op.join(
ieeg_dir, f'sub-{sub}', 'MR', 'reg_affine.npz'),
reg_affine=reg_affine)
ac = mne.transforms.apply_trans(np.linalg.inv(reg_affine), [0, 0, 0])
pc = mne.transforms.apply_trans(np.linalg.inv(reg_affine), [0, -25, 0])
print(sub, 'AC', ac, 'PC', pc)
for sub in subjects:
fname = f'{bids_root}/sub-{sub}/ieeg/sub-{sub}_space-ACPC_electrodes.tsv'
df = pd.read_csv(fname, sep='\t')
df['name'] = [n.replace(' ', '') for n in df['name']]
df['size'] = 'n/a'
df.to_csv(fname, sep='\t', index=False)
'''
# %%
# For a few subjects the above didn't work and so this was used to
# align (11 and 12). This has since been fixed in MNE-Python (using
# manual pre-alignment)
'''
import ants
T1 = ants.image_read(op.join(subjects_dir, f'sub-{sub}', 'mri', 'T1.mgz'))
CT_orig = ants.image_read(op.join(bids_root, f'sub-{sub}', 'anat',
f'sub-{sub}_ct.nii.gz'))
trans = ants.registration(fixed=T1, moving=CT_orig, type_of_transform='Rigid')
ants.image_write(trans['warpedmovout'],
op.join(subjects_dir, f'sub-{sub}',
'CT', 'CT_aligned_test.mgz'))
CT_orig = nib.load(op.join(bids_root, f'sub-{sub}', 'anat',
f'sub-{sub}_ct.nii.gz'))
CT_aligned = nib.load(op.join(subjects_dir, f'sub-{sub}',
'CT', 'CT_aligned.mgz'))
reg_affine, _ = mne.transforms.compute_volume_registration(
CT_orig, CT_aligned, pipeline='rigids')
np.savez_compressed(op.join(
ieeg_dir, f'sub-{sub}', 'CT', 'reg_affine.npz'),
reg_affine=reg_affine)
'''
# %%
# Pick contact locations, requires user input
for sub in subjects:
path.update(subject=str(sub))
raw = mne_bids.read_raw_bids(path)
raw.set_montage(None)
T1 = nib.load(op.join(subjects_dir, f'sub-{sub}', 'mri', 'T1.mgz'))
CT_aligned = nib.load(op.join(
ieeg_dir, f'sub-{sub}', 'CT', 'CT_aligned.mgz'))
trans = mne.coreg.estimate_head_mri_t(f'sub-{sub}', subjects_dir)
gui = mne.gui.locate_ieeg(raw.info, trans, CT_aligned,
subject=f'sub-{sub}', subjects_dir=subjects_dir)
while input('Finished, save to disk? (y/N)\t') != 'y':
mne.io.write_info(op.join(
ieeg_dir, f'sub-{sub}', 'ieeg',
f'sub-{sub}_task-{task}_info.fif'), raw.info)
# %%
# Warp to template, takes ~15 minutes per subject, no user input
template_subjects_dir = op.join(os.environ['FREESURFER_HOME'], 'subjects')
if not op.exists(op.join(subjects_dir, template)):
os.symlink(op.join(template_subjects_dir, template),
op.join(subjects_dir, template))
template_brain = nib.load(
op.join(subjects_dir, template, 'mri', 'brain.mgz'))
template_trans = mne.coreg.estimate_head_mri_t(template, subjects_dir)
for sub in subjects:
path.update(subject=str(sub))
raw = mne_bids.read_raw_bids(path)
info = mne.io.read_info(op.join(
ieeg_dir, f'sub-{sub}', 'ieeg', f'sub-{sub}_task-{task}_info.fif'))
raw.drop_channels([ch for ch in raw.ch_names if ch not in info.ch_names])
raw.info = info
trans = mne.coreg.estimate_head_mri_t(f'sub-{sub}', subjects_dir)
subject_brain = nib.load(
op.join(subjects_dir, f'sub-{sub}', 'mri', 'brain.mgz'))
reg_affine, sdr_morph = mne.transforms.compute_volume_registration(
subject_brain, template_brain, verbose=True)
montage = raw.get_montage()
montage.apply_trans(trans)
CT_aligned = nib.load(op.join(
ieeg_dir, f'sub-{sub}', 'CT', 'CT_aligned.mgz'))
montage_warped, elec_image, warped_elec_image = mne.warp_montage_volume(
montage, CT_aligned, reg_affine, sdr_morph,
subject_from=f'sub-{sub}', subject_to=template,
subjects_dir_from=subjects_dir, subjects_dir_to=subjects_dir)
ch_pos = montage_warped.get_positions()['ch_pos'].copy() # use these later
# now go back to "head" coordinates to save to raw
montage_warped.apply_trans(mne.transforms.invert_transform(template_trans))
raw.set_montage(montage_warped, on_missing='warn')
mne.io.write_info(op.join(
ieeg_dir, f'sub-{sub}', 'ieeg',
f'sub-{sub}_template-{template}_task-{task}_info.fif'), raw.info)
nib.save(elec_image, op.join(
ieeg_dir, f'sub-{sub}', 'ieeg', 'elec_image.mgz'))
# %%
# Plot final result
template_trans = mne.coreg.estimate_head_mri_t(template, subjects_dir)
for sub in subjects:
info = mne.io.read_info(op.join(
ieeg_dir, f'sub-{sub}', 'ieeg',
f'sub-{sub}_task-{task}_info.fif'))
trans = mne.coreg.estimate_head_mri_t(f'sub-{sub}', subjects_dir)
brain = mne.viz.Brain(f'sub-{sub}', subjects_dir=subjects_dir,
cortex='low_contrast', alpha=0.2, background='white')
brain.add_sensors(info, trans)
# plot warped
info = mne.io.read_info(op.join(
bids_root, 'derivatives', f'sub-{sub}', 'ieeg',
f'sub-{sub}_template-{template}_task-{task}_info.fif'))
brain = mne.viz.Brain(template, subjects_dir=subjects_dir,
cortex='low_contrast', alpha=0.2, background='white')
brain.add_sensors(info, template_trans)
# %%
# Save locations to BIDS
# Note: requires mne-bids version 0.11 (dev)
for sub in subjects:
path.update(subject=str(sub))
raw = mne_bids.read_raw_bids(path)
info = mne.io.read_info(op.join(
bids_root, 'derivatives', f'sub-{sub}', 'ieeg',
f'sub-{sub}_task-{task}_info.fif'))
for ch in info['chs']:
raw.info['chs'][raw.ch_names.index(ch['ch_name'])] = ch
with raw.info._unlock():
raw.info['dig'] = info['dig']
montage = raw.get_montage()
trans = mne.coreg.estimate_head_mri_t(f'sub-{sub}', subjects_dir)
montage.apply_trans(trans)
mne_bids.convert_montage_to_ras(montage, f'sub-{sub}', subjects_dir)
dig_path = path.copy().update(datatype='ieeg', space='ACPC')
mne_bids.dig._write_dig_bids(dig_path, raw, montage=montage,
acpc_aligned=True, overwrite=True)
# %%
# Perform automatic trial rejection
for sub in subjects:
raw = load_raw(sub)
keep = reject_epochs(raw)
pd.DataFrame(dict(keep=keep)).to_csv(
op.join(ieeg_dir, f'sub-{sub}', 'ieeg',
f'sub-{sub}_reject_mask.tsv'), sep='\t', index=False)
plot_evoked(raw, sub, keep)