diff --git a/src/wfdb/io/_signal.py b/src/wfdb/io/_signal.py index c7a1f3a2..409bd003 100644 --- a/src/wfdb/io/_signal.py +++ b/src/wfdb/io/_signal.py @@ -11,6 +11,8 @@ MAX_I32 = 2147483647 MIN_I32 = -2147483648 +CHECKSUM_MODULUS = 65536 + # 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 @@ -111,6 +113,40 @@ } +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) + + +def _checksums(signal): + """Return the WFDB checksum of each channel (column) of a 2-D signal array. + + Parameters + ---------- + signal : ndarray + A 2-D array of digital samples with one column per channel. + + Returns + ------- + list of int + The sum of each column modulo 65536. + + """ + return [_checksum(channel) for channel in np.transpose(signal)] + + class SignalMixin: """Mixin class with signal methods. Inherited by Record class. @@ -940,10 +976,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 = _checksums(self.d_signal) return cs def wr_dat_files(self, expanded=False, write_dir="", wfdb_archive=None): diff --git a/src/wfdb/io/convert/edf.py b/src/wfdb/io/convert/edf.py index 3be6ae04..5e74366c 100644 --- a/src/wfdb/io/convert/edf.py +++ b/src/wfdb/io/convert/edf.py @@ -9,6 +9,7 @@ import pandas as pd from wfdb.io import download +from wfdb.io._signal import _checksums from wfdb.io.annotation import Annotation, format_ann_from_df, wrann from wfdb.io.record import SIG_UNITS, Record, rdrecord @@ -424,9 +425,7 @@ 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 = [ - int(np.sum(v) % 65536) for v in np.transpose(sig_data) - ] # not all values correct? + checksum = _checksums(sig_data) # not all values correct? edf_file.close() diff --git a/src/wfdb/io/convert/matlab.py b/src/wfdb/io/convert/matlab.py index 717c3ebd..d365a8d3 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 _checksums, _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("