Skip to content

Commit 51fd0fc

Browse files
authored
Correct near-start peak guesses regardless of input order (#680)
`correct_peaks()` can return a negative index for a valid near-start guess when that guess follows a later one in the input array. The boundary-adjustment loop stops at the first guess outside the near-start window, leaving subsequent early guesses unadjusted. For a length-30 signal with maxima at samples 0 and 20: ```python sig = np.zeros(30) sig[[0, 20]] = 5 processing.correct_peaks(sig, np.array([20, 2]), 5, 9, "up") # before: array([20, -3]); after: array([20, 0]) ``` Skip individual later guesses instead of ending the boundary-adjustment loop. Preserve input order and duplicates. This extends the near-start correction from #613 to unsorted inputs without changing sorted-input behavior. Tests cover all four direction options, duplicate guesses, sorted controls, unchanged input, and a written/read digital WFDB record passed to the public API. The 17 added cases produce 13 failures and 4 passing controls on the unchanged code. Validation on current main 05db3a2 plus this patch: - `tests/test_processing.py`: 151 passed on Python 3.12.16. - Ruff 0.17.3 check and format passed for the changed Python files. - Strict Sphinx HTML build with `-W --keep-going` passed. Earlier same-file validation: 113 peak tests passed on Python 3.10.12; 4,000 seeded permutation checks and 4,000 sorted baseline/fixed equivalence checks passed. The full repository suite and remote CI have not been verified. These tests check index correction, not clinical detection accuracy. AI assistance was used to develop and test this change. Independent review has not been completed.
2 parents 05db3a2 + fb7c5de commit 51fd0fc

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
**Correct unsorted peak guesses near the signal start**
2+
``correct_peaks`` now applies its boundary adjustment to every near-start
3+
guess, including those listed after a later guess, while preserving input
4+
order.

‎src/wfdb/processing/peaks.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def shift_peaks(sig, peak_inds, search_radius, peak_up):
294294
for i in range(n_peaks):
295295
ind = peak_inds[i]
296296
if ind >= search_radius:
297-
break
297+
continue
298298
# The window starts at 0 rather than ind - search_radius
299299
shift_inds[i] += search_radius - ind
300300

‎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)