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 EmptyRequest.get defaults to Bunch of METHODS #28371

Merged
17 changes: 17 additions & 0 deletions sklearn/metrics/tests/test_score_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1490,3 +1490,20 @@ def test_make_scorer_deprecation(deprecated_params, new_params, warn_msg):
assert deprecated_roc_auc_scorer(classifier, X, y) == pytest.approx(
roc_auc_scorer(classifier, X, y)
)


def test_metadata_routing_multimetric_without_metadata_works_with_and_without_routing():
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
def test_metadata_routing_multimetric_without_metadata_works_with_and_without_routing():
@pytest.mark.parametrize("enable_metadata_routing", [True, False])
def test_metadata_routing_multimetric_metadata_routing(enable_metadata_routing):

"""Test multimetric scorer works with and without metadata routing enabled when
there is no actual metadata to pass.

Non-regression test for https://github.com/scikit-learn/scikit-learn/issues/28256
"""
X, y = make_classification(n_samples=50, n_features=10, random_state=0)
estimator = EstimatorWithFitAndPredict().fit(X, y)

multimetric_scorer = _MultimetricScorer(scorers={"acc": get_scorer("accuracy")})
with config_context(enable_metadata_routing=True):
multimetric_scorer(estimator, X, y)

with config_context(enable_metadata_routing=False):
multimetric_scorer(estimator, X, y)
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
with config_context(enable_metadata_routing=True):
multimetric_scorer(estimator, X, y)
with config_context(enable_metadata_routing=False):
multimetric_scorer(estimator, X, y)
with config_context(enable_metadata_routing=enable_metadata_routing):
multimetric_scorer(estimator, X, y)

17 changes: 17 additions & 0 deletions sklearn/tests/test_metadata_routing.py
eddiebergman marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
BaseEstimator,
clone,
)
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LinearRegression
from sklearn.metrics._scorer import _MultimetricScorer, get_scorer
from sklearn.tests.metadata_routing_common import (
ConsumingClassifier,
ConsumingRegressor,
Expand Down Expand Up @@ -990,3 +992,18 @@ def fit(self, X, y, metadata=None):
NotImplementedError, match="Estimator has not implemented metadata routing yet."
):
MetaRegressor(estimator=Estimator()).fit(X, y, metadata=my_groups)


def test_metadata_routing_multimetric_behaves_equally_with_and_without_routing():
eddiebergman marked this conversation as resolved.
Show resolved Hide resolved
"""Test multimetric scorer works with and without metadata routing enabled.

Non-regression test for https://github.com/scikit-learn/scikit-learn/issues/28256
"""

multimetric_scorer = _MultimetricScorer(scorers={"acc": get_scorer("accuracy")})
estimator = DummyClassifier().fit(X, y)
with config_context(enable_metadata_routing=True):
multimetric_scorer(estimator, X, y)

with config_context(enable_metadata_routing=False):
multimetric_scorer(estimator, X, y)
5 changes: 4 additions & 1 deletion sklearn/utils/_metadata_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,10 @@
# an empty dict on routed_params.ANYTHING.ANY_METHOD.
class EmptyRequest:
def get(self, name, default=None):
return default if default else {}
if not default:
return Bunch(**{method: dict() for method in METHODS})

return default

Check warning on line 1536 in sklearn/utils/_metadata_requests.py

View check run for this annotation

Codecov / codecov/patch

sklearn/utils/_metadata_requests.py#L1536

Added line #L1536 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

codecov is not happy here. I need to figure out when is it the case that default=None

Copy link
Member

Choose a reason for hiding this comment

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

@adrinjalali I assume that we should be able to cover this one because it would be equivalent to call e.g.

routed_params = _process_routing(self, "score", **kwargs)
routed_params.get("score", default="default")

I don't where is the best place to test this. This looks like a metadata routing test to me.


def __getitem__(self, name):
return Bunch(**{method: dict() for method in METHODS})
Expand Down