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 a0580ec9..53112fbe 100644 --- a/tests/test_record.py +++ b/tests/test_record.py @@ -1949,6 +1949,53 @@ def test_invalid_base_time_is_ignored_with_a_warning(tmp_path, time_and_date, wa 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) + + class TestWriteAppliedSkew: """Writing a record whose signals were read with their skew applied."""