Repository navigation
Tidy up wfdb_to_mat and its tests (#642 follow-ups) #653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,26 @@ | |||||||||||||
| MAX_I32 = 2147483647 | ||||||||||||||
| MIN_I32 = -2147483648 | ||||||||||||||
|
|
||||||||||||||
| CHECKSUM_MODULUS = 65536 | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _checksum(values): | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||||||||||||||
| """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 | ||||||||||||||
|
|
@@ -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] | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two side effects of switching from
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 ( |
||||||||||||||
| return cs | ||||||||||||||
|
|
||||||||||||||
| def wr_dat_files(self, expanded=False, write_dir="", wfdb_archive=None): | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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) | ||||||||||
|
|
@@ -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) | ||||||||||
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||
| src = (self.SAMPLE_DATA / "100").as_posix() | ||||||||||
|
|
||||||||||
| wfdb_to_mat(src, sampto=8) | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
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_I32and the format constants below (ALIGNED_FMTSand so on). KeepingCHECKSUM_MODULUSwith the other constants and moving_checksumafter them would keep the constants together.