Skip to content

Commit 3547521

Browse files
authored
Sum whole signals at once in _checksums (#676)
A small follow-up to #653. `_checksums` sums each column in a Python loop, which has two side effects for `calc_checksum`: | | `axis=0` (this PR) | per column | |---|---|---| | 10M x 12, int16 | 74 ms | 40 ms | | 10M x 2, int32 | 40 ms | 6 ms | | 1M x 64, int16 | 25 ms | 138 ms | | 1-D `d_signal` | `TypeError` | `[0, 1, 2, ...]` | The loop is faster for records with few channels, but several times slower for recordings with many channels. The absolute cost is small either way. The more important difference is that a 1-D array returns one value per sample instead of raising an error. `_checksums` goes back to a single reduction along axis 0, which is what `calc_checksum` and `wfdb_to_mat` used before #653. For integer samples the result is the same either way. `read_edf` is the only caller with float samples, where the order of the additions can change the truncated result (ten values of 0.1 sum to 1.0 by column, and to 0.9999999999999999 along axis 0). It keeps summing each channel separately, so its checksums don't change. Testing: - `test_checksums_rejects_1d` is restored. - New `test_float_checksum` reads a small two-channel EDF file and checks the checksums. It fails if `read_edf` uses `_checksums`. - `uv run pytest -m "not network" -W error` passes (566), as do `ruff format --check` and `ruff check`. No changelog entry, since #653 is not released and behaviour is the same as in the last release. <sub>@tompollard 🟥 red 🐳 whale · quince</sub>
2 parents d7087ea + 02de0d7 commit 3547521

4 files changed

Lines changed: 51 additions & 8 deletions

File tree

‎src/wfdb/io/_signal.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def _checksums(signal):
144144
The sum of each column modulo 65536.
145145
146146
"""
147-
return [_checksum(channel) for channel in np.transpose(signal)]
147+
return [int(c) for c in np.sum(signal, axis=0) % CHECKSUM_MODULUS]
148148

149149

150150
class SignalMixin:

‎src/wfdb/io/convert/edf.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from wfdb.io import download
1212
from wfdb.io._coreio import _open_input_file, _split_path
13-
from wfdb.io._signal import _checksums
13+
from wfdb.io._signal import _checksum
1414
from wfdb.io.annotation import Annotation, format_ann_from_df, wrann
1515
from wfdb.io.record import SIG_UNITS, Record, rdrecord
1616

@@ -431,7 +431,11 @@ def read_edf(
431431
# [ -5.004123, 43.546991, -99.481966, 51.64255 , -43.079939],...
432432

433433
init_value = [int(s[0, 0]) for s in temp_all_sigs]
434-
checksum = _checksums(sig_data) # not all values correct?
434+
# Sum each channel separately: sig_data holds floats, so the order of the
435+
# additions can change the result
436+
checksum = [
437+
_checksum(channel) for channel in np.transpose(sig_data)
438+
] # not all values correct?
435439

436440
edf_file.close()
437441

‎tests/io/test_convert.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,46 @@ def test_edf_non_uniform(self):
142142
target_results = len(fields) * [True]
143143
assert np.array_equal(test_results, target_results)
144144

145+
def test_float_checksum(self, tmp_path):
146+
"""Each channel of the float signal is summed separately."""
147+
# Two signals of ten samples, each read as a physical value of about 0.1.
148+
# Summing the rows of the array in order would give 0.9999999999999999.
149+
n_sig = 2
150+
record_fields = [
151+
("0", 8),
152+
("", 80),
153+
("", 80),
154+
("01.01.00", 8),
155+
("00.00.00", 8),
156+
(str(256 * (n_sig + 1)), 8),
157+
("", 44),
158+
("1", 8),
159+
("1", 8),
160+
(str(n_sig), 4),
161+
]
162+
signal_fields = [
163+
("x", 16),
164+
("", 80),
165+
("mV", 8),
166+
("-3276.8", 8),
167+
("3276.7", 8),
168+
("-32768", 8),
169+
("32767", 8),
170+
("", 80),
171+
("10", 8),
172+
("", 32),
173+
]
174+
header = "".join(value.ljust(width) for value, width in record_fields)
175+
for value, width in signal_fields:
176+
header += value.ljust(width) * n_sig
177+
edf_file = tmp_path / "float.edf"
178+
samples = np.full(10 * n_sig, 2, "<i2")
179+
edf_file.write_bytes(header.encode() + samples.tobytes())
180+
181+
record = read_edf(str(edf_file))
182+
np.testing.assert_allclose(record.p_signal, 0.1)
183+
assert record.checksum == [1, 1]
184+
145185

146186
class TestCsvToWfdb:
147187
"""Tests for the io.convert.csv module."""

‎tests/test_record.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,11 +1873,10 @@ def test_checksums_empty_signal(self):
18731873
signal = np.empty((0, 3), dtype=np.int16)
18741874
assert wfdb.io._signal._checksums(signal) == [0, 0, 0]
18751875

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

18821881

18831882
def write_record_with_date(tmp_path, date):

0 commit comments

Comments
 (0)