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-path.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**Accept pathlib.Path record names in rdann**
``rdann`` now accepts a ``pathlib.Path`` (or other path-like object) as ``record_name``, as ``rdrecord``, ``rdsamp`` and ``rdheader`` already do. It used to fail with ``TypeError: unsupported operand type(s) for +``.
3 changes: 2 additions & 1 deletion src/wfdb/io/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1892,7 +1892,7 @@ def rdann(

Parameters
----------
record_name : str
record_name : str or os.PathLike
The record name of the WFDB annotation file. ie. for file '100.atr',
record_name='100'. The path to the file can be a cloud URL. To read
the annotation file from a local .wfdb archive, pass the path of
Expand Down Expand Up @@ -1942,6 +1942,7 @@ def rdann(
>>> ann = wfdb.rdann('100.wfdb', 'atr')

"""
record_name = os.fspath(record_name)
is_archive = is_archive_path(record_name)
if is_archive:
record._check_archive_read_args(record_name, pn_dir)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_annotation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import pathlib
import re

import numpy as np
Expand Down Expand Up @@ -357,3 +358,12 @@ def test_contained_labels_from_symbols():
]
assert labels.loc[1, "symbol"] == "N"
assert labels["n_occurrences"].sum() == len(ann.sample)


def test_rdann_path():
"""rdann accepts a pathlib.Path as the record name"""
expected = wfdb.rdann("sample-data/100", "atr")
annotation = wfdb.rdann(pathlib.Path("sample-data/100"), "atr")
assert annotation.record_name == "100"
np.testing.assert_array_equal(annotation.sample, expected.sample)
assert annotation.symbol == expected.symbol
16 changes: 16 additions & 0 deletions tests/test_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,22 @@ def test_rdann_archive(tmp_path):
assert ann.aux_note == expected.aux_note


def test_rdann_archive_path(tmp_path):
"""rdann accepts a pathlib.Path to an archive."""
archive_path = tmp_path / "100.wfdb"
create_archive(
record_files("sample-data/100") + ["sample-data/100.atr"],
os.fspath(archive_path),
)

ann = wfdb.rdann(archive_path, "atr")

assert ann.record_name == "100"
np.testing.assert_array_equal(
ann.sample, wfdb.rdann("sample-data/100", "atr").sample
)


def test_rdann_archive_header_named_differently(tmp_path):
"""Annotation files are named after the record's header, not the archive."""
archive_path = os.fspath(tmp_path / "renamed.wfdb")
Expand Down
Loading