diff --git a/changelog.d/correct-peaks-integer-indices.rst b/changelog.d/correct-peaks-integer-indices.rst new file mode 100644 index 00000000..c1fff1b0 --- /dev/null +++ b/changelog.d/correct-peaks-integer-indices.rst @@ -0,0 +1,4 @@ +**Handle unsigned and narrow integer guesses in peak correction** + ``processing.correct_peaks`` widens integer peak indices before window + arithmetic, avoiding wraparound near signal boundaries and keeping corrected + ``uint64`` indices usable for NumPy indexing. diff --git a/src/wfdb/processing/peaks.py b/src/wfdb/processing/peaks.py index 08e7b286..e1e4c13d 100644 --- a/src/wfdb/processing/peaks.py +++ b/src/wfdb/processing/peaks.py @@ -281,6 +281,20 @@ def shift_peaks(sig, peak_inds, search_radius, peak_up): Array of the corrected peak indices. """ + # Valid sample indices fit in np.intp. Widen before window arithmetic: + # unsigned subtraction wraps near zero, narrow integers can wrap at the + # right edge, and uint64 plus signed shifts can otherwise become float64. + peak_inds = np.asarray(peak_inds) + if peak_inds.dtype.kind in "iu": + limits = np.iinfo(np.intp) + if peak_inds.size and ( + np.any(peak_inds > limits.max) or np.any(peak_inds < limits.min) + ): + raise ValueError( + "peak_inds contains a value outside the platform index range" + ) + peak_inds = peak_inds.astype(np.intp, copy=False) + sig_len = sig.shape[0] n_peaks = len(peak_inds) # The indices to shift each peak ind by diff --git a/tests/test_correct_peaks_integer_indices.py b/tests/test_correct_peaks_integer_indices.py new file mode 100644 index 00000000..691f9299 --- /dev/null +++ b/tests/test_correct_peaks_integer_indices.py @@ -0,0 +1,109 @@ +import warnings + +import numpy as np +import pytest +from numpy.testing import assert_array_equal + +import wfdb +from wfdb import processing + + +@pytest.mark.parametrize( + "dtype", [np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16] +) +@pytest.mark.parametrize("direction", ["up", "down", "both", "compare"]) +def test_correct_peaks_integer_indices(dtype, direction): + signal = np.zeros(100) + signal[[1, 70]] = -5 if direction == "down" else 5 + guesses = np.array([72, 2, 2], dtype=dtype) + original = guesses.copy() + with warnings.catch_warnings(): + warnings.simplefilter("error", RuntimeWarning) + actual = processing.correct_peaks(signal, guesses, 5, 9, direction) + assert_array_equal(actual, [70, 1, 1]) + assert_array_equal(guesses, original) + assert actual.dtype.kind in "iu" + assert_array_equal(signal[actual], signal[[70, 1, 1]]) + + +@pytest.mark.parametrize( + "dtype,center,length", [(np.int8, 125, 128), (np.uint8, 253, 256)] +) +def test_correct_peaks_narrow_integer_right_boundary(dtype, center, length): + signal = np.zeros(length) + signal[-1] = 5 + with warnings.catch_warnings(): + warnings.simplefilter("error", RuntimeWarning) + actual = processing.correct_peaks( + signal, np.array([center], dtype=dtype), 5, 9, "up" + ) + assert_array_equal(actual, [length - 1]) + + +def test_correct_peaks_uint64_remains_usable_as_index(): + signal = np.zeros(100) + signal[70] = 5 + actual = processing.correct_peaks( + signal, np.array([72], dtype=np.uint64), 5, 9, "up" + ) + assert_array_equal(actual, [70]) + assert_array_equal(signal[actual], [5]) + + +def test_correct_peaks_unsigned_public_record(tmp_path): + wfdb.wrsamp( + "indices", + fs=10, + units=["mV"], + sig_name=["ECG"], + d_signal=np.array([0, 5] + [0] * 98, dtype=np.int64).reshape(-1, 1), + fmt=["16"], + adc_gain=[1], + baseline=[0], + write_dir=str(tmp_path), + ) + signal, _ = wfdb.rdsamp(str(tmp_path / "indices")) + assert_array_equal( + processing.correct_peaks( + signal[:, 0], np.array([2], dtype=np.uint32), 5, 9, "up" + ), + [1], + ) + + +def test_correct_peaks_signed_control(): + signal = np.zeros(100) + signal[[1, 70]] = 5 + assert_array_equal( + processing.correct_peaks(signal, np.array([72, 2], dtype=np.int64), 5, 9, "up"), + [70, 1], + ) + + +def test_correct_peaks_python_integer_list(): + signal = np.zeros(100) + signal[[1, 70]] = 5 + assert_array_equal(processing.correct_peaks(signal, [72, 2], 5, 9, "up"), [70, 1]) + + +@pytest.mark.parametrize("direction", ["up", "down", "both", "compare"]) +@pytest.mark.parametrize("value", [np.iinfo(np.intp).max + 1, np.iinfo(np.uint64).max]) +def test_correct_peaks_rejects_uint64_outside_platform_index_range(value, direction): + signal = np.zeros(20) + signal[1] = 5 + guesses = np.array([value], dtype=np.uint64) + original = guesses.copy() + with warnings.catch_warnings(): + warnings.simplefilter("error", RuntimeWarning) + with pytest.raises(ValueError, match="platform index range"): + processing.correct_peaks(signal, guesses, 5, 9, direction) + assert_array_equal(guesses, original) + + +def test_correct_peaks_rejects_mixed_valid_and_unrepresentable_uint64(): + signal = np.zeros(20) + signal[1] = 5 + with pytest.raises(ValueError, match="platform index range"): + processing.correct_peaks( + signal, np.array([2, np.iinfo(np.uint64).max], dtype=np.uint64), 5, 9, "up" + )