-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset_generator.py
296 lines (246 loc) · 9.71 KB
/
dataset_generator.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
# Import libraries
from tensorflow.keras.utils import Sequence
import numpy as np
import os
import csv
from sklearn.preprocessing import LabelBinarizer
class DataGenerator(Sequence):
"""Generates data for loading (preprocessed) EEG timeseries data.
Create batches for training or prediction from given folders and filenames.
"""
def __init__(self,
list_IDs,
main_labels,
label_dict,
binarizer_dict,
ignore_labels,
path_data,
filenames,
data_path,
to_fit=True, #TODO: implement properly for False
n_average = 30,
batch_size=32,
iter_per_epoch = 2,
up_sampling = True,
n_timepoints = 501,
n_channels=30,
include_baseline = False,
subtract_baseline = None,
shuffle=True,
warnings=False):
"""Initialization
Args:
--------
list_IDs:
list of all filename/label ids to use in the generator
main_labels:
list of all main labels.
label_dict: dict
Dictionary for how to ouput the found labels to the model.
path_data: str
Foldername for all npy and csv files...
filenames:
list of image filenames (file names)
data_path:
path to data directory
to_fit:
True to return X and y, False to return X only
n_average: int
Number of EEG/time series epochs to average.
batch_size:
batch size at each iteration
iter_per_epoch: int
Number of iterations over all data points within one epoch.
up_sampling: bool
If true, create equal amounts of data for all main labels.
n_timepoints: int
Timepoint dimension of data.
n_channels:
number of input channels
subtract_baseline: None, or list
Give label (or list of labels) which should be used as a baseline.,
All epochs of those labels will be averaged and then subtracted
from all other epochs.
shuffle:
True to shuffle label indexes after every epoch
"""
self.list_IDs = list_IDs
self.main_labels = main_labels
self.label_dict = label_dict
self.ignore_labels = ignore_labels
self.path_data = path_data
self.filenames = filenames
self.data_path = data_path
self.to_fit = to_fit
self.n_average = n_average
self.batch_size = batch_size
self.iter_per_epoch = iter_per_epoch
self.up_sampling = up_sampling
self.n_timepoints = n_timepoints
self.n_channels = n_channels
self.include_baseline = include_baseline
self.subtract_baseline = subtract_baseline
self.shuffle = shuffle
self.warnings = warnings
self.on_epoch_end()
self.binarizer_dict = binarizer_dict
#self.label_binarize = LabelBinarizer()
def __len__(self):
"""Denotes the number of batches per epoch
return: number of batches per epoch
"""
return int(np.floor(len(self.list_IDs) / self.batch_size))
def __getitem__(self, index):
"""Generate one batch of data
Args:
--------
index: int
index of the batch
return: X and y when fitting. X only when predicting
"""
# Generate indexes of the batch
indexes = self.indexes[index * self.batch_size:((index + 1) * self.batch_size + 1)]
# Find list of IDs
list_IDs_temp = [self.list_IDs[int(k)] for k in indexes]
# Generate data
X, y = self.generate_data(list_IDs_temp)
if self.to_fit:
return X, y
else:
return X
def on_epoch_end(self):
"""Updates indexes after each epoch.
Takes care of up-sampling and sampling frequency per "epoch".
"""
idx_labels = []
label_count = []
# Up-sampling
if self.up_sampling:
main_labels = [self.main_labels[ID] for ID in self.list_IDs]
labels_unique = list(set(main_labels))
for label in labels_unique:
idx = np.where(np.array(main_labels) == label)[0]
idx_labels.append(idx)
label_count.append(len(idx))
idx_upsampled = np.zeros((0))
for i in range(len(labels_unique)):
up_sample_factor = self.iter_per_epoch * max(label_count)/label_count[i]
idx_upsampled = np.concatenate((idx_upsampled, np.tile(idx_labels[i], int(up_sample_factor // 1))),
axis = 0)
idx_upsampled = np.concatenate((idx_upsampled, np.random.choice(idx_labels[i], int(label_count[i] * up_sample_factor % 1), replace=True)),
axis = 0)
self.indexes = idx_upsampled
else:
# No upsampling
idx_base = np.arange(len(self.list_IDs))
idx_epoch = np.tile(idx_base, self.iter_per_epoch)
self.indexes = idx_epoch
if self.shuffle == True:
np.random.shuffle(self.indexes)
def generate_data(self, list_IDs_temp):
"""Generates data containing batch_size averaged time series.
Args:
-------
list_IDs_temp: list
list of label ids to load
return: batch of averaged time series
"""
X_data = np.zeros((0, self.n_channels, self.n_timepoints))
y_data = []
for i, ID in enumerate(list_IDs_temp):
filename = self.filenames[ID]
data_signal = self.load_signal(self.path_data, filename + '.npy')
data_labels = self.load_labels(self.path_data, filename + '.csv')
X, y = self.create_averaged_epoch(data_signal,
data_labels)
X_data = np.concatenate((X_data, X), axis=0)
# Replace labels based on label_dict:
y = [self.transform_label(label) for label in y]
y_data += y
if self.shuffle:
idx = np.arange(len(y_data))
np.random.shuffle(idx)
X_data = X_data[idx, :, :]
y_data = [y_data[i] for i in idx]
#y_data = self.label_binarize.fit_transform(np.array(y_data).astype(int))
y_data = np.array([self.binarizer_dict[x] for x in y_data])
return np.swapaxes(X_data,1,2), y_data
def create_averaged_epoch(self,
data_signal,
data_labels):
"""
Function to create averages of self.n_average epochs.
Will create one averaged epoch per found unique label from self.n_average random epochs.
Args:
--------
data_signal: numpy array
Data from one person as numpy array
data_labels: list
List of labels for all data from one person.
"""
# Create new data collection:
X_data = np.zeros((0, self.n_channels, self.n_timepoints))
y_data = []
categories_found = list(set(data_labels))
if self.subtract_baseline is None:
self.subtract_baseline = []
baseline_labels_found = list(set(categories_found) & set(self.subtract_baseline))
if len(baseline_labels_found) > 0:
idx_baseline = []
for cat in baseline_labels_found:
# Create baseline to subtract from all other epochs.
idx_baseline.extend(np.where(np.array(data_labels) == cat)[0])
baseline = np.mean(data_signal[idx_baseline,:,:], axis=0)
else:
baseline = 0
idx_cat = []
for cat in list(set(categories_found) - set(self.subtract_baseline)):
if cat not in self.ignore_labels:
idx = np.where(np.array(data_labels) == cat)[0]
idx_cat.append(idx)
if len(idx) >= self.n_average:
select = np.random.choice(idx, self.n_average, replace=False)
else:
if self.warnings:
print("Found only", len(idx), " epochs and will take those!")
signal_averaged = np.mean(data_signal[idx,:,:], axis=0)
break
# Average signal and subtract baseline (if any)
signal_averaged = np.mean(data_signal[select,:,:], axis=0) - baseline
X_data = np.concatenate([X_data, np.expand_dims(signal_averaged, axis=0)], axis=0)
y_data.append(cat)
else:
pass
return X_data, y_data
def load_signal(self,
PATH,
filename):
"""Load EEG signal from one person.
Args:
-------
filename: str
filename...
PATH: str
path name to file to load
return: loaded array
"""
return np.load(os.path.join(PATH, filename))
def load_labels(self,
PATH,
filename):
metadata = []
filename = os.path.join(PATH, filename)
with open(filename, 'r') as readFile:
reader = csv.reader(readFile, delimiter=',')
for row in reader:
#if len(row) > 0:
metadata.append(row)
readFile.close()
return metadata[0]
def transform_label(self,
label):
if label in self.label_dict:
label_new = self.label_dict[label]
else:
label_new = None
return label_new