Repository navigation
Support multi-segment records in .wfdb archive files - #583
Merged
Merged
Conversation
tompollard
force-pushed
the
tp/wfdb_file
branch
from
October 28, 2026 21:27
a80f917 to
51ef012
Compare
tompollard
force-pushed
the
tp/wfdb_file_multiseg
branch
from
October 29, 2026 01:53
bb41a90 to
6344eb8
Compare
tompollard
force-pushed
the
tp/wfdb_file_multiseg
branch
3 times, most recently
from
October 29, 2026 15:44
75c188c to
680c727
Compare
tompollard
force-pushed
the
tp/wfdb_file
branch
2 times, most recently
from
October 31, 2026 19:38
303895f to
c7dba93
Compare
The archive of a multi-segment record holds the main header together with the header and signal files of each segment. rdrecord, rdsamp and rdheader (with rd_segments) read the segments from the same open archive, and MultiRecord.wrsamp takes a wfdb_archive argument to write the whole record into one archive. An archive still holds a single record: it can contain several headers only if one is a multi-segment header listing all the others as its segments. This is now checked when the archive is closed, so files can be added in any order. Writing a file that is already in the archive is allowed if the content is the same, since segments can share a signal file. Reading a fixed-layout record with channel_names now uses the first non-empty segment for the signal names, as intended, instead of the first segment, which can be empty.
The layout header of a variable layout multi-segment record lists its signals as null signals (format 0, file name '~'), with no samples. Writing such a record failed: the header validation rejected format 0 and the file name '~', and Record.wrsamp tried to compute checksums and write dat files for signals with no data. Null signals are now accepted (file name '~' together with format 0), and only the header is written for a record whose signals have no files. This applies to records written to files as well as to .wfdb archives.
set_defaults() raised a KeyError for format 0 (null signals), so a variable layout record created from scratch couldn't be written. Use the WFDB library's default resolution (12 bits) for format 0, as readheader does for formats without a natural resolution. A record that mixes null signals with signals that have samples is rejected with a clear error before anything is written, instead of failing with a KeyError.
Checking each header against the set of all other header names took quadratic time when the main header was not the first header added (0.3 s for 5000 segments). Headers that list fewer segments than there are other headers are now skipped without building that set.
tompollard
force-pushed
the
tp/wfdb_file_multiseg
branch
from
October 31, 2026 19:55
680c727 to
0af27fe
Compare
tompollard
marked this pull request as ready for review
October 31, 2026 20:07
lukepayyapilli
approved these changes
Nov 1, 2026
lukepayyapilli
left a comment
Contributor
There was a problem hiding this comment.
Thanks Tom, checked 0af27fe2 locally, merged with current main. The offline tests pass (294), ruff check and ruff format --check are clean, and CI is green.
- Round trips:
fixed1,041s,s00001ands25047all read back from an archive the same as from the files, both when written withMultiRecord.wrsamp(wfdb_archive=...)and when built withcreate_archive, for full reads and sample ranges across segment boundaries. - Shared signal files: with one segment of
fixed1changed so its share ofv102s.datdiffers, the archive write fails with the "different content" error and the existing archive at that path is left unchanged. Writing the same record to a directory silently overwritesv102s.dat(so segment 1 ends up with segment 3's samples). That behaviour predates this PR, and the archive check is an improvement on it. - Tests: removing
BIT_RES["0"], the mixed null-signal check or the skip for re-written identical files each makes tests fail. The fixed-layoutchannel_nameschange isn't covered, but perheader(5)fixed-layout segments always contain samples, so it only guards against invalid input.
Approving.
Member
Author
|
thanks luke! |
tompollard
added a commit
that referenced
this pull request
Nov 1, 2026
As discussed in #606: each change now adds its own file in `changelog.d/` instead of editing `docs/changes.rst`, so pull requests no longer conflict on the changelog. - Fragments are plain `.rst` files named after the change (for example `rr2ann-fixes.rst`), in the same bold-heading format as now. `changelog.d/README.rst` explains this. - At release time, `uv run towncrier build --version X.Y.Z --date "Mon YYYY"` adds a `Version X.Y.Z (Mon YYYY)` section to `docs/changes.rst` and deletes the fragments. Entries are sorted by file name. - The 25 existing unreleased entries are moved into `changelog.d/` unchanged. I checked that `towncrier build --draft` reproduces them exactly, and ran a real build on a copy: the docs build passes with `-W` both before and after. - `towncrier` is pinned in the `dev` group, like Ruff. AGENTS.md and DEVELOPING.md are updated. One trade-off: the unreleased changes no longer show on the docs site until a release. If you'd like them there, `sphinxcontrib-towncrier` can render a draft section, but that touches `docs/conf.py`, so I'd do it after #616. #619 and #583 add entries to `docs/changes.rst`, so they'd need to move them into a fragment after this is merged (or the other way round, whichever goes in first).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #541, which added
.wfdbarchive files for single-segment records. This adds support for multi-segment records.Changes
rdrecord,rdsampandrdheader(withrd_segments=True) read the segments from the same open archive. This covers fixed and variable layouts, empty segments (~), sample ranges,channels,channel_namesandm2s=False.MultiRecord.wrsamptakes awfdb_archiveargument and writes the whole record into one archive.sample-data/multi-segment/fixed1), so writing a file that is already in the archive is allowed if the content is the same.channel_namesnow takes the signal names from the first non-empty segment, as the code intended, instead of the first segment, which can be empty.MultiRecord.wrsampfailed for variable layout records, such as those read withrdrecord(..., m2s=False). The layout header lists its signals as null signals (format0, file name~), which the header validation rejected, andRecord.wrsamptried to compute checksums and write.datfiles for signals without samples. Null signals are now accepted, and only the header is written for a record whose signals have no files.set_defaults()no longer fails for null signals: they get the WFDB library's default resolution (12 bits), so a variable layout record created from scratch can be written too. A record that mixes null signals with signals that have samples is rejected with a clear error.Example
Tests
sample-data/multi-segment. This includes sample ranges, channel selection andm2s=False.test_record.pyhas new tests that write a variable layout record to files and read it back (one read fromsample-data, one created from scratch), and that check the error for a record that mixes null signals with other signals.p000878isn't used in the archive tests, because one of its segments is not included insample-data.