Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/wfdb/io/_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions src/wfdb/io/convert/edf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()

Expand Down
40 changes: 40 additions & 0 deletions tests/io/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<i2")
edf_file.write_bytes(header.encode() + samples.tobytes())

record = read_edf(str(edf_file))
np.testing.assert_allclose(record.p_signal, 0.1)
assert record.checksum == [1, 1]


class TestCsvToWfdb:
"""Tests for the io.convert.csv module."""
Expand Down
9 changes: 4 additions & 5 deletions tests/test_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -1873,11 +1873,10 @@ def test_checksums_empty_signal(self):
signal = np.empty((0, 3), dtype=np.int16)
assert wfdb.io._signal._checksums(signal) == [0, 0, 0]

def test_checksums_float_matches_per_channel_sum(self):
"""Float signals sum each column individually, preserving read_edf parity."""
signal = np.array([[0.1, 0.0]] * 10)
expected = [int(np.sum(col) % 65536) for col in np.transpose(signal)]
assert wfdb.io._signal._checksums(signal) == expected
def test_checksums_rejects_1d(self):
"""A 1-D array raises TypeError rather than returning per-sample values."""
with pytest.raises(TypeError):
wfdb.io._signal._checksums(np.arange(10, dtype=np.int16))


def write_record_with_date(tmp_path, date):
Expand Down
Loading