-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
504 lines (448 loc) · 18 KB
/
util.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
"""
Author: Christian Goelz <[email protected]>
Created: 24th February, 2022
"""
from pathlib import Path
import glob
import numpy as np
import mne
from mne.decoding import Vectorizer
from mne.preprocessing.xdawn import _XdawnTransformer
mne.set_log_level('critical')
from mne.preprocessing import ICA
from autoreject import AutoReject
from sklearn.metrics import balanced_accuracy_score, roc_auc_score
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier as RF
from sklearn.preprocessing import StandardScaler
from imblearn.under_sampling import RandomUnderSampler
import pyreadstat
import warnings
warnings.filterwarnings("ignore")
# GET FILE LIST FROM LOCATION
#if str(Path.cwd()).split('/')[-2] == 'lifespan':
# input = Path.cwd().parent / 'input' / '*.*df'
#elif str(Path.cwd()).split('/')[-1] == 'lifespan':
# input = Path.cwd() / 'input' / '*.*df'
#FILES = glob.glob(str(input))
FILES = glob.glob('/home/christian/Schreibtisch/lifespan/input/EEG_Daten_Flanker/*.*df')
METADATA = glob.glob('/home/christian/Schreibtisch/lifespan/input/EEG_Daten_Flanker/stats/*.sav')[0]
METADATA, _ = pyreadstat.read_sav(METADATA)
METADATA['part'] = METADATA["VPN_file"].str.lower()
METADATA.set_index('part', inplace = True)
METADATA = METADATA[['GRUPPE','Alter','Geschlecht','eegusedNEW']]
OUTPATH = str(Path.cwd().parent / 'results')
def read_file(f, preload = True):
"""Reads in EEG files in edf or bdf format. Returns MNE raw object and name of file
Parameters
----------
f : string
string with file path to edf/ bdf file
Returns
-------
name: string
name of file
raw: object
MNE raw object
"""
name = f.split('/')[-1][:-4]
if f[-3:] == 'edf':
raw = mne.io.read_raw_edf(f, eog=['EXG1', 'EXG2', 'EXG3', 'EXG4'],
exclude = ['EXG7', 'EXG8'], misc=['EXG5', 'EXG6'],
preload=preload)
else:
raw = mne.io.read_raw_bdf(f, eog=['EXG1', 'EXG2', 'EXG3', 'EXG4'],
exclude = ['EXG7', 'EXG8'],misc=['EXG5', 'EXG6'],
preload=preload)
return name.lower(), raw
def correct_events(raw):
""" Function to correct the event coding of the Events given by Presentation:
Ignores the evaluation done online during the experiment and reavaluates it.
The experimental setup together with the event coding is visualized in resources/setup.pdf
Correct: correct response between 100ms and 1200ms and response button pressed
only once
Parameters
----------
raw : mne raw object
raw EEG data structure of MNE python
Returns
-------
events: array, shape = (n_events,3)
corrected events
mapping: dict,
mapping between event id and label
response_time: list of dicts,
a list containing a dict for each correct trial
- dict with key representing the task label and value the response time-
accuracy: dict
the accuracy per stimulus
"""
response_time = []
labels = [16,32,64]
buttons = [1,2]
mapping = {201: 'start_C_correct',
202: 'start_N_correct',
203: 'start_IC_correct',
204: 'start_C_wrong',
205: 'start_N_wrong',
206: 'start_IC_wrong',
101: 're_C_correct',
102: 're_N_correct',
103: 're_IC_correct',
104: 're_C_wrong',
105: 're_N_wrong',
106: 're_IC_wrong'}
events = mne.find_events(raw, shortest_event=1)
trial_start_ids = np.where(events[:,2] == 8)[0]
for start_id in trial_start_ids:
# get corresponding label
stim_id = start_id-1
while events[stim_id,2] not in range(41,47):
stim_id -= 1
if stim_id < 0:
stim_id = None
break
elif events[stim_id,2] >= 201: # reach last event
stim_id = None
break
# get button press
button_id = start_id+1
while events[button_id,2] not in buttons:
# end of experiment
button_id += 1
if button_id >= len(events)-1:
button_id = None
break
elif events[button_id,2] == 8:
button_id = None
break
# re-evaluate
# case button pressed
if button_id is not None and stim_id is not None:
stim = events[stim_id,2]
button = events[button_id,2]
rt = ((events[button_id,0] - events[start_id,0]) / raw.info['sfreq']) * 1000 # response time in ms
# evaluate
if (stim in [41,44,46] and button == 2) or (stim in [42,43,45] and button == 1):
correct = True
# evaluate if there is a second press:
id = button_id
while events[id,2] != 8:
id += 1
if id >= len(events):
break
elif events[id,2] in buttons and events[id,2] != events[button_id,2]:
correct = False
#print("double pressed other")
else:
correct = False
if rt < 100 or rt > 1200:
correct = False
#print("to slow")
# rewrite coding
if correct:
# Congruent correct
if stim in [41,42]:
events[start_id, 2] = 201
events[button_id, 2] = 101
response_time.append({'task':'C','rt':rt})
# Neutral correct
elif stim in [43,44]:
events[start_id, 2] = 202
events[button_id, 2] = 102
response_time.append({'task':'N','rt':rt})
# Incongruent correct
else:
events[start_id, 2] = 203
events[button_id, 2] = 103
response_time.append({'task':'IC','rt':rt})
else:
# Congruent wrong
if stim in [41,42]:
events[start_id, 2] = 204
events[button_id, 2] = 104
# Neutral wrong
elif stim in [43,44]:
events[start_id, 2] = 205
events[button_id, 2] = 105
# Incongruent wrong
else:
events[start_id, 2] = 206
events[button_id, 2] = 106
# case no button pressed
elif stim_id is not None:
stim = events[stim_id,2]
# Congruent wrong
if stim in [41,42]:
events[start_id, 2] = 204
# Neutral wrong
elif stim in [43,44]:
events[start_id, 2] = 205
# Incongruent wrong
else:
events[start_id, 2] = 206
# calculate accuracy
accuracy = []
c = 201 # code correct (see mapping)
w = 204 # code wrong answer (see mapping)
for task in ['C','N','IC']:
corrects = len(np.where(events[:,2] == c)[0])
all = len(np.where((events[:,2] == c) | (events[:,2] == w))[0])
acc = (corrects / all) * 100
accuracy.append({'task':task, '#corrects': corrects, 'all': all, 'acc':acc})
c += 1
w += 1
return events, mapping, response_time, accuracy
def correct_events_per_color(raw):
""" Function to correct the event coding of the Events given by Presentation:
Ignores the evaluation done online during the experiment and reavaluates it.
The experimental setup together with the event coding is visualized in resources/setup.pdf
Correct: correct response between 100ms and 1200ms and response button pressed
only once
Parameters
----------
raw : mne raw object
raw EEG data structure of MNE python
Returns
-------
events: array, shape = (n_events,3)
corrected events
mapping: dict,
mapping between event id and label
response_time: list of dicts,
a list containing a dict for each correct trial
- dict with key representing the task label and value the response time-
accuracy: dict
the accuracy per stimulus
"""
response_time = []
labels = [16,32,64]
buttons = [1,2]
mapping = {201: 'start_C_correct_red',
202: 'start_N_correct_red',
203: 'start_IC_correct_red',
301: 'start_C_correct_green',
302: 'start_N_correct_green',
303: 'start_IC_correct_green',
204: 'start_C_wrong',
205: 'start_N_wrong',
206: 'start_IC_wrong',
101: 're_C_correct',
102: 're_N_correct',
103: 're_IC_correct',
104: 're_C_wrong',
105: 're_N_wrong',
106: 're_IC_wrong'}
events = mne.find_events(raw, shortest_event=1)
trial_start_ids = np.where(events[:,2] == 8)[0]
for start_id in trial_start_ids:
# get corresponding label
stim_id = start_id-1
while events[stim_id,2] not in range(41,47):
stim_id -= 1
if stim_id < 0:
stim_id = None
break
elif events[stim_id,2] >= 201: # reach last event
stim_id = None
break
# get button press
button_id = start_id+1
while events[button_id,2] not in buttons:
# end of experiment
button_id += 1
if button_id >= len(events)-1:
button_id = None
break
elif events[button_id,2] == 8:
button_id = None
break
# re-evaluate
# case button pressed
if button_id is not None and stim_id is not None:
stim = events[stim_id,2]
button = events[button_id,2]
rt = ((events[button_id,0] - events[start_id,0]) / raw.info['sfreq']) * 1000 # response time in ms
# evaluate
if (stim in [41,44,46] and button == 2) or (stim in [42,43,45] and button == 1):
correct = True
# evaluate if there is a second press:
id = button_id
while events[id,2] != 8:
id += 1
if id >= len(events):
break
elif events[id,2] in buttons and events[id,2] != events[button_id,2]:
correct = False
#print("double pressed other")
else:
correct = False
if rt < 100 or rt > 1200:
correct = False
#print("to slow")
# rewrite coding
if correct:
# Congruent correct red
if stim == 41:
events[start_id, 2] = 201
events[button_id, 2] = 101
response_time.append({'task':'C','rt':rt})
# Neutral correct red
elif stim == 44:
events[start_id, 2] = 202
events[button_id, 2] = 102
response_time.append({'task':'N','rt':rt})
# Incongruent correct red
elif stim == 46:
events[start_id, 2] = 203
events[button_id, 2] = 103
response_time.append({'task':'IC','rt':rt})
# Congruent correct green
elif stim == 42:
events[start_id, 2] = 301
events[button_id, 2] = 101
response_time.append({'task':'C','rt':rt})
# Neutral correct green
elif stim == 43:
events[start_id, 2] = 202
events[button_id, 2] = 102
response_time.append({'task':'N','rt':rt})
# Incongruent correct green
elif stim == 45:
events[start_id, 2] = 203
events[button_id, 2] = 103
response_time.append({'task':'IC','rt':rt})
else:
# Congruent wrong
if stim in [41,42]:
events[start_id, 2] = 204
events[button_id, 2] = 104
# Neutral wrong
elif stim in [43,44]:
events[start_id, 2] = 205
events[button_id, 2] = 105
# Incongruent wrong
else:
events[start_id, 2] = 206
events[button_id, 2] = 106
# case no button pressed
elif stim_id is not None:
stim = events[stim_id,2]
# Congruent wrong
if stim in [41,42]:
events[start_id, 2] = 204
# Neutral wrong
elif stim in [43,44]:
events[start_id, 2] = 205
# Incongruent wrong
else:
events[start_id, 2] = 206
# calculate accuracy
accuracy = []
c = 201 # code correct (see mapping)
w = 204 # code wrong answer (see mapping)
for task in ['C','N','IC']:
corrects = len(np.where(events[:,2] == c)[0])
all = len(np.where((events[:,2] == c) | (events[:,2] == w))[0])
acc = (corrects / all) * 100
accuracy.append({'task':task, '#corrects': corrects, 'all': all, 'acc':acc})
c += 1
w += 1
return events, mapping, response_time, accuracy
def prepare_data(f, param):
try:
subj, raw = read_file(f)
print(f'Preprocessing participant: {subj}')
try:
raw = raw.drop_channels(['GSR1', 'GSR2', 'Erg1', 'Erg2', 'Resp', 'Plet', 'Temp'])
except:
pass
event_id = {'C_correct':201, 'IC_correct':203}
ref_ch = ['EXG5','EXG6']
raw.set_eeg_reference(ref_ch)
events, _, _, _ = correct_events(raw)
raw.filter(param['filt_low'], param['filt_high'], fir_design="firwin")
picks = mne.pick_types(raw.info, eeg=True, stim=False, eog=False, exclude="bads")
raw, events = raw.resample(param['sfreq'], events=events)
raw.set_montage('standard_1020')
if param['ica']:
ica = ICA(method='fastica', random_state=param['random_state'])
events_ica = mne.make_fixed_length_events(raw, duration=1.0)
epochs_ica = mne.Epochs(raw, events_ica, tmin=0.0, tmax=1.0, baseline=None, preload = True)
ica.fit(epochs_ica)
eog_indices, eog_scores = ica.find_bads_eog(raw)
ica.exclude = eog_indices
ica.apply(raw)
epochs = mne.Epochs(
raw,
events,
event_id,
param['tmin'],
param['tmax'],
proj=False,
picks=picks,
baseline=None,
preload=True,
verbose=False,
detrend=None
)
if param['ar']:
ar = AutoReject(random_state = param['random_state'], verbose=False, n_interpolate = np.array([1, 2, 4, 8]))
epochs = ar.fit_transform(epochs)
if len(epochs['IC_correct']) < param['exclude_thresh'] or len(epochs['C_correct']) < param['exclude_thresh']:
return (subj, None)
return(subj, epochs)
except:
return(subj, None)
def task_classification(epochs, param, subj, time_resovled = True, permutation = False, perm_seed = None):
xd = _XdawnTransformer(n_components=param['n_filter'])
steps = [('vec',Vectorizer()),
('scale',StandardScaler()),
('svc',SVC())]
# defining parameter range
pipe = Pipeline(steps)
scores_all = []
labels = epochs.events[:, -1]
# Permute labels for test score
if permutation == True:
seed = np.random.RandomState(perm_seed)
labels = seed.permutation(labels)
vec = Vectorizer()
epochs_data = vec.fit_transform(epochs)
epochs_data = vec.inverse_transform(epochs_data)
w_start = np.arange(0, epochs_data.shape[2] - param['w_length'], param['w_step'])
# split and cv
cv = StratifiedShuffleSplit(param['k'], test_size=param['test_size'], random_state=123)#i)
cv_split = cv.split(epochs_data, labels)
for train_idx, test_idx in cv_split:
y_train, y_test = labels[train_idx], labels[test_idx]
X_train = xd.fit_transform(epochs_data[train_idx])
X_test = xd.transform(epochs_data[test_idx])
score_this = []
if time_resovled == True:
score_this = []
for n in w_start:
pipe.fit(X_train[:,:,n:n+param['w_length']], y_train)
# score
if param['score'] == 'balanced_accuracy':
y_pred = pipe.predict(X_test[:,:,n:n+param['w_length']])
score_this.append(balanced_accuracy_score(y_test, y_pred))
elif param['score'] == 'accuracy':
score_this.append(pipe.score(X_test[:,:,n:n+param['w_length']], y_test))
elif param['score'] == 'roc_auc':
y_score = pipe.decision_function(X_test[:,:,n:n+param['w_length']])
score_this.append(roc_auc_score(y_test, y_score))
else:
pipe.fit(X_train, y_train)
if param['score'] == 'balanced_accuracy':
y_pred = pipe.predict(X_test)
score_this.append(balanced_accuracy_score(y_test, y_pred))
elif param['score'] == 'accuracy':
score_this.append(pipe.score(X_test, y_test))
elif param['score'] == 'roc_auc':
y_score = pipe.decision_function(X_test)
score_this.append(roc_auc_score(y_test, y_score))
scores_all.append(score_this)
return(scores_all)