Repository navigation
Add validated RecordData channel and comment edits - #914
Conversation
tompollard
left a comment
There was a problem hiding this comment.
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_fieldsopens local headers withencoding="ascii", errors="ignore"(record.py:3126), which silently drops every non-ASCII character. Remote headers are decoded asiso-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 onmaintoo: legacywrsampfollowed byrdheaderturnsé üintoNoneand°CintoC.- 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.
|
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 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. |
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
Recordfields or theread_recordreturn type.The package-wide header-encoding and comment-parsing inconsistency is tracked separately in #915.
Checks