[bb64db]: / EEG-preprocessing / preprocessing.py

Download this file

84 lines (68 with data), 3.2 kB

 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
"""
Refer to the code of Things-EEG2 but with a few differences.
Many thanks!
https://www.sciencedirect.com/science/article/pii/S1053811922008758
"""
"""Preprocess the raw EEG data: channel selection, epoching, frequency
downsampling, baseline correction, multivariate noise normalization (MVNN),
sorting of the data image conditions and reshaping the data to:
Image conditions × EEG repetitions × EEG channels × EEG time points.
Then, the data of both test and training EEG partitions is saved.
Parameters
----------
sub : int
Used subject.
n_ses : int
Number of EEG sessions.
sfreq : int
Downsampling frequency.
mvnn_dim : str
Whether to compute the MVNN covariace matrices for each time point
('time') or for each epoch/repetition ('epochs').
project_dir : str
Directory of the project folder.
"""
import argparse
from preprocessing_utils import epoching
from preprocessing_utils import mvnn
from preprocessing_utils import save_prepr
# =============================================================================
# Input arguments
# =============================================================================
parser = argparse.ArgumentParser()
parser.add_argument('--sub', default=10, type=int)
parser.add_argument('--n_ses', default=4, type=int)
parser.add_argument('--sfreq', default=250, type=int)
parser.add_argument('--mvnn_dim', default='epochs', type=str)
parser.add_argument('--project_dir', default='/home/Data/Things-EEG2/', type=str)
args = parser.parse_args()
print('>>> EEG data preprocessing <<<')
print('\nInput arguments:')
for key, val in vars(args).items():
print('{:16} {}'.format(key, val))
# Set random seed for reproducible results
seed = 20200220
# =============================================================================
# Epoch and sort the data
# =============================================================================
# Channel selection, epoching, baseline correction and frequency downsampling of
# the test and training data partitions.
# Then, the conditions are sorted and the EEG data is reshaped to:
# Image conditions × EGG repetitions × EEG channels × EEG time points
# This step is applied independently to the data of each partition and session.
epoched_test, _, ch_names, times = epoching(args, 'test', seed)
epoched_train, img_conditions_train, _, _ = epoching(args, 'training', seed)
# =============================================================================
# Multivariate Noise Normalization
# =============================================================================
# MVNN is applied independently to the data of each session.
whitened_test, whitened_train = mvnn(args, epoched_test, epoched_train)
del epoched_test, epoched_train
# =============================================================================
# Merge and save the preprocessed data
# =============================================================================
# In this step the data of all sessions is merged into the shape:
# Image conditions × EGG repetitions × EEG channels × EEG time points
# Then, the preprocessed data of the test and training data partitions is saved.
save_prepr(args, whitened_test, whitened_train, img_conditions_train, ch_names,
times, seed)