Skip to content

Commit 7c4dcf5

Browse files
authored
Accept sample numbers stored as floats in wrann (#625)
Fixes #201. `wrann` failed with an unhelpful error when `sample` was a float array, which is easy to get from a detector or model: ```python 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`.
2 parents f298146 + c62b992 commit 7c4dcf5

3 files changed

Lines changed: 90 additions & 5 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
**Accept sample numbers stored as floats in wrann**
2+
``wrann`` now accepts a ``sample`` array of floats if all the values are whole numbers (as from a detector that returns floats), and writes them as integers. Other floats, such as ``20.5`` or NaN, raise a ``ValueError``. Float arrays used to fail with ``TypeError: ufunc 'bitwise_and' not supported for the input types``.

‎src/wfdb/io/annotation.py‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,30 @@ def check_field(self, field):
565565
)
566566

567567
elif field == "sample":
568+
# Sample numbers may be stored as floats (for example, the
569+
# output of a detector), as long as they are whole numbers.
570+
# NaN and infinity are rejected before rounding, so that no
571+
# floating-point exception is raised for them.
572+
if np.issubdtype(self.sample.dtype, np.floating):
573+
if not np.all(np.isfinite(self.sample)) or np.any(
574+
self.sample != np.round(self.sample)
575+
):
576+
raise ValueError(
577+
"The 'sample' field must only contain whole numbers"
578+
)
579+
# Object arrays may hold any type of number. Check that
580+
# converting them to integers does not change any value.
581+
elif self.sample.dtype == object:
582+
try:
583+
int_sample = self.sample.astype(np.int64)
584+
except (TypeError, ValueError, OverflowError):
585+
raise ValueError(
586+
"The 'sample' field must only contain whole numbers"
587+
) from None
588+
if np.any(int_sample != self.sample):
589+
raise ValueError(
590+
"The 'sample' field must only contain whole numbers"
591+
)
568592
if len(self.sample) == 1:
569593
sampdiffs = np.array([self.sample[0]])
570594
elif len(self.sample) > 1:
@@ -1156,11 +1180,13 @@ def calc_core_bytes(self):
11561180
All of the bytes to be written to the annotation file.
11571181
11581182
"""
1159-
# The difference sample to write
1160-
if len(self.sample) == 1:
1161-
sampdiff = np.array([self.sample[0]])
1183+
# The difference sample to write. Sample numbers stored as floats
1184+
# have been checked to be whole numbers.
1185+
sample = self.sample.astype(np.int64)
1186+
if len(sample) == 1:
1187+
sampdiff = np.array([sample[0]])
11621188
else:
1163-
sampdiff = np.concatenate(([self.sample[0]], np.diff(self.sample)))
1189+
sampdiff = np.concatenate(([sample[0]], np.diff(sample)))
11641190

11651191
# Create a copy of the annotation object with a
11661192
# compact version of fields to write
@@ -1734,7 +1760,8 @@ def wrann(
17341760
The string annotation file extension.
17351761
sample : ndarray
17361762
A numpy array containing the annotation locations in samples relative to
1737-
the beginning of the record.
1763+
the beginning of the record. Floats are accepted if they are whole
1764+
numbers.
17381765
symbol : list, numpy array, optional
17391766
The symbols used to display the annotation labels. List or numpy array.
17401767
If this field is present, `label_store` must not be present.

‎tests/test_annotation.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22
import pathlib
33
import re
4+
from decimal import Decimal
5+
from fractions import Fraction
46

57
import numpy as np
68
import pandas as pd
@@ -360,6 +362,60 @@ def test_contained_labels_from_symbols():
360362
assert labels["n_occurrences"].sum() == len(ann.sample)
361363

362364

365+
def test_wrann_float_samples(temp_path):
366+
"""Sample numbers stored as whole-number floats are written as integers"""
367+
sample = np.array([100, 2500, 70000])
368+
symbol = ["N", "V", "N"]
369+
wfdb.wrann("int", "atr", sample, symbol=symbol, fs=360, write_dir=temp_path)
370+
371+
float_sample = sample.astype("float64")
372+
wfdb.wrann("float", "atr", float_sample, symbol=symbol, fs=360, write_dir=temp_path)
373+
assert float_sample.dtype == np.float64
374+
375+
with (
376+
open(os.path.join(temp_path, "int.atr"), "rb") as f1,
377+
open(os.path.join(temp_path, "float.atr"), "rb") as f2,
378+
):
379+
assert f1.read() == f2.read()
380+
381+
382+
@pytest.mark.parametrize("sample", [[10.0, 20.5], [10.0, np.nan], [10.0, np.inf]])
383+
def test_wrann_non_whole_samples(temp_path, sample):
384+
with pytest.raises(ValueError, match="whole numbers"):
385+
wfdb.wrann("x", "atr", np.array(sample), symbol=["N", "N"], write_dir=temp_path)
386+
387+
388+
@pytest.mark.parametrize(
389+
"sample", [[10, 20], [10.0, 20.0], [np.float32(10), Fraction(20)]]
390+
)
391+
def test_wrann_object_samples(temp_path, sample):
392+
"""Whole numbers in an object array are written as integers"""
393+
wfdb.wrann(
394+
"x",
395+
"atr",
396+
np.array(sample, dtype=object),
397+
symbol=["N", "N"],
398+
write_dir=temp_path,
399+
)
400+
ann = wfdb.rdann(os.path.join(temp_path, "x"), "atr")
401+
assert ann.sample.tolist() == [10, 20]
402+
403+
404+
@pytest.mark.parametrize(
405+
"sample",
406+
[[10.0, 20.5], [10, Fraction(41, 2)], [10, Decimal("20.5")], [10.0, np.nan]],
407+
)
408+
def test_wrann_non_whole_object_samples(temp_path, sample):
409+
with pytest.raises(ValueError, match="whole numbers"):
410+
wfdb.wrann(
411+
"x",
412+
"atr",
413+
np.array(sample, dtype=object),
414+
symbol=["N", "N"],
415+
write_dir=temp_path,
416+
)
417+
418+
363419
def test_rdann_path():
364420
"""rdann accepts a pathlib.Path as the record name"""
365421
expected = wfdb.rdann("sample-data/100", "atr")

0 commit comments

Comments
 (0)