From 8921f7517b3975c231525a3dc30bd96fcf3d64c6 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Fri, 4 Dec 2026 12:21:06 -0500 Subject: [PATCH 1/2] Use start and stop in multi-segment read errors --- changelog.d/read-signals-range-errors.rst | 4 +++ src/wfdb/io/record.py | 37 ++++++++++++++--------- tests/test_read_signals_range_errors.py | 23 ++++++++++++++ 3 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 changelog.d/read-signals-range-errors.rst create mode 100644 tests/test_read_signals_range_errors.py diff --git a/changelog.d/read-signals-range-errors.rst b/changelog.d/read-signals-range-errors.rst new file mode 100644 index 00000000..935a39c9 --- /dev/null +++ b/changelog.d/read-signals-range-errors.rst @@ -0,0 +1,4 @@ +**Use modern range names in multi-segment signal-read errors** + ``read_signals`` now reports ``start`` and ``stop`` when a multi-segment + selection is out of bounds, matching its public arguments and single-segment + behavior. diff --git a/src/wfdb/io/record.py b/src/wfdb/io/record.py index 48f4e676..909ad72b 100644 --- a/src/wfdb/io/record.py +++ b/src/wfdb/io/record.py @@ -3866,20 +3866,29 @@ def read_signals( dir_name, _ = _split_path(normalized_path) if not any(dir_name.startswith(proto) for proto in READ_URI_PROTOCOLS): dir_name = os.path.abspath(dir_name) - model = read_multi_segment( - parsed, - dir_name=dir_name, - wfdb_archive=archive, - start=start, - stop=stop, - channels=channels, - channel_names=channel_names, - missing_channels=missing_channels, - kind=kind, - layout=layout, - resolution=resolution, - force_channels=force_channels, - ) + try: + model = read_multi_segment( + parsed, + dir_name=dir_name, + wfdb_archive=archive, + start=start, + stop=stop, + channels=channels, + channel_names=channel_names, + missing_channels=missing_channels, + kind=kind, + layout=layout, + resolution=resolution, + force_channels=force_channels, + ) + except (TypeError, ValueError) as error: + message = str(error) + if message.startswith(("sampfrom ", "sampto ")): + message = message.replace("sampfrom", "start").replace( + "sampto", "stop" + ) + raise type(error)(message) from error + raise return _from_read_model(model) diff --git a/tests/test_read_signals_range_errors.py b/tests/test_read_signals_range_errors.py new file mode 100644 index 00000000..3f8c9a88 --- /dev/null +++ b/tests/test_read_signals_range_errors.py @@ -0,0 +1,23 @@ +"""Modern readers should name their public frame-range arguments in errors.""" + +import pytest + +import wfdb + + +@pytest.mark.parametrize( + ("path", "length"), + [ + ("sample-data/100", 650000), + ("sample-data/multi-segment/fixed1/v102s", 225000), + ], +) +@pytest.mark.parametrize("reader", [wfdb.read_signals, wfdb.read_record]) +def test_invalid_window_names_start_and_stop(path, length, reader): + """Single- and multi-segment errors use the same modern names.""" + with pytest.raises(ValueError, match="stop must be greater than start") as error: + reader(path, layout="uniform", start=length) + assert "sampto" not in str(error.value) + with pytest.raises(ValueError, match="stop must be shorter") as error: + reader(path, layout="uniform", stop=length + 1) + assert "sampto" not in str(error.value) From 198e15e63f67a75b0bbb25d2eeff6bb0cfc19216 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Fri, 4 Dec 2026 12:35:31 -0500 Subject: [PATCH 2/2] Unify modern read range error wording --- changelog.d/read-signals-range-errors.rst | 4 +-- src/wfdb/io/record.py | 34 ++++++++++++++--------- tests/test_read_record.py | 2 +- tests/test_read_signals_range_errors.py | 22 +++++++++++++-- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/changelog.d/read-signals-range-errors.rst b/changelog.d/read-signals-range-errors.rst index 935a39c9..3f36f3d2 100644 --- a/changelog.d/read-signals-range-errors.rst +++ b/changelog.d/read-signals-range-errors.rst @@ -1,4 +1,4 @@ **Use modern range names in multi-segment signal-read errors** ``read_signals`` now reports ``start`` and ``stop`` when a multi-segment - selection is out of bounds, matching its public arguments and single-segment - behavior. + selection is out of bounds. ``read_signals`` and ``read_record`` also use + accurate record-length wording consistent with ``read_segments``. diff --git a/src/wfdb/io/record.py b/src/wfdb/io/record.py index 909ad72b..67869242 100644 --- a/src/wfdb/io/record.py +++ b/src/wfdb/io/record.py @@ -3684,13 +3684,29 @@ def read_record( missing_channels=missing_channels, ) except (TypeError, ValueError) as error: - message = str(error) - if message.startswith(("sampfrom ", "sampto ")): - message = message.replace("sampfrom", "start").replace("sampto", "stop") + if message := _modern_read_range_message(error, start, stop): raise type(error)(message) from error raise +def _modern_read_range_message(error, start, stop): + """Translate legacy frame-range errors to precise modern argument names.""" + message = str(error) + if message.startswith("sampto must be greater than sampfrom"): + return ( + "start must be shorter than the record length" + if stop is None and start > 0 + else "stop must be greater than start" + ) + if message.startswith("sampfrom must be shorter than the signal length"): + return "start must be shorter than the record length" + if message.startswith("sampto must be shorter than the signal length"): + return "stop must not exceed the record length" + if message.startswith(("sampfrom ", "sampto ")): + return message.replace("sampfrom", "start").replace("sampto", "stop") + return None + + def _normalize_modern_channel_selection(channels, channel_names, missing_channels): """Validate modern channel selectors and return normalized lists.""" if channels is not None and channel_names is not None: @@ -3852,11 +3868,7 @@ def read_signals( resolution=resolution, ) except (TypeError, ValueError) as error: - message = str(error) - if message.startswith(("sampfrom ", "sampto ")): - message = message.replace("sampfrom", "start").replace( - "sampto", "stop" - ) + if message := _modern_read_range_message(error, start, stop): raise type(error)(message) from error raise return _from_read_model(model) @@ -3882,11 +3894,7 @@ def read_signals( force_channels=force_channels, ) except (TypeError, ValueError) as error: - message = str(error) - if message.startswith(("sampfrom ", "sampto ")): - message = message.replace("sampfrom", "start").replace( - "sampto", "stop" - ) + if message := _modern_read_range_message(error, start, stop): raise type(error)(message) from error raise return _from_read_model(model) diff --git a/tests/test_read_record.py b/tests/test_read_record.py index 3950d2c5..b30b95c1 100644 --- a/tests/test_read_record.py +++ b/tests/test_read_record.py @@ -357,7 +357,7 @@ def test_range_errors_use_new_argument_names(reader, extra): reader("missing", start=-1, **extra) with pytest.raises(TypeError, match="stop must be an integer"): reader("missing", stop="five", **extra) - with pytest.raises(ValueError, match="stop must be shorter than"): + with pytest.raises(ValueError, match="stop must not exceed the record length"): reader("sample-data/100", stop=650001, **extra) diff --git a/tests/test_read_signals_range_errors.py b/tests/test_read_signals_range_errors.py index 3f8c9a88..9abe5e75 100644 --- a/tests/test_read_signals_range_errors.py +++ b/tests/test_read_signals_range_errors.py @@ -15,9 +15,27 @@ @pytest.mark.parametrize("reader", [wfdb.read_signals, wfdb.read_record]) def test_invalid_window_names_start_and_stop(path, length, reader): """Single- and multi-segment errors use the same modern names.""" - with pytest.raises(ValueError, match="stop must be greater than start") as error: + with pytest.raises( + ValueError, match="start must be shorter than the record length" + ) as error: reader(path, layout="uniform", start=length) assert "sampto" not in str(error.value) - with pytest.raises(ValueError, match="stop must be shorter") as error: + with pytest.raises( + ValueError, match="stop must not exceed the record length" + ) as error: reader(path, layout="uniform", stop=length + 1) assert "sampto" not in str(error.value) + + +@pytest.mark.parametrize( + "reader", [wfdb.read_signals, wfdb.read_record, wfdb.read_segments] +) +def test_multi_segment_readers_share_range_wording(reader): + """All three modern multi-segment readers report the same bounds.""" + path = "sample-data/multi-segment/fixed1/v102s" + with pytest.raises( + ValueError, match="start must be shorter than the record length" + ): + reader(path, layout="uniform", start=225000) + with pytest.raises(ValueError, match="stop must not exceed the record length"): + reader(path, layout="uniform", stop=225001)