forked from neuropsychology/NeuroKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_data.py
176 lines (128 loc) · 5.75 KB
/
make_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
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
import pandas as pd
import neurokit2 as nk
# Load ECGs
ecgs = ["../../data/gudb/ECGs.csv",
"../../data/mit_arrhythmia/ECGs.csv",
"../../data/mit_normal/ECGs.csv",
"../../data/ludb/ECGs.csv",
"../../data/fantasia/ECGs.csv"]
# Load True R-peaks location
rpeaks = [pd.read_csv("../../data/gudb/Rpeaks.csv"),
pd.read_csv("../../data/mit_arrhythmia/Rpeaks.csv"),
pd.read_csv("../../data/mit_normal/Rpeaks.csv"),
pd.read_csv("../../data/ludb/Rpeaks.csv"),
pd.read_csv("../../data/fantasia/Rpeaks.csv")]
# =============================================================================
# Study 1
# =============================================================================
def neurokit(ecg, sampling_rate):
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="neurokit")
return info["ECG_R_Peaks"]
def pantompkins1985(ecg, sampling_rate):
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="pantompkins1985")
return info["ECG_R_Peaks"]
def hamilton2002(ecg, sampling_rate):
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="hamilton2002")
return info["ECG_R_Peaks"]
def martinez2003(ecg, sampling_rate):
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="martinez2003")
return info["ECG_R_Peaks"]
def christov2004(ecg, sampling_rate):
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="christov2004")
return info["ECG_R_Peaks"]
def gamboa2008(ecg, sampling_rate):
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="gamboa2008")
return info["ECG_R_Peaks"]
def elgendi2010(ecg, sampling_rate):
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="elgendi2010")
return info["ECG_R_Peaks"]
def engzeemod2012(ecg, sampling_rate):
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="engzeemod2012")
return info["ECG_R_Peaks"]
def kalidas2017(ecg, sampling_rate):
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="kalidas2017")
return info["ECG_R_Peaks"]
def rodrigues2020(ecg, sampling_rate):
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="rodrigues2020")
return info["ECG_R_Peaks"]
results = []
for method in [neurokit, pantompkins1985, hamilton2002, martinez2003, christov2004,
gamboa2008, elgendi2010, engzeemod2012, kalidas2017, rodrigues2020]:
print(method.__name__)
for i in range(len(rpeaks)):
print(" - " + str(i))
data_ecg = pd.read_csv(ecgs[i])
result = nk.benchmark_ecg_preprocessing(method, data_ecg, rpeaks[i])
result["Method"] = method.__name__
results.append(result)
results = pd.concat(results).reset_index(drop=True)
results.to_csv("data_detectors.csv", index=False)
# =============================================================================
# Study 2
# =============================================================================
def none(ecg, sampling_rate):
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="neurokit")
return info["ECG_R_Peaks"]
def mean_removal(ecg, sampling_rate):
ecg = nk.signal_detrend(ecg, order=0)
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="neurokit")
return info["ECG_R_Peaks"]
def standardization(ecg, sampling_rate):
ecg = nk.standardize(ecg)
signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="neurokit")
return info["ECG_R_Peaks"]
results = []
for method in [none, mean_removal, standardization]:
print(method.__name__)
for i in range(len(rpeaks)):
print(" - " + str(i))
data_ecg = pd.read_csv(ecgs[i])
result = nk.benchmark_ecg_preprocessing(method, data_ecg, rpeaks[i])
result["Method"] = method.__name__
results.append(result)
results = pd.concat(results).reset_index(drop=True)
results.to_csv("data_normalization.csv", index=False)
# =============================================================================
# Study 3
# =============================================================================
#def none(ecg, sampling_rate):
# signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="neurokit")
# return info["ECG_R_Peaks"]
#
## Detrending-based
#def polylength(ecg, sampling_rate):
# length = len(ecg) / sampling_rate
# ecg = nk.signal_detrend(ecg, method="polynomial", order=int(length / 2))
# signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="neurokit")
# return info["ECG_R_Peaks"]
#
#def tarvainen(ecg, sampling_rate):
# ecg = nk.signal_detrend(ecg, method="tarvainen2002")
# signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="neurokit")
# return info["ECG_R_Peaks"]
#
#def locreg(ecg, sampling_rate):
# ecg = nk.signal_detrend(ecg,
# method="locreg",
# window=0.5*sampling_rate,
# stepsize=0.02*sampling_rate)
# signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="neurokit")
# return info["ECG_R_Peaks"]
#
#def rollingz(ecg, sampling_rate):
# ecg = nk.standardize(ecg, window=sampling_rate*2)
# signal, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="neurokit")
# return info["ECG_R_Peaks"]
#
#
#results = []
#for method in [none, polylength, tarvainen, locreg, rollingz]:
# print(method.__name__)
# for i in range(len(ecgs)):
# print(" - " + str(i))
# result = nk.benchmark_ecg_preprocessing(method, ecgs[i], rpeaks[i])
# result["Method"] = method.__name__
# results.append(result)
#results = pd.concat(results).reset_index(drop=True)
#
#results.to_csv("data_lowfreq.csv", index=False)