-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
86 lines (68 loc) · 2.34 KB
/
utils.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
import mne
import numpy as np
from scipy.io import loadmat
from sklearn.preprocessing import LabelEncoder
def load_data(file):
# Extract the MATLAB struct from the file
m = loadmat(file)
# Get the sampling rate (downsampled to 100 Hz)
sample_rate = m['nfo']['fs'][0][0][0][0]
# Get the raw EEG data
# data: (n_channels x n_samples)
data = m['cnt'].T
# Get the number of channels (59) and samples (190473)
# _, n_samples = data.shape
# Get the names of the electrodes (channels)
ch_names = [ch[0] for ch in m['nfo']['clab'][0][0][0]]
# Create the info structure needed by MNE
info = mne.create_info(ch_names, sample_rate, 'eeg')
# Add the system name to the info
info['description'] = 'BrainAmp MR plus'
# Create the Raw object
raw = mne.io.RawArray(data, info)
# Create a montage out of the 10-20 system
montage = mne.channels.make_standard_montage('standard_1020')
# Exclude channels which are missing in the 10-20 montage
raw.pick(
'eeg',
exclude=[
'CFC7',
'CFC5',
'CFC3',
'CFC1',
'CFC2',
'CFC4',
'CFC6',
'CFC8',
'CCP7',
'CCP5',
'CCP3',
'CCP1',
'CCP2',
'CCP4',
'CCP6',
'CCP8',
],
)
# Apply the montage
raw.set_montage(montage, on_missing='ignore')
# Get the class labels (left and right)
classes = [cl[0] for cl in m['nfo']['classes'][0][0][0]]
# Get the onsets (indices) of the events (markers)
event_onsets = m['mrk'][0][0][0][0]
# Get the encoded labels of the events
event_codes = m['mrk'][0][0][1][0]
# Convert event codes to a list of class names
event_names = [classes[0] if x == -1 else classes[1] for x in event_codes]
# Initialize the label encoder
le = LabelEncoder()
# Encode the cues using the label encoder
events_encoded = le.fit_transform(event_names)
# Create the event information
event_arr = np.zeros((len(event_onsets), 3), dtype=int)
event_arr[:, 0] = event_onsets
event_arr[:, 2] = events_encoded
# Create a class-encoding correspondence dictionary for the Epochs
# object
event_id = dict(zip(classes, range(len(le.classes_))))
return raw, event_arr, event_id