From fbba652681f0d3c6e1a9f6f33318b984ca44da2f Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Mon, 23 Nov 2026 09:55:56 -0500 Subject: [PATCH 1/3] Keep every annotation field in Annotation.apply_range apply_range filtered sample, label_store, subtype, chan, num and aux_note, but not symbol or description, which then no longer matched sample. It also failed with a TypeError on an annotation read by rdann with the default return_label_elements, where label_store is None. Filter every per-annotation field that is set, keeping arrays as arrays and lists as lists, and leave fields that are not set as None. Calling apply_range on an rdann result now gives the same fields as reading only that range. rdann's own results are unchanged for every annotation file in sample-data, with and without sampfrom, sampto and shift_samps. --- changelog.d/apply-range-fields.rst | 2 ++ src/wfdb/io/annotation.py | 26 ++++++++++++++++++++++---- tests/test_annotation.py | 24 ++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 changelog.d/apply-range-fields.rst diff --git a/changelog.d/apply-range-fields.rst b/changelog.d/apply-range-fields.rst new file mode 100644 index 00000000..8e6e8f1d --- /dev/null +++ b/changelog.d/apply-range-fields.rst @@ -0,0 +1,2 @@ +**Keep every annotation field in Annotation.apply_range** + ``Annotation.apply_range`` now keeps the same annotations in every field that is set, including ``symbol`` and ``description``, which it used to leave unchanged, so that they no longer matched ``sample``. It also works on an annotation read by ``rdann`` with the default ``return_label_elements``, where ``label_store`` is ``None``, instead of failing with a ``TypeError``. diff --git a/src/wfdb/io/annotation.py b/src/wfdb/io/annotation.py index dc81e30f..7dd7695e 100644 --- a/src/wfdb/io/annotation.py +++ b/src/wfdb/io/annotation.py @@ -244,16 +244,34 @@ def apply_range(self, sampfrom=0, sampto=None): The maximum sample number for annotations to be returned. If None, there is no upper limit. + Every field with one value per annotation that is set (such as + `symbol` and `aux_note`) is kept for the same annotations. Fields that + are not set are left as None. + """ + n_annotations = len(self.sample) 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]) - - self.aux_note = [self.aux_note[i] for i in kept_inds] + for field in [ + "sample", + "label_store", + "symbol", + "description", + "subtype", + "chan", + "num", + "aux_note", + ]: + values = getattr(self, field) + if values is None or np.ndim(values) == 0 or len(values) != n_annotations: + continue + if isinstance(values, np.ndarray): + setattr(self, field, values[kept_inds]) + else: + setattr(self, field, [values[i] for i in kept_inds]) self.ann_len = len(self.sample) diff --git a/tests/test_annotation.py b/tests/test_annotation.py index 030a8d0e..79750d4d 100644 --- a/tests/test_annotation.py +++ b/tests/test_annotation.py @@ -486,3 +486,27 @@ def test_rdann_sampfrom_empty_file(tmp_path): (tmp_path / "empty.atr").write_bytes(b"\x00\x00") ann = wfdb.rdann(str(tmp_path / "empty"), "atr", sampfrom=10) assert ann.sample.size == 0 + + +@pytest.mark.parametrize( + "label_elements", [None, ["label_store", "symbol", "description"]] +) +def test_apply_range_keeps_every_field_consistent(label_elements): + """apply_range gives the same result as reading only that range.""" + kwargs = {} if label_elements is None else {"return_label_elements": label_elements} + ann = wfdb.rdann("sample-data/100", "atr", **kwargs) + ann.apply_range(sampfrom=600000, sampto=620000) + + expected = wfdb.rdann( + "sample-data/100", "atr", sampfrom=600000, sampto=620000, **kwargs + ) + assert ann.ann_len == expected.ann_len == 71 + for field in ["sample", "subtype", "chan", "num"]: + np.testing.assert_array_equal(getattr(ann, field), getattr(expected, field)) + for field in ["label_store", "symbol", "description", "aux_note"]: + value, expected_value = getattr(ann, field), getattr(expected, field) + if expected_value is None: + # Fields that are not set stay None + assert value is None + else: + assert list(value) == list(expected_value) From 60246e77cead6dee45e5ee9dae43d97caf1034f6 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Mon, 23 Nov 2026 09:56:16 -0500 Subject: [PATCH 2/3] Move the apply_range test next to the related rdann tests --- tests/test_annotation.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/test_annotation.py b/tests/test_annotation.py index 79750d4d..84873f45 100644 --- a/tests/test_annotation.py +++ b/tests/test_annotation.py @@ -468,26 +468,6 @@ def rdheader(*args, **kwargs): 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 - - @pytest.mark.parametrize( "label_elements", [None, ["label_store", "symbol", "description"]] ) @@ -510,3 +490,23 @@ def test_apply_range_keeps_every_field_consistent(label_elements): assert value is None else: assert list(value) == list(expected_value) + + +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 From 9610d5b37a3868e3bff213761aa2154381ddb741 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Mon, 23 Nov 2026 11:46:05 -0500 Subject: [PATCH 3/3] Update the label summary in Annotation.apply_range apply_range kept contained_labels as it was, so after summarize_labels=True the summary still counted every annotation, not only those kept. It now summarizes the labels again when there is a summary, giving the same contained_labels as reading only that range. --- src/wfdb/io/annotation.py | 14 ++++++++++---- tests/test_annotation.py | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/wfdb/io/annotation.py b/src/wfdb/io/annotation.py index c0524551..be02fe73 100644 --- a/src/wfdb/io/annotation.py +++ b/src/wfdb/io/annotation.py @@ -236,6 +236,12 @@ def __eq__(self, other): def apply_range(self, sampfrom=0, sampto=None): """Keep only the annotation items between the desired sample values. + Every field with one value per annotation that is set (such as + `symbol` and `aux_note`) is kept for the same annotations. Fields that + are not set are left as None. If the labels have been summarized in + `contained_labels`, the summary is made again for the annotations that + are kept. + Parameters ---------- sampfrom : int, optional @@ -244,10 +250,6 @@ def apply_range(self, sampfrom=0, sampto=None): The maximum sample number for annotations to be returned. If None, there is no upper limit. - Every field with one value per annotation that is set (such as - `symbol` and `aux_note`) is kept for the same annotations. Fields that - are not set are left as None. - """ n_annotations = len(self.sample) kept = self.sample >= sampfrom @@ -275,6 +277,10 @@ def apply_range(self, sampfrom=0, sampto=None): self.ann_len = len(self.sample) + # A summary of the labels would still count the annotations removed + if self.contained_labels is not None: + self.get_contained_labels() + def wrann(self, write_fs=False, write_dir=""): """Write a WFDB annotation file from this object. diff --git a/tests/test_annotation.py b/tests/test_annotation.py index f8c19837..534d9adc 100644 --- a/tests/test_annotation.py +++ b/tests/test_annotation.py @@ -492,6 +492,25 @@ def test_apply_range_keeps_every_field_consistent(label_elements): assert list(value) == list(expected_value) +@pytest.mark.parametrize(("sampfrom", "sampto"), [(600000, 620000), (10**9, None)]) +def test_apply_range_updates_the_label_summary(sampfrom, sampto): + """A summary made before apply_range counts only the annotations kept.""" + ann = wfdb.rdann("sample-data/100", "atr", summarize_labels=True) + ann.apply_range(sampfrom=sampfrom, sampto=sampto) + + expected = wfdb.rdann( + "sample-data/100", + "atr", + sampfrom=sampfrom, + sampto=sampto, + summarize_labels=True, + ) + assert ann.contained_labels["n_occurrences"].sum() == ann.ann_len + pd.testing.assert_frame_equal( + ann.contained_labels.sort_index(), expected.contained_labels.sort_index() + ) + + def test_rdann_sampfrom_without_sampto(): """rdann with only sampfrom returns the annotations from sampfrom onwards.""" full = wfdb.rdann("sample-data/100", "atr")