-
Notifications
You must be signed in to change notification settings - Fork 45
/
data.py
60 lines (46 loc) · 1.62 KB
/
data.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
import os
import re
import numpy as np
def get_subject_files(dataset, files, sid):
"""Get a list of files storing each subject data."""
# Pattern of the subject files from different datasets
if "mass" in dataset:
reg_exp = f".*-00{str(sid+1).zfill(2)} PSG.npz"
# reg_exp = "SS3_00{}\.npz$".format(str(sid+1).zfill(2))
elif "sleepedf" in dataset:
reg_exp = f"S[C|T][4|7]{str(sid).zfill(2)}[a-zA-Z0-9]+\.npz$"
# reg_exp = "[a-zA-Z0-9]*{}[1-9]E0\.npz$".format(str(sid).zfill(2))
elif "isruc" in dataset:
reg_exp = f"subject{sid+1}.npz"
else:
raise Exception("Invalid datasets.")
# Get the subject files based on ID
subject_files = []
for i, f in enumerate(files):
pattern = re.compile(reg_exp)
if pattern.search(f):
subject_files.append(f)
return subject_files
def load_data(subject_files):
"""Load data from subject files."""
signals = []
labels = []
sampling_rate = None
for sf in subject_files:
with np.load(sf) as f:
x = f['x']
y = f['y']
fs = f['fs']
if sampling_rate is None:
sampling_rate = fs
elif sampling_rate != fs:
raise Exception("Mismatch sampling rate.")
# Reshape the data to match the input of the model - conv2d
x = np.squeeze(x)
x = x[:, :, np.newaxis, np.newaxis]
# Casting
x = x.astype(np.float32)
y = y.astype(np.int32)
signals.append(x)
labels.append(y)
return signals, labels, sampling_rate