Skip to content

Commit fc4cfec

Browse files
author
Benjamin Moody
committed
Record.wrsamp: always set checksums to correct values.
When writing a record, the application generally shouldn't have to worry about details like calculating checksums. The wfdb package *should* ensure that the output files it generates are correct, and *should not* trust applications to calculate checksums correctly. Moreover, although it was originally intended for SignalMixin.wr_dats to verify that the application-supplied checksums were correct, this never actually worked, and existing test cases depend on that fact. So it is not reasonable for wr_dats to raise an exception if the checksums are wrong. Instead, update the checksums in the Record object when wrsamp is called (and note that we have to do this before calling wrheader, which is called before wr_dats). For compatibility with old test cases, that expect to be able to read and write records quasi-unmodified, we don't set checksums for signals that previously had a checksum of None, or that previously had a checksum that was correct mod 2**16.
1 parent 768d833 commit fc4cfec

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

wfdb/io/record.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,16 @@ def wrsamp(self, expanded=False, write_dir=""):
922922
N/A
923923
924924
"""
925+
# Update the checksum field (except for channels that did not have
926+
# a checksum to begin with, or where the checksum was already
927+
# valid.)
928+
if self.checksum is not None:
929+
checksums = self.calc_checksum(expanded=expanded)
930+
for ch, old_val in enumerate(self.checksum):
931+
if old_val is None or (checksums[ch] - old_val) % 65536 == 0:
932+
checksums[ch] = old_val
933+
self.checksum = checksums
934+
925935
# Perform field validity and cohesion checks, and write the
926936
# header file.
927937
self.wrheader(write_dir=write_dir, expanded=expanded)

0 commit comments

Comments
 (0)