Skip to content

Commit 84f3579

Browse files
committed
Add documentation and heart rate computing function.
1 parent 3eca998 commit 84f3579

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
@@ -579,7 +579,7 @@ Example Usage:
579579
tf = 20000
580580
sig, fields = wfdb.srdsamp('sampledata/100', sampfrom=t0, sampto=tf, channels=[0])
581581
record = wfdb.rdsamp("sampledata/100", sampfrom=t0, sampto=tf, channels=[0], physical=False)
582-
peak_indexes = wfdb.processing.gqrs_detect(x=sig[:,0], freq=fields['fs'], gain=record.adcgain[0], adczero=record.adczero[0], threshold=1.0)
582+
peaks_indexes = wfdb.processing.gqrs_detect(x=sig[:,0], frequency=fields['fs'], adcgain=record.adcgain[0], adczero=record.adczero[0], threshold=1.0)
583583

584584
Input arguments:
585585

@@ -599,6 +599,10 @@ Input arguments:
599599
- ``QRSa`` (default=750): Typical QRS peak-to-peak amplitude, in microvolts.
600600
- ``QRSamin`` (default=130): Minimum QRS peak-to-peak amplitude, in microvolts.
601601

602+
Output Arguments:
603+
604+
- ``peaks_indexes``: A python list containing the peaks indexes.
605+
602606

603607
**correct_peaks** - A post-processing algorithm to correct peaks position.
604608

@@ -624,7 +628,7 @@ Example Usage:
624628
max_bpm = 350
625629
min_gap = fs*60/min_bpm
626630
max_gap = fs*60/max_bpm
627-
y_idxs = wfdb.processing.correct_peaks(x=sig[:,0], peak_indexes=peak_indexes, min_gap=min_gap, max_gap=max_gap, smooth_window=150)
631+
new_indexes = wfdb.processing.correct_peaks(x=sig[:,0], peak_indexes=peak_indexes, min_gap=min_gap, max_gap=max_gap, smooth_window=150)
628632

629633
Input arguments:
630634
peaks_indexes, min_gap, max_gap, smooth_window
@@ -634,6 +638,45 @@ peaks_indexes, min_gap, max_gap, smooth_window
634638
- ``max_gap`` (required): The maximum gap in samples between two peaks.
635639
- ``smooth_window`` (required): The size of the smoothing window.
636640

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

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)