Skip to content
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

FIX return proper instance class in displays classmethod #27675

Merged

Conversation

johncant
Copy link
Contributor

At the moment, if you try this:

class Subclass(RocCurveDisplay):
    pass

type(Subclass.from_predictions(....))

You would get RocCurveDisplay. If users want to reuse/override functionality, like I do, and invoke by calling these named constructors, this needs to return Subclass.

Easily fixed by instantiating cls in the named constructor methods instead of i.e. RocCurveDisplay.

@github-actions
Copy link

github-actions bot commented Oct 27, 2023

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 282c2d6. Link to the linter CI: here

@glemaitre
Copy link
Member

Yep it makes a lot of sense. I think that we can considered it as a bug.
Could you modify the other displays that suffer from the same pattern.
From what I see, the following will need to be modified as well.

  • CalibrationDisplay
  • PartialDependenceDisplay
  • DecisionBoundaryDisplay

I think that we need an entry in the changelog as a FIX.
We also need a non-regression tests for these displays. We could for instance tests to subclassing is working as expected.

@johncant
Copy link
Contributor Author

Hi - I still plan to do this

@glemaitre
Copy link
Member

Cool. Thanks @johncant I am assigning the issue to you.
Do not hesitate to ping my GitHub handle when you submit the pull request.

@glemaitre
Copy link
Member

Ups, this is actually already a pull-request :). I will be notified when you are going to make changes, so all good.

@johncant
Copy link
Contributor Author

johncant commented Dec 17, 2023

#Classes to fix (from it grep 'class [A-Za-z0-9]\+Display'):

Class Named constructors need fixing Now returns subclass instance Non-regression test
CalibrationDisplay
DetCurveDisplay
PrecisionRecallDisplay
RocCurveDisplay
DecisionBoundaryDisplay:
PartialDependenceDisplay:
ConfusionMatrixDisplay
LearningCurveDisplay
ValidationCurveDisplay
PredictionErrorDisplay

I'll update this comment as I go

@johncant
Copy link
Contributor Author

@glemaitre - this is what I've done:

  • Fixed all *Display class named constructors to return the correct type
  • Added non-regression tests for all the *Display classes, even those that didn't need fixing
  • Updated the changelog

PR is now ready!

@glemaitre
Copy link
Member

Thanks. I'm going to review it.

@glemaitre glemaitre self-requested a review January 9, 2024 09:40
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. If we make it before the release, I think we can include it in 1.4.0. Otherwise we will have to move it for 1.4.1.

sklearn/model_selection/tests/test_plot.py Outdated Show resolved Hide resolved
sklearn/metrics/_plot/tests/test_common_curve_display.py Outdated Show resolved Hide resolved
sklearn/metrics/_plot/tests/test_common_curve_display.py Outdated Show resolved Hide resolved
sklearn/metrics/_plot/tests/test_common_curve_display.py Outdated Show resolved Hide resolved
@glemaitre glemaitre added this to the 1.4 milestone Jan 10, 2024
johncant and others added 6 commits January 11, 2024 21:02
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
@glemaitre glemaitre changed the title Fix sklearn.metics.RocCurveDisplay etc named constructors for subclassing FIX return proper instance class in displays classmethod Jan 11, 2024
@glemaitre
Copy link
Member

I might have introduce a linter issue. You can run black as specified by the bot to solve it.

@johncant
Copy link
Contributor Author

Hi @glemaitre - I've accepted all your suggestions and fixed the linter. LGTM!

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. I'm +1 to include it in 1.4.0. @glemaitre you want to take another 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.

Fine on my side as well.

@jeremiedbb jeremiedbb merged commit db971d1 into scikit-learn:main Jan 15, 2024
27 checks passed
jeremiedbb added a commit to jeremiedbb/scikit-learn that referenced this pull request Jan 17, 2024
…n#27675)

Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
jeremiedbb added a commit that referenced this pull request Jan 17, 2024
Co-authored-by: Guillaume Lemaitre <g.lemaitre58@gmail.com>
Co-authored-by: Jérémie du Boisberranger <34657725+jeremiedbb@users.noreply.github.com>
glemaitre added a commit to glemaitre/scikit-learn that referenced this pull request Feb 10, 2024
…n#27675)

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants