forked from c-labpl/qrs_detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QRSDetectorOffline.py
314 lines (246 loc) · 14.5 KB
/
QRSDetectorOffline.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
import numpy as np
import matplotlib.pyplot as plt
from time import gmtime, strftime
from scipy.signal import butter, lfilter
LOG_DIR = "logs/"
PLOT_DIR = "plots/"
class QRSDetectorOffline(object):
"""
Python Offline ECG QRS Detector based on the Pan-Tomkins algorithm.
Michał Sznajder (Jagiellonian University) - technical contact ([email protected])
Marta Łukowska (Jagiellonian University)
The module is offline Python implementation of QRS complex detection in the ECG signal based
on the Pan-Tomkins algorithm: Pan J, Tompkins W.J., A real-time QRS detection algorithm,
IEEE Transactions on Biomedical Engineering, Vol. BME-32, No. 3, March 1985, pp. 230-236.
The QRS complex corresponds to the depolarization of the right and left ventricles of the human heart. It is the most visually obvious part of the ECG signal. QRS complex detection is essential for time-domain ECG signal analyses, namely heart rate variability. It makes it possible to compute inter-beat interval (RR interval) values that correspond to the time between two consecutive R peaks. Thus, a QRS complex detector is an ECG-based heart contraction detector.
Offline version detects QRS complexes in a pre-recorded ECG signal dataset (e.g. stored in .csv format).
This implementation of a QRS Complex Detector is by no means a certified medical tool and should not be used in health monitoring. It was created and used for experimental purposes in psychophysiology and psychology.
You can find more information in module documentation:
https://github.com/c-labpl/qrs_detector
If you use these modules in a research project, please consider citing it:
https://zenodo.org/record/583770
If you use these modules in any other project, please refer to MIT open-source license.
MIT License
Copyright (c) 2017 Michał Sznajder, Marta Łukowska
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
def __init__(self, ecg_data_path, verbose=True, log_data=False, plot_data=False, show_plot=False):
"""
QRSDetectorOffline class initialisation method.
:param string ecg_data_path: path to the ECG dataset
:param bool verbose: flag for printing the results
:param bool log_data: flag for logging the results
:param bool plot_data: flag for plotting the results to a file
:param bool show_plot: flag for showing generated results plot - will not show anything if plot is not generated
"""
# Configuration parameters.
self.ecg_data_path = ecg_data_path
self.signal_frequency = 250 # Set ECG device frequency in samples per second here.
self.filter_lowcut = 0.0
self.filter_highcut = 15.0
self.filter_order = 1
self.integration_window = 15 # Change proportionally when adjusting frequency (in samples).
self.findpeaks_limit = 0.35
self.findpeaks_spacing = 50 # Change proportionally when adjusting frequency (in samples).
self.refractory_period = 120 # Change proportionally when adjusting frequency (in samples).
self.qrs_peak_filtering_factor = 0.125
self.noise_peak_filtering_factor = 0.125
self.qrs_noise_diff_weight = 0.25
# Loaded ECG data.
self.ecg_data_raw = None
# Measured and calculated values.
self.filtered_ecg_measurements = None
self.differentiated_ecg_measurements = None
self.squared_ecg_measurements = None
self.integrated_ecg_measurements = None
self.detected_peaks_indices = None
self.detected_peaks_values = None
self.qrs_peak_value = 0.0
self.noise_peak_value = 0.0
self.threshold_value = 0.0
# Detection results.
self.qrs_peaks_indices = np.array([], dtype=int)
self.noise_peaks_indices = np.array([], dtype=int)
# Final ECG data and QRS detection results array - samples with detected QRS are marked with 1 value.
self.ecg_data_detected = None
# Run whole detector flow.
self.load_ecg_data()
self.detect_peaks()
self.detect_qrs()
if verbose:
self.print_detection_data()
if log_data:
self.log_path = "{:s}QRS_offline_detector_log_{:s}.csv".format(LOG_DIR,
strftime("%Y_%m_%d_%H_%M_%S", gmtime()))
self.log_detection_data()
if plot_data:
self.plot_path = "{:s}QRS_offline_detector_plot_{:s}.png".format(PLOT_DIR,
strftime("%Y_%m_%d_%H_%M_%S", gmtime()))
self.plot_detection_data(show_plot=show_plot)
"""Loading ECG measurements data methods."""
def load_ecg_data(self):
"""
Method loading ECG data set from a file.
"""
self.ecg_data_raw = np.loadtxt(self.ecg_data_path, skiprows=1, delimiter=',')
"""ECG measurements data processing methods."""
def detect_peaks(self):
"""
Method responsible for extracting peaks from loaded ECG measurements data through measurements processing.
"""
# Extract measurements from loaded ECG data.
ecg_measurements = self.ecg_data_raw[:, 1]
# Measurements filtering - 0-15 Hz band pass filter.
self.filtered_ecg_measurements = self.bandpass_filter(ecg_measurements, lowcut=self.filter_lowcut,
highcut=self.filter_highcut, signal_freq=self.signal_frequency,
filter_order=self.filter_order)
self.filtered_ecg_measurements[:5] = self.filtered_ecg_measurements[5]
# Derivative - provides QRS slope information.
self.differentiated_ecg_measurements = np.ediff1d(self.filtered_ecg_measurements)
# Squaring - intensifies values received in derivative.
self.squared_ecg_measurements = self.differentiated_ecg_measurements ** 2
# Moving-window integration.
self.integrated_ecg_measurements = np.convolve(self.squared_ecg_measurements, np.ones(self.integration_window))
# Fiducial mark - peak detection on integrated measurements.
self.detected_peaks_indices = self.findpeaks(data=self.integrated_ecg_measurements,
limit=self.findpeaks_limit,
spacing=self.findpeaks_spacing)
self.detected_peaks_values = self.integrated_ecg_measurements[self.detected_peaks_indices]
"""QRS detection methods."""
def detect_qrs(self):
"""
Method responsible for classifying detected ECG measurements peaks either as noise or as QRS complex (heart beat).
"""
for detected_peak_index, detected_peaks_value in zip(self.detected_peaks_indices, self.detected_peaks_values):
try:
last_qrs_index = self.qrs_peaks_indices[-1]
except IndexError:
last_qrs_index = 0
# After a valid QRS complex detection, there is a 200 ms refractory period before next one can be detected.
if detected_peak_index - last_qrs_index > self.refractory_period or not self.qrs_peaks_indices.size:
# Peak must be classified either as a noise peak or a QRS peak.
# To be classified as a QRS peak it must exceed dynamically set threshold value.
if detected_peaks_value > self.threshold_value:
self.qrs_peaks_indices = np.append(self.qrs_peaks_indices, detected_peak_index)
# Adjust QRS peak value used later for setting QRS-noise threshold.
self.qrs_peak_value = self.qrs_peak_filtering_factor * detected_peaks_value + \
(1 - self.qrs_peak_filtering_factor) * self.qrs_peak_value
else:
self.noise_peaks_indices = np.append(self.noise_peaks_indices, detected_peak_index)
# Adjust noise peak value used later for setting QRS-noise threshold.
self.noise_peak_value = self.noise_peak_filtering_factor * detected_peaks_value + \
(1 - self.noise_peak_filtering_factor) * self.noise_peak_value
# Adjust QRS-noise threshold value based on previously detected QRS or noise peaks value.
self.threshold_value = self.noise_peak_value + \
self.qrs_noise_diff_weight * (self.qrs_peak_value - self.noise_peak_value)
# Create array containing both input ECG measurements data and QRS detection indication column.
# We mark QRS detection with '1' flag in 'qrs_detected' log column ('0' otherwise).
measurement_qrs_detection_flag = np.zeros([len(self.ecg_data_raw[:, 1]), 1])
measurement_qrs_detection_flag[self.qrs_peaks_indices] = 1
self.ecg_data_detected = np.append(self.ecg_data_raw, measurement_qrs_detection_flag, 1)
"""Results reporting methods."""
def print_detection_data(self):
"""
Method responsible for printing the results.
"""
print("qrs peaks indices")
print(self.qrs_peaks_indices)
print("noise peaks indices")
print(self.noise_peaks_indices)
def log_detection_data(self):
"""
Method responsible for logging measured ECG and detection results to a file.
"""
with open(self.log_path, "wb") as fin:
fin.write(b"timestamp,ecg_measurement,qrs_detected\n")
np.savetxt(fin, self.ecg_data_detected, delimiter=",")
def plot_detection_data(self, show_plot=False):
"""
Method responsible for plotting detection results.
:param bool show_plot: flag for plotting the results and showing plot
"""
def plot_data(axis, data, title='', fontsize=10):
axis.set_title(title, fontsize=fontsize)
axis.grid(which='both', axis='both', linestyle='--')
axis.plot(data, color="salmon", zorder=1)
def plot_points(axis, values, indices):
axis.scatter(x=indices, y=values[indices], c="black", s=50, zorder=2)
plt.close('all')
fig, axarr = plt.subplots(6, sharex=True, figsize=(15, 18))
plot_data(axis=axarr[0], data=self.ecg_data_raw[:, 1], title='Raw ECG measurements')
plot_data(axis=axarr[1], data=self.filtered_ecg_measurements, title='Filtered ECG measurements')
plot_data(axis=axarr[2], data=self.differentiated_ecg_measurements, title='Differentiated ECG measurements')
plot_data(axis=axarr[3], data=self.squared_ecg_measurements, title='Squared ECG measurements')
plot_data(axis=axarr[4], data=self.integrated_ecg_measurements, title='Integrated ECG measurements with QRS peaks marked (black)')
plot_points(axis=axarr[4], values=self.integrated_ecg_measurements, indices=self.qrs_peaks_indices)
plot_data(axis=axarr[5], data=self.ecg_data_detected[:, 1], title='Raw ECG measurements with QRS peaks marked (black)')
plot_points(axis=axarr[5], values=self.ecg_data_detected[:, 1], indices=self.qrs_peaks_indices)
plt.tight_layout()
fig.savefig(self.plot_path)
if show_plot:
plt.show()
plt.close()
"""Tools methods."""
def bandpass_filter(self, data, lowcut, highcut, signal_freq, filter_order):
"""
Method responsible for creating and applying Butterworth filter.
:param deque data: raw data
:param float lowcut: filter lowcut frequency value
:param float highcut: filter highcut frequency value
:param int signal_freq: signal frequency in samples per second (Hz)
:param int filter_order: filter order
:return array: filtered data
"""
nyquist_freq = 0.5 * signal_freq
low = lowcut / nyquist_freq
high = highcut / nyquist_freq
b, a = butter(filter_order, [low, high], btype="band")
y = lfilter(b, a, data)
return y
def findpeaks(self, data, spacing=1, limit=None):
"""
Janko Slavic peak detection algorithm and implementation.
https://github.com/jankoslavic/py-tools/tree/master/findpeaks
Finds peaks in `data` which are of `spacing` width and >=`limit`.
:param ndarray data: data
:param float spacing: minimum spacing to the next peak (should be 1 or more)
:param float limit: peaks should have value greater or equal
:return array: detected peaks indexes array
"""
len = data.size
x = np.zeros(len + 2 * spacing)
x[:spacing] = data[0] - 1.e-6
x[-spacing:] = data[-1] - 1.e-6
x[spacing:spacing + len] = data
peak_candidate = np.zeros(len)
peak_candidate[:] = True
for s in range(spacing):
start = spacing - s - 1
h_b = x[start: start + len] # before
start = spacing
h_c = x[start: start + len] # central
start = spacing + s + 1
h_a = x[start: start + len] # after
peak_candidate = np.logical_and(peak_candidate, np.logical_and(h_c > h_b, h_c > h_a))
ind = np.argwhere(peak_candidate)
ind = ind.reshape(ind.size)
if limit is not None:
ind = ind[data[ind] > limit]
return ind
if __name__ == "__main__":
qrs_detector = QRSDetectorOffline(ecg_data_path="ecg_data/ecg_data_1.csv", verbose=True,
log_data=True, plot_data=True, show_plot=False)