From 8490a859f94dd1aa8d1daea8d261be5add29b24f Mon Sep 17 00:00:00 2001 From: "Goktug Ozkan, MD" <232098340+goktugozkanmd@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:07:10 +0300 Subject: [PATCH] Tidy up wfdb_to_mat and its tests Follow-ups from the review of #633, tracked in #642. No behaviour change: the checksum helper is the same expression the inline code used. - Drop the no-op `record.n_sig = num_channels` assignment and the `num_channels` alias, and read `record.sig_len` directly instead of copying it into `desired_length`. - Share the WFDB checksum through `_signal._checksum`, so the header checksum in `wfdb_to_mat` and `calc_checksum` cannot drift apart. - Split `test_missing_samples_and_skew_preserved` into two tests, so a failure in the first part no longer hides the skew check. - Note in `test_forward_slash_path` that it only guards Windows paths. Closes #642. Tests: 397 passed, 3 skipped (`pytest -q`, Windows, CPython 3.12); `ruff check src tests` clean. The 3 skips are the POSIX-permission and network-dependent tests that skip on this machine regardless of the patch. --- src/wfdb/io/_signal.py | 25 ++++++++++++++++++++++--- src/wfdb/io/convert/matlab.py | 31 ++++++++++++++----------------- tests/io/test_convert.py | 11 ++++++++--- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/wfdb/io/_signal.py b/src/wfdb/io/_signal.py index 92e52741..ec081aeb 100644 --- a/src/wfdb/io/_signal.py +++ b/src/wfdb/io/_signal.py @@ -12,6 +12,26 @@ MAX_I32 = 2147483647 MIN_I32 = -2147483648 +CHECKSUM_MODULUS = 65536 + + +def _checksum(values): + """Return the WFDB checksum of one channel of digital samples. + + Parameters + ---------- + values : array_like + The digital samples of a single channel. + + Returns + ------- + int + The sum of the samples modulo 65536. + + """ + return int(np.sum(values) % CHECKSUM_MODULUS) + + # Formats in which all samples align with integer (power-of-two) boundaries ALIGNED_FMTS = ["8", "16", "32", "61", "80", "160"] # Formats in which not all samples align with integer boundaries @@ -941,10 +961,9 @@ def calc_checksum(self, expanded=False): """ if expanded: - cs = [int(np.sum(s) % 65536) for s in self.e_d_signal] + cs = [_checksum(s) for s in self.e_d_signal] else: - cs = np.sum(self.d_signal, 0) % 65536 - cs = [int(c) for c in cs] + cs = [_checksum(s) for s in self.d_signal.T] return cs def wr_dat_files(self, expanded=False, write_dir="", wfdb_archive=None): diff --git a/src/wfdb/io/convert/matlab.py b/src/wfdb/io/convert/matlab.py index 717c3ebd..6d2e7f83 100644 --- a/src/wfdb/io/convert/matlab.py +++ b/src/wfdb/io/convert/matlab.py @@ -4,7 +4,7 @@ import numpy as np -from wfdb.io._signal import _digi_nan +from wfdb.io._signal import _checksum, _digi_nan from wfdb.io.record import rdrecord @@ -185,13 +185,12 @@ def wfdb_to_mat(record_name, pn_dir=None, sampfrom=0, sampto=None, channels=None # Ensure the signal size does not exceed the 2^31 byte limit max_length = int((2**31) / bytes_per_element / record.n_sig) - desired_length = record.sig_len # Snip record - if desired_length > max_length: + if record.sig_len > max_length: raise Exception("Can't write .mat file: data size exceeds 2GB limit") # Bytes of actual data - bytes_of_data = bytes_per_element * record.n_sig * desired_length + bytes_of_data = bytes_per_element * record.n_sig * record.sig_len # This is the remaining number of bytes that don't fit into integer # multiple of 8: i.e. if 18 bytes, bytes_remain = 2, from 17 to 18 bytes_remain = bytes_of_data % 8 @@ -241,7 +240,7 @@ def wfdb_to_mat(record_name, pn_dir=None, sampfrom=0, sampto=None, channels=None # Number of signals (4 bytes) f.write(struct.pack("