Skip to content

MNT refactoring in routing _MetadataRequester #31534

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions examples/miscellaneous/plot_metadata_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def get_metadata_routing(self):
# This method defines the routing for this meta-estimator.
# In order to do so, a `MetadataRouter` instance is created, and the
# routing is added to it. More explanations follow below.
router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
estimator=self.estimator,
method_mapping=MethodMapping()
.add(caller="fit", callee="fit")
Expand Down Expand Up @@ -352,7 +352,7 @@ def __init__(self, estimator):

def get_metadata_routing(self):
router = (
MetadataRouter(owner=self.__class__.__name__)
MetadataRouter(owner=self)
# defining metadata routing request values for usage in the meta-estimator
.add_self_request(self)
# defining metadata routing request values for usage in the sub-estimator
Expand Down Expand Up @@ -483,7 +483,7 @@ def __init__(self, transformer, classifier):

def get_metadata_routing(self):
router = (
MetadataRouter(owner=self.__class__.__name__)
MetadataRouter(owner=self)
# We add the routing for the transformer.
.add(
transformer=self.transformer,
Expand Down Expand Up @@ -613,7 +613,7 @@ def fit(self, X, y, **fit_params):
self.estimator_ = clone(self.estimator).fit(X, y, **routed_params.estimator.fit)

def get_metadata_routing(self):
router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
estimator=self.estimator,
method_mapping=MethodMapping().add(caller="fit", callee="fit"),
)
Expand Down Expand Up @@ -650,7 +650,7 @@ def fit(self, X, y, sample_weight=None, **fit_params):

def get_metadata_routing(self):
router = (
MetadataRouter(owner=self.__class__.__name__)
MetadataRouter(owner=self)
.add_self_request(self)
.add(
estimator=self.estimator,
Expand Down
2 changes: 1 addition & 1 deletion sklearn/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def get_metadata_routing(self):
routing information.
"""
router = (
MetadataRouter(owner=self.__class__.__name__)
MetadataRouter(owner=self)
.add_self_request(self)
.add(
estimator=self._get_estimator(),
Expand Down
2 changes: 1 addition & 1 deletion sklearn/compose/_column_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__)
router = MetadataRouter(owner=self)
# Here we don't care about which columns are used for which
# transformers, and whether or not a transformer is used at all, which
# might happen if no columns are selected for that transformer. We
Expand Down
2 changes: 1 addition & 1 deletion sklearn/compose/_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
regressor=self._get_regressor(),
method_mapping=MethodMapping()
.add(caller="fit", callee="fit")
Expand Down
2 changes: 1 addition & 1 deletion sklearn/covariance/_graph_lasso.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
splitter=check_cv(self.cv),
method_mapping=MethodMapping().add(callee="split", caller="fit"),
)
Expand Down
2 changes: 1 addition & 1 deletion sklearn/ensemble/_bagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__)
router = MetadataRouter(owner=self)

method_mapping = MethodMapping()
method_mapping.add(caller="fit", callee="fit").add(
Expand Down
2 changes: 1 addition & 1 deletion sklearn/ensemble/_stacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__)
router = MetadataRouter(owner=self)

# `self.estimators` is a list of (name, est) tuples
for name, estimator in self.estimators:
Expand Down
2 changes: 1 addition & 1 deletion sklearn/ensemble/_voting.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__)
router = MetadataRouter(owner=self)

# `self.estimators` is a list of (name, est) tuples
for name, estimator in self.estimators:
Expand Down
2 changes: 1 addition & 1 deletion sklearn/feature_selection/_from_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
estimator=self.estimator,
method_mapping=MethodMapping()
.add(caller="partial_fit", callee="partial_fit")
Expand Down
4 changes: 2 additions & 2 deletions sklearn/feature_selection/_rfe.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
estimator=self.estimator,
method_mapping=MethodMapping()
.add(caller="fit", callee="fit")
Expand Down Expand Up @@ -1002,7 +1002,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__)
router = MetadataRouter(owner=self)
router.add(
estimator=self.estimator,
method_mapping=MethodMapping().add(caller="fit", callee="fit"),
Expand Down
2 changes: 1 addition & 1 deletion sklearn/feature_selection/_sequential.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__)
router = MetadataRouter(owner=self)
router.add(
estimator=self.estimator,
method_mapping=MethodMapping().add(caller="fit", callee="fit"),
Expand Down
2 changes: 1 addition & 1 deletion sklearn/impute/_iterative.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
estimator=self.estimator,
method_mapping=MethodMapping().add(callee="fit", caller="fit"),
)
Expand Down
2 changes: 1 addition & 1 deletion sklearn/linear_model/_coordinate_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1910,7 +1910,7 @@ def get_metadata_routing(self):
routing information.
"""
router = (
MetadataRouter(owner=self.__class__.__name__)
MetadataRouter(owner=self)
.add_self_request(self)
.add(
splitter=check_cv(self.cv),
Expand Down
2 changes: 1 addition & 1 deletion sklearn/linear_model/_least_angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
splitter=check_cv(self.cv),
method_mapping=MethodMapping().add(caller="fit", callee="split"),
)
Expand Down
2 changes: 1 addition & 1 deletion sklearn/linear_model/_logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2301,7 +2301,7 @@ def get_metadata_routing(self):
"""

router = (
MetadataRouter(owner=self.__class__.__name__)
MetadataRouter(owner=self)
.add_self_request(self)
.add(
splitter=self.cv,
Expand Down
2 changes: 1 addition & 1 deletion sklearn/linear_model/_omp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ def get_metadata_routing(self):
routing information.
"""

router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
splitter=self.cv,
method_mapping=MethodMapping().add(caller="fit", callee="split"),
)
Expand Down
2 changes: 1 addition & 1 deletion sklearn/linear_model/_ransac.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
estimator=self.estimator,
method_mapping=MethodMapping()
.add(caller="fit", callee="fit")
Expand Down
2 changes: 1 addition & 1 deletion sklearn/linear_model/_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2496,7 +2496,7 @@ def get_metadata_routing(self):
routing information.
"""
router = (
MetadataRouter(owner=self.__class__.__name__)
MetadataRouter(owner=self)
.add_self_request(self)
.add(
scorer=self.scoring,
Expand Down
14 changes: 10 additions & 4 deletions sklearn/metrics/_scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def get_metadata_routing(self):
A :class:`~utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
return MetadataRouter(owner=self.__class__.__name__).add(
return MetadataRouter(owner=self).add(
**self._scorers,
method_mapping=MethodMapping().add(caller="score", callee="score"),
)
Expand Down Expand Up @@ -266,6 +266,9 @@ def __repr__(self):
f"{response_method_string}{kwargs_string})"
)

def _routing_repr(self):
return repr(self)

def __call__(self, estimator, X, y_true, sample_weight=None, **kwargs):
"""Evaluate predicted target values for X relative to y_true.

Expand Down Expand Up @@ -355,7 +358,7 @@ def set_score_request(self, **kwargs):
),
kwargs=kwargs,
)
self._metadata_request = MetadataRequest(owner=self.__class__.__name__)
self._metadata_request = MetadataRequest(owner=self)
for param, alias in kwargs.items():
self._metadata_request.score.add_request(param=param, alias=alias)
return self
Expand Down Expand Up @@ -481,7 +484,7 @@ class _PassthroughScorer(_MetadataRequester):
def __init__(self, estimator):
self._estimator = estimator

requests = MetadataRequest(owner=self.__class__.__name__)
requests = MetadataRequest(owner=self)
try:
requests.score = copy.deepcopy(estimator._metadata_request.score)
except AttributeError:
Expand All @@ -497,7 +500,10 @@ def __call__(self, estimator, *args, **kwargs):
return estimator.score(*args, **kwargs)

def __repr__(self):
return f"{self._estimator.__class__}.score"
return f"{type(self._estimator).__name__}.score"

def _routing_repr(self):
return repr(self)

def _accept_sample_weight(self):
# TODO(slep006): remove when metadata routing is the only way
Expand Down
4 changes: 2 additions & 2 deletions sklearn/model_selection/_classification_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
estimator=self.estimator,
method_mapping=MethodMapping().add(callee="fit", caller="fit"),
)
Expand Down Expand Up @@ -858,7 +858,7 @@ def get_metadata_routing(self):
routing information.
"""
router = (
MetadataRouter(owner=self.__class__.__name__)
MetadataRouter(owner=self)
.add(
estimator=self.estimator,
method_mapping=MethodMapping().add(callee="fit", caller="fit"),
Expand Down
2 changes: 1 addition & 1 deletion sklearn/model_selection/_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__)
router = MetadataRouter(owner=self)
router.add(
estimator=self.estimator,
method_mapping=MethodMapping().add(caller="fit", callee="fit"),
Expand Down
6 changes: 3 additions & 3 deletions sklearn/multiclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ def get_metadata_routing(self):
"""

router = (
MetadataRouter(owner=self.__class__.__name__)
MetadataRouter(owner=self)
.add_self_request(self)
.add(
estimator=self.estimator,
Expand Down Expand Up @@ -1028,7 +1028,7 @@ def get_metadata_routing(self):
"""

router = (
MetadataRouter(owner=self.__class__.__name__)
MetadataRouter(owner=self)
.add_self_request(self)
.add(
estimator=self.estimator,
Expand Down Expand Up @@ -1277,7 +1277,7 @@ def get_metadata_routing(self):
routing information.
"""

router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
estimator=self.estimator,
method_mapping=MethodMapping().add(caller="fit", callee="fit"),
)
Expand Down
6 changes: 3 additions & 3 deletions sklearn/multioutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
estimator=self.estimator,
method_mapping=MethodMapping()
.add(caller="partial_fit", callee="partial_fit")
Expand Down Expand Up @@ -1149,7 +1149,7 @@ def get_metadata_routing(self):
routing information.
"""

router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
estimator=self._get_estimator(),
method_mapping=MethodMapping().add(caller="fit", callee="fit"),
)
Expand Down Expand Up @@ -1311,7 +1311,7 @@ def get_metadata_routing(self):
routing information.
"""

router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
estimator=self._get_estimator(),
method_mapping=MethodMapping().add(caller="fit", callee="fit"),
)
Expand Down
4 changes: 2 additions & 2 deletions sklearn/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__)
router = MetadataRouter(owner=self)

