Skip to content

Check that QRS indices are increasing in compute_hr - #603

Merged
tompollard merged 2 commits into
MIT-LCP:mainfrom
TheNetherWatcher:compute-hr-check-order
Oct 31, 2026
Merged

tompollard merged 2 commits into
MIT-LCP:mainfrom
TheNetherWatcher:compute-hr-check-order

Conversation

@TheNetherWatcher

@TheNetherWatcher TheNetherWatcher commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #602.

compute_hr now raises a ValueError if qrs_inds is not strictly increasing, naming the first pair that isn't, for example:

ValueError: qrs_inds must be strictly increasing, but qrs_inds[1] = 8 is followed by qrs_inds[2] = 8

Before, repeated beats gave a ZeroDivisionError (lists) or inf with a RuntimeWarning (arrays), and beats out of order gave a negative heart rate. The docstring now says the indices must be strictly increasing and documents the error.

Tests: repeated, decreasing and repeated-last-beat inputs (lists and arrays) raise the error. pytest -m "not network" passes (103 tests), and the docs build passes.

Repeated beats gave a ZeroDivisionError (or inf for NumPy arrays), and
beats out of order gave a negative heart rate. Raise a ValueError
naming the first pair that is not increasing.

Fixes #602.
@TheNetherWatcher

Copy link
Copy Markdown
Contributor Author

Rebased onto main now that #601 is merged, so this only contains the new check. Tests pass.

@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, this looks good! The error message naming the first bad pair is helpful.

One small suggestion: np.diff wraps around for unsigned integer arrays, so out-of-order beats aren't caught there. For example, np.array([2, 8, 5], dtype=np.uint32) passes the check and gives a meaningless heart rate. Comparing neighbours directly avoids this:

q = np.asarray(qrs_inds)
not_increasing = np.flatnonzero(q[1:] <= q[:-1])

Unsigned beat indices are unusual, so I'm happy to merge either way.

np.diff wraps around for unsigned integers, so decreasing unsigned
indices weren't caught.
@TheNetherWatcher

Copy link
Copy Markdown
Contributor Author

Good catch, thanks! I've switched to comparing neighbours directly, as you suggested, and added a test with np.array([2, 8, 5], dtype=np.uint32), which fails with the np.diff version. Tests pass (104).

@tompollard
tompollard merged commit 2cdbd2a into MIT-LCP:main Oct 31, 2026
18 checks passed
@TheNetherWatcher
TheNetherWatcher deleted the compute-hr-check-order branch November 1, 2026 06:12
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.

compute_hr doesn't check that QRS indices are strictly increasing

2 participants