Skip to content

Make flattened multi-segment records writable - #728

Merged
tompollard merged 2 commits into
mainfrom
issue-727-write-flattened
Nov 25, 2026
Merged

tompollard merged 2 commits into
mainfrom
issue-727-write-flattened

Conversation

@tompollard

@tompollard tompollard commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Prepare a copy of a flattened multi-segment Record when Record.wrsamp is called, leaving the caller's object unchanged. Use source_segments to detect records flattened from a MultiRecord.
  • Fill required header fields from the output digital signal. When source formats or calibration are incomplete, or channels sharing a file have incompatible formats, use format 32 and recalibrate physical signals. Digital missing samples are mapped to the format 32 missing marker.
  • Require callers to resolve conflicting units explicitly; channels absent from the selected window use NU.

Callers should assign a new record_name and file_name before writing a copy, as shown in the issue. Format 32 can make the output larger than the source.

Fixes #727.

Validation

  • pytest tests/test_multi_record.py -q — 41 passed
  • pytest -m 'not network' -q — 762 passed, 4 skipped, 21 deselected
  • Ruff check and format checks — passed
  • Sphinx HTML build with -W --keep-going — passed

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes #727: every flattened record I wrote read back correctly. Two changes I'd suggest before merging: avoid modifying the caller's record, and detect flattened records explicitly.

Checked:

  • Round trips: 99 random windows across p000878-2137-10-26-16-57, s25047-2704-05-04-10-44, v102s and 041s, with physical and digital reads, and with smoothed and expanded frames. Each was written with wrsamp and read back, and all matched: physical values within half a digital unit, digital values exactly (with missing markers mapped to the format 32 marker).
  • The WFDB C tools: rdsamp -p (10.7.0) reads a written p000878 window, showing missing samples as -.
  • Tests: pytest -m "not network" (756 passed) and ruff pass.

Suggestions:

  1. wrsamp changes the caller's record. After writing a digital p000878 window (sampfrom=3700, sampto=3800), these fields had changed in memory: d_signal, fmt, adc_gain, baseline, units, adc_res, adc_zero, init_value, checksum and block_size. For example, fmt went from ['80', None, None, None] to ['32', '32', '32', '32'], and the missing samples in d_signal went from -128 to -2147483648. Code that keeps using the record afterwards, such as dac() or a comparison with another read, would then see different data from what rdrecord returned. Preparing and writing a copy would leave the caller's record unchanged.
  2. Detect flattened records explicitly. getattr(self, "layout", None) in ("fixed", "variable") depends on multi_to_single leaving the multi-segment layout attribute on the flattened Record. Once #726 merges, self.source_segments is not None would mark these records explicitly.

Minor: the docstring or changelog could mention that a channel with no samples in the window is written with gain 1 and baseline -2147483648 in format 32. The PR description notes that format 32 can make the output larger than the source, and that would be useful in the user-facing docs too.

@tompollard
tompollard force-pushed the issue-727-write-flattened branch from 473405e to 9dec71b Compare November 25, 2026 00:26
@tompollard

Copy link
Copy Markdown
Member Author

Thanks for the thorough review. I rebased on main after #726 merged and updated the PR in 9dec71b:

  • wrsamp now uses source_segments is not None to identify flattened records and prepares a separate copy for writing. The caller's signal arrays and metadata stay unchanged. The round-trip tests now assert that explicitly for physical and digital records, with smoothed and expanded frames.
  • The docstring and changelog explain that format 32 can increase file size, that missing samples use its digital missing marker, and that entirely missing channels have placeholder calibration values.

One clarification on the suggested exact baseline: it is not always -2147483648. For the reported p000878 physical window, the entirely absent channels are written with gain 1 and baseline 1; in the digital path their fallback baseline is 0. -2147483648 is the format 32 missing-sample marker. I left the exact placeholder calibration out of the public contract.

Validation: 41 multi-record tests, 762 offline tests, Ruff, and the strict Sphinx build pass.

@tompollard
tompollard merged commit f2e16bd into main Nov 25, 2026
19 checks passed
@tompollard
tompollard deleted the issue-727-write-flattened branch November 25, 2026 00:35
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.

Records flattened from multi-segment records can't be written with wrsamp

1 participant