Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH added consistency checks for args of check_scalar function #22027

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 13 commits into from
Jan 25, 2022

Conversation

mlant
Copy link
Contributor

@mlant mlant commented Dec 20, 2021

Added two checks on the args val_min, val_max and include_boudaries of the function check_scalar #21948:

if max_val is None and include_boundaries in ("right"):
       raise ValueError(
           "`include_boundaries`='right' with no `max_val` is inconsistent."
       )

if min_val is None and include_boundaries in ("left"):
       raise ValueError(
           "`include_boundaries`='left' with no `min_val` is inconsistent."
       )

Here is the new output of @sply88 short script :
1: include_boundaries=left -> x=1, min_val=1: pass
2: include_boundaries=left -> x=1, max_val=1: ValueError("include_boundaries='left' with no min_val is inconsistent.")
3: include_boundaries=right -> x=1, min_val=1: ValueError("include_boundaries='right' with no max_val is inconsistent.")
4: include_boundaries=right -> x=1, max_val=1: pass
5: include_boundaries=both -> x=1, min_val=1: pass
6: include_boundaries=both -> x=1, max_val=1: pass
7: include_boundaries=neither -> x=1, min_val=1: ValueError('input == 1, must be > 1.')
8: include_boundaries=neither -> x=1, max_val=1: ValueError('input == 1, must be < 1.')

@mlant
Copy link
Contributor Author

mlant commented Dec 20, 2021

Some checks are not successful but I don't understand the error messages.
Can someone help me?

@mlant mlant changed the title added consistency checks for args of check_scalar function ENH added consistency checks for args of check_scalar function Dec 20, 2021
@glemaitre
Copy link
Member

@mlant I can confirm that this is not related to your PR but an update of setuptools. We are trying to solve the problem in #22028

I will look at your PR but considering that the test regarding the docstring is passing, it should be OK.


if min_val is None and include_boundaries in ("left"):
raise ValueError(
"`include_boundaries`='left' with no `min_val` is inconsistent."
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"`include_boundaries`='left' with no `min_val` is inconsistent."
"`include_boundaries`='left' without specifying explicitly `min_val` is inconsistent."

@@ -1418,6 +1419,16 @@ def check_scalar(
f"Possible values are: {expected_include_boundaries}."
)

if max_val is None and include_boundaries in ("right"):
raise ValueError(
"`include_boundaries`='right' with no `max_val` is inconsistent."
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"`include_boundaries`='right' with no `max_val` is inconsistent."
"`include_boundaries`='right' without specifying explicitly `max_val` is inconsistent."

@mlant
Copy link
Contributor Author

mlant commented Dec 20, 2021

Thank you for your replies,
I added tests in test_validate.py and modified the messages errors is that alright for you?

@mlant
Copy link
Contributor Author

mlant commented Dec 29, 2021

@glemaitre, could you take a look?

Copy link
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

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

Please add an entry to the change log at doc/whats_new/v1.1.rst. Like the other entries there, please reference this pull request with :pr: and credit yourself (and other contributors if applicable) with :user:.

@@ -1418,6 +1419,18 @@ def check_scalar(
f"Possible values are: {expected_include_boundaries}."
)

if max_val is None and include_boundaries in ("right"):
Copy link
Member

Choose a reason for hiding this comment

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

I assume that here we want to check for both possibilities.

Suggested change
if max_val is None and include_boundaries in ("right"):
if max_val is None and include_boundaries in ("right", "both"):

Copy link
Member

Choose a reason for hiding this comment

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

Then we need to do:

Suggested change
if max_val is None and include_boundaries in ("right"):
if max_val is None and include_boundaries == "right":

"is inconsistent."
)

if min_val is None and include_boundaries in ("left"):
Copy link
Member

Choose a reason for hiding this comment

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

The same here.

Suggested change
if min_val is None and include_boundaries in ("left"):
if min_val is None and include_boundaries in ("left", "both"):

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if min_val is None and include_boundaries in ("left"):
if min_val is None and include_boundaries == "left":

int,
2,
None,
"right",
Copy link
Member

Choose a reason for hiding this comment

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

We should add the case where we also have "both" once it is included in the check below.

@mlant
Copy link
Contributor Author

mlant commented Jan 4, 2022

I found that writing "if min_val is None and include_boundaries in ("left", "both"):" obliges the user to specify 'include_boundary' every time only one boundary is defined, because "both" is the default value. This would be equivalent to removing the default value of this parameter. What's more, it raises dozens of errors in the existing code. Therefore, I don't think it is the behavior we want. This is what I reverted to the previous writing (and only updated the changelog).

@glemaitre
Copy link
Member

I found that writing "if min_val is None and include_boundaries in ("left", "both"):" obliges the user to specify 'include_boundary' every time only one boundary is defined, because "both" is the default value.

This is a bit annoying but we need "both" to be lenient as a default indeed. We might think about changing the default and make "both" stricter in the future. We can see what a second reviewer will do.

@@ -1418,6 +1419,18 @@ def check_scalar(
f"Possible values are: {expected_include_boundaries}."
)

if max_val is None and include_boundaries in ("right"):
Copy link
Member

Choose a reason for hiding this comment

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

Then we need to do:

Suggested change
if max_val is None and include_boundaries in ("right"):
if max_val is None and include_boundaries == "right":

"is inconsistent."
)

if min_val is None and include_boundaries in ("left"):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if min_val is None and include_boundaries in ("left"):
if min_val is None and include_boundaries == "left":

Comment on lines 436 to 439
- |Enhancement| :func:`check_scalar` raises new errors when inconsistent
boundaries are entered (e.g. `include_boundaries`='left' without
specifying explicitly `min_val`).
:pr:`22027` by `Marie Lanternier <mlant>`.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- |Enhancement| :func:`check_scalar` raises new errors when inconsistent
boundaries are entered (e.g. `include_boundaries`='left' without
specifying explicitly `min_val`).
:pr:`22027` by `Marie Lanternier <mlant>`.
- |Fix| :func:`check_scalar` raises an error when `include_boundaries={"left", "right"}`
and the boundaries are not set.
:pr:`22027` by `Marie Lanternier <mlant>`.

raise ValueError(
"`include_boundaries`='left' "
"without specifying explicitly `min_val` "
"is inconsistent."
Copy link
Member

Choose a reason for hiding this comment

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

Could you wrap the error message on 2 lines only and take profit of the 88 characters width.

raise ValueError(
"`include_boundaries`='right' "
"without specifying explicitly `max_val` "
"is inconsistent."
Copy link
Member

Choose a reason for hiding this comment

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

Could you wrap the error message on 2 lines only and take profit of the 88 characters width.

@mlant
Copy link
Contributor Author

mlant commented Jan 6, 2022

@glemaitre I have fixed a few typos. Now everything LGTM and is ready for review.

Copy link
Member

@glemaitre glemaitre left a comment

Choose a reason for hiding this comment

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

LGTM. Maybe @thomasjpfan wants to have a look.

Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

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

LGTM

@thomasjpfan thomasjpfan merged commit 1e4ac04 into scikit-learn:main Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants