Skip to content

Commit f298146

Browse files
authored
Accept pathlib.Path record names in rdann (#626)
`rdann` fails when `record_name` is a `pathlib.Path`: ```python wfdb.rdann(pathlib.Path("sample-data/100"), "atr") # TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str' ``` The file name is built with `record_name + "." + extension`. `rdrecord`, `rdsamp` and `rdheader` already accept path-like objects, so `rdann` now converts `record_name` with `os.fspath` at the start, and the docstring says `str or os.PathLike`. This is the remaining part of #346, without the changes to how PhysioNet URLs are built. Tests: reading `sample-data/100` with a `Path` gives the same annotations as with a string (this fails on `main`), and reading from a `.wfdb` archive given as a `Path` works too.
2 parents 944760c + 4370a52 commit f298146

4 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎changelog.d/rdann-path.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
**Accept pathlib.Path record names in rdann**
2+
``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 +``.

‎src/wfdb/io/annotation.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,7 @@ def rdann(
18921892
18931893
Parameters
18941894
----------
1895-
record_name : str
1895+
record_name : str or os.PathLike
18961896
The record name of the WFDB annotation file. ie. for file '100.atr',
18971897
record_name='100'. The path to the file can be a cloud URL. To read
18981898
the annotation file from a local .wfdb archive, pass the path of
@@ -1942,6 +1942,7 @@ def rdann(
19421942
>>> ann = wfdb.rdann('100.wfdb', 'atr')
19431943
19441944
"""
1945+
record_name = os.fspath(record_name)
19451946
is_archive = is_archive_path(record_name)
19461947
if is_archive:
19471948
record._check_archive_read_args(record_name, pn_dir)

‎tests/test_annotation.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import pathlib
23
import re
34

45
import numpy as np
@@ -357,3 +358,12 @@ def test_contained_labels_from_symbols():
357358
]
358359
assert labels.loc[1, "symbol"] == "N"
359360
assert labels["n_occurrences"].sum() == len(ann.sample)
361+
362+
363+
def test_rdann_path():
364+
"""rdann accepts a pathlib.Path as the record name"""
365+
expected = wfdb.rdann("sample-data/100", "atr")
366+
annotation = wfdb.rdann(pathlib.Path("sample-data/100"), "atr")
367+
assert annotation.record_name == "100"
368+
np.testing.assert_array_equal(annotation.sample, expected.sample)
369+
assert annotation.symbol == expected.symbol

‎tests/test_archive.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,22 @@ def test_rdann_archive(tmp_path):
704704
assert ann.aux_note == expected.aux_note
705705

706706

707+
def test_rdann_archive_path(tmp_path):
708+
"""rdann accepts a pathlib.Path to an archive."""
709+
archive_path = tmp_path / "100.wfdb"
710+
create_archive(
711+
record_files("sample-data/100") + ["sample-data/100.atr"],
712+
os.fspath(archive_path),
713+
)
714+
715+
ann = wfdb.rdann(archive_path, "atr")
716+
717+
assert ann.record_name == "100"
718+
np.testing.assert_array_equal(
719+
ann.sample, wfdb.rdann("sample-data/100", "atr").sample
720+
)
721+
722+
707723
def test_rdann_archive_header_named_differently(tmp_path):
708724
"""Annotation files are named after the record's header, not the archive."""
709725
archive_path = os.fspath(tmp_path / "renamed.wfdb")

0 commit comments

Comments
 (0)