From 902abeee061b01e731cae85b8bb492709afe0aa3 Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Tue, 24 Nov 2026 02:48:49 +0530 Subject: [PATCH 1/3] Avoid overflow in integer peak classification Compare adjacent integer samples directly, keeping the existing floating-point path unchanged. --- src/wfdb/processing/peaks.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wfdb/processing/peaks.py b/src/wfdb/processing/peaks.py index c5d0b7e1..f71cc27f 100644 --- a/src/wfdb/processing/peaks.py +++ b/src/wfdb/processing/peaks.py @@ -28,7 +28,11 @@ def find_peaks(sig): tmp = sig[1:] tmp = np.append(tmp, [sig[-1]]) - tmp = sig - tmp + if np.asarray(sig).dtype.kind in "iu": + # Comparing integer samples avoids overflow before slopes are classified. + tmp = (sig > tmp).astype(np.int8) - (sig < tmp).astype(np.int8) + else: + tmp = sig - tmp tmp[np.where(tmp > 0)] = 1 tmp[np.where(tmp == 0)] = 0 tmp[np.where(tmp < 0)] = -1 From 4e10125a6ddf827917270f6364206572fcbd5a1d Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Tue, 24 Nov 2026 02:49:40 +0530 Subject: [PATCH 2/3] Test integer extrema and digital-record peak finding Cover signed and unsigned dtype limits, plateaus, uint64 precision, and a real digital-record round trip. --- tests/test_processing.py | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/test_processing.py b/tests/test_processing.py index 53353304..84e40ab1 100644 --- a/tests/test_processing.py +++ b/tests/test_processing.py @@ -101,6 +101,48 @@ def test_find_peaks(self): assert np.array_equal(hp, [1, 8]) assert np.array_equal(sp, [6, 10]) + @pytest.mark.parametrize("dtype", [np.int8, np.int16, np.int32, np.int64]) + def test_find_peaks_signed_integer_limits(self, dtype): + limits = np.iinfo(dtype) + x = np.array([0, limits.max, limits.min, 0], dtype=dtype) + hp, sp = processing.find_peaks(x) + np.testing.assert_array_equal(hp, [1, 2]) + assert sp.size == 0 + + @pytest.mark.parametrize("dtype", [np.uint8, np.uint16, np.uint32, np.uint64]) + def test_find_peaks_unsigned_integer_limits(self, dtype): + limits = np.iinfo(dtype) + x = np.array([0, limits.max, 0, 0, limits.max, limits.max, 0], dtype=dtype) + hp, sp = processing.find_peaks(x) + np.testing.assert_array_equal(hp, [1]) + np.testing.assert_array_equal(sp, [2, 4]) + + def test_find_peaks_uint64_small_differences(self): + # These distinct samples become equal if first converted to float64. + base = 2**63 + x = np.array([base, base + 1, base, base + 1], dtype=np.uint64) + hp, sp = processing.find_peaks(x) + np.testing.assert_array_equal(hp, [1, 2]) + assert sp.size == 0 + + def test_find_peaks_digital_record(self, tmp_path): + x = np.array([0, 32767, -32767, 0], dtype=np.int16) + wfdb.wrsamp( + "peaks", + fs=250, + units=["mV"], + sig_name=["ECG"], + d_signal=x[:, None], + fmt=["16"], + adc_gain=[200], + baseline=[0], + write_dir=tmp_path, + ) + record = wfdb.rdrecord(str(tmp_path / "peaks"), physical=False, return_res=16) + hp, sp = processing.find_peaks(record.d_signal[:, 0]) + np.testing.assert_array_equal(hp, [1, 2]) + assert sp.size == 0 + def test_find_peaks_empty(self): x = [] hp, sp = processing.find_peaks(x) From e20b487fc3e29c770ec7e95e4729c38cf2b60ea9 Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Tue, 24 Nov 2026 02:50:17 +0530 Subject: [PATCH 3/3] Document integer peak-finding fix Add changelog fragment for direct integer-sample comparisons. --- changelog.d/find-peaks-integer-overflow.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog.d/find-peaks-integer-overflow.rst diff --git a/changelog.d/find-peaks-integer-overflow.rst b/changelog.d/find-peaks-integer-overflow.rst new file mode 100644 index 00000000..70fccb94 --- /dev/null +++ b/changelog.d/find-peaks-integer-overflow.rst @@ -0,0 +1,4 @@ +**Fix peak detection for integer signals** + ``processing.find_peaks`` now compares integer samples without subtracting + them first, avoiding overflow for signed samples and errors for unsigned + samples. This also preserves differences between large 64-bit integers.