Skip to content

Write selected signal views to a destination path - #776

Merged
tompollard merged 5 commits into
mainfrom
signal-read-write-path
Nov 30, 2026
Merged

tompollard merged 5 commits into
mainfrom
signal-read-write-path

Conversation

@tompollard

@tompollard tompollard commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Summary

Add SignalRead.write(path) so a selected result from read_signal_values can be saved under a new record name without changing record_name and file_name on a legacy Record. 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 .wfdb archive. Cloud archives and physionet:// 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.wrsamp and leaves the source SignalRead unchanged. The new reader and writer are still unreleased, so the existing explicit-signal-values changelog entry describes both together.

Also validate writable signal formats in Record.wrsamp before converting physical
values 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

  • Round-trip tests cover selected frames/channels, edited physical values, multi-frequency expanded digital data, renamed signal file groups, local .wfdb archives, applied and ignored skew, flattened multi-segment records, a memory:// cloud destination, invalid names and paths, source byte offsets, and unsupported formats in both physical and digital writes.
  • Offline suite: 1,055 passed, 4 skipped, 22 network tests deselected (MPLBACKEND=Agg).
  • Ruff check and format pass.
  • Sphinx HTML build passes with warnings treated as errors.

@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 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, and a103l (a .mat file 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") and read.write("my rec") both succeed. Reading either back raises HeaderSyntaxError: invalid syntax in record line.
  • The cause predates this PR. The checks in Record.check_field for record_name (record.py:482) and file_name (record.py:521) compare re.match(...).string with the name, but .string is the whole input string, so the comparison always passes. The only names rejected are those whose first characters are invalid, such as a+b.
  • This PR makes the problem much easier to hit, because a path like out/session.2024 looks like a reasonable thing to pass.
  • Using re.fullmatch in both checks fixes it. With that change, the offline suite still passes, and write("x.v1"), write("my rec") and plain wrsamp all 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_paths would 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, but metadata would 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 binformats fails in adc_inplace with TypeError: Cannot cast array data from dtype('O') to dtype('float64'). rdrecord followed by wrsamp fails the same way.
  • The writer's "only supports writing the following formats" message leaves out 212, though 212 is writable.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for the detailed review. I fixed the destination-name check in Record.check_field with re.fullmatch for both record and signal file names, so invalid names now fail before writing. SignalRead.write also clears the source byte offset on its copy, and the tests use the public API instead of _record.

I added the unsupported-format test you suggested and addressed #777 and #778 in this PR. Record.wrsamp now checks writable formats before converting physical values or creating files. Physical and digital writes of binformats raise the same clear ValueError, and the supported-format list comes from one constant that includes 212. Tests cover both writers and confirm no files are left behind on failure.

The full offline suite passes (1,054 passed, 4 skipped); Ruff and the strict Sphinx build pass too.

@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.

Thanks, this looks ready to merge. Everything from the last review is addressed, and so are #777 and #778.

  • Names. write("x.v1") and write("my rec") now raise the clear record_name error. The stricter file-name check doesn't reject anything that worked before:
    • a-b.dat, rec_1.d0 and rec.DAT still round-trip;
    • a.b.dat is now rejected, but on main it 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 binformats give the same ValueError.
    • Archive, cloud and flattened multi-segment writes fail without leaving files behind. On main, the flattened case raised an IndexError and left a .hea file behind.
  • 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.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for checking the updates. I picked up the optional cleanup in 06950c1: a non-string format such as 16 is still treated as invalid, but the error now formats it safely and raises the usual ValueError instead of a TypeError. I added a regression test for [16, "212"] that also checks no files are created.

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.

@tompollard
tompollard merged commit 709dcc6 into main Nov 30, 2026
19 checks passed
@tompollard
tompollard deleted the signal-read-write-path branch November 30, 2026 04:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant