forked from mne-tools/mne-bids-pipeline
/
06a-apply_ica.py
236 lines (184 loc) · 8.9 KB
/
06a-apply_ica.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
"""
===============
06. Apply ICA
===============
Blinks and ECG artifacts are automatically detected and the corresponding ICA
components are removed from the data.
This relies on the ICAs computed in 05-run_ica.py
!! If you manually add components to remove (config.rejcomps_man),
make sure you did not re-run the ICA in the meantime. Otherwise (especially if
the random state was not set, or you used a different machine, the component
order might differ).
"""
import os.path as op
import itertools
import mne
from mne.parallel import parallel_func
from mne.preprocessing import read_ica
from mne.preprocessing import create_eog_epochs, create_ecg_epochs
from mne.report import Report
from mne_bids import make_bids_basename
import numpy as np
import config
def apply_ica(subject, run, session):
print("Processing subject: %s" % subject)
# Construct the search path for the data file. `sub` is mandatory
subject_path = op.join('sub-{}'.format(subject))
# `session` is optional
if session is not None:
subject_path = op.join(subject_path, 'ses-{}'.format(session))
subject_path = op.join(subject_path, config.kind)
bids_basename = make_bids_basename(subject=subject,
session=session,
task=config.task,
acquisition=config.acq,
run=None,
processing=config.proc,
recording=config.rec,
space=config.space
)
fpath_deriv = op.join(config.bids_root, 'derivatives',
config.PIPELINE_NAME, subject_path)
fname_in = \
op.join(fpath_deriv, bids_basename + '-epo.fif')
fname_out = \
op.join(fpath_deriv, bids_basename + '_cleaned-epo.fif')
# load epochs to reject ICA components
epochs = mne.read_epochs(fname_in, preload=True)
print("Input: ", fname_in)
print("Output: ", fname_out)
# load first run of raw data for ecg /eog epochs
print(" Loading one run from raw data")
bids_basename = make_bids_basename(subject=subject,
session=session,
task=config.task,
acquisition=config.acq,
run=config.runs[0],
processing=config.proc,
recording=config.rec,
space=config.space
)
if config.use_maxwell_filter:
raw_fname_in = \
op.join(fpath_deriv, bids_basename + '_sss_raw.fif')
else:
raw_fname_in = \
op.join(fpath_deriv, bids_basename + '_filt_raw.fif')
raw = mne.io.read_raw_fif(raw_fname_in, preload=True)
# run ICA on MEG and EEG
picks_meg = mne.pick_types(raw.info, meg=True, eeg=False,
eog=False, stim=False, exclude='bads')
picks_eeg = mne.pick_types(raw.info, meg=False, eeg=True,
eog=False, stim=False, exclude='bads')
all_picks = {'meg': picks_meg, 'eeg': picks_eeg}
for ch_type in config.ch_types:
report = None
print(ch_type)
picks = all_picks[ch_type]
# Load ICA
fname_ica = \
op.join(fpath_deriv, bids_basename + '_%s-ica.fif' % ch_type)
print('Reading ICA: ' + fname_ica)
ica = read_ica(fname=fname_ica)
pick_ecg = mne.pick_types(raw.info, meg=False, eeg=False,
ecg=True, eog=False)
# ECG
# either needs an ecg channel, or avg of the mags (i.e. MEG data)
ecg_inds = list()
if pick_ecg or ch_type == 'meg':
picks_ecg = np.concatenate([picks, pick_ecg])
# Create ecg epochs
if ch_type == 'meg':
reject = {'mag': config.reject['mag'],
'grad': config.reject['grad']}
elif ch_type == 'eeg':
reject = {'eeg': config.reject['eeg']}
ecg_epochs = create_ecg_epochs(raw, picks=picks_ecg, reject=reject,
baseline=(None, 0), tmin=-0.5,
tmax=0.5)
ecg_average = ecg_epochs.average()
ecg_inds, scores = \
ica.find_bads_ecg(ecg_epochs, method='ctps',
threshold=config.ica_ctps_ecg_threshold)
del ecg_epochs
report_fname = \
op.join(fpath_deriv,
bids_basename + '_%s-reject_ica.html' % ch_type)
report = Report(report_fname, verbose=False)
# Plot r score
report.add_figs_to_section(ica.plot_scores(scores,
exclude=ecg_inds,
show=config.plot),
captions=ch_type.upper() + ' - ECG - ' +
'R scores')
# Plot source time course
report.add_figs_to_section(ica.plot_sources(ecg_average,
exclude=ecg_inds,
show=config.plot),
captions=ch_type.upper() + ' - ECG - ' +
'Sources time course')
# Plot source time course
report.add_figs_to_section(ica.plot_overlay(ecg_average,
exclude=ecg_inds,
show=config.plot),
captions=ch_type.upper() + ' - ECG - ' +
'Corrections')
else:
# XXX : to check when EEG only is processed
print('no ECG channel is present. Cannot automate ICAs component '
'detection for ECG!')
# EOG
pick_eog = mne.pick_types(raw.info, meg=False, eeg=False,
ecg=False, eog=True)
eog_inds = list()
if pick_eog.any():
print('using EOG channel')
picks_eog = np.concatenate([picks, pick_eog])
# Create eog epochs
eog_epochs = create_eog_epochs(raw, picks=picks_eog, reject=None,
baseline=(None, 0), tmin=-0.5,
tmax=0.5)
eog_average = eog_epochs.average()
eog_inds, scores = ica.find_bads_eog(eog_epochs, threshold=3.0)
del eog_epochs
params = dict(exclude=eog_inds, show=config.plot)
# Plot r score
report.add_figs_to_section(ica.plot_scores(scores, **params),
captions=ch_type.upper() + ' - EOG - ' +
'R scores')
# Plot source time course
report.add_figs_to_section(ica.plot_sources(eog_average, **params),
captions=ch_type.upper() + ' - EOG - ' +
'Sources time course')
# Plot source time course
report.add_figs_to_section(ica.plot_overlay(eog_average, **params),
captions=ch_type.upper() + ' - EOG - ' +
'Corrections')
report.save(report_fname, overwrite=True, open_browser=False)
else:
print('no EOG channel is present. Cannot automate ICAs component '
'detection for EOG!')
ica_reject = (list(ecg_inds) + list(eog_inds) +
list(config.rejcomps_man[subject][ch_type]))
# now reject the components
print('Rejecting from %s: %s' % (ch_type, ica_reject))
epochs = ica.apply(epochs, exclude=ica_reject)
print('Saving cleaned epochs')
epochs.save(fname_out)
if report is not None:
fig = ica.plot_overlay(raw, exclude=ica_reject, show=config.plot)
report.add_figs_to_section(fig, captions=ch_type.upper() +
' - ALL(epochs) - Corrections')
if config.plot:
epochs.plot_image(combine='gfp', group_by='type', sigma=2.,
cmap="YlGnBu_r", show=config.plot)
def main():
"""Apply ICA."""
if not config.use_ica:
return
parallel, run_func, _ = parallel_func(apply_ica, n_jobs=config.N_JOBS)
parallel(run_func(subject, run, session) for subject, run, session in
itertools.product(config.subjects_list, config.runs,
config.sessions))
if __name__ == '__main__':
main()