MetadataRouter#

class sklearn.utils.metadata_routing.MetadataRouter(owner)[source]#

Coordinates metadata routing for a router object.

This class is used by meta-estimators or functions that can route metadata, to handle their metadata routing. Routing information is stored in a dictionary-like structure of the form {"object_name": RouterMappingPair(mapping, router)}, where mapping is an instance of MethodMapping and router is either a MetadataRequest or another MetadataRouter instance.

Added in version 1.3.

Parameters:
ownerstr

The name of the object to which these requests belong.

add(*, method_mapping, **objs)[source]#

Add consumers to the MetadataRouter.

The estimators that consume metadata are passed as named objects along with a method mapping, that defines how their methods relate to those of the router.

Parameters:
method_mappingMethodMapping

The mapping between the child (consumer) and the parent’s (router’s) methods.

**objsdict

A dictionary of objects, whose requests are extracted by calling get_routing_for_object on them.

Returns:
selfMetadataRouter

Returns self.

add_self_request(obj)[source]#

Add self (as a consumer) to the MetadataRouter.

This method is used if the router is also a consumer, and hence the router itself needs to be included in the routing. The passed object can be an estimator or a MetadataRequest.

A router should add itself using this method instead of add since it should be treated differently than the other consumer objects to which metadata is routed by the router.

Parameters:
objobject

This is typically the router instance, i.e. self in a get_metadata_routing() implementation. It can also be a MetadataRequest instance.

Returns:
selfMetadataRouter

Returns self.

consumes(method, params)[source]#

Return params consumed as metadata in a router or its sub-estimators.

This method returns the subset of params that are consumed by the method. A param is considered consumed if it is used in the specified method of the router itself or any of its sub-estimators (or their sub-estimators).

Added in version 1.4.

Parameters:
methodstr

The name of the method for which to determine consumed parameters.

paramsiterable of str

An iterable of parameter names to test for consumption.

Returns:
consumed_paramsset of str

A subset of parameters from params which are consumed by this method.

route_params(*, caller, params)[source]#

Get the values of metadata requested by consumers.

Returns a Bunch containing the metadata that this router’s caller method needs to route, organized by each consumer and their corresponding methods.

This can be used to pass the required metadata to corresponding methods in consumers.

Parameters:
callerstr

The name of the router’s method through which the metadata is routed. For example, if called inside the fit method of a router, this would be "fit".

paramsdict

A dictionary of provided metadata.

Returns:
paramsBunch

A Bunch of the form {"object_name": {"method_name": {metadata: value}}}.

validate_metadata(*, method, params)[source]#

Validate given metadata for a method.

This raises a TypeError if some of the passed metadata are not understood by child objects.

Parameters:
methodstr

The name of the router’s method through which the metadata is routed. For example, if called inside the fit method of a router, this would be "fit".

paramsdict

A dictionary of provided metadata.