Skip to content

Handle unsigned and narrow integer guesses in peak correction - #765

Merged
tompollard merged 5 commits into
MIT-LCP:mainfrom
Kulraj69:patch-1
Nov 29, 2026
Merged

tompollard merged 5 commits into
MIT-LCP:mainfrom
Kulraj69:patch-1

Conversation

@Kulraj69

Copy link
Copy Markdown
Contributor

Problem

processing.correct_peaks accepts integer index arrays but performs search-window arithmetic in their original dtype. Unsigned indices can wrap near the left boundary, narrow signed/unsigned indices can overflow near the right boundary, and uint64 indices combined with signed shifts can produce float results that cannot index NumPy arrays.

Change

Normalize array-like guesses with np.asarray, then widen integer arrays to platform index type np.intp inside shift_peaks before arithmetic. Valid sample indices are assumed to fit np.intp. Python integer lists remain accepted; output integer dtype can widen. This does not introduce validation guarantees for invalid/out-of-range indices, bools or floats.

Tests cover all four direction modes, six integer dtypes, left/right boundaries, order and duplicates, input nonmutation, usable integer output, a Python-list control and a locally written/read WFDB record. Includes a changelog fragment. This is distinct from the earlier right-window and near-start/order changes in #611, #613 and #680.

Validation

Requalified on current upstream abfe426a5eadb7130bd6b55978e1324a8ea04a3d, Python 3.12.16, NumPy 2.5.5, SciPy 1.18.1:

  • New regressions with unmodified runtime: 20 failed, 10 passed.
  • Full available offline suite with this patch, uv run pytest -q -m 'not network': 957 passed, 4 skipped because the external bxb utility is absent, 21 network tests deselected.
  • Pinned Ruff 0.17.3 check and format: passed for both changed Python files.
  • Strict Sphinx 9.0.4 HTML build, -W --keep-going -E: passed.

Only this fix was applied. Network tests, other platforms/dependency versions and remote CI were not run locally. Prepared and tested with AI assistance; no independent human QA, clinical validation or waveform-quality claim is made.

Add handling for peak index data types to ensure proper arithmetic operations.
This change improves the handling of integer indices in peak correction by widening them to avoid wraparound issues, ensuring compatibility with NumPy indexing.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I found one case where the new cast silently changes a peak index. np.uint64(2**64 - 1) becomes -1 when cast to np.intp. With a 20-sample signal that has a peak at index 1, correct_peaks(..., peak_dir="up") then returns [1]; the base version raises an error.

Could we reject unsigned values above np.iinfo(np.intp).max before casting? That would prevent an unrepresentable index from becoming a plausible corrected peak without adding general bounds validation.

Check platform integer limits before conversion to avoid uint64 wraparound.
Add nine regression cases for platform index range conversion.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Kulraj69!

@tompollard
tompollard merged commit f438a68 into MIT-LCP:main Nov 29, 2026
19 checks passed
tompollard added a commit that referenced this pull request Nov 29, 2026
## Summary

Prepare WFDB-Python 5.7.0 with the opt-in `read_header` API merged in
#763 and the peak-correction fix merged in #765. This release collects
both changelog fragments and bumps the version. Existing `rdheader`
behavior remains available.

The new reader returns an immutable description of one physical header.
It supports ordinary paths, supported remote URIs, and local `.wfdb`
archives, including selecting a header inside an archive. It does not
load signal samples or recursively read segment headers.

`processing.correct_peaks` now widens narrow and unsigned integer
guesses before search-window arithmetic, avoiding overflow near signal
boundaries.

## Checks

- `uv build` produced the 5.7.0 source distribution and wheel. The wheel
contains `wfdb.io.headers` and no `py.typed` marker.
- The focused `read_header` and peak-correction tests passed (49 tests).
- Sphinx HTML build passed with warnings treated as errors.
- Ruff format and lint checks passed.
- `git diff --check` passed.

#763 and #765 passed CI before merging. The updated release PR will run
CI again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants