Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/wfdb/io/_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@
MAX_I32 = 2147483647
MIN_I32 = -2147483648

CHECKSUM_MODULUS = 65536

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: the new function now sits between MIN_I32 and the format constants below (ALIGNED_FMTS and so on). Keeping CHECKSUM_MODULUS with the other constants and moving _checksum after them would keep the constants together.



def _checksum(values):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to have the checksum in one place. As mentioned in the review summary, edf.py:429 and wav.py:317 still compute the same expression inline, and record.py:936 uses a bare 65536, so for the checksums to really be unable to drift apart those would need to use this helper (or the _checksums variant suggested on calc_checksum) and CHECKSUM_MODULUS too.

"""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
Expand Down Expand Up @@ -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]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two side effects of switching from np.sum(self.d_signal, 0) to a loop over self.d_signal.T:

  1. Speed depends a lot on the number of channels. Each column of the C-ordered d_signal is a strided view, so summing column by column reads memory differently from one axis=0 reduction. Timings of the old and new expressions (numpy 2.5.4, best of 5):

    d_signal before after
    10M x 12, int16 74 ms 40 ms
    10M x 2, int32 39 ms 6 ms
    1M x 64, int16 24 ms 137 ms

    Faster for typical ECG records, but about 5.6x slower for 64-channel recordings such as EEG. The absolute cost is small next to writing the files, so this does not block anything.

  2. A 1-D d_signal is now silently wrong instead of an error. Before, calc_checksum() raised TypeError: 'numpy.int64' object is not iterable. Now the loop runs over samples, and returns one "checksum" per sample ([0, 1, 2, ...] for np.arange(10)). Normal use does not reach this, because d_signal.shape[1] is read earlier (line 508) and wrsamp reshapes 1-D input, so only a hand-built Record calling calc_checksum() directly hits it.

A helper for whole signals would keep the vectorised reduction and avoid both:

def _checksums(signal):
    """Return the WFDB checksum of each channel (column) of a 2-D array of digital samples."""
    return [int(c) for c in np.sum(signal, axis=0) % CHECKSUM_MODULUS]

used here (cs = _checksums(self.d_signal)), in wfdb_to_mat, and in edf.py:429 and wav.py:317, keeping _checksum for the expanded branch, where the channels of e_d_signal can differ in length.

return cs

def wr_dat_files(self, expanded=False, write_dir="", wfdb_archive=None):
Expand Down
31 changes: 14 additions & 17 deletions src/wfdb/io/convert/matlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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("<I", record.n_sig))
# Number of rows (4 bytes)
f.write(struct.pack("<I", desired_length))
f.write(struct.pack("<I", record.sig_len))

# Subelement 3: Array name
# Subtag 3: data type (4 bytes)
Expand Down Expand Up @@ -293,9 +292,9 @@ def wfdb_to_mat(record_name, pn_dir=None, sampfrom=0, sampto=None, channels=None

print(f"Source: record {record_name}\t\tStart: {datetime_string}")
print(
f"val has {record.n_sig} rows (signals) and {desired_length} columns (samples/signal)"
f"val has {record.n_sig} rows (signals) and {record.sig_len} columns (samples/signal)"
)
duration_string = str(datetime.timedelta(seconds=desired_length / record.fs))
duration_string = str(datetime.timedelta(seconds=record.sig_len / record.fs))
print(f"Duration: {duration_string}")
print(f"Sampling frequency: {record.fs} Hz\tSampling interval: {1 / record.fs} sec")
print(
Expand All @@ -312,20 +311,18 @@ def wfdb_to_mat(record_name, pn_dir=None, sampfrom=0, sampto=None, channels=None
)

# Modify the record file to reflect the new data
num_channels = record.n_sig
record.record_name = record_name_out
record.n_sig = num_channels
record.samps_per_frame = num_channels * [1]
record.samps_per_frame = record.n_sig * [1]
record.skew = None
record.file_name = num_channels * [output_file]
record.fmt = num_channels * [wfdb_type]
record.byte_offset = num_channels * [192]
record.file_name = record.n_sig * [output_file]
record.fmt = record.n_sig * [wfdb_type]
record.byte_offset = record.n_sig * [192]
record.adc_res = adc_res
record.baseline = [baseline[i] - adc_zero[i] for i in range(num_channels)]
record.adc_zero = num_channels * [0]
record.block_size = num_channels * [0]
record.baseline = [baseline[i] - adc_zero[i] for i in range(record.n_sig)]
record.adc_zero = record.n_sig * [0]
record.block_size = record.n_sig * [0]
record.init_value = shifted_data[0].tolist()
record.checksum = [int(c) for c in np.sum(shifted_data, axis=0) % 65536]
record.checksum = [_checksum(s) for s in shifted_data.T]

# Write the header file RECm.hea
record.wrheader()
Expand Down
11 changes: 8 additions & 3 deletions tests/io/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,8 @@ def test_shifted_samples_exceed_int32(self, tmp_path, monkeypatch):
wfdb_to_mat("too_large")
assert not (tmp_path / "too_largem.mat").exists()

def test_missing_samples_and_skew_preserved(self, tmp_path, monkeypatch):
"""Missing samples are preserved without overflow and skew is not doubled."""
def test_missing_samples_preserved(self, tmp_path, monkeypatch):
"""Missing samples are preserved without overflow."""
monkeypatch.chdir(tmp_path)

# Missing samples with non-zero adc_zero (format 516)
Expand All @@ -661,7 +661,10 @@ def test_missing_samples_and_skew_preserved(self, tmp_path, monkeypatch):
mixed_reloaded.p_signal, mixed_orig.p_signal, equal_nan=True
)

# Skewed record should not be skewed a second time when read back
def test_skew_not_applied_twice(self, tmp_path, monkeypatch):
"""Skewed record should not be skewed a second time when read back."""
monkeypatch.chdir(tmp_path)

skew_src = str(self.SAMPLE_DATA / "100skew")
wfdb_to_mat(skew_src, sampto=20)
skew_orig = rdrecord(skew_src, sampto=20)
Expand All @@ -671,6 +674,8 @@ def test_missing_samples_and_skew_preserved(self, tmp_path, monkeypatch):
def test_forward_slash_path(self, tmp_path, monkeypatch):
"""Forward-slash record paths work cross-platform and write into cwd."""
monkeypatch.chdir(tmp_path)
# Only meaningful on Windows: elsewhere the old code handled this
# path already, so keep the test to guard the basename handling.
Comment on lines +677 to +678

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads as both "the test is pointless here" and "keep it". Maybe:

Suggested change
# Only meaningful on Windows: elsewhere the old code handled this
# path already, so keep the test to guard the basename handling.
# The bug this guards against only appeared on Windows; on other
# platforms this checks that the basename is still used.

src = (self.SAMPLE_DATA / "100").as_posix()

wfdb_to_mat(src, sampto=8)
Expand Down
Loading