-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataGeneration.py
More file actions
207 lines (202 loc) · 8.43 KB
/
Copy pathDataGeneration.py
File metadata and controls
207 lines (202 loc) · 8.43 KB
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
import os
import random
import argparse
import itertools
import wfdb
import heapq
import numpy as np
import pandas as pd
from wfdb import processing
from tqdm import tqdm
from scipy.stats import pearsonr
parser = argparse.ArgumentParser("Authentication", add_help=False)
parser.add_argument(
"--data_path",
default="../storage/ssd/public/guoxin/physionet.org/files/ptbdb/1.0.0/",
type=str,
help="dataset path",
)
parser.add_argument(
"--prefix",
default="patient",
type=str,
help="dataset prefix",
)
parser.add_argument(
"--output_path",
default="PTB_dataset.csv",
type=str,
help="output path",
)
args = parser.parse_args()
def dataGeneration(data_path, csv_path, record_path):
# initialize dataset
dataset = pd.DataFrame(columns=["label", "record"])
if record_path == None:
# a loop for each patient
detail_path = data_path + "/"
record_files = [
i.split(".")[0]
for i in os.listdir(detail_path)
if (not i.startswith(".") and i.endswith(".hea"))
]
# a loop for each record
for record_name in tqdm(record_files, desc="Processing"):
# load record
signal, info = wfdb.rdsamp(detail_path + record_name)
fs = 200
signal = processing.resample_sig(signal[:, 0], info["fs"], fs)[0]
# set some parameters
window_size_half = int(fs * 0.125 / 2)
max_bpm = 230
# detect QRS peaks
qrs_inds = processing.gqrs_detect(signal, fs=fs)
search_radius = int(fs * 60 / max_bpm)
corrected_qrs_inds = processing.correct_peaks(
signal,
peak_inds=qrs_inds,
search_radius=search_radius,
smooth_window_size=150,
)
average_qrs = 0
count = 0
for i in range(1, len(corrected_qrs_inds) - 1):
start_ind = corrected_qrs_inds[i] - window_size_half
end_ind = corrected_qrs_inds[i] + window_size_half + 1
if (
start_ind < corrected_qrs_inds[i - 1]
or end_ind > corrected_qrs_inds[i + 1]
):
continue
average_qrs = average_qrs + signal[start_ind:end_ind]
count = count + 1
# remove outliers
if count < 8:
print("\noutlier detected, discard " + record_name)
continue
average_qrs = average_qrs / count
corrcoefs = []
for i in range(1, len(corrected_qrs_inds) - 1):
start_ind = corrected_qrs_inds[i] - window_size_half
end_ind = corrected_qrs_inds[i] + window_size_half + 1
if (
start_ind < corrected_qrs_inds[i - 1]
or end_ind > corrected_qrs_inds[i + 1]
):
corrcoefs.append(-100)
continue
corrcoef = pearsonr(signal[start_ind:end_ind], average_qrs)[0]
corrcoefs.append(corrcoef)
max_corr = list(map(corrcoefs.index, heapq.nlargest(8, corrcoefs)))
index_corr = random.sample(list(itertools.permutations(max_corr, 8)), 100)
for index in index_corr:
# a temp dataframe to store one record
record_temp = pd.DataFrame()
signal_temp = []
for i in index:
start_ind = corrected_qrs_inds[i + 1] - window_size_half
end_ind = corrected_qrs_inds[i + 1] + window_size_half + 1
sig = processing.normalize_bound(signal[start_ind:end_ind], -1, 1)
signal_temp = np.concatenate((signal_temp, sig))
record_temp = record_temp._append(
pd.DataFrame(signal_temp.reshape(-1, signal_temp.shape[0])),
ignore_index=True,
sort=False,
)
record_temp["label"] = record_name
record_temp["record"] = record_name
# add it to final dataset
dataset = dataset._append(record_temp, ignore_index=True, sort=False)
else:
patient_folders = [
i
for i in os.listdir(data_path)
if (not i.startswith(".") and i.startswith(record_path))
]
# a loop for each patient
for patient_name in tqdm(patient_folders, desc="Processing"):
detail_path = data_path + patient_name + "/"
record_files = [
i.split(".")[0] for i in os.listdir(detail_path) if i.endswith(".hea")
]
# a loop for each record
for record_name in record_files:
# load record
signal, info = wfdb.rdsamp(detail_path + record_name)
fs = 200
signal = processing.resample_sig(signal[:, 0], info["fs"], fs)[0]
# set some parameters
window_size_half = int(fs * 0.125 / 2)
max_bpm = 230
# detect QRS peaks
qrs_inds = processing.gqrs_detect(signal, fs=fs)
search_radius = int(fs * 60 / max_bpm)
corrected_qrs_inds = processing.correct_peaks(
signal,
peak_inds=qrs_inds,
search_radius=search_radius,
smooth_window_size=150,
)
average_qrs = 0
count = 0
for i in range(1, len(corrected_qrs_inds) - 1):
start_ind = corrected_qrs_inds[i] - window_size_half
end_ind = corrected_qrs_inds[i] + window_size_half + 1
if (
start_ind < corrected_qrs_inds[i - 1]
or end_ind > corrected_qrs_inds[i + 1]
):
continue
average_qrs = average_qrs + signal[start_ind:end_ind]
count = count + 1
# remove outliers
if count < 8:
print(
"\noutlier detected, discard "
+ record_name
+ " of "
+ patient_name
)
continue
average_qrs = average_qrs / count
corrcoefs = []
for i in range(1, len(corrected_qrs_inds) - 1):
start_ind = corrected_qrs_inds[i] - window_size_half
end_ind = corrected_qrs_inds[i] + window_size_half + 1
if (
start_ind < corrected_qrs_inds[i - 1]
or end_ind > corrected_qrs_inds[i + 1]
):
corrcoefs.append(-100)
continue
corrcoef = pearsonr(signal[start_ind:end_ind], average_qrs)[0]
corrcoefs.append(corrcoef)
max_corr = list(map(corrcoefs.index, heapq.nlargest(8, corrcoefs)))
index_corr = random.sample(
list(itertools.permutations(max_corr, 8)), 100
)
for index in index_corr:
# a temp dataframe to store one record
record_temp = pd.DataFrame()
signal_temp = []
for i in index:
start_ind = corrected_qrs_inds[i + 1] - window_size_half
end_ind = corrected_qrs_inds[i + 1] + window_size_half + 1
sig = processing.normalize_bound(
signal[start_ind:end_ind], -1, 1
)
signal_temp = np.concatenate((signal_temp, sig))
record_temp = record_temp._append(
pd.DataFrame(signal_temp.reshape(-1, signal_temp.shape[0])),
ignore_index=True,
sort=False,
)
record_temp["label"] = patient_name
record_temp["record"] = record_name
# add it to final dataset
dataset = dataset._append(record_temp, ignore_index=True, sort=False)
# save for further use
dataset.to_csv(csv_path, index=False)
print("processing completed")
if __name__ == "__main__":
dataGeneration(args.data_path, args.output_path, args.prefix)