Skip to content

FIX change the meaning of include_boundaries in check_scalar #20921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 7, 2021
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: 2 additions & 2 deletions sklearn/cluster/_affinity_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class AffinityPropagation(ClusterMixin, BaseEstimator):
Parameters
----------
damping : float, default=0.5
Damping factor (between 0.5 and 1) is the extent to
Damping factor in the range `[0.5, 1.0)` is the extent to
which the current value is maintained relative to
incoming values (weighted 1 - damping). This in order
to avoid numerical oscillations when updating these
Expand Down Expand Up @@ -469,7 +469,7 @@ def fit(self, X, y=None):
target_type=numbers.Real,
min_val=0.5,
max_val=1,
closed="right",
Copy link
Member

@jeremiedbb jeremiedbb Sep 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the description of the param says "between 0.5 and 1". If 1 is excluded, maybe we should mention it there

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me second check the previous code to be sure but I think it was excluded.

Copy link
Member Author

@glemaitre glemaitre Sep 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad the interval is 0.5 < dumping <= 1.0 no I read it bad again, it is 0.5 <= dumping < 1.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hum, but 0.5 is the default so it should be included :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hum, but 0.5 is the default so it should be included :)

Yes, I edited my answer. I confused the valid interval and the condition used to raise the error :)

include_boundaries="left",
)
check_scalar(self.max_iter, "max_iter", target_type=numbers.Integral, min_val=1)
check_scalar(
Expand Down
28 changes: 20 additions & 8 deletions sklearn/utils/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,14 +1031,14 @@ def test_check_scalar_valid(x):
target_type=numbers.Real,
min_val=2,
max_val=5,
closed="neither",
include_boundaries="both",
)
assert len(record) == 0
assert scalar == x


@pytest.mark.parametrize(
"x, target_name, target_type, min_val, max_val, closed, err_msg",
"x, target_name, target_type, min_val, max_val, include_boundaries, err_msg",
[
(
1,
Expand All @@ -1058,7 +1058,7 @@ def test_check_scalar_valid(x):
2,
4,
"neither",
ValueError("test_name2 == 1, must be >= 2."),
ValueError("test_name2 == 1, must be > 2."),
),
(
5,
Expand All @@ -1067,15 +1067,15 @@ def test_check_scalar_valid(x):
2,
4,
"neither",
ValueError("test_name3 == 5, must be <= 4."),
ValueError("test_name3 == 5, must be < 4."),
),
(
2,
"test_name4",
int,
2,
4,
"left",
"right",
ValueError("test_name4 == 2, must be > 2."),
),
(
Expand All @@ -1084,13 +1084,25 @@ def test_check_scalar_valid(x):
int,
2,
4,
"right",
"left",
ValueError("test_name5 == 4, must be < 4."),
),
(
4,
"test_name6",
int,
2,
4,
"bad parameter value",
ValueError(
"Unknown value for `include_boundaries`: 'bad parameter value'. "
"Possible values are: ('left', 'right', 'both', 'neither')."
),
),
],
)
def test_check_scalar_invalid(
x, target_name, target_type, min_val, max_val, closed, err_msg
x, target_name, target_type, min_val, max_val, include_boundaries, err_msg
):
"""Test that check_scalar returns the right error if a wrong input is
given"""
Expand All @@ -1101,7 +1113,7 @@ def test_check_scalar_invalid(
target_type=target_type,
min_val=min_val,
max_val=max_val,
closed=closed,
include_boundaries=include_boundaries,
)
assert str(raised_error.value) == str(err_msg)
assert type(raised_error.value) == type(err_msg)
Expand Down
35 changes: 24 additions & 11 deletions sklearn/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ def check_scalar(
*,
min_val=None,
max_val=None,
closed="neither",
include_boundaries="both",
):
"""Validate scalar parameters type and value.

Expand All @@ -1265,9 +1265,15 @@ def check_scalar(
The maximum valid value the parameter can take. If None (default) it
is implied that the parameter does not have an upper bound.

closed : {"left", "right", "both", "neither"}, default="neither"
Whether the interval is closed on the left-side, right-side, both or
neither.
include_boundaries : {"left", "right", "both", "neither"}, default="both"
Whether the interval defined by `min_val` and `max_val` should include
the boundaries. Possible choices are:

- `"left"`: only `min_val` is included in the valid interval;
- `"right"`: only `max_val` is included in the valid interval;
- `"both"`: `min_val` and `max_val` are included in the valid interval;
- `"neither"`: neither `min_val` nor `max_val` are included in the
valid interval.

Returns
-------
Expand All @@ -1286,22 +1292,29 @@ def check_scalar(
if not isinstance(x, target_type):
raise TypeError(f"{name} must be an instance of {target_type}, not {type(x)}.")

expected_closed = {"left", "right", "both", "neither"}
if closed not in expected_closed:
raise ValueError(f"Unknown value for `closed`: {closed}")
expected_include_boundaries = ("left", "right", "both", "neither")
if include_boundaries not in expected_include_boundaries:
raise ValueError(
f"Unknown value for `include_boundaries`: {repr(include_boundaries)}. "
f"Possible values are: {expected_include_boundaries}."
)

comparison_operator = operator.le if closed in ("left", "both") else operator.lt
comparison_operator = (
operator.lt if include_boundaries in ("left", "both") else operator.le
)
if min_val is not None and comparison_operator(x, min_val):
raise ValueError(
f"{name} == {x}, must be"
f" {'>' if closed in ('left', 'both') else '>='} {min_val}."
f" {'>=' if include_boundaries in ('left', 'both') else '>'} {min_val}."
)

comparison_operator = operator.ge if closed in ("right", "both") else operator.gt
comparison_operator = (
operator.gt if include_boundaries in ("right", "both") else operator.ge
)
if max_val is not None and comparison_operator(x, max_val):
raise ValueError(
f"{name} == {x}, must be"
f" {'<' if closed in ('right', 'both') else '<='} {max_val}."
f" {'<=' if include_boundaries in ('right', 'both') else '<'} {max_val}."
)

return x
Expand Down