Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.d/read-signals-range-errors.rst
Original file line number Diff line number Diff line change
@@ -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``.
61 changes: 39 additions & 22 deletions src/wfdb/io/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_read_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
41 changes: 41 additions & 0 deletions tests/test_read_signals_range_errors.py
Original file line number Diff line number Diff line change
@@ -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)
Loading