From 02de0d7efa2a4c5c5537e038f2b558ecbb3c9d21 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Mon, 23 Nov 2026 20:29:50 -0500 Subject: [PATCH] Sum whole signals at once in _checksums Summing a C-ordered signal column by column is several times slower for records with many channels, and a 1-D array returned one value per sample instead of raising an error. Go back to a single reduction along axis 0. read_edf is the only caller with float samples, where the order of the additions can change the result, so it keeps summing each channel separately. --- src/wfdb/io/_signal.py | 2 +- src/wfdb/io/convert/edf.py | 8 ++++++-- tests/io/test_convert.py | 40 ++++++++++++++++++++++++++++++++++++++ tests/test_record.py | 9 ++++----- 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/wfdb/io/_signal.py b/src/wfdb/io/_signal.py index 409bd003..af7f1421 100644 --- a/src/wfdb/io/_signal.py +++ b/src/wfdb/io/_signal.py @@ -144,7 +144,7 @@ def _checksums(signal): The sum of each column modulo 65536. """ - return [_checksum(channel) for channel in np.transpose(signal)] + return [int(c) for c in np.sum(signal, axis=0) % CHECKSUM_MODULUS] class SignalMixin: diff --git a/src/wfdb/io/convert/edf.py b/src/wfdb/io/convert/edf.py index a6c6458d..62e03787 100644 --- a/src/wfdb/io/convert/edf.py +++ b/src/wfdb/io/convert/edf.py @@ -10,7 +10,7 @@ from wfdb.io import download from wfdb.io._coreio import _open_input_file, _split_path -from wfdb.io._signal import _checksums +from wfdb.io._signal import _checksum from wfdb.io.annotation import Annotation, format_ann_from_df, wrann from wfdb.io.record import SIG_UNITS, Record, rdrecord @@ -431,7 +431,11 @@ def read_edf( # [ -5.004123, 43.546991, -99.481966, 51.64255 , -43.079939],... init_value = [int(s[0, 0]) for s in temp_all_sigs] - checksum = _checksums(sig_data) # not all values correct? + # Sum each channel separately: sig_data holds floats, so the order of the + # additions can change the result + checksum = [ + _checksum(channel) for channel in np.transpose(sig_data) + ] # not all values correct? edf_file.close() diff --git a/tests/io/test_convert.py b/tests/io/test_convert.py index f9708d40..9d0ecbd9 100644 --- a/tests/io/test_convert.py +++ b/tests/io/test_convert.py @@ -142,6 +142,46 @@ def test_edf_non_uniform(self): target_results = len(fields) * [True] assert np.array_equal(test_results, target_results) + def test_float_checksum(self, tmp_path): + """Each channel of the float signal is summed separately.""" + # Two signals of ten samples, each read as a physical value of about 0.1. + # Summing the rows of the array in order would give 0.9999999999999999. + n_sig = 2 + record_fields = [ + ("0", 8), + ("", 80), + ("", 80), + ("01.01.00", 8), + ("00.00.00", 8), + (str(256 * (n_sig + 1)), 8), + ("", 44), + ("1", 8), + ("1", 8), + (str(n_sig), 4), + ] + signal_fields = [ + ("x", 16), + ("", 80), + ("mV", 8), + ("-3276.8", 8), + ("3276.7", 8), + ("-32768", 8), + ("32767", 8), + ("", 80), + ("10", 8), + ("", 32), + ] + header = "".join(value.ljust(width) for value, width in record_fields) + for value, width in signal_fields: + header += value.ljust(width) * n_sig + edf_file = tmp_path / "float.edf" + samples = np.full(10 * n_sig, 2, "