-
Notifications
You must be signed in to change notification settings - Fork 45
/
prepare_sleepedf.py
194 lines (161 loc) · 7.4 KB
/
prepare_sleepedf.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
import argparse
import glob
import math
import ntpath
import os
import shutil
import pyedflib
import numpy as np
import pandas as pd
from sleepstage import stage_dict
from logger import get_logger
# Have to manually define based on the dataset
ann2label = {
"Sleep stage W": 0,
"Sleep stage 1": 1,
"Sleep stage 2": 2,
"Sleep stage 3": 3, "Sleep stage 4": 3, # Follow AASM Manual
"Sleep stage R": 4,
"Sleep stage ?": 6,
"Movement time": 5
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--data_dir", type=str, default="./data/sleepedf/sleep-cassette",
help="File path to the Sleep-EDF dataset.")
parser.add_argument("--output_dir", type=str, default="./data/sleepedf/sleep-cassette/eeg_fpz_cz",
help="Directory where to save outputs.")
parser.add_argument("--select_ch", type=str, default="EEG Fpz-Cz",
help="Name of the channel in the dataset.")
parser.add_argument("--log_file", type=str, default="info_ch_extract.log",
help="Log file.")
args = parser.parse_args()
# Output dir
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
else:
shutil.rmtree(args.output_dir)
os.makedirs(args.output_dir)
args.log_file = os.path.join(args.output_dir, args.log_file)
# Create logger
logger = get_logger(args.log_file, level="info")
# Select channel
select_ch = args.select_ch
# Read raw and annotation from EDF files
psg_fnames = glob.glob(os.path.join(args.data_dir, "*PSG.edf"))
ann_fnames = glob.glob(os.path.join(args.data_dir, "*Hypnogram.edf"))
psg_fnames.sort()
ann_fnames.sort()
psg_fnames = np.asarray(psg_fnames)
ann_fnames = np.asarray(ann_fnames)
for i in range(len(psg_fnames)):
logger.info("Loading ...")
logger.info("Signal file: {}".format(psg_fnames[i]))
logger.info("Annotation file: {}".format(ann_fnames[i]))
psg_f = pyedflib.EdfReader(psg_fnames[i])
ann_f = pyedflib.EdfReader(ann_fnames[i])
assert psg_f.getStartdatetime() == ann_f.getStartdatetime()
start_datetime = psg_f.getStartdatetime()
logger.info("Start datetime: {}".format(str(start_datetime)))
file_duration = psg_f.getFileDuration()
logger.info("File duration: {} sec".format(file_duration))
epoch_duration = psg_f.datarecord_duration
if psg_f.datarecord_duration == 60: # Fix problems of SC4362F0-PSG.edf, SC4362FC-Hypnogram.edf
epoch_duration = epoch_duration / 2
logger.info("Epoch duration: {} sec (changed from 60 sec)".format(epoch_duration))
else:
logger.info("Epoch duration: {} sec".format(epoch_duration))
# Extract signal from the selected channel
ch_names = psg_f.getSignalLabels()
ch_samples = psg_f.getNSamples()
select_ch_idx = -1
for s in range(psg_f.signals_in_file):
if ch_names[s] == select_ch:
select_ch_idx = s
break
if select_ch_idx == -1:
raise Exception("Channel not found.")
sampling_rate = psg_f.getSampleFrequency(select_ch_idx)
n_epoch_samples = int(epoch_duration * sampling_rate)
signals = psg_f.readSignal(select_ch_idx).reshape(-1, n_epoch_samples)
logger.info("Select channel: {}".format(select_ch))
logger.info("Select channel samples: {}".format(ch_samples[select_ch_idx]))
logger.info("Sample rate: {}".format(sampling_rate))
# Sanity check
n_epochs = psg_f.datarecords_in_file
if psg_f.datarecord_duration == 60: # Fix problems of SC4362F0-PSG.edf, SC4362FC-Hypnogram.edf
n_epochs = n_epochs * 2
assert len(signals) == n_epochs, f"signal: {signals.shape} != {n_epochs}"
# Generate labels from onset and duration annotation
labels = []
total_duration = 0
ann_onsets, ann_durations, ann_stages = ann_f.readAnnotations()
for a in range(len(ann_stages)):
onset_sec = int(ann_onsets[a])
duration_sec = int(ann_durations[a])
ann_str = "".join(ann_stages[a])
# Sanity check
assert onset_sec == total_duration
# Get label value
label = ann2label[ann_str]
# Compute # of epoch for this stage
if duration_sec % epoch_duration != 0:
logger.info(f"Something wrong: {duration_sec} {epoch_duration}")
raise Exception(f"Something wrong: {duration_sec} {epoch_duration}")
duration_epoch = int(duration_sec / epoch_duration)
# Generate sleep stage labels
label_epoch = np.ones(duration_epoch, dtype=np.int) * label
labels.append(label_epoch)
total_duration += duration_sec
logger.info("Include onset:{}, duration:{}, label:{} ({})".format(
onset_sec, duration_sec, label, ann_str
))
labels = np.hstack(labels)
# Remove annotations that are longer than the recorded signals
labels = labels[:len(signals)]
# Get epochs and their corresponding labels
x = signals.astype(np.float32)
y = labels.astype(np.int32)
# Select only sleep periods
w_edge_mins = 30
nw_idx = np.where(y != stage_dict["W"])[0]
start_idx = nw_idx[0] - (w_edge_mins * 2)
end_idx = nw_idx[-1] + (w_edge_mins * 2)
if start_idx < 0: start_idx = 0
if end_idx >= len(y): end_idx = len(y) - 1
select_idx = np.arange(start_idx, end_idx+1)
logger.info("Data before selection: {}, {}".format(x.shape, y.shape))
x = x[select_idx]
y = y[select_idx]
logger.info("Data after selection: {}, {}".format(x.shape, y.shape))
# Remove movement and unknown
move_idx = np.where(y == stage_dict["MOVE"])[0]
unk_idx = np.where(y == stage_dict["UNK"])[0]
if len(move_idx) > 0 or len(unk_idx) > 0:
remove_idx = np.union1d(move_idx, unk_idx)
logger.info("Remove irrelavant stages")
logger.info(" Movement: ({}) {}".format(len(move_idx), move_idx))
logger.info(" Unknown: ({}) {}".format(len(unk_idx), unk_idx))
logger.info(" Remove: ({}) {}".format(len(remove_idx), remove_idx))
logger.info(" Data before removal: {}, {}".format(x.shape, y.shape))
select_idx = np.setdiff1d(np.arange(len(x)), remove_idx)
x = x[select_idx]
y = y[select_idx]
logger.info(" Data after removal: {}, {}".format(x.shape, y.shape))
# Save
filename = ntpath.basename(psg_fnames[i]).replace("-PSG.edf", ".npz")
save_dict = {
"x": x,
"y": y,
"fs": sampling_rate,
"ch_label": select_ch,
"start_datetime": start_datetime,
"file_duration": file_duration,
"epoch_duration": epoch_duration,
"n_all_epochs": n_epochs,
"n_epochs": len(x),
}
np.savez(os.path.join(args.output_dir, filename), **save_dict)
logger.info("\n=======================================\n")
if __name__ == "__main__":
main()