forked from mne-tools/mne-bids-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-make_epochs.py
135 lines (107 loc) · 4.84 KB
/
03-make_epochs.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
"""
====================
04. Construct epochs
====================
The epochs are constructed by using the events created in script 03. MNE
supports hierarchical events that allows selection to different groups more
easily (see config.event_id). Automatic rejection is applied to the epochs.
Finally the epochs are saved to disk.
To save space, the epoch data can be decimated.
"""
import os.path as op
import itertools
import logging
import mne
from mne.parallel import parallel_func
from mne_bids import make_bids_basename
import config
from config import gen_log_message, on_error, failsafe_run
logger = logging.getLogger('mne-study-template')
###############################################################################
@failsafe_run(on_error=on_error)
def run_epochs(subject, session=None):
"""Extract epochs for one subject."""
raw_list = list()
deriv_path = config.get_subject_deriv_path(subject=subject,
session=session,
kind=config.get_kind())
for run in config.get_runs():
bids_basename = make_bids_basename(subject=subject,
session=session,
task=config.get_task(),
acquisition=config.acq,
run=run,
processing=config.proc,
recording=config.rec,
space=config.space
)
# Prepare a name to save the data
raw_fname_in = \
op.join(deriv_path, bids_basename + '_filt_raw.fif')
msg = f'Loading filtered raw data from {raw_fname_in}'
logger.info(gen_log_message(message=msg, step=3, subject=subject,
session=session, run=run))
raw = mne.io.read_raw_fif(raw_fname_in, preload=True)
raw_list.append(raw)
msg = 'Concatenating runs'
logger.info(gen_log_message(message=msg, step=3, subject=subject,
session=session))
raw = mne.concatenate_raws(raw_list)
events, event_id = mne.events_from_annotations(raw)
if "eeg" in config.ch_types:
raw.set_eeg_reference(projection=True)
del raw_list
meg = False
if 'meg' in config.ch_types:
meg = True
elif 'grad' in config.ch_types:
meg = 'grad'
elif 'mag' in config.ch_types:
meg = 'mag'
eeg = config.get_kind() == 'eeg'
picks = mne.pick_types(raw.info, meg=meg, eeg=eeg, stim=True,
eog=True, exclude=())
# Construct metadata from the epochs
# Add here if you need to attach a pandas dataframe as metadata
# to your epochs object:
# https://martinos.org/mne/dev/auto_tutorials/plot_metadata_epochs.html
# Epoch the data
msg = 'Epoching'
logger.info(gen_log_message(message=msg, step=3, subject=subject,
session=session))
epochs = mne.Epochs(raw, events, event_id, config.tmin, config.tmax,
proj=True, picks=picks, baseline=config.baseline,
preload=False, decim=config.decim,
reject=config.get_reject())
msg = 'Writing epochs to disk'
logger.info(gen_log_message(message=msg, step=3, subject=subject,
session=session))
bids_basename = make_bids_basename(subject=subject,
session=session,
task=config.get_task(),
acquisition=config.acq,
run=None,
processing=config.proc,
recording=config.rec,
space=config.space
)
epochs_fname = \
op.join(deriv_path, bids_basename + '-epo.fif')
epochs.save(epochs_fname, overwrite=True)
if config.interactive:
epochs.plot()
epochs.plot_image(combine='gfp', picks=config.ch_types, sigma=2.,
cmap='YlGnBu_r')
def main():
"""Run epochs."""
msg = 'Running Step 3: Epoching'
logger.info(gen_log_message(step=3, message=msg))
# Here we use fewer N_JOBS to prevent potential memory problems
parallel, run_func, _ = parallel_func(run_epochs,
n_jobs=max(config.N_JOBS // 4, 1))
parallel(run_func(subject, session) for subject, session in
itertools.product(config.get_subjects(), config.get_sessions()))
msg = 'Completed Step 3: Epoching'
logger.info(gen_log_message(step=3, message=msg))
if __name__ == '__main__':
main()