# first we add all steps except the last one
for _, name, trans in self._iter(with_final=False, filter_passthrough=True):
Expand Down Expand Up @@ -2103,7 +2103,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__)
router = MetadataRouter(owner=self)

for name, transformer in self.transformer_list:
router.add(
Expand Down
2 changes: 1 addition & 1 deletion sklearn/semi_supervised/_self_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ def get_metadata_routing(self):
A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
routing information.
"""
router = MetadataRouter(owner=self.__class__.__name__)
router = MetadataRouter(owner=self)
router.add(
estimator=self.estimator,
method_mapping=(
Expand Down
8 changes: 4 additions & 4 deletions sklearn/tests/metadata_routing_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def fit(self, X, y, **fit_params):
self.estimator_ = clone(self.estimator).fit(X, y, **params.estimator.fit)

def get_metadata_routing(self):
router = MetadataRouter(owner=self.__class__.__name__).add(
router = MetadataRouter(owner=self).add(
estimator=self.estimator,
method_mapping=MethodMapping().add(caller="fit", callee="fit"),
)
Expand Down Expand Up @@ -520,7 +520,7 @@ def predict(self, X, **predict_params):

def get_metadata_routing(self):
router = (
MetadataRouter(owner=self.__class__.__name__)
MetadataRouter(owner=self)
.add_self_request(self)
.add(
estimator=self.estimator,
Expand Down Expand Up @@ -550,7 +550,7 @@ def fit(self, X, y, sample_weight=None, **kwargs):

def get_metadata_routing(self):
router = (
MetadataRouter(owner=self.__class__.__name__)
MetadataRouter(owner=self)
.add_self_request(self)
.add(
estimator=self.estimator,
Expand All @@ -576,7 +576,7 @@ def transform(self, X, y=None, **transform_params):
return self.transformer_.transform(X, **params.transformer.transform)

def get_metadata_routing(self):
return MetadataRouter(owner=self.__class__.__name__).add(
return MetadataRouter(owner=self).add(
transformer=self.transformer,
method_mapping=MethodMapping()
.add(caller="fit", callee="fit")
Expand Down
Loading
Loading