From 8170c9adeac659cef043384cf89644dc37bd9158 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Mon, 23 Nov 2026 20:55:07 -0500 Subject: [PATCH] Read format 61 signals with smooth_frames=False convert_dtype compared dtypes by parsing their names, which fails for the big-endian dtype of format 61 (>i2). Use np.can_cast instead, which also handles byte order. --- changelog.d/expanded-big-endian-read.rst | 2 + src/wfdb/io/_signal.py | 4 +- tests/test_record.py | 47 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 changelog.d/expanded-big-endian-read.rst diff --git a/changelog.d/expanded-big-endian-read.rst b/changelog.d/expanded-big-endian-read.rst new file mode 100644 index 00000000..6cd51618 --- /dev/null +++ b/changelog.d/expanded-big-endian-read.rst @@ -0,0 +1,2 @@ +**Read format 61 signals with smooth_frames=False** + ``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. diff --git a/src/wfdb/io/_signal.py b/src/wfdb/io/_signal.py index af7f1421..3dd0d29f 100644 --- a/src/wfdb/io/_signal.py +++ b/src/wfdb/io/_signal.py @@ -940,7 +940,7 @@ def convert_dtype(self, physical, return_res, smooth_frames): if current_dtype != return_dtype: # Do not allow changing integer dtype to lower value due to # over/underflow - if int(str(current_dtype)[3:]) > int(str(return_dtype)[3:]): + if not np.can_cast(current_dtype, return_dtype, "safe"): raise Exception( "Cannot convert digital samples to lower dtype. Risk of overflow/underflow." ) @@ -951,7 +951,7 @@ def convert_dtype(self, physical, return_res, smooth_frames): if current_dtype != return_dtype: # Do not allow changing integer dtype to lower value due to # over/underflow - if int(str(current_dtype)[3:]) > int(str(return_dtype)[3:]): + if not np.can_cast(current_dtype, return_dtype, "safe"): raise Exception( "Cannot convert digital samples to lower dtype. Risk of overflow/underflow." ) diff --git a/tests/test_record.py b/tests/test_record.py index 06d93674..81ce5172 100644 --- a/tests/test_record.py +++ b/tests/test_record.py @@ -1946,3 +1946,50 @@ def test_invalid_base_time_is_ignored_with_a_warning(tmp_path, time_and_date, wa assert record.sig_len == 10 if time_and_date.endswith("25/12/2020"): assert record.base_date == datetime.date(2020, 12, 25) + + +class TestExpandedBigEndian: + """Reading format 61 (big-endian) signals as expanded digital samples.""" + + @pytest.mark.parametrize("return_res", [16, 32, 64]) + def test_dtype_and_samples(self, return_res): + """The samples match the uniform read, in the requested native dtype.""" + expanded = wfdb.rdrecord( + "sample-data/p10143", + physical=False, + smooth_frames=False, + return_res=return_res, + sampto=100, + ) + uniform = wfdb.rdrecord( + "sample-data/p10143", + physical=False, + return_res=return_res, + sampto=100, + ) + dtype = np.dtype(f"int{return_res}") + for ch, signal in enumerate(expanded.e_d_signal): + assert signal.dtype == dtype + assert signal.dtype.isnative + np.testing.assert_array_equal(signal, uniform.d_signal[:, ch]) + + def test_lower_dtype_rejected(self): + """Samples are not converted to a dtype that cannot hold them.""" + with pytest.raises(Exception, match="lower dtype"): + wfdb.rdrecord( + "sample-data/p10143", + physical=False, + smooth_frames=False, + return_res=8, + sampto=100, + ) + + def test_mixed_formats(self): + """A record mixing format 61 with other formats can be read.""" + record = wfdb.rdrecord( + "sample-data/binformats", + physical=False, + smooth_frames=False, + return_res=32, + ) + assert all(signal.dtype == np.dtype("int32") for signal in record.e_d_signal)