-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_raw.py
60 lines (48 loc) · 1.75 KB
/
prepare_raw.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
from mne import concatenate_raws
from mne.channels import read_montage
from mne.datasets.eegbci import load_data
from mne.io import read_raw_edf
# EDF files for subjects 88, 89, 92, 100 have overlapping events, which
# read_raw_edf() cannot handle.
def load_subject(id_num, runs):
'''
Loads raw EEG recordings for one subject and at least one run of
experiments.
Arguments:
id_num: int, the subject's ID number
runs: int or list of ints -- which experiment(s) to read data from
Returns:
MNE Raw object
'''
edf_files = load_data(id_num, runs)
if len(edf_files) > 1:
raw_objects = [read_raw_edf(file, preload=True) for file in edf_files]
mne_raw = concatenate_raws(raw_objects, preload=True)
else:
mne_raw = read_raw_edf(edf_files[0], preload=True)
return mne_raw
def fix_channels(mne_raw, channels='data/misc/channel-names.txt'):
'''
1) Fixes channel names to comply with 'standard_1005' format.
2) Fixes stimulus channel type.
Arguments:
mne_raw: MNE Raw object
Returns:
MNE Raw object (modified in place)
'''
ch_names = open(channels).read().splitlines()
renamer = {old: new for old, new in zip(mne_raw.ch_names[:-1], ch_names)}
mne_raw.rename_channels(renamer)
mne_raw.set_channel_types({'STI 014': 'stim'})
def add_montage(mne_raw, channels='data/misc/channel-names.txt'):
'''
Creates 'standard_1005' montage with corrected channel names and
adds it to MNE raw object.
Arguments:
mne_raw: MNE Raw object
Returns:
MNE Raw object (modified in place)
'''
ch_names = open(channels).read().splitlines()
montage = read_montage('standard_1005', ch_names=ch_names)
mne_raw.set_montage(montage)