Skip to content

Commit 91ca89c

Browse files
committed
Add documentation and heart rate computing function.
1 parent dfc5b08 commit 91ca89c

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

README.rst

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ Example Usage:
576576
tf = 20000
577577
sig, fields = wfdb.srdsamp('sampledata/100', sampfrom=t0, sampto=tf, channels=[0])
578578
record = wfdb.rdsamp("sampledata/100", sampfrom=t0, sampto=tf, channels=[0], physical=False)
579-
peak_indexes = wfdb.processing.gqrs_detect(x=sig[:,0], freq=fields['fs'], gain=record.adcgain[0], adczero=record.adczero[0], threshold=1.0)
579+
peaks_indexes = wfdb.processing.gqrs_detect(x=sig[:,0], frequency=fields['fs'], adcgain=record.adcgain[0], adczero=record.adczero[0], threshold=1.0)
580580

581581
Input arguments:
582582

@@ -596,6 +596,10 @@ Input arguments:
596596
- ``QRSa`` (default=750): Typical QRS peak-to-peak amplitude, in microvolts.
597597
- ``QRSamin`` (default=130): Minimum QRS peak-to-peak amplitude, in microvolts.
598598

599+
Output Arguments:
600+
601+
- ``peaks_indexes``: A python list containing the peaks indexes.
602+
599603

600604
**correct_peaks** - A post-processing algorithm to correct peaks position.
601605

@@ -621,7 +625,7 @@ Example Usage:
621625
max_bpm = 350
622626
min_gap = fs*60/min_bpm
623627
max_gap = fs*60/max_bpm
624-
y_idxs = wfdb.processing.correct_peaks(x=sig[:,0], peak_indexes=peak_indexes, min_gap=min_gap, max_gap=max_gap, smooth_window=150)
628+
new_indexes = wfdb.processing.correct_peaks(x=sig[:,0], peak_indexes=peak_indexes, min_gap=min_gap, max_gap=max_gap, smooth_window=150)
625629

626630
Input arguments:
627631
peaks_indexes, min_gap, max_gap, smooth_window
@@ -631,6 +635,45 @@ peaks_indexes, min_gap, max_gap, smooth_window
631635
- ``max_gap`` (required): The maximum gap in samples between two peaks.
632636
- ``smooth_window`` (required): The size of the smoothing window.
633637

638+
Output Arguments:
639+
640+
- ``new_indexes``: A python list containing the new peaks indexes.
641+
642+
643+
Heart rate
644+
~~~~~~~~~~~~~~
645+
646+
**compute_hr** - Compute heart rate from peak indexes and signal frequency.
647+
648+
::
649+
650+
compute_hr(length, peaks_indexes, fs)
651+
652+
Example Usage:
653+
654+
::
655+
656+
import wfdb
657+
t0 = 10000
658+
tf = 20000
659+
sig, fields = wfdb.srdsamp('sampledata/100', sampfrom=t0, sampto=tf, channels=[0])
660+
record = wfdb.rdsamp("sampledata/100", sampfrom=t0, sampto=tf, channels=[0], physical=False)
661+
peaks_indexes = wfdb.processing.gqrs_detect(x=sig[:,0], frequency=fields['fs'], adcgain=record.adcgain[0], adczero=record.adczero[0], threshold=1.0)
662+
hr = compute_hr(length=tf-t0, peaks_indexes=peaks_indexes, fs=fields['fs'])
663+
664+
Input arguments:
665+
666+
- ``length`` (required): The length of the corresponding signal.
667+
- ``peaks_indexes`` (required): The peak indexes.
668+
- ``fs`` (required): The signal frequency.
669+
670+
671+
Output Arguments:
672+
673+
- ``hr``: A numpy.array containing heart rate for each sample. Contains numpy.nan where heart rate could not be computed.
674+
675+
676+
634677
Based on the original WFDB software package specifications
635678
----------------------------------------------------------
636679

tests/test_processing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_6(self):
6161
peaks = wfdb.processing.gqrs_detect(x, frequency, adcgain, adczero, threshold=1.0)
6262
print(peaks)
6363
print(expecting)
64-
assert peaks == expecting
64+
assert numpy.array_equal(peaks, expecting)
6565

6666
def test_7(self):
6767
sig, fields = wfdb.srdsamp('sampledata/100', channels = [0, 1])

wfdb/processing/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .basic import resample_ann, resample_sig, resample_singlechan, resample_multichan, normalize
22
from .gqrs import gqrs_detect
3+
from .hr import compute_hr
34
from .peaks import find_peaks, correct_peaks

wfdb/processing/hr.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import numpy
2+
3+
4+
def compute_hr(length, peaks_indexes, fs):
5+
result = numpy.full(length, numpy.nan, dtype='float32')
6+
7+
if len(peaks_indexes) < 2:
8+
return result
9+
10+
current_hr = numpy.nan
11+
12+
for i in range(0, len(peaks_indexes)-2):
13+
a = peaks_indexes[i]
14+
b = peaks_indexes[i+1]
15+
c = peaks_indexes[i+2]
16+
RR = (b-a) * (1.0 / fs) * 1000
17+
hr = 60000.0 / RR
18+
result[b+1:c+1] = hr
19+
result[peaks_indexes[-1]:] = result[peaks_indexes[-1]]
20+
21+
return result

wfdb/processing/peaks.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,9 @@ def correct_peaks(x, peaks_indexes, min_gap, max_gap, smooth_window):
8282
rpeaks = numpy.zeros(N)
8383
rpeaks[peaks_indexes] = 1.0
8484

85-
# Multiple ones '1' side by side:
86-
# in order to prevent that, the following code computes the best peak
8785
rpeaks = rpeaks.astype('int32')
8886

89-
# 1- Extract ranges where we have one or many ones side by side (rpeaks locations predicted by NN)
87+
# 1- Extract ranges where we have one or many ones side by side
9088
rpeaks_ranges = []
9189
tmp_idx = 0
9290
for i in range(1, len(rpeaks)):
@@ -144,4 +142,4 @@ def correct_peaks(x, peaks_indexes, min_gap, max_gap, smooth_window):
144142
for v, _ in to_remove.items():
145143
rpeaks_indexes.remove(v)
146144

147-
return rpeaks_indexes
145+
return sorted(rpeaks_indexes)

0 commit comments

Comments
 (0)