Skip to content

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

Open
Kulraj69 wants to merge 5 commits into
MIT-LCP:mainfrom
Kulraj69:patch-1
Open

Kulraj69 wants to merge 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.

This branch has not been deployed

No deployments
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