forked from brainthemind/CogBrainDyn_MEG_Pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-extract_events.py
62 lines (46 loc) · 2.04 KB
/
03-extract_events.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
"""
============================================
03. Extract events from the stimulus channel
============================================
Here, all events present in the stimulus channel indicated in
config.stim_channel are extracted.
The events are saved to the subject's MEG directory.
This is done early in the pipeline to avoid distorting event-time,
for instance by resampling.
"""
import os.path as op
import mne
import numpy as np
from mne.parallel import parallel_func
import config
def run_events(subject):
print("Processing subject: %s" % subject)
meg_subject_dir = op.join(config.meg_dir, subject)
for run in config.runs:
if config.use_maxwell_filter:
extension = run + '_sss_raw'
else:
extension = run + '_filt_raw'
raw_fname_in = op.join(meg_subject_dir,
config.base_fname.format(**locals()))
eve_fname_out = op.splitext(raw_fname_in)[0] + '-eve.fif'
raw = mne.io.read_raw_fif(raw_fname_in)
events = mne.find_events(raw, stim_channel=config.stim_channel,
consecutive=True,
min_duration=config.min_event_duration,
shortest_event=config.shortest_event)
if config.trigger_time_shift:
events = mne.event.shift_time_events(events,
np.unique(events[:, 2]),
config.trigger_time_shift,
raw.info['sfreq'])
print("Input: ", raw_fname_in)
print("Output: ", eve_fname_out)
mne.write_events(eve_fname_out, events)
if config.plot:
# plot events
figure = mne.viz.plot_events(events, sfreq=raw.info['sfreq'],
first_samp=raw.first_samp)
figure.show()
parallel, run_func, _ = parallel_func(run_events, n_jobs=config.N_JOBS)
parallel(run_func(subject) for subject in config.subjects_list)