Skip to content

Accept sample numbers stored as floats in wrann - #625

Merged
tompollard merged 4 commits into
mainfrom
tp/wrann-float-samples
Nov 4, 2026
Merged

tompollard merged 4 commits into
mainfrom
tp/wrann-float-samples

Conversation

@tompollard

Copy link
Copy Markdown
Member

Fixes #201.

wrann failed with an unhelpful error when sample was a float array, which is easy to get from a detector or model:

wfdb.wrann("100", "atr", np.array([100.0, 2500.0]), symbol=["N", "V"])
# TypeError: ufunc 'bitwise_and' not supported for the input types, ...

The float values reached the bit operations in field2bytes, which only work on integers.

  • Float sample numbers are now accepted if they are all whole numbers, and written as integers. The file is identical to the one written from an integer array, and the caller's array isn't changed.
  • Other floats (such as 20.5, NaN or infinity) raise ValueError: The 'sample' field must only contain whole numbers.
  • Other dtypes are handled as before (for example, object arrays of Python ints still work).

This matches the other integer fields (subtype, chan, num and label_store), which already accept whole-number floats.

Tests: a float array gives the same file as an integer array (including a gap large enough to need a SKIP), and fractions, NaN and infinity raise a ValueError. All four fail on main.

wrann failed with "ufunc 'bitwise_and' not supported" when sample was a
float array, which is easy to get from a detector or model. Accept float
sample numbers if they are all whole numbers, and convert them to
integers when writing, without changing the caller's array. Other floats
(fractions, NaN, infinity) raise a ValueError. Other dtypes are handled
as before.
On x86, np.mod(inf, 1) sets the invalid flag, which the tests turn into
a FloatingPointError. Reject NaN and infinity first, and compare finite
values with their rounded values instead.
Comment thread src/wfdb/io/annotation.py Resolved
Converting an object array of sample numbers to integers truncated values such as 20.5 without an error. Check that the conversion doesn't change any value.

@briangow briangow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, thanks!

@tompollard
tompollard merged commit 7c4dcf5 into main Nov 4, 2026
18 checks passed
@tompollard
tompollard deleted the tp/wrann-float-samples branch November 4, 2026 15:36
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.

Subelements of the symbol field must be strings of length 1 to 3 error

2 participants