-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0.0 seed preprocessing.py
172 lines (130 loc) · 5.84 KB
/
0.0 seed 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
import os
from os.path import join
import numpy as np
from scipy import io
import re
import argparse
def save_datas_seg(window, stride, data_dir, saved_dir):
print('Segmentation x: (samples, 62, segment size), y: (samples, 2)')
labels = [-1, 1, 0, 2, 2, 0, 1, 2, 0, 1, 1, 0, 2, 0, 1, 2]
dir_list = os.listdir(data_dir)
skip_set = ['label.mat', 'readme.txt']
dir_list = [f for f in dir_list if f not in skip_set]
sub_dir_list = [[] for _ in range(0,16)]
for dir_name in dir_list:
sub_num = int(dir_name.split('_')[0])
sub_dir_list[sub_num].append(dir_name)
sub_list = [i for i in range(1,16)]
for subidx in sub_list:
x, y = [], []
for session in range(0,3):
path = join(data_dir, sub_dir_list[subidx][session])
datas = io.loadmat(path)
trial_name_ids = [(trial_name, int(re.findall(r".*_eeg(\d+)", trial_name)[0]))
for trial_name in datas.keys() if 'eeg' in trial_name]
for trial_name, trial_id in trial_name_ids:
idx = 0
data = datas[trial_name]
time_size = len(data[0])
while idx + window < time_size:
seg = data[:, idx : idx+window]
x.append(seg)
y.append([labels[trial_id], subidx])
idx += stride
x = np.array(x)
x = np.array(x, dtype='float16')
np.nan_to_num(x, copy=False)
y = np.array(y)
print(f'EEG:{x.shape} label:{y.shape}')
os.makedirs(saved_dir, exist_ok=True)
np.savez(join(saved_dir, str(subidx).zfill(2)), x=x, y=y)
print(f'saved in {saved_dir}')
from utils.transform import BandDifferentialEntropy
def save_datas_seg_DE(window, stride, data_dir, saved_dir):
print('Segmentation with DE x: (samples, 62, 4), y: (samples, 2)')
bde = BandDifferentialEntropy()
labels = [-1, 1, 0, 2, 2, 0, 1, 2, 0, 1, 1, 0, 2, 0, 1, 2]
dir_list = os.listdir(data_dir)
skip_set = ['label.mat', 'readme.txt']
dir_list = [f for f in dir_list if f not in skip_set]
sub_dir_list = [[] for _ in range(0,16)]
for dir_name in dir_list:
sub_num = int(dir_name.split('_')[0])
sub_dir_list[sub_num].append(dir_name)
sub_list = [i for i in range(1,16)]
for subidx in sub_list:
x, y = [], []
for session in range(0,3):
path = join(data_dir, sub_dir_list[subidx][session])
datas = io.loadmat(path)
trial_name_ids = [(trial_name, int(re.findall(r".*_eeg(\d+)", trial_name)[0]))
for trial_name in datas.keys() if 'eeg' in trial_name]
for trial_name, trial_id in trial_name_ids:
idx = 0
data = datas[trial_name]
time_size = len(data[0])
while idx + window < time_size:
seg = data[:, idx : idx+window]
x.append(bde.apply(seg))
y.append([labels[trial_id], subidx])
idx += stride
x = np.array(x)
y = np.array(y)
print(f'EEG:{x.shape} label:{y.shape}')
os.makedirs(saved_dir, exist_ok=True)
np.savez(join(saved_dir, str(subidx).zfill(2)), x=x, y=y)
print(f'saved in {saved_dir}')
from utils.transform import BandPowerSpectralDensity
def save_datas_seg_PSD(window, stride, data_dir, saved_dir):
print('Segmentation with PSD x: (samples, 62, 4), y: (samples, 2)')
psd = BandPowerSpectralDensity()
labels = [-1, 1, 0, 2, 2, 0, 1, 2, 0, 1, 1, 0, 2, 0, 1, 2]
dir_list = os.listdir(data_dir)
skip_set = ['label.mat', 'readme.txt']
dir_list = [f for f in dir_list if f not in skip_set]
sub_dir_list = [[] for _ in range(0,16)]
for dir_name in dir_list:
sub_num = int(dir_name.split('_')[0])
sub_dir_list[sub_num].append(dir_name)
sub_list = [i for i in range(1,16)]
for subidx in sub_list:
x, y = [], []
for session in range(0,3):
path = join(data_dir, sub_dir_list[subidx][session])
datas = io.loadmat(path)
trial_name_ids = [(trial_name, int(re.findall(r".*_eeg(\d+)", trial_name)[0]))
for trial_name in datas.keys() if 'eeg' in trial_name]
for trial_name, trial_id in trial_name_ids:
idx = 0
data = datas[trial_name]
time_size = len(data[0])
while idx + window < time_size:
seg = data[:, idx : idx+window]
x.append(psd.apply(seg))
y.append([labels[trial_id], subidx])
idx += stride
x = np.array(x)
y = np.array(y)
print(f'EEG:{x.shape} label:{y.shape}')
os.makedirs(saved_dir, exist_ok=True)
np.savez(join(saved_dir, str(subidx).zfill(2)), x=x, y=y)
print(f'saved in {saved_dir}')
# -----------------------------------------save-data---------------------------------------------------
parser = argparse.ArgumentParser()
parser.add_argument("--src_dir", type=str, default="/mnt/data")
parser.add_argument("--window", type=int, default=400)
parser.add_argument("--stride", type=int, default=200)
parser.add_argument("--method", type=str, default="seg", help='seg, PSD, DE')
args = parser.parse_args()
SRC = args.src_dir
WINDOW = args.window
STRIDE = args.stride
METHOD = args.method
src_dir = join(SRC, 'SEED', 'Preprocessed_EEG')
saved_dir = join(os.getcwd(), 'datasets', "SEED", 'npz', "Preprocessed")
if METHOD == 'seg':
save_datas_seg(WINDOW, STRIDE, src_dir,join(saved_dir, 'seg'))
elif METHOD == 'PSD':
save_datas_seg_PSD(WINDOW, STRIDE, src_dir, join(saved_dir, 'seg_PSD'))
elif METHOD == 'DE':
save_datas_seg_DE(WINDOW, STRIDE, src_dir, join(saved_dir, 'seg_DE'))