Skip to content

MAINT validate parameter affinity_propagation public function #25026

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 7 commits into from
Dec 28, 2022

Conversation

Ala-Na
Copy link
Contributor

@Ala-Na Ala-Na commented Nov 24, 2022

Reference Issues/PRs

Towards #24862

What does this implement/fix? Explain your changes.

Added @validate_params decorator for affinity_propagation public function.

Any other comments?

Re post of a previous PR (follow up of #24868) after decision of double validation.

Copy link
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@jeremiedbb jeremiedbb added the Validation related to input validation label Nov 25, 2022
@adrinjalali
Copy link
Member

@glemaitre I see #24868 (comment) that you say we've decided to do double validation. Is there any benefit to that?

@glemaitre
Copy link
Member

Is there any benefit to that?

The signatures of functions and classes are sometimes different. Not having the validation means that we hope for the best because we will not have any tests. Usually, the validation time will be negligible (here we never make data validation or searching for nan) in comparison with the processing.

A nicer solution would be to create a validation only for parameters that are different but we should have a system that checks all parameters, even the one validated later by the class. I fear that it makes things complex.

@adrinjalali
Copy link
Member

hmm, but then if the validation changes in the class, like, adding a new solver, a new metric, etc, we would need to update two places, and that can go out of sync as well. Either way, we have to make sure the two are in sync, and if that's the case, wouldn't it make more sense to only validate the parameters which are not directly passed to the class?

@Ala-Na
Copy link
Contributor Author

Ala-Na commented Dec 4, 2022

Fixed merge conflicts.

@adrinjalali
Copy link
Member

This would need to be changed based on this PR: #25087

@glemaitre glemaitre self-requested a review December 8, 2022 18:16
@glemaitre glemaitre changed the title Double validation for affinity_propagation public function MAINT validate parameter affinity_propagation public function Dec 8, 2022
@glemaitre
Copy link
Member

I solved the conflicts with main and I will make a review with the new partial check.

Comment on lines 181 to 186
@validate_params(
{
"S": ["array-like"],
"preference": [
"array-like",
Interval(Real, None, None, closed="neither"),
None,
],
"convergence_iter": [Interval(Integral, 1, None, closed="left")],
"max_iter": [Interval(Integral, 1, None, closed="left")],
"damping": [Interval(Real, 0.5, 1.0, closed="both")],
"copy": ["boolean"],
"verbose": ["boolean"],
"return_n_iter": ["boolean"],
"random_state": ["random_state"],
}
)
Copy link
Member

Choose a reason for hiding this comment

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

I think that we only have to validate these two parameters which are not checked by the class AffinityPropagation

Suggested change
@validate_params(
{
"S": ["array-like"],
"preference": [
"array-like",
Interval(Real, None, None, closed="neither"),
None,
],
"convergence_iter": [Interval(Integral, 1, None, closed="left")],
"max_iter": [Interval(Integral, 1, None, closed="left")],
"damping": [Interval(Real, 0.5, 1.0, closed="both")],
"copy": ["boolean"],
"verbose": ["boolean"],
"return_n_iter": ["boolean"],
"random_state": ["random_state"],
}
)
@validate_params({"S": ["array-like"], "return_n_iter": ["boolean"]})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -449,7 +466,6 @@ def __init__(
verbose=False,
random_state=None,
):

Copy link
Member

Choose a reason for hiding this comment

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

We usually don't make these types of changes if they are not triggered for PEP8 reasons.

Could you revert this part?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, done.

@Ala-Na Ala-Na force-pushed the affinity_propagation branch from 7260c5c to 900b023 Compare December 8, 2022 21:09
@Ala-Na Ala-Na force-pushed the affinity_propagation branch from 900b023 to 258dff1 Compare December 8, 2022 21:18
@Ala-Na
Copy link
Contributor Author

Ala-Na commented Dec 8, 2022

Fixed conflicts with main branch.

@glemaitre glemaitre self-requested a review December 9, 2022 16:33
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.

I think that we converge on the right solution.
I added the last comment that should make the test pass. We still have a remaining double validation of S in affinity_propagation to be removed. It has to be delegated to the underlying class.

@@ -124,6 +124,7 @@ def test_function_param_validation(func_module):

PARAM_VALIDATION_CLASS_WRAPPER_LIST = [
("sklearn.decomposition.non_negative_factorization", "sklearn.decomposition.NMF"),
("sklearn.cluster.affinity_propagation", "sklearn.cluster.AffinityPropagation"),
Copy link
Member

Choose a reason for hiding this comment

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

I made this change here.
We modified the code such that we check all parameters, even the ones that are not provided. To be able to do so we need to provide the relation between the function and the class.

"S": ["array-like"],
"return_n_iter": ["boolean"],
}
)
def affinity_propagation(
S,
*,
Copy link
Member

Choose a reason for hiding this comment

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

The test is going to fail because we don't delegate all parameter validation to the class. We need to change the code below from

    S = as_float_array(S, copy=copy)

    estimator = AffinityPropagation(
        damping=damping,
        max_iter=max_iter,
        convergence_iter=convergence_iter,
        copy=False,
        preference=preference,
        affinity="precomputed",
        verbose=verbose,
        random_state=random_state,
    ).fit(S)

to

    estimator = AffinityPropagation(
        damping=damping,
        max_iter=max_iter,
        convergence_iter=convergence_iter,
        copy=copy,
        preference=preference,
        affinity="precomputed",
        verbose=verbose,
        random_state=random_state,
    ).fit(S)

So, we should not validate S and pass the parameter copy that will be validated by the class.

Copy link
Member

@jeremiedbb jeremiedbb left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @Ala-Na

@jeremiedbb jeremiedbb merged commit ec95edc into scikit-learn:main Dec 28, 2022
jjerphan pushed a commit to jjerphan/scikit-learn that referenced this pull request Jan 3, 2023
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
jjerphan pushed a commit to jjerphan/scikit-learn that referenced this pull request Jan 20, 2023
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
jjerphan pushed a commit to jjerphan/scikit-learn that referenced this pull request Jan 20, 2023
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants