Skip to content

Commit aabe2bd

Browse files
authored
Test correction of unsorted and duplicate peak guesses
1 parent bc03eae commit aabe2bd

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

‎tests/test_processing.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,50 @@ def test_correct_peaks_near_start(self, peak, peak_dir):
472472
sig, np.array([2]), search_radius=5, smooth_window_size=9, peak_dir=peak_dir
473473
).tolist() == [peak]
474474

475+
@pytest.mark.parametrize("peak_dir", ["up", "down", "both", "compare"])
476+
@pytest.mark.parametrize("guesses", [[20, 2], [20, 2, 20], [2, 20, 2], [2, 20]])
477+
def test_correct_peaks_preserves_input_order(self, peak_dir, guesses):
478+
"""Unsorted guesses are corrected independently, including near zero."""
479+
sig = np.zeros(30)
480+
sig[[0, 20]] = -5 if peak_dir == "down" else 5
481+
result = processing.correct_peaks(
482+
sig,
483+
np.array(guesses),
484+
search_radius=5,
485+
smooth_window_size=9,
486+
peak_dir=peak_dir,
487+
)
488+
expected = [0 if guess == 2 else 20 for guess in guesses]
489+
np.testing.assert_array_equal(result, expected)
490+
491+
def test_correct_peaks_unsorted_digital_record(self, tmp_path):
492+
digital = np.zeros((30, 1), dtype=np.int16)
493+
digital[[0, 20], 0] = 500
494+
wfdb.wrsamp(
495+
"unsorted_peaks",
496+
fs=250,
497+
units=["mV"],
498+
sig_name=["ECG"],
499+
d_signal=digital,
500+
fmt=["16"],
501+
adc_gain=[200],
502+
baseline=[0],
503+
write_dir=str(tmp_path),
504+
)
505+
record = wfdb.rdrecord(
506+
str(tmp_path / "unsorted_peaks"), physical=False, return_res=16
507+
)
508+
guesses = np.array([20, 2])
509+
result = processing.correct_peaks(
510+
record.d_signal[:, 0],
511+
guesses,
512+
search_radius=5,
513+
smooth_window_size=9,
514+
peak_dir="up",
515+
)
516+
np.testing.assert_array_equal(result, [20, 0])
517+
np.testing.assert_array_equal(guesses, [20, 2])
518+
475519

476520
class TestComputeHr:
477521
"""Tests for compute_hr."""

0 commit comments

Comments
 (0)