Skip to content

Skewed signals lose samples stored after the declared record length #934

Done

Description

@tompollard

When a signal has skew, an aligned read needs that many samples from after the record's last declared frame. wfdb-python never reads past the declared length (sig_len). It marks those samples missing even when the signal file contains them.

Example

This is the skew-and-byte-offset test record proposed for the WFDB specification (wfdb/wfdb-spec#32). Two format 16 signals share a file. Signal b has skew 2, and the file holds six frames, (1,7) (2,-7) (3,10) (4,20) (0,30) (0,40), while the header declares 4:

skew 2 250 4
skew.dat 16+8 200(0)/mV 12 0 1 10 0 a
skew.dat 16:2+8 200(0)/mV 12 0 10 100 0 b

The aligned samples of b are 10, 20, 30, 40. wfdb-python returns:

wfdb.rdrecord(path, physical=False, smooth_frames=False).e_d_signal[1]
# [10, 20, -32768, -32768]

wfdb.read_record(path, kind="digital", layout="expanded").signals.values[1]
# [10, 20, --, --]   (masked)

The same happens without the byte offset, and 5.17.0 behaves identically.

Cause

_dat_read_params in _signal.py caps the read at sig_len frames:

# Cannot exceed sig_len * tsamps_per_frame, the number of samples
# stored in the file.
if (sampto + max(skew)) > sig_len:
    end_flat_sample = sig_len * tsamps_per_frame
    extra_flat_samples = (sampto + max(skew) - sig_len) * tsamps_per_frame
...
nan_replace = [max(0, sampto + s - sig_len) for s in skew]

The comment's assumption is wrong for skewed records. Skew counts samples before frame 0 in the file, so a file that keeps every signal's samples after alignment has up to max(skew) frames past the declared length. The proposed specification's test record expects a reader to use those frames.

Expected

Read up to sampto + max(skew) frames, limited by the number of complete frames the file actually holds, which is derived from its size and byte offset as _infer_sig_len already does. Mark as missing only the samples that are really absent. A file that does end at the declared length still gives missing samples, as it does now, including the format 8 handling from #913.

Tests

  • The record above, read with both readers, in both layouts and with signal_alignment="aligned". Raw reads are already correct.
  • The same record without the byte offset, and a version truncated at the declared length (still missing).
  • A window ending before the last frame, to check that stop still limits what is returned.
  • Formats 212 and 310/311, where the extra frames end partway through a block.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions