Skip to content

Read units containing any non-whitespace characters - #609

Merged
tompollard merged 1 commit into
MIT-LCP:mainfrom
TheNetherWatcher:header-units-regex
Oct 31, 2026
Merged

tompollard merged 1 commit into
MIT-LCP:mainfrom
TheNetherWatcher:header-units-regex

Conversation

@TheNetherWatcher

Copy link
Copy Markdown
Contributor

The units in a signal line are matched with [\w\^\-\?%\/]*, which leaves out characters such as . and *. Units like a.u. are cut off at the first such character, and the rest of the line ends up in the signal name:

wfdb.wrsamp("h", fs=100, units=["a.u."], sig_name=["ECG II"], d_signal=np.array([[1], [2]]),
            fmt=["16"], adc_gain=[1.5], baseline=[0])
h = wfdb.rdheader("h")
h.units     # ['a']
h.sig_name  # ['.u. 16 0 1 3 0 ECG II']
h.adc_res   # [None]

WFDB only excludes whitespace from units, and wrsamp already allows any units without whitespace, so the regex now matches \S*.

I parsed every signal line in sample-data/ with the old and new regex. The only differences are in test_generator_2.hea, where two signals have units ° and the old regex put the whole rest of the line into the signal name. (rdheader currently drops non-ASCII characters before parsing, so this doesn't show up when reading that record.)

Tests: wrsamp then rdheader round trip for mV, a.u., uV*s, mmHg/s and 10^-3, checking units, signal name and ADC resolution. The a.u. and uV*s cases fail on main. pytest -m "not network" passes (101 tests), and the docs build passes.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good! \S* matches what the WFDB C library does: readheader in lib/signal.c takes everything after the first / in the gain field, up to the next whitespace, as the units.

I also read all 88 headers in sample-data with rdheader on main and on this branch, and every field is identical.

@TheNetherWatcher
TheNetherWatcher force-pushed the header-units-regex branch 2 times, most recently from ac1fde9 to de5e47e Compare October 31, 2026 18:41
The units in a signal line were matched with a character class that
left out characters such as '.' and '*', so units like 'a.u.' were cut
off and the rest of the line was read as the signal name. WFDB only
excludes whitespace from units, which is also what wrsamp checks, so
match any non-whitespace characters.
@tompollard
tompollard merged commit cb6ac77 into MIT-LCP:main Oct 31, 2026
18 checks passed
@TheNetherWatcher
TheNetherWatcher deleted the header-units-regex branch November 1, 2026 06:13
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.

2 participants