-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocessing.py
237 lines (205 loc) · 11.3 KB
/
preprocessing.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
237
import numpy as np
import mne
from scipy import stats
import os
def remove_runs(files):
unwanted_runs = {
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-003/ses-EcogLfpMedOff01/ieeg/sub-003_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-2_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-003/ses-EcogLfpMedOff01/ieeg/sub-003_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-3_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-003/ses-EcogLfpMedOn01/ieeg/sub-003_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-004/ses-EcogLfpMedOff01/ieeg/sub-004_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-2_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-004/ses-EcogLfpMedOn01/ieeg/sub-004_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-004/ses-EcogLfpMedOn01/ieeg/sub-004_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-3_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-004/ses-EcogLfpMedOn01/ieeg/sub-004_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-4_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-004/ses-EcogLfpMedOn02/ieeg/sub-004_ses-EcogLfpMedOn02_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-005/ses-EcogLfpMedOff01/ieeg/sub-005_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-005/ses-EcogLfpMedOff01/ieeg/sub-005_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-2_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-007/ses-EcogLfpMedOn01/ieeg/sub-007_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-2_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-007/ses-EcogLfpMedOn01/ieeg/sub-007_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-3_ieeg.vhdr",
}
files = [ele for ele in files if ele not in unwanted_runs]
return files
def pick_runs(files):
runs = {
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-003/ses-EcogLfpMedOff01/ieeg/sub-003_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-003/ses-EcogLfpMedOn03/ieeg/sub-003_ses-EcogLfpMedOn03_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-004/ses-EcogLfpMedOff01/ieeg/sub-004_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-004/ses-EcogLfpMedOn01/ieeg/sub-004_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-2_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-005/ses-EcogLfpMedOff02/ieeg/sub-005_ses-EcogLfpMedOff02_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-005/ses-EcogLfpMedOn01/ieeg/sub-005_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-006/ses-EcogLfpMedOff01/ieeg/sub-006_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-006/ses-EcogLfpMedOn01/ieeg/sub-006_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-007/ses-EcogLfpMedOff01/ieeg/sub-007_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-007/ses-EcogLfpMedOn01/ieeg/sub-007_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-2_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-008/ses-EcogLfpMedOff01/ieeg/sub-008_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-008/ses-EcogLfpMedOn01/ieeg/sub-008_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-009/ses-EcogLfpMedOff01/ieeg/sub-009_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-009/ses-EcogLfpMedOn01/ieeg/sub-009_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
#'/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-010/ses-EcogLfpMedOff01/ieeg/sub-010_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr',
#'/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-010/ses-EcogLfpMedOn01/ieeg/sub-010_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-1_ieeg.vhdr',
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-011/ses-EcogLfpMedOff01/ieeg/sub-011_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-011/ses-EcogLfpMedOn01/ieeg/sub-011_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-012/ses-EcogLfpMedOff02/ieeg/sub-012_ses-EcogLfpMedOff02_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-012/ses-EcogLfpMedOn01/ieeg/sub-012_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-013/ses-EcogLfpMedOff01/ieeg/sub-013_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-013/ses-EcogLfpMedOn02/ieeg/sub-013_ses-EcogLfpMedOn02_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-014/ses-EcogLfpMedOff01/ieeg/sub-014_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-014/ses-EcogLfpMedOn01/ieeg/sub-014_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-015/ses-EcogLfpMedOff01/ieeg/sub-015_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
"/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-015/ses-EcogLfpMedOn01/ieeg/sub-015_ses-EcogLfpMedOn01_task-Rest_acq-StimOff_run-1_ieeg.vhdr",
'/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-EL016/ses-EcogLfpMedOff01/ieeg/sub-EL016_ses-EcogLfpMedOff01_task-Rest_acq-StimOff_run-1_ieeg.vhdr',
'/Users/alidzaye/BIDS_Berlin_ECOG_LFP/rawdata/sub-EL016/ses-EcogLfpMedOn02/ieeg/sub-EL016_ses-EcogLfpMedOn02_task-Rest_acq-StimOff_run-1_ieeg.vhdr'
}
files = [i for i in files if i in runs]
return files
def pick_ecog(raw):
"""
pick ECoG channels
"""
raw_ecog = []
raw_ecog.append(raw.pick_types(ecog=True).ch_names)
raw_ecog_red = raw_ecog[0][0:6]
return raw_ecog_red
def pick_ecog_s13(raw):
"""
pick ECoG channels
"""
raw_ecog = []
raw_ecog.append(raw.pick_types(ecog=True).ch_names)
raw_ecog_red = raw_ecog[0][6:12]
return raw_ecog_red
def pick_lfp(raw):
raw_lfp = []
raw_lfp.append(raw.pick_channels(["LFP_R_1_STN_MT", "LFP_R_8_STN_MT"]))
return raw_lfp
def bipolar_reference(raw, raw_ecog, new_ch_names):
anode = raw_ecog[0:5]
cathode = raw_ecog[1:6]
raw_ecog_bi = mne.set_bipolar_reference(
raw.load_data(), anode=anode, cathode=cathode, ch_name=new_ch_names
)
return raw_ecog_bi
def bipolar_reference_s12(raw, raw_ecog, new_ch_names):
anode = raw_ecog[0:4]
cathode = raw_ecog[1:5]
raw_ecog_bi = mne.set_bipolar_reference(
raw.load_data(), anode=anode, cathode=cathode, ch_name=new_ch_names
)
return raw_ecog_bi
def bipolar_reference_s13(raw, raw_ecog, new_ch_names):
anode = raw_ecog[0:5]
cathode = raw_ecog[1:6]
raw_new = mne.set_bipolar_reference(
raw.load_data(), anode=anode, cathode=cathode, ch_name=new_ch_names
)
raw_ecog_bi = raw_new.drop_channels(
[
"ECOG_R_01_SMC_AT",
"ECOG_R_02_SMC_AT",
"ECOG_R_03_SMC_AT",
"ECOG_R_04_SMC_AT",
"ECOG_R_05_SMC_AT",
"ECOG_R_06_SMC_AT",
]
)
return raw_ecog_bi
def bipolar_reference_s7_on(raw, raw_ecog, new_ch_names):
anode = raw_ecog[0:5]
cathode = raw_ecog[1:6]
raw_new = mne.set_bipolar_reference(
raw.load_data(), anode=anode, cathode=cathode, ch_name=new_ch_names
)
raw_ecog_bi = raw_new.drop_channels(
[
"ECOG_R_7_SMC_AT",
"ECOG_R_8_SMC_AT",
"ECOG_R_9_SMC_AT",
"ECOG_R_10_SMC_AT",
"ECOG_R_11_SMC_AT",
]
)
return raw_ecog_bi
def filtering(raw_ecog_bi):
"""
Filtering (Highpass 3 Hz, Notchfilter 50,251,50Hz, Lowpass 250Hz)
"""
raw_ecog_hi = raw_ecog_bi.filter(3, None,)
raw_ecog_hi_lo = raw_ecog_hi.filter(None, 250)
raw_ecog_filt = raw_ecog_hi_lo.notch_filter(
np.arange(50, 251, 50), filter_length="auto", phase="zero"
)
return raw_ecog_filt
def downsample(raw_ecog_filt):
"""
Downsample Data to 1600Hz for faster processing
"""
raw_ecog_dow = raw_ecog_filt.resample(1600)
return raw_ecog_dow
def get_data(raw_ecog_dow):
signal = raw_ecog_dow.get_data(reject_by_annotation="omit")
signal, time = raw_ecog_dow.get_data(picks=None, start=0, stop=None, reject_by_annotation='omit', return_times=True, units=None, tmin=None, tmax=raw_ecog_dow.times[-1]-5.0, verbose=None)
return signal, time
def z_score_signal(signal):
stand_signal = stats.zscore(signal, axis=1)
return stand_signal
def generate_annotations_fpath(
folderpath: str,
dataset: str,
subject: str,
session: str,
task: str,
acquisition: str,
run: str,
) -> str:
"""Generates a filepath for an annotations file that corresponds to an individual
recording session based on the MNE data-storage filepath structure.
PARAMETERS
----------
folderpath : str
- The path of the folder where the datasets are located.
dataset : str
- The name of the dataset folder within the folder given in 'folderpath'.
subject : str
- The name of the subject for which the filepath should be generated.
session : str
- The name of the session for which the filepath should be generated.
task : str
- The name of the task for which the filepath should be generated.
acquisition : str
- The name of the acquisition mode for which the filepath should be
generated.
run : str
- The name of the run for which the filepath should be generated.
RETURNS
-------
str
- The filepath of the annotations object.
"""
subfolders = f"{dataset}/sub-{subject}/ses-{session}"
filename = (
f"sub-{subject}_ses-{session}_task-{task}_acq-{acquisition}_run-{run}_"
"annotations.csv"
)
return os.path.join(folderpath, subfolders, filename)
def check_annots_orig_time(annots: mne.Annotations) -> mne.Annotations:
"""Checks whether a meaningful origin time (i.e. not 1970-01-01) is present
in the MNE Annotations object, setting it to 'None' if this is not the case.
PARAMETERS
----------
annots : MNE Annotations
- The annotations to check.
RETURNS
-------
annots : MNE Annotations
- The annotations, with non-meaningful origin time corrected, if
applicable.
"""
orig_time = annots.orig_time
if orig_time.day == 1 and orig_time.month == 1 and orig_time.year == 1970:
annots = mne.Annotations(
onset=annots.onset,
duration=annots.duration,
description=annots.description,
orig_time=None,
)
return annots