Skip to content

Commit 5373b0a

Browse files
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.
1 parent 74a3f56 commit 5373b0a

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎docs/changes.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Unreleased
1414
**Raise minimum versions of dependencies**
1515
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.
1616

17+
**Read units containing any non-whitespace characters**
18+
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.
19+
1720
**Add timeouts to requests for remote files**
1821
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.
1922

‎src/wfdb/io/header.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class HeaderSyntaxError(ValueError):
4242
\+?(?P<byte_offset>\d*)
4343
[ \t]* (?P<adc_gain>-?\d*\.?\d*e?[\+-]?\d*)
4444
\(?(?P<baseline>-?\d*)\)?
45-
/?(?P<units>[\w\^\-\?%\/]*)
45+
/?(?P<units>\S*)
4646
[ \t]* (?P<adc_res>\d*)
4747
[ \t]* (?P<adc_zero>-?\d*)
4848
[ \t]* (?P<init_value>-?\d*)

‎tests/test_record.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,25 @@ def test_header_with_non_utf8(self):
888888
]
889889
assert record.units.__eq__(sig_units_target)
890890

891+
@pytest.mark.parametrize("units", ["mV", "a.u.", "uV*s", "mmHg/s", "10^-3"])
892+
def test_units_round_trip(self, units, temp_path):
893+
"""Units with any non-whitespace characters are read back unchanged."""
894+
wfdb.wrsamp(
895+
"units",
896+
fs=100,
897+
units=[units],
898+
sig_name=["ECG II"],
899+
d_signal=np.array([[1], [2]]),
900+
fmt=["16"],
901+
adc_gain=[1.5],
902+
baseline=[0],
903+
write_dir=temp_path,
904+
)
905+
header = wfdb.rdheader(os.path.join(temp_path, "units"))
906+
assert header.units == [units]
907+
assert header.sig_name == ["ECG II"]
908+
assert header.adc_res == [16]
909+
891910

892911
class TestMultiRecord:
893912
"""Test read and write of multi segment WFDB records, including PhysioNet streaming.

0 commit comments

Comments
 (0)