diff --git a/changelog.d/rdann-sampto-zero.rst b/changelog.d/rdann-sampto-zero.rst new file mode 100644 index 00000000..c6d95a08 --- /dev/null +++ b/changelog.d/rdann-sampto-zero.rst @@ -0,0 +1,2 @@ +**Reject sampto=0 in rdann, and summarize no annotations** + ``rdann`` now raises a ``ValueError`` when ``sampto`` is not greater than ``sampfrom``, as ``rdrecord`` does. ``sampto=0`` used to return every annotation, but with ``sampfrom`` it returned none. With ``summarize_labels=True``, ``rdann`` and ``Annotation.get_contained_labels`` no longer fail with ``KeyError: 'n_occurrences'`` when there are no annotations, for example when ``sampfrom`` is after the last one. diff --git a/src/wfdb/io/annotation.py b/src/wfdb/io/annotation.py index dc81e30f..0f9090b8 100644 --- a/src/wfdb/io/annotation.py +++ b/src/wfdb/io/annotation.py @@ -1386,9 +1386,10 @@ def get_contained_labels(self, inplace=True): else: raise Exception("No annotation labels contained in object") - contained_labels = label_map.loc[list(index_vals), :] + contained_labels = label_map.loc[list(index_vals), :].copy() - # Add the counts + # Add the counts. With no annotations, there are none to add. + contained_labels["n_occurrences"] = 0 for i in range(len(counts[0])): contained_labels.loc[counts[0][i], "n_occurrences"] = counts[1][i] contained_labels["n_occurrences"] = pd.to_numeric( @@ -1930,7 +1931,8 @@ def rdann( 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. It must + be greater than `sampfrom`. Leave it out to read to the end. shift_samps : bool, optional Specifies whether to return the sample indices relative to `sampfrom` (True), or sample 0 (False). @@ -2087,7 +2089,7 @@ def check_read_inputs(sampfrom, sampto, return_label_elements): 'label_store', 'description'. """ - if sampto and sampto <= sampfrom: + if sampto is not None and sampto <= sampfrom: raise ValueError("sampto must be greater than sampfrom") if sampfrom < 0: raise ValueError("sampfrom must be a non-negative integer") diff --git a/tests/test_annotation.py b/tests/test_annotation.py index 030a8d0e..bece35e2 100644 --- a/tests/test_annotation.py +++ b/tests/test_annotation.py @@ -486,3 +486,20 @@ 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( + ("sampfrom", "sampto"), [(0, 0), (10, 0), (100, 100), (100, 50)] +) +def test_rdann_sampto_must_be_after_sampfrom(sampfrom, sampto): + """As in rdrecord, sampto must be greater than sampfrom (0 is not 'no limit').""" + with pytest.raises(ValueError, match="sampto must be greater than sampfrom"): + wfdb.rdann("sample-data/100", "atr", sampfrom=sampfrom, sampto=sampto) + + +def test_rdann_summarize_labels_without_annotations(): + """summarize_labels works when the range holds no annotations.""" + ann = wfdb.rdann("sample-data/100", "atr", sampfrom=10**9, summarize_labels=True) + assert ann.ann_len == 0 + assert ann.contained_labels.empty + assert "n_occurrences" in ann.contained_labels.columns