Skip to content

Correct near-start peak guesses regardless of input order - #680

Merged
tompollard merged 3 commits into
MIT-LCP:mainfrom
Kulraj69:fix-unsorted-peak-guesses
Nov 24, 2026
Merged

tompollard merged 3 commits into
MIT-LCP:mainfrom
Kulraj69:fix-unsorted-peak-guesses

Conversation

@Kulraj69

Copy link
Copy Markdown
Contributor

correct_peaks() can return a negative index for a valid near-start guess when that guess follows a later one in the input array. The boundary-adjustment loop stops at the first guess outside the near-start window, leaving subsequent early guesses unadjusted.

For a length-30 signal with maxima at samples 0 and 20:

sig = np.zeros(30)
sig[[0, 20]] = 5
processing.correct_peaks(sig, np.array([20, 2]), 5, 9, "up")
# before: array([20, -3]); after: array([20, 0])

Skip individual later guesses instead of ending the boundary-adjustment loop. Preserve input order and duplicates. This extends the near-start correction from #613 to unsorted inputs without changing sorted-input behavior.

Tests cover all four direction options, duplicate guesses, sorted controls, unchanged input, and a written/read digital WFDB record passed to the public API. The 17 added cases produce 13 failures and 4 passing controls on the unchanged code.

Validation on current main 05db3a2 plus this patch:

  • tests/test_processing.py: 151 passed on Python 3.12.16.
  • Ruff 0.17.3 check and format passed for the changed Python files.
  • Strict Sphinx HTML build with -W --keep-going passed.

Earlier same-file validation: 113 peak tests passed on Python 3.10.12; 4,000 seeded permutation checks and 4,000 sorted baseline/fixed equivalence checks passed. The full repository suite and remote CI have not been verified. These tests check index correction, not clinical detection accuracy.

AI assistance was used to develop and test this change. Independent review has not been completed.

@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.

Reviewed at fb7c5de. The change corrects each near-start guess independently while preserving input order and sorted-input behavior. The added tests cover unsorted and duplicate guesses across all peak directions and a written/read WFDB record. I ran the processing tests locally (151 passed), and all PR checks are green. Looks good to merge.

@tompollard
tompollard merged commit 51fd0fc into MIT-LCP:main Nov 24, 2026
18 checks passed
tompollard added a commit that referenced this pull request Nov 29, 2026
## 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.
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