Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.d/rdann-sampto-zero.rst
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 6 additions & 4 deletions src/wfdb/io/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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")
Expand Down
17 changes: 17 additions & 0 deletions tests/test_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading