From 5373b0ab577d10db1507785b3fbd7dce975ddcd8 Mon Sep 17 00:00:00 2001 From: Bharat Kaurav <117659673+TheNetherWatcher@users.noreply.github.com> Date: Sat, 31 Oct 2026 22:51:44 +0530 Subject: [PATCH] Read units containing any non-whitespace characters 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. --- docs/changes.rst | 3 +++ src/wfdb/io/header.py | 2 +- tests/test_record.py | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/docs/changes.rst b/docs/changes.rst index 8c63c074..4b7b064a 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -14,6 +14,9 @@ Unreleased **Raise minimum versions of dependencies** The minimum versions of ``soundfile`` (0.12.1), ``matplotlib`` (3.8.0) and ``requests`` (2.31.0) have been raised. The minimum versions of the other dependencies are unchanged. +**Read units containing any non-whitespace characters** + Header files with units containing characters such as ``.`` or ``*`` (for example ``a.u.`` or ``uV*s``) are now read correctly. The units used to be cut off at the first such character, and the rest of the signal line (ADC resolution, ADC zero and so on) ended up in the signal name. + **Add timeouts to requests for remote files** Requests for files on PhysioNet (and other web servers) now time out if the server does not respond (10 seconds to connect, and 60 seconds to wait for data), instead of waiting indefinitely. A ``requests.exceptions.Timeout`` (``ConnectTimeout`` or ``ReadTimeout``) is raised in this case. diff --git a/src/wfdb/io/header.py b/src/wfdb/io/header.py index 4cc7cbe6..6b785ebb 100644 --- a/src/wfdb/io/header.py +++ b/src/wfdb/io/header.py @@ -42,7 +42,7 @@ class HeaderSyntaxError(ValueError): \+?(?P\d*) [ \t]* (?P-?\d*\.?\d*e?[\+-]?\d*) \(?(?P-?\d*)\)? - /?(?P[\w\^\-\?%\/]*) + /?(?P\S*) [ \t]* (?P\d*) [ \t]* (?P-?\d*) [ \t]* (?P-?\d*) diff --git a/tests/test_record.py b/tests/test_record.py index 2bb636ae..1b67efe6 100644 --- a/tests/test_record.py +++ b/tests/test_record.py @@ -888,6 +888,25 @@ def test_header_with_non_utf8(self): ] assert record.units.__eq__(sig_units_target) + @pytest.mark.parametrize("units", ["mV", "a.u.", "uV*s", "mmHg/s", "10^-3"]) + def test_units_round_trip(self, units, temp_path): + """Units with any non-whitespace characters are read back unchanged.""" + wfdb.wrsamp( + "units", + fs=100, + units=[units], + sig_name=["ECG II"], + d_signal=np.array([[1], [2]]), + fmt=["16"], + adc_gain=[1.5], + baseline=[0], + write_dir=temp_path, + ) + header = wfdb.rdheader(os.path.join(temp_path, "units")) + assert header.units == [units] + assert header.sig_name == ["ECG II"] + assert header.adc_res == [16] + class TestMultiRecord: """Test read and write of multi segment WFDB records, including PhysioNet streaming.