Skip to content

Add selected master metadata and validated segmented edits - #930

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

tompollard merged 3 commits into
mainfrom
segmented-write-model

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

Add an immutable selected master view to SegmentedRecordData, separate from its original source_header. It records the selected frame count, channels, signal kind and layout, start time, and adjusted base counter.

Add with_segment(index, data) to replace a real segment after checking its frame count, sampling frequency, signal kind and layout, and fixed- or variable-layout channel rules. Add shift_time(delta) to move the selected master start and any stated segment times together while leaving source headers as provenance.

This prepares a reliable input for a later write_segments implementation; this PR does not write a multi-segment record.

Verification

  • Focused segmented-read and edit tests pass, including selected counters, replacement validation, and time shifts.
  • The offline suite passed before the final counter-field test was added (2,385 passed, 4 skipped, 23 network tests deselected). That new test passes locally.
  • Ruff and the Sphinx documentation build pass.

@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 is the foundation the writer needs, and the validation catches the cases I tried. One compatibility problem needs fixing first: adding the required metadata field breaks SegmentedRecordData objects pickled by released versions. There's also one missing check. The offline suite (2,386 passed, 4 skipped) and Ruff pass.

What I checked

  • with_segment rejects invalid replacements: a gap index, a replacement one frame short, a variable-layout segment renamed to channels not in the master, and a fixed-layout segment that is renamed or has its channels reordered. Each fails with a clear message. Replacing a segment with its own data succeeds.
  • shift_time keeps segments consistent with the master. On s25047, shifting by 10 days and 3 hours moves the master from 2704-05-04 10:44:19.329 to 2704-05-14 13:44:19.329. Each segment's stated time (time-only in this record) moves by the same 3 hours, wrapping as RecordData.shift_time does, and stays within 1 ms of master time plus offset, as in the source. source_header keeps the original 10:44:18.529. A record with no stated times (v102s) raises the existing "Cannot shift a record without a base date or time".

To fix: older pickles lose n_frames

read_segments shipped in 5.18.0 and 6.0.0. I pickled two of its results with 5.18.0: a fixed-layout v102s window and a variable-layout expanded s25047 window. Loaded on this branch, n_frames, metadata and shift_time all fail with:

AttributeError: 'SegmentedRecordData' object has no attribute 'metadata'

It's the same problem #904 fixed for SignalRead. A __setstate__ that rebuilds metadata when it's missing would fix it, using the stored source_header, channel_header, source_selection and segments: the frame count, the master's start time adjusted for the selection, channel names, and kind and layout from the first real segment. Please add checked-in pickles generated at v6.0.0 as fixtures, as #904 did, covering fixed and variable layouts, and test that n_frames, plot, with_segment and shift_time work after loading.

To fix: variable-layout replacements aren't checked for samples per frame

For fixed layout, with_segment compares samples_per_frame with the master. For variable layout it checks only that the names are among the master channels. An expanded replacement whose channel has a different number of samples per frame from the layout header passes, but the combined reader rejects such a record ("Incorrect samples per frame ... for signal ... in segment ..."), so a written record would fail to read as combined data. For expanded data, comparing each present channel's samples per frame with metadata.samples_per_frame at that channel's master position would catch it when the edit is made.

For later

  • Renaming channels in a fixed-layout record. with_segment requires fixed-layout names to match the master, so channels can't be renamed through it at all. A container-level with_channels would rename the master and every segment together. That isn't needed for the writer, but users will expect it.
  • Shifting segment times when the master has none. shift_time raises if the master has no stated time, even when segments do. That's consistent with RecordData, so it's fine for now, but the writer should decide which header's time is authoritative when they differ.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for catching these. I added compatibility handling for segmented reads pickled by v6.0.0. On load, it reconstructs the selected metadata from the saved master header, channel header, selection, and segments. The regression fixtures were generated with the v6.0.0 tag and cover fixed/uniform and variable/expanded windows; the tests exercise n_frames, plotting, with_segment, and shift_time.

with_segment now checks each present channel's samples per frame against the selected master layout in variable-layout expanded reads. I added a replacement-segment test that would previously pass validation and produce an invalid combined record.

I left the container-level channel edit and timestamp-free shift suggestions for follow-ups, since neither is needed for this writer foundation.

@tompollard
tompollard merged commit 887ac09 into main Dec 4, 2026
19 checks passed
@tompollard
tompollard deleted the segmented-write-model branch December 4, 2026 22:10
tompollard added a commit that referenced this pull request Dec 4, 2026
## 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.
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