MetadataRouter#
- class sklearn.utils.metadata_routing.MetadataRouter(owner)[source]#
Stores and handles metadata routing for a router object.
This class is used by router objects to store and handle metadata routing. Routing information is stored as a dictionary of the form
{"object_name": RouteMappingPair(method_mapping, routing_info)}
, wheremethod_mapping
is an instance ofMethodMapping
androuting_info
is either aMetadataRequest
or aMetadataRouter
instance.Added in version 1.3.
- Parameters:
- ownerstr
The name of the object to which these requests belong.
- add(*, method_mapping, **objs)[source]#
Add named objects with their corresponding method mapping.
- Parameters:
- method_mappingMethodMapping
The mapping between the child and the parent’s methods.
- **objsdict
A dictionary of objects from which metadata is 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 routing.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 objects to which metadata is routed by the router.- Parameters:
- objobject
This is typically the router instance, i.e.
self
in aget_metadata_routing()
implementation. It can also be aMetadataRequest
instance.
- Returns:
- selfMetadataRouter
Returns
self
.
- consumes(method, params)[source]#
check whether the given parameters are consumed by the given method.
Added in version 1.4.
- Parameters:
- methodstr
The name of the method to check.
- paramsiterable of str
An iterable of parameters to check.
- Returns:
- consumedset of str
A set of parameters which are consumed by the given method.
- route_params(*, caller, params)[source]#
Return the input parameters requested by child objects.
The output of this method is a bunch, which includes the metadata for all methods of each child object that is used in the router’s
caller
method.If the router is also a consumer, it also checks for warnings of
self
’s/consumer’s requested metadata.- Parameters:
- callerstr
The name of the method for which the parameters are requested and routed. If called inside the fit method of a router, it would be
"fit"
.- paramsdict
A dictionary of provided metadata.
- Returns:
- paramsBunch
A
Bunch
of the form{"object_name": {"method_name": {params: value}}}
which can be used to pass the required metadata to corresponding methods or corresponding child objects.
- 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 method for which the parameters are requested and routed. If called inside the fit method of a router, it would be
"fit"
.- paramsdict
A dictionary of provided metadata.