From bc03eae799fe3988e9904e7331c3a9c979356a0f Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:10:17 +0530 Subject: [PATCH 1/3] Correct every near-start peak regardless of input order --- src/wfdb/processing/peaks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wfdb/processing/peaks.py b/src/wfdb/processing/peaks.py index f71cc27f..bcf4d6be 100644 --- a/src/wfdb/processing/peaks.py +++ b/src/wfdb/processing/peaks.py @@ -294,7 +294,7 @@ def shift_peaks(sig, peak_inds, search_radius, peak_up): for i in range(n_peaks): ind = peak_inds[i] if ind >= search_radius: - break + continue # The window starts at 0 rather than ind - search_radius shift_inds[i] += search_radius - ind From aabe2bdd7232e626e6712266461de7df13c7fa63 Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:10:45 +0530 Subject: [PATCH 2/3] Test correction of unsorted and duplicate peak guesses --- tests/test_processing.py | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/test_processing.py b/tests/test_processing.py index 84e40ab1..e9d02214 100644 --- a/tests/test_processing.py +++ b/tests/test_processing.py @@ -472,6 +472,50 @@ def test_correct_peaks_near_start(self, peak, peak_dir): sig, np.array([2]), search_radius=5, smooth_window_size=9, peak_dir=peak_dir ).tolist() == [peak] + @pytest.mark.parametrize("peak_dir", ["up", "down", "both", "compare"]) + @pytest.mark.parametrize("guesses", [[20, 2], [20, 2, 20], [2, 20, 2], [2, 20]]) + def test_correct_peaks_preserves_input_order(self, peak_dir, guesses): + """Unsorted guesses are corrected independently, including near zero.""" + sig = np.zeros(30) + sig[[0, 20]] = -5 if peak_dir == "down" else 5 + result = processing.correct_peaks( + sig, + np.array(guesses), + search_radius=5, + smooth_window_size=9, + peak_dir=peak_dir, + ) + expected = [0 if guess == 2 else 20 for guess in guesses] + np.testing.assert_array_equal(result, expected) + + def test_correct_peaks_unsorted_digital_record(self, tmp_path): + digital = np.zeros((30, 1), dtype=np.int16) + digital[[0, 20], 0] = 500 + wfdb.wrsamp( + "unsorted_peaks", + fs=250, + units=["mV"], + sig_name=["ECG"], + d_signal=digital, + fmt=["16"], + adc_gain=[200], + baseline=[0], + write_dir=str(tmp_path), + ) + record = wfdb.rdrecord( + str(tmp_path / "unsorted_peaks"), physical=False, return_res=16 + ) + guesses = np.array([20, 2]) + result = processing.correct_peaks( + record.d_signal[:, 0], + guesses, + search_radius=5, + smooth_window_size=9, + peak_dir="up", + ) + np.testing.assert_array_equal(result, [20, 0]) + np.testing.assert_array_equal(guesses, [20, 2]) + class TestComputeHr: """Tests for compute_hr.""" From fb7c5dec565fbf98c731714f3c68418348af3316 Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:11:04 +0530 Subject: [PATCH 3/3] Document correction of unsorted peak guesses --- changelog.d/correct-peaks-unsorted.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog.d/correct-peaks-unsorted.rst diff --git a/changelog.d/correct-peaks-unsorted.rst b/changelog.d/correct-peaks-unsorted.rst new file mode 100644 index 00000000..459dd700 --- /dev/null +++ b/changelog.d/correct-peaks-unsorted.rst @@ -0,0 +1,4 @@ +**Correct unsorted peak guesses near the signal start** + ``correct_peaks`` now applies its boundary adjustment to every near-start + guess, including those listed after a later guess, while preserving input + order.