diff --git a/docs/changes.rst b/docs/changes.rst index 8c63c074..03cbb1e8 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -38,6 +38,9 @@ Unreleased **Use Ruff for formatting and linting** The ``dev`` extra now includes ``ruff`` instead of ``black`` and ``pylint``, and the code is formatted with a line length of 88 (the default). +**Fix search window in correct_peaks** + ``correct_peaks`` now searches ``search_radius`` samples on both sides of each peak, including the last sample of the signal. It used to stop one sample short on the right, so a peak exactly ``search_radius`` samples after the original index, or on the last sample, could not be found. + **Fix resample_sig for signals starting with NaN** ``resample_sig`` (and the functions that use it, such as ``resample_singlechan`` and ``resample_multichan``) returned only NaN values if the signal started with NaN. Leading NaN values now take the first valid value before resampling, as trailing ones already took the last. diff --git a/src/wfdb/processing/peaks.py b/src/wfdb/processing/peaks.py index 7c618d91..6dcc9ed6 100644 --- a/src/wfdb/processing/peaks.py +++ b/src/wfdb/processing/peaks.py @@ -225,7 +225,7 @@ def shift_peaks(sig, peak_inds, search_radius, peak_up): for i in range(n_peaks): ind = peak_inds[i] local_sig = sig[ - max(0, ind - search_radius) : min(ind + search_radius, sig_len - 1) + max(0, ind - search_radius) : min(ind + search_radius + 1, sig_len) ] if peak_up: diff --git a/tests/test_processing.py b/tests/test_processing.py index 60bb7bd0..1e5943b1 100644 --- a/tests/test_processing.py +++ b/tests/test_processing.py @@ -195,6 +195,21 @@ def test_correct_peaks(self): assert np.array_equal(yz, expected_peaks) + def test_correct_peaks_window_edges(self): + """Peaks at the edge of the search radius or the signal are found.""" + sig = np.zeros(30) + sig[13] = 5 + sig[29] = 5 + + # Peak exactly search_radius samples after the guess + assert processing.correct_peaks( + sig, np.array([10]), search_radius=3, smooth_window_size=9, peak_dir="up" + ).tolist() == [13] + # Peak on the last sample of the signal + assert processing.correct_peaks( + sig, np.array([27]), search_radius=5, smooth_window_size=9, peak_dir="up" + ).tolist() == [29] + class TestComputeHr: """Tests for compute_hr."""