Repository navigation
Write selected signal views to a destination path - #776
Conversation
tompollard
left a comment
There was a problem hiding this comment.
This works well, but one thing should be fixed before merge: the destination name isn't validated, so write() can produce records that can't be read back.
Round trips. I wrote frames 5–405 of 13 sample records in both layouts and both kinds, then read them back. Every writable record gave identical values (physical within 1e-3), and check_record reported no issues.
- This covered multi-rate records (
mixedsignals,03700181,drive02), FLAC, skew, anda103l(a.matfile with a byte offset). - A uniform write of a multi-rate record correctly writes one sample per frame.
- Cloud archives and
physionet://destinations are rejected, and the source result is unchanged.
Should fix: destination names that can't be read back. In write(), the record name comes from the basename of path, and nothing checks it.
read.write("x.v1")andread.write("my rec")both succeed. Reading either back raisesHeaderSyntaxError: invalid syntax in record line.- The cause predates this PR. The checks in
Record.check_fieldforrecord_name(record.py:482) andfile_name(record.py:521) comparere.match(...).stringwith the name, but.stringis the whole input string, so the comparison always passes. The only names rejected are those whose first characters are invalid, such asa+b. - This PR makes the problem much easier to hit, because a path like
out/session.2024looks like a reasonable thing to pass. - Using
re.fullmatchin both checks fixes it. With that change, the offline suite still passes, andwrite("x.v1"),write("my rec")and plainwrsampall raise the clear "record_name must only comprise of letters, digits, hyphens, and underscores" error. - A case for this in
test_write_rejects_ambiguous_or_unsupported_pathswould keep it covered.
Optional: reset the byte offset. Writing a103l prints "Writing file out.dat with 24 empty leading bytes", because byte_offset is copied from the source. The record is valid, but a newly named record doesn't need padding from the source's layout. Setting record.byte_offset = None in write() would avoid it.
Tests. The tests cover each claim in the new docs section.
- Several assertions read
read._record. That's fine for checking the source is unchanged, butmetadatawould cover most of them without using a private attribute. - One test for an unwritable format (for example
310derive) would cover the documented failure.
Older problems found along the way, not for this PR (I'll open issues):
- A physical read of
binformatsfails inadc_inplacewithTypeError: Cannot cast array data from dtype('O') to dtype('float64').rdrecordfollowed bywrsampfails the same way. - The writer's "only supports writing the following formats" message leaves out 212, though 212 is writable.
|
Thanks for the detailed review. I fixed the destination-name check in I added the unsupported-format test you suggested and addressed #777 and #778 in this PR. The full offline suite passes (1,054 passed, 4 skipped); Ruff and the strict Sphinx build pass too. |
tompollard
left a comment
There was a problem hiding this comment.
Thanks, this looks ready to merge. Everything from the last review is addressed, and so are #777 and #778.
- Names.
write("x.v1")andwrite("my rec")now raise the clearrecord_nameerror. The stricter file-name check doesn't reject anything that worked before:a-b.dat,rec_1.d0andrec.DATstill round-trip;a.b.datis now rejected, but onmainit was written with a header that couldn't be read back.
- Byte offset. It's dropped on the copy, and the source is unchanged.
- Unsupported formats. The check runs before conversion, and the message includes 212.
- Physical and digital writes of
binformatsgive the sameValueError. - Archive, cloud and flattened multi-segment writes fail without leaving files behind. On
main, the flattened case raised anIndexErrorand left a.heafile behind.
- Physical and digital writes of
- The offline suite, ruff and the format check pass locally.
One optional cleanup: if fmt contains an integer, for example [16, "212"], building the new error message raises TypeError: sequence item 0: expected str instance, int found. This isn't a regression, because main raised KeyError: 16. Comparing str(fmt) and joining with map(str, unsupported) would give the normal message.
|
Thanks for checking the updates. I picked up the optional cleanup in The full offline suite passes locally (1,055 passed, 4 skipped), as do Ruff and the format check. This is ready to merge from my side. |
Summary
Add
SignalRead.write(path)so a selected result fromread_signal_valuescan be saved under a new record name without changingrecord_nameandfile_nameon a legacyRecord. The method writes from an independent copy, gives the destination record and signal files new names, and preserves signal file groups. It accepts a local record stem, a supported writable cloud URI for separate files, or a local.wfdbarchive. Cloud archives andphysionet://writes remain unsupported.Document the round-trip contract for the new API: preserve the selected logical data, including channel and frame selection, rather than promise identical source bytes or file layout. Digital values remain exact when their encoding is retained; physical values are subject to quantization; format changes may recode digital missing-value markers. Flattened multi-segment results become one record. The existing skew handling prevents already aligned samples from being shifted again.
The implementation delegates encoding to
Record.wrsampand leaves the sourceSignalReadunchanged. The new reader and writer are still unreleased, so the existingexplicit-signal-valueschangelog entry describes both together.Also validate writable signal formats in
Record.wrsampbefore converting physicalvalues or creating output files. Physical and digital writes now report the same
clear error for unsupported formats, and the supported-format list includes 212.
Closes #747. Closes #777. Closes #778. Related: #500 and #672.
Verification
.wfdbarchives, applied and ignored skew, flattened multi-segment records, amemory://cloud destination, invalid names and paths, source byte offsets, and unsupported formats in both physical and digital writes.MPLBACKEND=Agg).