Skip to content

Commit f59c445

Browse files
authored
Read format 61 signals with smooth_frames=False (#678)
`rdrecord` with `physical=False` and `smooth_frames=False` fails for any record with a format 61 signal: ```python wfdb.rdrecord("sample-data/p10143", physical=False, smooth_frames=False) # ValueError: invalid literal for int() with base 10: '' ``` `convert_dtype` compared dtypes by parsing their names (`int(str(dtype)[3:])`). Format 61 is stored in big-endian byte order, so its dtype is `>i2`, which has nothing after the first three characters. It now uses `np.can_cast(current_dtype, return_dtype, "safe")`, which handles byte order. Samples are returned in the requested native dtype, and asking for a dtype that is too small still raises the same "lower dtype" error. This is the read that a lossless round trip needs (#484, #500). It affects `p10143` and `binformats` in `sample-data`. Writing format 61 is still not supported, so those two records can be read this way but not yet written back. Testing: - New `TestExpandedBigEndian` checks the samples and dtype for `return_res` of 16, 32 and 64, that a dtype that is too small is rejected, and a record mixing format 61 with other formats. All of them fail on `main`. - `uv run pytest -m "not network" -W error` (571 passed), the network tests in `tests/test_record.py` (14 passed), `ruff format --check` and `ruff check` pass. Changelog entry added in `changelog.d/`. Refs #484, #500 <sub>@tompollard 🟥 red 🐳 whale · quince</sub>
2 parents 72fec54 + bfcc7fc commit f59c445

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
**Read format 61 signals with smooth_frames=False**
2+
``rdrecord`` with ``physical=False`` and ``smooth_frames=False`` failed with ``ValueError: invalid literal for int()`` for records with format 61 signals, which are stored in big-endian byte order. These signals are now returned with the requested native dtype.

‎src/wfdb/io/_signal.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ def convert_dtype(self, physical, return_res, smooth_frames):
940940
if current_dtype != return_dtype:
941941
# Do not allow changing integer dtype to lower value due to
942942
# over/underflow
943-
if int(str(current_dtype)[3:]) > int(str(return_dtype)[3:]):
943+
if not np.can_cast(current_dtype, return_dtype, "safe"):
944944
raise Exception(
945945
"Cannot convert digital samples to lower dtype. Risk of overflow/underflow."
946946
)
@@ -951,7 +951,7 @@ def convert_dtype(self, physical, return_res, smooth_frames):
951951
if current_dtype != return_dtype:
952952
# Do not allow changing integer dtype to lower value due to
953953
# over/underflow
954-
if int(str(current_dtype)[3:]) > int(str(return_dtype)[3:]):
954+
if not np.can_cast(current_dtype, return_dtype, "safe"):
955955
raise Exception(
956956
"Cannot convert digital samples to lower dtype. Risk of overflow/underflow."
957957
)

‎tests/test_record.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,53 @@ def test_invalid_base_time_is_ignored_with_a_warning(tmp_path, time_and_date, wa
19491949
assert record.base_date == datetime.date(2020, 12, 25)
19501950

19511951

1952+
class TestExpandedBigEndian:
1953+
"""Reading format 61 (big-endian) signals as expanded digital samples."""
1954+
1955+
@pytest.mark.parametrize("return_res", [16, 32, 64])
1956+
def test_dtype_and_samples(self, return_res):
1957+
"""The samples match the uniform read, in the requested native dtype."""
1958+
expanded = wfdb.rdrecord(
1959+
"sample-data/p10143",
1960+
physical=False,
1961+
smooth_frames=False,
1962+
return_res=return_res,
1963+
sampto=100,
1964+
)
1965+
uniform = wfdb.rdrecord(
1966+
"sample-data/p10143",
1967+
physical=False,
1968+
return_res=return_res,
1969+
sampto=100,
1970+
)
1971+
dtype = np.dtype(f"int{return_res}")
1972+
for ch, signal in enumerate(expanded.e_d_signal):
1973+
assert signal.dtype == dtype
1974+
assert signal.dtype.isnative
1975+
np.testing.assert_array_equal(signal, uniform.d_signal[:, ch])
1976+
1977+
def test_lower_dtype_rejected(self):
1978+
"""Samples are not converted to a dtype that cannot hold them."""
1979+
with pytest.raises(Exception, match="lower dtype"):
1980+
wfdb.rdrecord(
1981+
"sample-data/p10143",
1982+
physical=False,
1983+
smooth_frames=False,
1984+
return_res=8,
1985+
sampto=100,
1986+
)
1987+
1988+
def test_mixed_formats(self):
1989+
"""A record mixing format 61 with other formats can be read."""
1990+
record = wfdb.rdrecord(
1991+
"sample-data/binformats",
1992+
physical=False,
1993+
smooth_frames=False,
1994+
return_res=32,
1995+
)
1996+
assert all(signal.dtype == np.dtype("int32") for signal in record.e_d_signal)
1997+
1998+
19521999
class TestWriteAppliedSkew:
19532000
"""Writing a record whose signals were read with their skew applied."""
19542001

0 commit comments

Comments
 (0)