diff --git a/changelog.d/read-signals-range-errors.rst b/changelog.d/read-signals-range-errors.rst new file mode 100644 index 00000000..3f36f3d2 --- /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. ``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 48f4e676..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) @@ -3866,20 +3878,25 @@ 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: + 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 new file mode 100644 index 00000000..9abe5e75 --- /dev/null +++ b/tests/test_read_signals_range_errors.py @@ -0,0 +1,41 @@ +"""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="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 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)