-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathutils.py
401 lines (344 loc) · 12.7 KB
/
utils.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
import pickle
import torch
import numpy as np
import torch.nn.functional as F
import os
from scipy.signal import resample
from scipy.signal import butter, iirnotch, filtfilt
from scipy.interpolate import interp1d
from scipy.signal import butter, lfilter
class TUABLoader(torch.utils.data.Dataset):
def __init__(self, root, files, sampling_rate=200):
self.root = root
self.files = files
self.default_rate = 200
self.sampling_rate = sampling_rate
def __len__(self):
return len(self.files)
def __getitem__(self, index):
sample = pickle.load(open(os.path.join(self.root, self.files[index]), "rb"))
X = sample["X"]
# from default 200Hz to ?
if self.sampling_rate != self.default_rate:
X = resample(X, 10 * self.sampling_rate, axis=-1)
X = X / (
np.quantile(np.abs(X), q=0.95, method="linear", axis=-1, keepdims=True)
+ 1e-8
)
Y = sample["y"]
X = torch.FloatTensor(X)
return X, Y
class CHBMITLoader(torch.utils.data.Dataset):
def __init__(self, root, files, sampling_rate=200):
self.root = root
self.files = files
self.default_rate = 256
self.sampling_rate = sampling_rate
def __len__(self):
return len(self.files)
def __getitem__(self, index):
sample = pickle.load(open(os.path.join(self.root, self.files[index]), "rb"))
X = sample["X"]
# 2560 -> 2000, from 256Hz to ?
if self.sampling_rate != self.default_rate:
X = resample(X, 10 * self.sampling_rate, axis=-1)
X = X / (
np.quantile(np.abs(X), q=0.95, method="linear", axis=-1, keepdims=True)
+ 1e-8
)
Y = sample["y"]
X = torch.FloatTensor(X)
return X, Y
class PTBLoader(torch.utils.data.Dataset):
def __init__(self, root, files, sampling_rate=500):
self.root = root
self.files = files
self.default_rate = 500
self.sampling_rate = sampling_rate
def __len__(self):
return len(self.files)
def __getitem__(self, index):
sample = pickle.load(open(os.path.join(self.root, self.files[index]), "rb"))
X = sample["X"]
if self.sampling_rate != self.default_rate:
X = resample(X, self.freq * 5, axis=-1)
X = X / (
np.quantile(np.abs(X), q=0.95, method="linear", axis=-1, keepdims=True)
+ 1e-8
)
Y = sample["y"]
X = torch.FloatTensor(X)
return X, Y
class TUEVLoader(torch.utils.data.Dataset):
def __init__(self, root, files, sampling_rate=200):
self.root = root
self.files = files
self.default_rate = 256
self.sampling_rate = sampling_rate
def __len__(self):
return len(self.files)
def __getitem__(self, index):
sample = pickle.load(open(os.path.join(self.root, self.files[index]), "rb"))
X = sample["signal"]
# 256 * 5 -> 1000, from 256Hz to ?
if self.sampling_rate != self.default_rate:
X = resample(X, 5 * self.sampling_rate, axis=-1)
X = X / (
np.quantile(np.abs(X), q=0.95, method="linear", axis=-1, keepdims=True)
+ 1e-8
)
Y = int(sample["label"][0] - 1)
X = torch.FloatTensor(X)
return X, Y
class HARLoader(torch.utils.data.Dataset):
def __init__(self, dir, list_IDs, sampling_rate=50):
self.list_IDs = list_IDs
self.dir = dir
self.label_map = ["1", "2", "3", "4", "5", "6"]
self.default_rate = 50
self.sampling_rate = sampling_rate
def __len__(self):
return len(self.list_IDs)
def __getitem__(self, index):
path = os.path.join(self.dir, self.list_IDs[index])
sample = pickle.load(open(path, "rb"))
X, y = sample["X"], self.label_map.index(sample["y"])
if self.sampling_rate != self.default_rate:
X = resample(X, int(2.56 * self.sampling_rate), axis=-1)
X = X / (
np.quantile(
np.abs(X), q=0.95, interpolation="linear", axis=-1, keepdims=True
)
+ 1e-8
)
return torch.FloatTensor(X), y
class UnsupervisedPretrainLoader(torch.utils.data.Dataset):
def __init__(self, root_prest, root_shhs):
# prest dataset
self.root_prest = root_prest
exception_files = ["319431_data.npy"]
self.prest_list = list(
filter(
lambda x: ("data" in x) and (x not in exception_files),
os.listdir(self.root_prest),
)
)
PREST_LENGTH = 2000
WINDOW_SIZE = 200
print("(prest) unlabeled data size:", len(self.prest_list) * 16)
self.prest_idx_all = np.arange(PREST_LENGTH // WINDOW_SIZE)
self.prest_mask_idx_N = PREST_LENGTH // WINDOW_SIZE // 3
SHHS_LENGTH = 6000
# shhs dataset
self.root_shhs = root_shhs
self.shhs_list = os.listdir(self.root_shhs)
print("(shhs) unlabeled data size:", len(self.shhs_list))
self.shhs_idx_all = np.arange(SHHS_LENGTH // WINDOW_SIZE)
self.shhs_mask_idx_N = SHHS_LENGTH // WINDOW_SIZE // 5
def __len__(self):
return len(self.prest_list) + len(self.shhs_list)
def prest_load(self, index):
sample_path = self.prest_list[index]
# (16, 16, 2000), 10s
samples = np.load(os.path.join(self.root_prest, sample_path)).astype("float32")
# find all zeros or all 500 signals and then remove them
samples_max = np.max(samples, axis=(1, 2))
samples_min = np.min(samples, axis=(1, 2))
valid = np.where((samples_max > 0) & (samples_min < 0))[0]
valid = np.random.choice(valid, min(8, len(valid)), replace=False)
samples = samples[valid]
# normalize samples (remove the amplitude)
samples = samples / (
np.quantile(
np.abs(samples), q=0.95, method="linear", axis=-1, keepdims=True
)
+ 1e-8
)
samples = torch.FloatTensor(samples)
return samples, 0
def shhs_load(self, index):
sample_path = self.shhs_list[index]
# (2, 3750) sampled at 125
sample = pickle.load(open(os.path.join(self.root_shhs, sample_path), "rb"))
# (2, 6000) resample to 200
samples = resample(sample, 6000, axis=-1)
# normalize samples (remove the amplitude)
samples = samples / (
np.quantile(
np.abs(samples), q=0.95, method="linear", axis=-1, keepdims=True
)
+ 1e-8
)
# generate samples and targets and mask_indices
samples = torch.FloatTensor(samples)
return samples, 1
def __getitem__(self, index):
if index < len(self.prest_list):
return self.prest_load(index)
else:
index = index - len(self.prest_list)
return self.shhs_load(index)
def collate_fn_unsupervised_pretrain(batch):
prest_samples, shhs_samples = [], []
for sample, flag in batch:
if flag == 0:
prest_samples.append(sample)
else:
shhs_samples.append(sample)
shhs_samples = torch.stack(shhs_samples, 0)
if len(prest_samples) > 0:
prest_samples = torch.cat(prest_samples, 0)
return prest_samples, shhs_samples
return 0, shhs_samples
class EEGSupervisedPretrainLoader(torch.utils.data.Dataset):
def __init__(self, tuev_data, chb_mit_data, iiic_data, tuab_data):
# for TUEV
tuev_root, tuev_files = tuev_data
self.tuev_root = tuev_root
self.tuev_files = tuev_files
self.tuev_size = len(self.tuev_files)
# for CHB-MIT
chb_mit_root, chb_mit_files = chb_mit_data
self.chb_mit_root = chb_mit_root
self.chb_mit_files = chb_mit_files
self.chb_mit_size = len(self.chb_mit_files)
# for IIIC seizure
iiic_x, iiic_y = iiic_data
self.iiic_x = iiic_x
self.iiic_y = iiic_y
self.iiic_size = len(self.iiic_x)
# for TUAB
tuab_root, tuab_files = tuab_data
self.tuab_root = tuab_root
self.tuab_files = tuab_files
self.tuab_size = len(self.tuab_files)
def __len__(self):
return self.tuev_size + self.chb_mit_size + self.iiic_size + self.tuab_size
def tuev_load(self, index):
sample = pickle.load(
open(os.path.join(self.tuev_root, self.tuev_files[index]), "rb")
)
X = sample["signal"]
# 256 * 5 -> 1000
X = resample(X, 1000, axis=-1)
X = X / (
np.quantile(np.abs(X), q=0.95, method="linear", axis=-1, keepdims=True)
+ 1e-8
)
Y = int(sample["label"][0] - 1)
X = torch.FloatTensor(X)
return X, Y, 0
def chb_mit_load(self, index):
sample = pickle.load(
open(os.path.join(self.chb_mit_root, self.chb_mit_files[index]), "rb")
)
X = sample["X"]
# 2560 -> 2000
X = resample(X, 2000, axis=-1)
X = X / (
np.quantile(np.abs(X), q=0.95, method="linear", axis=-1, keepdims=True)
+ 1e-8
)
Y = sample["y"]
X = torch.FloatTensor(X)
return X, Y, 1
def iiic_load(self, index):
data = self.iiic_x[index]
samples = torch.FloatTensor(data)
samples = samples / (
torch.quantile(torch.abs(samples), q=0.95, dim=-1, keepdim=True) + 1e-8
)
y = np.argmax(self.iiic_y[index])
return samples, y, 2
def tuab_load(self, index):
sample = pickle.load(
open(os.path.join(self.tuab_root, self.tuab_files[index]), "rb")
)
X = sample["X"]
X = X / (
np.quantile(np.abs(X), q=0.95, method="linear", axis=-1, keepdims=True)
+ 1e-8
)
Y = sample["y"]
X = torch.FloatTensor(X)
return X, Y, 3
def __getitem__(self, index):
if index < self.tuev_size:
return self.tuev_load(index)
elif index < self.tuev_size + self.chb_mit_size:
index = index - self.tuev_size
return self.chb_mit_load(index)
elif index < self.tuev_size + self.chb_mit_size + self.iiic_size:
index = index - self.tuev_size - self.chb_mit_size
return self.iiic_load(index)
elif (
index < self.tuev_size + self.chb_mit_size + self.iiic_size + self.tuab_size
):
index = index - self.tuev_size - self.chb_mit_size - self.iiic_size
return self.tuab_load(index)
else:
raise ValueError("index out of range")
def collate_fn_supervised_pretrain(batch):
tuev_samples, tuev_labels = [], []
iiic_samples, iiic_labels = [], []
chb_mit_samples, chb_mit_labels = [], []
tuab_samples, tuab_labels = [], []
for sample, labels, idx in batch:
if idx == 0:
tuev_samples.append(sample)
tuev_labels.append(labels)
elif idx == 1:
iiic_samples.append(sample)
iiic_labels.append(labels)
elif idx == 2:
chb_mit_samples.append(sample)
chb_mit_labels.append(labels)
elif idx == 3:
tuab_samples.append(sample)
tuab_labels.append(labels)
else:
raise ValueError("idx out of range")
if len(tuev_samples) > 0:
tuev_samples = torch.stack(tuev_samples)
tuev_labels = torch.LongTensor(tuev_labels)
if len(iiic_samples) > 0:
iiic_samples = torch.stack(iiic_samples)
iiic_labels = torch.LongTensor(iiic_labels)
if len(chb_mit_samples) > 0:
chb_mit_samples = torch.stack(chb_mit_samples)
chb_mit_labels = torch.LongTensor(chb_mit_labels)
if len(tuab_samples) > 0:
tuab_samples = torch.stack(tuab_samples)
tuab_labels = torch.LongTensor(tuab_labels)
return (
(tuev_samples, tuev_labels),
(iiic_samples, iiic_labels),
(chb_mit_samples, chb_mit_labels),
(tuab_samples, tuab_labels),
)
# define focal loss on binary classification
def focal_loss(y_hat, y, alpha=0.8, gamma=0.7):
# y_hat: (N, 1)
# y: (N, 1)
# alpha: float
# gamma: float
y_hat = y_hat.view(-1, 1)
y = y.view(-1, 1)
# y_hat = torch.clamp(y_hat, -75, 75)
p = torch.sigmoid(y_hat)
loss = -alpha * (1 - p) ** gamma * y * torch.log(p) - (1 - alpha) * p**gamma * (
1 - y
) * torch.log(1 - p)
return loss.mean()
# define binary cross entropy loss
def BCE(y_hat, y):
# y_hat: (N, 1)
# y: (N, 1)
y_hat = y_hat.view(-1, 1)
y = y.view(-1, 1)
loss = (
-y * y_hat
+ torch.log(1 + torch.exp(-torch.abs(y_hat)))
+ torch.max(y_hat, torch.zeros_like(y_hat))
)
return loss.mean()