Skip to content

Add validated RecordData channel and comment edits - #914

Merged
tompollard merged 2 commits into
mainfrom
feature/record-data-metadata-edits
Dec 4, 2026
Merged

tompollard merged 2 commits into
mainfrom
feature/record-data-metadata-edits

Conversation

@tompollard

@tompollard tompollard commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Add two validated edits to RecordData:

  • with_channels(names=..., units=...) replaces complete channel-name and/or unit sequences. Names can contain internal spaces; supplied names must be nonempty and unique, with no surrounding whitespace. Units must be nonempty and contain no whitespace. Unchanged names are not revalidated, so records with duplicate source names can still have their units corrected.
  • with_comments([...]) replaces the comments. Supplied names, units, and comments must be printable ASCII because the current local reader silently drops non-ASCII text. Comments also cannot begin or end with spaces or #, which the current parser strips.

Both methods return independent values and masks, update the selected view, and retain the original source header and segment provenance. WFDB's signal description is the channel name, so this does not add a second description field. A unit edit changes the label only; calibration and samples stay unchanged. Gain and baseline edits remain a separate design decision. The current full-sequence signature leaves room for a by-name mapping form later.

This advances #744 and #749. It does not change legacy Record fields or the read_record return type.

The package-wide header-encoding and comment-parsing inconsistency is tracked separately in #915.

Checks

  • Offline suite: 2,228 passed, 4 skipped, 23 deselected (using the project's compatible PhysioNet package path).
  • Ruff check and format passed.
  • Sphinx docs build passed with warnings treated as errors.
  • Tests cover read/write round trips with internal spaces and punctuation, rejection of text the reader changes, conversion after edits, duplicate source names, multi-segment provenance, independent arrays, and 5.17.0 pickles.

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

The contract is implemented as agreed. It validates only the fields that are supplied, names may contain internal spaces, and provenance is unchanged. But "validated" should mean an edit that's accepted comes back unchanged after writing and reading. For several kinds of text it doesn't. Most of the cause is in the reader, not here, but this PR is where users will meet it. The offline suite (2,207 passed, 4 skipped) and Ruff pass.

What I checked

I applied names, units and comments to sample-data/100, wrote the result, read it back with read_signals, and compared:

Edit Accepted? Read back
names ECG lead II, a:b, #x, 200/mV, 300 characters yes unchanged
units mV/s, uV(x), %, a:b, m#V, (, mV) yes unchanged
name é ü yes None
name a b (no-break space) yes ab
unit °C yes C
comment é unicode yes unicode
comment leading space yes leading space
comment #hash yes hash

The written header is right: it contains /°C, é ü, # lead and # #hash. The losses happen on reading:

  • _read_header_fields opens local headers with encoding="ascii", errors="ignore" (record.py:3126), which silently drops every non-ASCII character. Remote headers are decoded as iso-8859-1 (download.py:174), and the writer has used UTF-8 since #883. So the same file reads differently locally and remotely, and neither matches what was written. This is on main too: legacy wrsamp followed by rdheader turns é ü into None and °C into C.
  • Comment parsing uses line.strip(" \t#") (_header.py:1624), which removes spaces, tabs and # from both ends of every comment.

To fix in this PR: reject what can't be read back

Until the reader is fixed, the validation should accept only text that survives a round trip:

  • Names, units and comments: printable ASCII only. That's also the WFDB convention for headers.
  • Comments: no leading or trailing space, tab or #.

The error messages should say why, for example "WFDB header text must be printable ASCII". Please add the rows above to the round-trip tests: the accepted ones as round trips, the rest as rejections. If the reader later handles UTF-8 consistently, the ASCII rule can be relaxed in one place.

Separate issue: header encoding is inconsistent

The reader's ascii/ignore choice for local files, iso-8859-1 for remote ones, and UTF-8 from the writer should become one documented rule. UTF-8 on both read paths would preserve everything the writer produces, but it needs a decision about existing Latin-1 headers on PhysioNet. That belongs in its own issue, with the comment-stripping behaviour.

@tompollard

Copy link
Copy Markdown
Member Author

Agreed. I tightened the new edit methods so they reject text the current reader would change: names, units and comments must be printable ASCII, and comments cannot begin or end with spaces or #. Validation still applies only to fields the caller supplies, so a unit correction works when the source has duplicate names.

I added write/read tests for the accepted punctuation and long names in your table, plus rejection tests for the Unicode, no-break space and comment-edge cases. I also updated the docs and changelog. The full offline suite passes (2,228 passed, 4 skipped), as do Ruff and the warning-as-error docs build.

I opened #915 for the underlying inconsistency between local ASCII reads, PhysioNet Latin-1 reads, UTF-8 writes and comment stripping. That needs a package-wide encoding and compatibility decision.

@tompollard
tompollard merged commit a79795c into main Dec 4, 2026
19 checks passed
@tompollard
tompollard deleted the feature/record-data-metadata-edits branch December 4, 2026 18:07
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