Skip to content

Fix wfdb_to_mat encoding, slicing, and header metadata - #633

Open
goktugozkanmd wants to merge 1 commit into
MIT-LCP:mainfrom
goktugozkanmd:fix/wfdb-to-mat
Open

goktugozkanmd wants to merge 1 commit into
MIT-LCP:mainfrom
goktugozkanmd:fix/wfdb-to-mat

Conversation

@goktugozkanmd

Copy link
Copy Markdown

Fixes several crashes and data-corruption bugs in wfdb_to_mat (src/wfdb/io/convert/matlab.py), which previously had no unit tests:

  • Channel selection (channels): channels was not forwarded to rdrecord(), so all channels were written to .mat while record.n_sig was overwritten to len(channels), causing record.wrheader() to raise ValueError.
  • Sample slicing (sampfrom without sampto): desired_length = sampto - sampfrom subtracted sampfrom a second time after rdrecord() had already sliced the signal, corrupting the declared matrix dimensions or raising struct.error when sampfrom > sig_len / 2.
  • 8-bit (80) and 24/32-bit (24/32) binary encoding & MAT v5 padding: signal samples were always packed with struct.pack("<%sh", ...) regardless of bytes_per_element, corrupting format 80 files (TypeError in scipy.io.loadmat, missing the +128 offset-binary shift) and crashing on 32-bit values outside int16 range. Trailing 8-byte MAT v5 alignment padding (bytes_remain) was also accounted for in master_bytes but never written.
  • Missing samples & unset header fields: reading via physical=False (d_signal) avoids float round-trip errors and NaN-cast warnings; missing/invalid samples (_digi_nan) are mapped to the target format's sentinel instead of overflowing when subtracting adc_zero (e.g., sample-data/mixedsignals). Unset (None) adc_res, adc_zero, baseline, units, and sig_name fields (e.g., sample-data/n8_evoked_raw_95_F1_R9) no longer raise TypeError/AttributeError.
  • Companion .hea accuracy & cross-platform paths: clears already-applied skew, sets block_size to 0, sets init_value from signed decoded samples, recomputes checksum for the written output, and uses os.path.basename(os.fspath(record_name)) so forward-slash paths work on Windows as well as POSIX.

Changes

  • src/wfdb/io/convert/matlab.py: fix reading, binary serialization, padding, missing-sample preservation, and companion header metadata in wfdb_to_mat.
  • tests/io/test_convert.py: add TestWfdbToMat covering 16-bit padding and round-trip, channels + sampfrom slicing, 8-bit (format 80) offset-binary output, 24/32-bit records with unset header fields, missing-sample preservation and skew handling, and forward-slash paths (verified with both scipy.io.loadmat and wfdb.rdrecord).
  • changelog.d/wfdb-to-mat-fixes.rst: add towncrier changelog fragment.

Verification

  • uv run ruff format --check --diff . — passed
  • uv run ruff check . — passed
  • uv run towncrier build --draft — passed
  • uv run pytest tests/io/test_convert.py -v — 22 passed
  • uv run pytest -n auto -q -k "not url and not pn_dir" — 360 passed, 4 skipped (2 POSIX permissions, 2 remote PhysioNet 502)

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant