From 36684b581db019e3634af2ffc02f3bf32c4d1b7c Mon Sep 17 00:00:00 2001 From: Ethan Sanders <19353344+ethsanders@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:22:05 +0000 Subject: [PATCH] Apply sampfrom in rdann when sampto is not given rdann only applied the sample range when both sampfrom and sampto were given. With only sampfrom, it returned every annotation in the file, and shift_samps=True then gave negative sample numbers. Apply the range whenever sampfrom is given. Annotation.apply_range now treats sampto=None as no upper limit, instead of using the last annotation's sample, which failed on a file with no annotations and dropped annotations when they were not sorted. --- changelog.d/rdann-sampfrom.rst | 2 ++ src/wfdb/io/annotation.py | 14 +++++++------- tests/test_annotation.py | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 changelog.d/rdann-sampfrom.rst diff --git a/changelog.d/rdann-sampfrom.rst b/changelog.d/rdann-sampfrom.rst new file mode 100644 index 00000000..0cd97121 --- /dev/null +++ b/changelog.d/rdann-sampfrom.rst @@ -0,0 +1,2 @@ +**Apply sampfrom in rdann without sampto** + ``rdann`` now returns only the annotations at or after ``sampfrom`` when ``sampto`` is not given. It used to ignore ``sampfrom`` and return every annotation in the file, which gave negative sample numbers with ``shift_samps=True``. ``Annotation.apply_range`` with ``sampto=None`` now keeps annotations up to the end without assuming they are sorted, and no longer fails when there are no annotations. diff --git a/src/wfdb/io/annotation.py b/src/wfdb/io/annotation.py index e2154588..8a071499 100644 --- a/src/wfdb/io/annotation.py +++ b/src/wfdb/io/annotation.py @@ -242,14 +242,14 @@ def apply_range(self, sampfrom=0, sampto=None): sampfrom : int, optional The minimum sample number for annotations to be returned. sampto : int, optional - The maximum sample number for annotations to be returned. + The maximum sample number for annotations to be returned. If None, + there is no upper limit. """ - sampto = sampto or self.sample[-1] - - kept_inds = np.intersect1d( - np.where(self.sample >= sampfrom), np.where(self.sample <= sampto) - ) + kept = self.sample >= sampfrom + if sampto is not None: + kept &= self.sample <= sampto + kept_inds = np.flatnonzero(kept) for field in ["sample", "label_store", "subtype", "chan", "num"]: setattr(self, field, getattr(self, field)[kept_inds]) @@ -2047,7 +2047,7 @@ def rdann( ) # Apply the desired index range - if sampfrom > 0 and sampto is not None: + if sampfrom > 0: annotation.apply_range(sampfrom=sampfrom, sampto=sampto) # If specified, obtain annotation samples relative to the starting diff --git a/tests/test_annotation.py b/tests/test_annotation.py index 0eb2f9f6..030a8d0e 100644 --- a/tests/test_annotation.py +++ b/tests/test_annotation.py @@ -466,3 +466,23 @@ def rdheader(*args, **kwargs): monkeypatch.setattr(wfdb.io.record, "rdheader", rdheader) with pytest.raises(wfdb.io._url.NetFileError, match="Bad Gateway"): wfdb.rdann(str(tmp_path / "err"), "atr") + + +def test_rdann_sampfrom_without_sampto(): + """rdann with only sampfrom returns the annotations from sampfrom onwards.""" + full = wfdb.rdann("sample-data/100", "atr") + start = np.searchsorted(full.sample, 600000) + ann = wfdb.rdann("sample-data/100", "atr", sampfrom=600000) + np.testing.assert_array_equal(ann.sample, full.sample[start:]) + assert ann.symbol == full.symbol[start:] + assert ann.ann_len == full.ann_len - start + shifted = wfdb.rdann("sample-data/100", "atr", sampfrom=600000, shift_samps=True) + np.testing.assert_array_equal(shifted.sample, full.sample[start:] - 600000) + + +def test_rdann_sampfrom_empty_file(tmp_path): + """rdann with only sampfrom reads an annotation file with no annotations.""" + # A file that holds only the end-of-file marker + (tmp_path / "empty.atr").write_bytes(b"\x00\x00") + ann = wfdb.rdann(str(tmp_path / "empty"), "atr", sampfrom=10) + assert ann.sample.size == 0