Skip to content

Write selected multi-segment records with the modern API - #931

Merged
tompollard merged 4 commits into
mainfrom
write-segmented-record
Dec 4, 2026
Merged

tompollard merged 4 commits into
mainfrom
write-segmented-record

Conversation

@tompollard

@tompollard tompollard commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Add write_segments(path, data) and SegmentedRecordData.write(path) for writing a selected multi-segment read as a new WFDB record. This follows #930 and is the second PR in the segmented-write plan.

The writer generates unique segment names, preserves fixed or variable layout, keeps gaps at their selected lengths, writes a new variable-layout header in selected channel order, and uses the selected master time and comments rather than source provenance. Real segments with no selected channels become ~ gaps. It accepts local record stems, supported writable cloud URIs, and local .wfdb archives. Each real segment uses the existing RecordData.write validation and encoding path.

Writes are staged so a validation or encoding failure leaves the destination untouched. Existing destination files produce an overwrite warning. The legacy MultiRecord.wrsamp path is unchanged.

formats= chooses one target format for every channel or one per selected master channel. This lets records read from a format that cannot be written, such as format 8, be saved in a supported format. A fixed-layout window containing only gaps is rejected because its output would have no data segment to define the channels.

Tests

  • Fixed and variable layouts, physical and digital values, uniform and expanded signals.
  • Round trips through read_segments and combined read_record, including selected windows, channel order, gaps and absent channels.
  • Edited segment values and shifted times; output headers do not retain the original source date.
  • Cloud and archive targets, overwrite warnings, staging on failure, and explicit skew alignment.
  • Unwritable source formats converted with formats=, per-channel format mapping in variable layout, and rejection of fixed-layout gap-only output.
  • Full offline suite: 2405 passed, 4 skipped, 23 network tests deselected.
  • Ruff and warning-as-error Sphinx docs build pass.

A fixed-layout selection made only of ~ gaps was written as a master
listing gaps alone, which no reader accepts because no data segment
defines the channels. Raise before any file is created instead.

Add formats= to write_segments and SegmentedRecordData.write, for every
channel or one per selected channel in master order, mapped to each
segment's own channels. Records stored in formats the library cannot
write, such as format 8, can now be written.
@tompollard

Copy link
Copy Markdown
Member Author

I pushed b010628 with two fixes, on top of 00331f2.

What I checked first

I covered the five multi-segment sample records and small test records: fixed layout with a ~ gap, and format 8 in fixed and variable layout. For each, both kinds and both layouts, I used windows across the first five segment boundaries, a window from the start, and a window inside a gap, each with default and reversed channel order. All 272 written records that could be read back match the source in their segments, frame counts, gaps, channel names, values and masks. In all 252 windows where a combined read exists, read_record(output) equals read_record(source, window). Two cases didn't work, and b010628 fixes both.

1. A fixed-layout window made only of gaps was written but unreadable

A window inside a ~ segment of a fixed-layout record was written as out/1 1 200 100 followed by ~ 100. read_segments and read_record then rejected it ("Fixed-layout multi-segment record has no signal-bearing segment"), because in fixed layout a data segment defines the channels. write_segments now raises before creating any file:

Cannot write a fixed-layout record without a data segment to define its channels; select a window that includes one

2. No way to choose a writable format

Segments stored in a format the library can't write, such as format 8, the bit-packed formats, 61 or 160, always failed, because write_segments had no formats= option. write_segments and SegmentedRecordData.write now accept formats=: one format for every channel, or one per selected channel in master order. For variable layout, each segment receives the formats of the channels it contains, matched by name. A list of the wrong length is rejected before any file exists.

Tests

test_write_segments.py gains four tests (7 cases):

  • the gap-only rejection, with the destination left empty;
  • format 8 segments failing without formats= and round-tripping with formats="16", in both kinds and both layouts;
  • per-channel formats mapped across s25047's variable-layout segments;
  • the length check.

All 7 fail without the source change. My round-trip check, re-run with formats="16", gives 64 of 64 matches on the format 8 and gap records, and the 8 gap-only windows are refused. The changelog entry and docs/io.rst describe both behaviours, and also that segment and layout headers take their dates from the selected master. The offline suite (2,412 passed, 4 skipped), Ruff, and the docs build with warnings as errors all pass.

Not changed here

The comment in _write_master_header points at an older bug. Legacy MultiRecord.wrsamp writes a counter field without its closing parenthesis, for example 041s/2 7 125/250(100 2000 …, and 5.18.0 does the same. The new writer avoids it. Fixing the legacy writer should be a separate change.

@tompollard
tompollard merged commit 7c596c3 into main Dec 4, 2026
19 checks passed
@tompollard
tompollard deleted the write-segmented-record branch December 4, 2026 22:17
tompollard added a commit that referenced this pull request Dec 4, 2026
## Summary

Prepare wfdb 6.1.0, the first minor release after 6.0.0. This release
adds the modern segmented-record workflow:

- Plot a `SegmentedRecordData` on one timeline with boundaries and gaps
(#928).
- Inspect selected master metadata and edit a segment or shift times
(#930).
- Write fixed- and variable-layout segmented records, including selected
windows, gaps, and explicit target formats (#931).

This PR bumps the version in `pyproject.toml` and collects the three
changelog fragments into `docs/changes.rst`.

## Verification

- `uv build --offline` produced `wfdb-6.1.0.tar.gz` and
`wfdb-6.1.0-py3-none-any.whl`.
- Sphinx HTML build passed with `-W --keep-going`.
- The release diff contains only the version and changelog changes.

After this PR is merged, publish GitHub release `v6.1.0`. The release
workflow will build and upload to PyPI through Trusted Publishing.
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