
    -i                        S r SSKrSSKJr  SSKJr  SSKJrJrJ	r	  SSK
Jr  SSKJr  SS	KJr  S
SKJr  / SQrSS/SS/S.r\\" \R+                  5       5      -   rS rS4S jrS r " S S5      rSrSrSrSSS\\/rS rS r  " S S5      r! " S S 5      r"\" S!S"S#/5      r#\" S$S%S&/5      r$ " S' S(5      r% " S) S*5      r&S4S+ jr'S,r(S-r)S.r* " S/ S05      r+ " S1 S25      r,S3 r-g)5a  
Metadata Routing Utility

In order to better understand the components implemented in this file, one
needs to understand their relationship to one another.

The only relevant public API for end users are the ``set_{method}_request`` methods,
e.g. ``estimator.set_fit_request(sample_weight=True)``. However, third-party
developers and users who implement custom meta-estimators, need to deal with
the objects implemented in this file.

The routing is coordinated by building ``MetadataRequest`` objects
for objects that consume metadata, and ``MetadataRouter`` objects for objects that
can route metadata, which are then aligned during a call to `process_routing()`. This
function returns a Bunch object (dictionary-like) with all the information on the
consumers and which metadata they had requested and the actual metadata values. A
routing method (such as `fit` in a meta-estimator) can now provide the metadata to the
relevant consuming method (such as `fit` in a sub-estimator).

The ``MetadataRequest`` and ``MetadataRouter`` objects are constructed via a
``get_metadata_routing`` method, which all scikit-learn estimators provide.
This method is automatically implemented via ``BaseEstimator`` for all simple
estimators, but needs a custom implementation for meta-estimators.

MetadataRequest
~~~~~~~~~~~~~~~

In non-routing consumers, the simplest case, e.g. ``SVM``, ``get_metadata_routing``
returns a ``MetadataRequest`` object  which is assigned to the consumer's
`_metadata_request` attribute. It stores which metadata is required by each method of
the consumer by including one ``MethodMetadataRequest`` per method in ``METHODS``
(e. g. ``fit``, ``score``, etc).

Users and developers almost never need to directly add a new ``MethodMetadataRequest``,
to the consumer's `_metadata_request` attribute, since these are generated
automatically. This attribute is modified while running `set_{method}_request` methods
(such as `set_fit_request()`), which adds the request via
`method_metadata_request.add_request(param=prop, alias=alias)`.

The ``alias`` in the ``add_request`` method has to be either a string (an alias),
or one of ``[True (requested), False (unrequested), None (error if passed)]``. There
are some other special values such as ``UNUSED`` and ``WARN`` which are used
for purposes such as warning of removing a metadata in a child class, but not
used by the end users.

MetadataRouter
~~~~~~~~~~~~~~

In routers (such as meta-estimators or multi metric scorers), ``get_metadata_routing``
returns a ``MetadataRouter`` object. It provides information about which method, from
the router object, calls which method in a consumer's object, and also, which metadata
had been requested by the consumer's methods, thus specifying how metadata is to be
passed. If a sub-estimator is a router as well, their routing information is also stored
in the meta-estimators router.

Conceptually, this information looks like:

```
{
    "sub_estimator1": (
        mapping=[(caller="fit", callee="transform"), ...],
        router=MetadataRequest(...),  # or another MetadataRouter
    ),
    ...
}
```

The `MetadataRouter` objects are never stored and are always recreated anew whenever
the object's `get_metadata_routing` method is called.

An object that is both a router and a consumer, e.g. a meta-estimator which
consumes ``sample_weight`` and routes ``sample_weight`` to its sub-estimators
also returns a ``MetadataRouter`` object. Its routing information includes both
information about what metadata is required by the object itself (added via
``MetadataRouter.add_self_request``), as well as the routing information for its
sub-estimators (added via ``MetadataRouter.add``).

Implementation Details
~~~~~~~~~~~~~~~~~~~~~~

To give the above representation some structure, we use the following objects:

- ``(caller=..., callee=...)`` is a namedtuple called ``MethodPair``.

- The list of ``MethodPair`` stored in the ``mapping`` field of a `RouterMappingPair` is
  a ``MethodMapping`` object.

- ``(mapping=..., router=...)`` is a namedtuple called ``RouterMappingPair``.

The ``set_{method}_request`` methods are dynamically generated for estimators
which inherit from ``BaseEstimator``. This is done by attaching instances
of the ``RequestMethod`` descriptor to classes, which is done in the
``_MetadataRequester`` class, and ``BaseEstimator`` inherits from this mixin.
This mixin also implements the ``get_metadata_routing``, which meta-estimators
need to override, but it works for simple consumers as is.
    N)
namedtuple)deepcopy)TYPE_CHECKINGOptionalUnion)warn   )
get_config)UnsetMetadataPassedError   )Bunch)
fitpartial_fitpredictpredict_probapredict_log_probadecision_functionscoresplit	transforminverse_transformr   r   r   )fit_transformfit_predictc                  6    [        5       R                  SS5      $ )zReturn whether metadata routing is enabled.

.. versionadded:: 1.3

Returns
-------
enabled : bool
    Whether metadata routing is enabled. If the config is not set, it
    defaults to False.
enable_metadata_routingF)r
   get     S/var/www/html/venv/lib/python3.13/site-packages/sklearn/utils/_metadata_requests.py_routing_enabledr       s     <5u==r   c                    U(       a  UR                   R                   SU 3OUR                   R                  nUb  UO0 n[        5       (       d3  U R                  5       U-
  (       a  [	        SU S[        U 5       35      egg)a  Raise an error if metadata routing is not enabled and params are passed.

.. versionadded:: 1.4

Parameters
----------
params : dict
    The metadata passed to a method.

owner : object
    The object to which the method belongs.

method : str
    The name of the method, e.g. "fit".

allow : list of str, default=None
    A list of parameters which are allowed to be passed even if metadata
    routing is not enabled.

Raises
------
ValueError
    If metadata routing is not enabled and params are passed.
.Nz#Passing extra keyword arguments to z is only supported if enable_metadata_routing=True, which you can set using `sklearn.set_config`. See the User Guide <https://scikit-learn.org/stable/metadata_routing.html> for more details. Extra parameters passed are: )	__class____name__r    keys
ValueErrorset)paramsownermethodallowcallers        r   _raise_for_paramsr-      s    4 395??##
$AfX.eoo>V>V  &EBE6;;=5#81& :6 7:&k]	D
 	
 $9r   c           
         UR                  5        VVs0 s H  u  p4Uc  M
  X4_M     nnn[        5       (       aL  U(       aD  U R                  R                  n[	        U SU S[        UR                  5       5       SU S35      eggs  snnf )a5  Raise when metadata routing is enabled and metadata is passed.

This is used in meta-estimators which have not implemented metadata routing
to prevent silent bugs. There is no need to use this function if the
meta-estimator is not accepting any metadata, especially in `fit`, since
if a meta-estimator accepts any metadata, they would do that in `fit` as
well.

Parameters
----------
obj : estimator
    The estimator for which we're raising the error.

method : str
    The method where the error is raised.

**kwargs : dict
    The metadata passed to the method.
Nr"   z cannot accept given metadata (z4) since metadata routing is not yet implemented for )itemsr    r#   r$   NotImplementedErrorr'   r%   )objr*   kwargskeyvaluecls_names         r   _raise_for_unsupported_routingr6      s    ( ,2<<>O>ZSUjcj>FOf==))!j&!@V[[]AS@T UBBJ1N
 	
 % Ps
   	BBc                       \ rS rSrSrS rSrg)_RoutingNotSupportedMixin   zA mixin to be used to remove the default `get_metadata_routing`.

This is used in meta-estimators where metadata routing is not yet
implemented.

This also makes it clear in our rendered documentation that this method
cannot be used.
c                 F    [        U R                  R                   S35      e)zSRaise `NotImplementedError`.

This estimator does not support metadata routing yet.z* has not implemented metadata routing yet.)r0   r#   r$   selfs    r   get_metadata_routing._RoutingNotSupportedMixin.get_metadata_routing   s'     "~~&&''QR
 	
r   r   N)r$   
__module____qualname____firstlineno____doc__r=   __static_attributes__r   r   r   r8   r8      s    
r   r8   z$UNUSED$z$WARN$z$UNCHANGED$FTc                 f    U [         ;   a  g[        U [        5      =(       a    U R                  5       $ )aq  Check if an item is a valid string alias for a metadata.

Values in ``VALID_REQUEST_VALUES`` are not considered aliases in this
context. Only a string which is a valid identifier is.

Parameters
----------
item : object
    The given item to be checked if it can be an alias for the metadata.

Returns
-------
result : bool
    Whether the given item is a valid alias.
F)VALID_REQUEST_VALUES
isinstancestrisidentifieritems    r   request_is_aliasrK   	  s-      ## dC 8T%6%6%88r   c                     U [         ;   $ )zCheck if an item is a valid request value (and not an alias).

Parameters
----------
item : object
    The given item to be checked.

Returns
-------
result : bool
    Whether the given item is valid.
)rE   rI   s    r   request_is_validrM      s     '''r   c                   b    \ rS rSrSrSS jr\S 5       rS rS r	S r
S	 rS
 rS rS rS rSrg)MethodMetadataRequesti6  a  Container for metadata requests associated with a single method.

Instances of this class get used within a :class:`MetadataRequest` - one per each
public method (`fit`, `transform`, ...) that its owning consumer has.

.. versionadded:: 1.3

Parameters
----------
owner : str
    A display name for the object owning these requests.

method : str
    The name of the method to which these requests belong.

requests : dict of {str: bool, None or str}, default=None
    The initial requests for this method.
Nc                 L    U=(       d
    [        5       U l        Xl        X l        g N)dict	_requestsr)   r*   )r<   r)   r*   requestss       r   __init__MethodMetadataRequest.__init__J  s    !+TV
r   c                     U R                   $ )z)Dictionary of the form: ``{key: alias}``.rS   r;   s    r   rT   MethodMetadataRequest.requestsO  s     ~~r   c                   [        U5      (       d"  [        U5      (       d  [        SU SU S35      eX!:X  a  SnU[        :X  a-  XR                  ;   a  U R                  U	 U $ [        SU S35      eX R                  U'   U $ )a  Add request info for a metadata.

Parameters
----------
param : str
    The metadata for which a request is set.

alias : str, or {True, False, None}
    Specifies which metadata should be routed to the method that owns this
    `MethodMetadataRequest`.

    - str: the name (or alias) of metadata given to a meta-estimator that
      should be routed to the method that owns this `MethodMetadataRequest`.

    - True: requested

    - False: not requested

    - None: error if passed
zThe alias you're setting for `zZ` should be either a valid identifier or one of {None, True, False}, but given value is: ``TzTrying to remove parameter z! with UNUSED which doesn't exist.)rK   rM   r&   UNUSEDrS   )r<   paramaliass      r   add_request!MethodMetadataRequest.add_requestT  s    4  &&/?/F/F0 8#WA'  >EF?&NN5)  !1% 9  
 %*NN5!r   c                 ^   ^ [        U4S jU R                  R                  5        5       5      $ )a  Get names of all metadata that can be consumed or routed by this method.

This method returns the names of all metadata, even the ``False``
ones.

Parameters
----------
return_alias : bool
    Controls whether original or aliased names should be returned. If
    ``False``, aliases are ignored and original names are returned.

Returns
-------
names : set of str
    A set of strings with the names of all metadata.
c              3      >#    U  H9  u  p[        U5      (       a  US Ld  M  T(       a  [        U5      (       d  UOUv   M;     g7f)FN)rM   ).0propr^   return_aliass      r   	<genexpr>9MethodMetadataRequest._get_param_names.<locals>.<genexpr>  sA      
5#E**e5.@ L\*:5*A*AEtK5s
   A"A)r'   rS   r/   )r<   re   s    `r   _get_param_names&MethodMetadataRequest._get_param_names  s,    "  
#~~335
 
 	
r   c          
          Uc  0 OUnU R                   R                  5        VVs1 s H  u  p#U[        :X  d  M  X!;   d  M  UiM     nnnU H"  n[        SU SU R                   SU S35        M$     gs  snnf )zCheck whether metadata is passed which is marked as WARN.

If any metadata is passed which is marked as WARN, a warning is raised.

Parameters
----------
params : dict
    The metadata passed to a method.
NzSupport for zj has recently been added to this class. To maintain backward compatibility, it is ignored now. Using `set_z	_request(z={True, False})` on this method of the class, you can set the request value to False to silence this warning, or to True to consume and use the metadata.)rS   r/   WARNr   r*   )r<   r(   rd   r^   warn_paramsr]   s         r   _check_warnings%MethodMetadataRequest._check_warnings  s     ~6  $~~335
5} !% 5 	 

 !Eug &"kk])E7 ;$$ !
s   A2A2 A2c                    U R                  US9  [        5       nUR                  5        VVs0 s H  u  pVUc  M
  XV_M     nnn[        5       nU R                  R                  5        HE  u  pU
SL d
  U
[
        :X  a  M  U
SL a  X;   a  Xy   X'   M(  U
c  X;   a  Xy   XI'   M8  X;   d  M?  Xz   X'   MG     U(       a  U R                  [        ;   a  [        U R                     nOU R                  /nSR                  U Vs/ s H	  nSU S3PM     sn5      nSSR                  U Vs/ s H  oPM     sn5       S	U R                   S
U R                   SU S
U SU R                   3U-   S-   n[        UUUS9eU$ s  snnf s  snf s  snf )a  Prepare the given metadata to be passed to the method.

The output of this method can be used directly as the input to the
corresponding method as **kwargs.

Parameters
----------
params : dict
    A dictionary of provided metadata.

parent : object
    Parent class object, that routes the metadata.

caller : str
    Method from the parent class object, where the metadata is routed from.

Returns
-------
params : Bunch
    A :class:`~sklearn.utils.Bunch` of {metadata: value} which can be
    passed to the corresponding method.
r(   FT z.set_z_request({metadata}=True/False)[, zJ] are passed but are not explicitly set as requested or not requested for r"   z, which is used within z. Call `z` for each metadata you want to request/ignore. See the Metadata Routing User guide <https://scikit-learn.org/stable/metadata_routing.html> for more information.)messageunrequested_paramsrouted_params)rm   rR   r/   r   rS   rk   r*   COMPOSITE_METHODSjoinr)   r   )r<   r(   parentr,   unrequestedargr4   argsresrd   r^   callee_methodsr*   set_requests_onr3   rt   s                   r   _route_params#MethodMetadataRequest._route_params  s   . 	F+f-3\\^Q^zsu

^Qg>>//1KD~$$4< J	4<$(J! K	 2 {{//!24;;!?"&++ gg #1"0 F8#DE"0O DIIk:kssk:;< =JJ<q .81VHHTZZL: "	"
    +#.! 
 
M R" ;s   	E6E6;E<"F
c                    [        U5      n[        5       nU R                  R                  5        HQ  u  p4USL a  X1;   a  UR                  U5        M"  [	        U[
        5      (       d  M9  XA;   d  M@  UR                  U5        MS     U$ )zCheck whether the given metadata are consumed by this method.

Parameters
----------
params : iterable of str
    An iterable of parameters to check.

Returns
-------
consumed : set of str
    A set of parameters which are consumed by this method.
T)r'   rS   r/   addrF   rG   )r<   r(   r}   rd   r^   s        r   	_consumesMethodMetadataRequest._consumes  sf     Ve>>//1KD}E3''EO	 2
 
r   c                     U R                   $ xSerialize the object.

Returns
-------
obj : dict
    A serialized version of the instance in the form of a dictionary.
rX   r;   s    r   
_serialize MethodMetadataRequest._serialize  s     ~~r   c                 4    [        U R                  5       5      $ rQ   rG   r   r;   s    r   __repr__MethodMetadataRequest.__repr__      4??$%%r   c                 *    [        [        U 5      5      $ rQ   rG   reprr;   s    r   __str__MethodMetadataRequest.__str__      4:r   )rS   r*   r)   rQ   )r$   r?   r@   rA   rB   rU   propertyrT   r_   rh   rm   r   r   r   r   r   rC   r   r   r   rO   rO   6  sJ    &
  /b
.4?B,&r   rO   c                   V    \ rS rSrSrSrS rS rS rSS jr	S	 r
S
 rS rS rS rSrg)MetadataRequesti  a  Contains the metadata request info of a consumer.

Instances of `MethodMetadataRequest` are used in this class for each
available method under `metadatarequest.{method}`.

Consumer-only classes such as simple estimators return a serialized
version of this class as the output of `get_metadata_routing()`.

.. versionadded:: 1.3

Parameters
----------
owner : str
    The name of the object to which these requests belong.
metadata_requestc           
      R    Xl         [         H  n[        U U[        XS95        M     g )Nr)   r*   )r)   SIMPLE_METHODSsetattrrO   )r<   r)   r*   s      r   rU   MetadataRequest.__init__4  s'    
$F%EA %r   c                 2    [        X5      R                  US9$ )aL  Check whether the given metadata are consumed by the given method.

.. versionadded:: 1.4

Parameters
----------
method : str
    The name of the method to check.

params : iterable of str
    An iterable of parameters to check.

Returns
-------
consumed : set of str
    A set of parameters which are consumed by the given method.
rp   )getattrr   r<   r*   r(   s      r   consumesMetadataRequest.consumes=  s    $ t$..f.==r   c                 X   U[         ;  a&  [        SU R                  R                   SU S35      e0 n[         U    H  n[	        X5      n[        UR                  5       5      n[        UR                  R                  5       5      nXV-  nU Vs/ s H  oU   UR                  U   :w  d  M  UPM     n	nU	(       a:  [        SSR                  U	5       SU SSR                  [         U   5       S35      eUR                  UR                  5        M     [        U R                  XS9$ s  snf )	N'z' object has no attribute 'z"Conflicting metadata requests for rs   z" while composing the requests for z*. Metadata with the same name for methods z$ should have the same request value.)r)   r*   rT   )rw   AttributeErrorr#   r$   r   r'   r%   rT   rS   r&   rx   updaterO   r)   )
r<   namerT   r*   mmrexistingupcomingcommonr3   	conflictss
             r   __getattr__MetadataRequest.__getattr__Q  s*    (( DNN++,,GvQO  '-F$'C8==?+H3<<,,./H(F(.V3-3==QTCU2UIV 899M8N O337& 9$$(II.?.E$F#G H++  OOCMM* . %4::dVV Ws   D'+D'Nc                 2    [        X5      R                  US9$ )aJ  Get names of all metadata that can be consumed or routed by specified             method.

This method returns the names of all metadata, even the ``False``
ones.

Parameters
----------
method : str
    The name of the method for which metadata names are requested.

return_alias : bool
    Controls whether original or aliased names should be returned. If
    ``False``, aliases are ignored and original names are returned.

ignore_self_request : bool
    Ignored. Present for API compatibility.

Returns
-------
names : set of str
    A set of strings with the names of all metadata.
)re   )r   rh   )r<   r*   re   ignore_self_requests       r   rh    MetadataRequest._get_param_nameso  s    0 t$55<5PPr   c                4    [        X5      R                  XUS9$ )a  Prepare the given parameters to be passed to the method.

The output of this method can be used directly as the input to the
corresponding method as extra keyword arguments to pass metadata.

Parameters
----------
params : dict
    A dictionary of provided metadata.

method : str
    The name of the method for which the parameters are requested and
    routed.

parent : object
    Parent class object, that routes the metadata.

caller : str
    Method from the parent class object, where the metadata is routed from.

Returns
-------
params : Bunch
    A :class:`~sklearn.utils.Bunch` of {metadata: value} which can be given to
    the corresponding method.
)r(   ry   r,   )r   r   )r<   r(   r*   ry   r,   s        r   r   MetadataRequest._route_params  s&    6 t$22 3 
 	
r   c                4    [        X5      R                  US9  g)a   Check whether metadata is passed which is marked as WARN.

If any metadata is passed which is marked as WARN, a warning is raised.

Parameters
----------
method : str
    The name of the method for which the warnings should be checked.

params : dict
    The metadata passed to a method.
rp   N)r   rm   r   s      r   rm   MetadataRequest._check_warnings  s     	--V-<r   c                     [        5       n[         H<  n[        X5      n[        UR                  5      (       d  M*  UR                  5       X'   M>     U$ r   )rR   r   r   lenrT   r   )r<   outputr*   r   s       r   r   MetadataRequest._serialize  sC     $F$'C3<<  !$!1 % r   c                 4    [        U R                  5       5      $ rQ   r   r;   s    r   r   MetadataRequest.__repr__  r   r   c                 *    [        [        U 5      5      $ rQ   r   r;   s    r   r   MetadataRequest.__str__  r   r   r)   rQ   )r$   r?   r@   rA   rB   _typerU   r   r   rh   r   rm   r   r   r   rC   r   r   r   r   r     s=    & E>(W<Q4
>=&r   r   RouterMappingPairmappingrouter
MethodPairr,   calleec                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)MethodMappingi  ax  Stores the mapping between caller and callee methods for a :term:`router`.

This class is primarily used in a ``get_metadata_routing()`` of a router
object when defining the mapping between the router's methods and a sub-object (a
sub-estimator or a scorer).

Iterating through an instance of this class yields
``MethodPair(caller, callee)`` instances.

.. versionadded:: 1.3
c                     / U l         g rQ   _routesr;   s    r   rU   MethodMapping.__init__  s	    r   c                 ,    [        U R                  5      $ rQ   )iterr   r;   s    r   __iter__MethodMapping.__iter__  s    DLL!!r   c                    U[         ;  a  [        SU S[          35      eU[         ;  a  [        SU S[          35      eU R                  R                  [	        XS95        U $ )a  Add a method mapping.

Parameters
----------

caller : str
    Parent estimator's method name in which the ``callee`` is called.

callee : str
    Child object's method name. This method is called in ``caller``.

Returns
-------
self : MethodMapping
    Returns self.
zGiven caller:z+ is not a valid method. Valid methods are: zGiven callee:r,   r   )METHODSr&   r   appendr   )r<   r,   r   s      r   r   MethodMapping.add  sz    "  x (9   x (9  	JfDEr   c                     [        5       nU R                   H+  nUR                  UR                  UR                  S.5        M-     U$ )zrSerialize the object.

Returns
-------
obj : list
    A serialized version of the instance in the form of a list.
r   )listr   r   r,   r   )r<   resultroutes      r   r   MethodMapping._serialize  s7     \\EMMU\\U\\JK "r   c                 4    [        U R                  5       5      $ rQ   r   r;   s    r   r   MethodMapping.__repr__  r   r   c                 *    [        [        U 5      5      $ rQ   r   r;   s    r   r   MethodMapping.__str__  r   r   r   N)r$   r?   r@   rA   rB   rU   r   r   r   r   r   rC   r   r   r   r   r     s%    
"<&r   r   c                   d    \ rS rSrSrSrS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rSrg)MetadataRouteri"  a  Coordinates metadata routing for a :term:`router` object.

This class is used by :term:`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 :class:`~sklearn.utils.metadata_routing.MethodMapping` and
``router`` is either a
:class:`~sklearn.utils.metadata_routing.MetadataRequest` or another
:class:`~sklearn.utils.metadata_routing.MetadataRouter` instance.

.. versionadded:: 1.3

Parameters
----------
owner : str
    The name of the object to which these requests belong.
metadata_routerc                 <    [        5       U l        S U l        Xl        g rQ   )rR   _route_mappings_self_requestr)   )r<   r)   s     r   rU   MetadataRouter.__init__;  s    #v
 "
r   c                     [        USS5      S:X  a  [        U5      U l        U $ [        US5      (       a   [        UR	                  5       5      U l        U $ [        S5      e)a  Add `self` (as a :term:`consumer`) to the `MetadataRouter`.

This method is used if the :term:`router` is also a :term:`consumer`, and hence
the router itself needs to be included in the routing. The passed object
can be an estimator or a
:class:`~sklearn.utils.metadata_routing.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
----------
obj : object
    This is typically the router instance, i.e. `self` in a
    ``get_metadata_routing()`` implementation. It can also be a
    ``MetadataRequest`` instance.

Returns
-------
self : MetadataRouter
    Returns `self`.
r   Nr   _get_metadata_requestzGiven `obj` is neither a `MetadataRequest` nor does it implement the required API. Inheriting from `BaseEstimator` implements the required API.)r   r   r   hasattrr   r&   )r<   r1   s     r   add_self_requestMetadataRouter.add_self_requestD  sj    0 3&*<<!)#D  S122!)#*C*C*E!FD   r   c                    [        U5      nUR                  5        H%  u  p4[        U[        U5      S9U R                  U'   M'     U $ )aV  Add :term:`consumers <consumer>` 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
:term:`router`.

Parameters
----------
method_mapping : MethodMapping
    The mapping between the child (:term:`consumer`) and the parent's
    (:term:`router`'s) methods.

**objs : dict
    A dictionary of objects, whose requests are extracted by calling
    :func:`~sklearn.utils.metadata_routing.get_routing_for_object` on them.

Returns
-------
self : MetadataRouter
    Returns `self`.
r   r   )r   r/   r   get_routing_for_objectr   )r<   method_mappingobjsr   r1   s        r   r   MetadataRouter.addh  sG    , ".1ID):&/Ec/J*D  & & r   c                 &   [        5       nU R                  (       a  X0R                  R                  XS9-  nU R                  R	                  5        H<  u  pEUR
                   H'  u  pgXa:X  d  M  X5R                  R                  XrS9-  nM)     M>     U$ )aK  Check whether the given metadata is consumed by the given method.

.. versionadded:: 1.4

Parameters
----------
method : str
    The name of the method to check.

params : iterable of str
    An iterable of parameters to check.

Returns
-------
consumed : set of str
    A set of parameters which are consumed by the given method.
)r*   r(   )r'   r   r   r   r/   r   r   )r<   r*   r(   r}   _route_mappingr,   r   s           r   r   MetadataRouter.consumes  s    $ e**3363QQC $ 4 4 : : <A"/"7"7# 4 4 = =% !> ! C #8 != 
r   c          
      l   [        5       nU R                  (       a/  U(       d(  UR                  U R                  R                  XS95      nU R                  R                  5        HK  u  pVUR                   H6  u  pxXq:X  d  M  UR                  UR                  R                  USSS95      nM8     MM     U$ )a  Get names of all metadata that can be consumed or routed by specified             method.

This method returns the names of all metadata, even the ``False``
ones.

Parameters
----------
method : str
    The name of the method for which metadata names are requested.

return_alias : bool
    Controls whether original or aliased names should be returned,
    which only applies to the stored `self`. If no `self` routing
    object is stored, this parameter has no effect.

ignore_self_request : bool
    If `self._self_request` should be ignored. This is used in `_route_params`.
    If ``True``, ``return_alias`` has no effect.

Returns
-------
names : set of str
    A set of strings with the names of all metadata.
r*   re   TFr*   re   r   )r'   r   unionrh   r   r/   r   r   )	r<   r*   re   r   r}   r   r   r,   r   s	            r   rh   MetadataRouter._get_param_names  s    4 e&9))""33! 4 C $(#7#7#=#=#?D"/"7"7#))%,,==#)RW > C #8 $@ 
r   c          	         [        5       nU R                  (       a+  UR                  U R                  R                  UUUUS95        U R	                  USSS9nUR                  5        VVs0 s H  u  pxXv;   d  M  Xx_M     n	nn[        UR                  5       5      R                  U	R                  5       5       H(  nX   XW   Ld  M  [        SU R                   SU S35      e   UR                  U	5        U$ s  snnf )a5  Prepare the given metadata to be passed to the method.

This is used when a router is used as a child object of another router.
The parent router then passes all parameters understood by the child
object to it and delegates their validation to the child.

The output of this method can be used directly as the input to the
corresponding method as **kwargs.

Parameters
----------
params : dict
    A dictionary of provided metadata.

method : str
    The name of the method for which the metadata is requested and routed.

parent : object
    Parent class object, that routes the metadata.

caller : str
    Method from the parent class object, where the metadata is routed from.

Returns
-------
params : Bunch
    A :class:`~sklearn.utils.Bunch` of {metadata: value} which can be given to
    the corresponding method.
r(   r*   ry   r,   Tr   zIn z, there is a conflict on z between what is requested for this estimator and what is requested by its children. You can resolve this conflict by using an alias for the child estimators' requested metadata.)r   r   r   r   rh   r/   r'   r%   intersectionr&   r)   )
r<   r(   r*   ry   r,   r}   param_namesr3   r4   child_paramss
             r   r   MetadataRouter._route_params  s   < gJJ""00!!!!	 1  ++$ , 
 *0
)7:33;MJCJ 	 
 sxxz?//0A0A0CDC  0 $**%>se DA A 	 E 	

< 

s   +C8:C8c          	      `   U R                   (       a  U R                   R                  X!S9  [        5       nU R                  R	                  5        H[  u  pEUR
                  UR                  pv[        5       X4'   U H-  u  pX:X  d  M  UR                  UU	U R                  US9X4   U	'   M/     M]     U$ )a  Get the values of metadata requested by :term:`consumers <consumer>`.

Returns a :class:`~sklearn.utils.Bunch` containing the metadata that this
:term:`router`'s `caller` method needs to route, organized by each
:term:`consumer` and their corresponding methods.

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

Parameters
----------
caller : str
    The name of the :term:`router`'s method through which the metadata is
    routed. For example, if called inside the :term:`fit` method of a router,
    this would be `"fit"`.

params : dict
    A dictionary of provided metadata.

Returns
-------
params : Bunch
    A :class:`~sklearn.utils.Bunch` of the form
    ``{"object_name": {"method_name": {metadata: value}}}``.
r(   r*   r   )	r   rm   r   r   r/   r   r   r   r)   )
r<   r,   r(   r}   r   r   r   r   _caller_callees
             r   route_paramsMetadataRouter.route_params  s    4 ..f.Lg#'#7#7#=#=#?D+22M4I4IGCI$+ $)/)=)=%&#zz%	 *> *CIg& %,	 $@ 
r   c                   U R                  USSS9nU R                  (       a  U R                  R                  USS9nO
[        5       n[        UR                  5       5      U-
  U-
  nU(       a  [	        U R
                   SU SU S35      eg)a  Validate given metadata for a method.

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

Parameters
----------
method : str
    The name of the :term:`router`'s method through which the metadata is
    routed. For example, if called inside the :term:`fit` method of a router,
    this would be `"fit"`.

params : dict
    A dictionary of provided metadata.
Fr   r   r"   z got unexpected argument(s) z%, which are not routed to any object.N)rh   r   r'   r%   	TypeErrorr)   )r<   r*   r(   r   self_params
extra_keyss         r   validate_metadata MetadataRouter.validate_metadata:  s      ++5 , 
 ,,==E > K %K'+5C
::,ax'CJ< P1 1  r   c                 P   [        5       nU R                  (       a  U R                  R                  5       US'   U R                  R	                  5        HO  u  p#[        5       X'   UR
                  R                  5       X   S'   UR                  R                  5       X   S'   MQ     U$ )r   $self_requestr   r   )rR   r   r   r   r/   r   r   )r<   r}   r   r   s       r   r   MetadataRouter._serializeZ  s     f#'#5#5#@#@#BC #'#7#7#=#=#?DCI#0#8#8#C#C#ECIi "/"6"6"A"A"CCIh $@
 
r   c              #      #    U R                   (       a=  [        5       n[         H  nUR                  X"S9  M     S[	        XR                   S94v   U R
                  R                  5        H
  u  p4X44v   M     g 7f)Nr   r  r   )r   r   r   r   r   r   r/   )r<   r   r*   r   r   s        r   r   MetadataRouter.__iter__l  su     *_N!""&"@ "  !.ASAST  $(#7#7#=#=#?D'' $@s   A8A:c                 4    [        U R                  5       5      $ rQ   r   r;   s    r   r   MetadataRouter.__repr__x  r   r   c                 *    [        [        U 5      5      $ rQ   r   r;   s    r   r   MetadataRouter.__str__{  r   r   )r   r   r)   N)r$   r?   r@   rA   rB   r   rU   r   r   r   rh   r   r  r	  r   r   r   r   rC   r   r   r   r   r   "  sO    , E"H<>*X;z*X@$
(&r   r   c                     [        U S5      (       a  [        U R                  5       5      $ [        U SS5      S;   a  [        U 5      $ [	        SS9$ )a  Get a ``Metadata{Router, Request}`` instance from the given object.

This function returns a
:class:`~sklearn.utils.metadata_routing.MetadataRouter` or a
:class:`~sklearn.utils.metadata_routing.MetadataRequest` from the given input.

This function always returns a copy or an instance constructed from the
input, such that changing the output of this function will not change the
original object.

.. versionadded:: 1.3

Parameters
----------
obj : object
    - If the object provides a `get_metadata_routing` method, return a copy
        of the output of that method.
    - If the object is already a
        :class:`~sklearn.utils.metadata_routing.MetadataRequest` or a
        :class:`~sklearn.utils.metadata_routing.MetadataRouter`, return a copy
        of that.
    - Returns an empty :class:`~sklearn.utils.metadata_routing.MetadataRequest`
        otherwise.

Returns
-------
obj : MetadataRequest or MetadataRouter
    A ``MetadataRequest`` or a ``MetadataRouter`` taken or created from
    the given object.
r=   r   N)r   r   r   )r   r   r=   r   r   )r1   s    r   r   r     sN    B s*++00233	gt	$(O	O}&&r   a          Configure whether metadata should be requested to be passed to the ``{method}`` method.

        Note that this method is only relevant when this estimator is used as a
        sub-estimator within a :term:`meta-estimator` and metadata routing is enabled
        with ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).
        Please check the :ref:`User Guide <metadata_routing>` on how the routing
        mechanism works.

        The options for each parameter are:

        - ``True``: metadata is requested, and passed to ``{method}`` if provided. The request is ignored if metadata is not provided.

        - ``False``: metadata is not requested and the meta-estimator will not pass it to ``{method}``.

        - ``None``: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

        - ``str``: metadata should be passed to the meta-estimator with this given alias instead of the original name.

        The default (``sklearn.utils.metadata_routing.UNCHANGED``) retains the
        existing request. This allows you to change the request for some
        parameters and not others.

        .. versionadded:: 1.3

        Parameters
        ----------
z        {metadata} : str, True, False, or None,                     default=sklearn.utils.metadata_routing.UNCHANGED
            Metadata routing for ``{metadata}`` parameter in ``{method}``.

zV        Returns
        -------
        self : object
            The updated object.
c                   (    \ rS rSrSrSS jrS rSrg)RequestMethodi  a;  
Descriptor for defining `set_{method}_request` methods in estimators.

.. versionadded:: 1.3

Parameters
----------
name : str
    The name of the method for which the request function should be
    created, e.g. ``"fit"`` would create a ``set_fit_request`` function.

keys : list of str
    A list of strings which are accepted parameters by the created
    function, e.g. ``["sample_weight"]`` if the corresponding method
    accepts it as a metadata.

validate_keys : bool, default=True
    Whether to check if the requested parameters fit the actual parameters
    of the method.

Notes
-----
This class is a descriptor [1]_ and uses PEP-362 to set the signature of
the returned function [2]_.

References
----------
.. [1] https://docs.python.org/3/howto/descriptor.html

.. [2] https://www.python.org/dev/peps/pep-0362/
c                 (    Xl         X l        X0l        g rQ   )r   r%   validate_keys)r<   r   r%   r  s       r   rU   RequestMethod.__init__  s    		*r   c                   ^ ^ UU 4S jnST R                    S3Ul        [        R                  " S[        R                  R                  US9/nUR                  T R                   Vs/ s HO  n[        R                  " U[        R                  R                  [        [        [        [        S [        4      S9PMQ     sn5        [        R                  " UUS9Ul        [        R!                  T R                   S9nT R                   H$  nU["        R!                  UT R                   S	9-  nM&     U[$        -  nXcl        U$ s  snf )
Nc            
        > [        5       (       d  [        S5      eTR                  (       av  [        U5      [        TR                  5      -
  (       aP  [        S[        U5      [        TR                  5      -
   STR                   S[        TR                  5       35      eTc  U S   nU SS n OTnU (       a%  [        STR                   S	[        U 5       S
35      eUR                  5       n[        UTR                  5      nUR                  5        H  u  pVU[        Ld  M  UR                  XVS9  M!     X2l        U$ )zUpdates the `_metadata_request` attribute of the consumer (`instance`)
for the parameters provided as `**kw`.

This docstring is overwritten below.
See REQUESTER_DOC for expected functionality.
zThis method is only available when metadata routing is enabled. You can enable it using sklearn.set_config(enable_metadata_routing=True).zUnexpected args: z in z. Accepted arguments are: Nr   r   set_z+_request() takes 0 positional argument but z were givenr]   r^   )r    RuntimeErrorr  r'   r%   r  r   r   r   r   r/   	UNCHANGEDr_   _metadata_request)	r|   kw	_instancerT   method_metadata_requestrd   r^   instancer<   s	          r   func#RequestMethod.__get__.<locals>.func  s6    $%%"I  !!s2wTYY'?'B#dii.(@'Adii[ Q//2499~.>@   G	ABx$	 499+ &D	{+/ 
 !668H&-h		&B#!xxz	)+77d7P  * +3'r   r  _requestr<   )r   kind
annotation)defaultr)  )return_annotation)r*   )metadatar*   )r   r$   inspect	ParameterPOSITIONAL_OR_KEYWORDextendr%   KEYWORD_ONLYr  r   r   boolrG   	Signature__signature__REQUESTER_DOCformatREQUESTER_DOC_PARAMREQUESTER_DOC_RETURNrB   )r<   r$  r)   r%  r(   kdocr,  s   ``      r   __get__RequestMethod.__get__  s*   -	b tyyk2&&<< 
 	  #A !!%%22%'dD#o(>?	 #
	
 %..#
 ""$))"4		H&--x		-RRC "##'s   'AE )r%   r   r  N)T)r$   r?   r@   rA   rB   rU   r;  rC   r   r   r   r  r    s    @+
Or   r  c                      ^  \ rS rSrSr\(       a  S rS rS rS r	S r
S rS	 rS
 rS rS rU 4S jr\S 5       r\S 5       rS rS rSrU =r$ )_MetadataRequesteriU  z{Mixin class for adding metadata request functionality.

``BaseEstimator`` inherits from this Mixin.

.. versionadded:: 1.3
c                     g rQ   r   r<   r2   s     r   set_fit_request"_MetadataRequester.set_fit_requestf  s    Tr   c                     g rQ   r   r@  s     r   set_partial_fit_request*_MetadataRequester.set_partial_fit_requestg  s    Tr   c                     g rQ   r   r@  s     r   set_predict_request&_MetadataRequester.set_predict_requesth  s    r   c                     g rQ   r   r@  s     r   set_predict_proba_request,_MetadataRequester.set_predict_proba_requesti  s    tr   c                     g rQ   r   r@  s     r   set_predict_log_proba_request0_MetadataRequester.set_predict_log_proba_requestj      4r   c                     g rQ   r   r@  s     r   set_decision_function_request0_MetadataRequester.set_decision_function_requestk  rO  r   c                     g rQ   r   r@  s     r   set_score_request$_MetadataRequester.set_score_requestl      tr   c                     g rQ   r   r@  s     r   set_split_request$_MetadataRequester.set_split_requestm  rV  r   c                     g rQ   r   r@  s     r   set_transform_request(_MetadataRequester.set_transform_requestn  s    4r   c                     g rQ   r   r@  s     r   set_inverse_transform_request0_MetadataRequester.set_inverse_transform_requesto  rO  r   c                 h  >  U R                  5       n[         Hf  n[        X#5      n[        UR                  5      (       d  M*  [        U SU S3[        U[        UR                  R                  5       5      5      5        Mh     [        TU ]  " S0 UD6  g! [         a    [        TU ]  " S0 UD6   gf = f)aZ  Set the ``set_{method}_request`` methods.

This uses PEP-487 [1]_ to set the ``set_{method}_request`` methods. It
looks for the information available in the set default values which are
set using ``__metadata_request__*`` class attributes, or inferred
from method signatures.

The ``__metadata_request__*`` class attributes are used when a method
does not explicitly accept a metadata through its arguments or if the
developer would like to specify a request value for those metadata
which are different from the default ``None``.

References
----------
.. [1] https://www.python.org/dev/peps/pep-0487
Nr  r'  r   )_get_default_requests	Exceptionsuper__init_subclass__r   r   r   rT   r   r  sortedr%   )clsr2   rT   r*   r   r#   s        r   rd  $_MetadataRequester.__init_subclass__r  s    "	002H %F(+Cs||$$vhh'ffS\\->->-@&AB % 	!+F+#  	 G%//	s   B B10B1c                    [        U R                  US9n[        X5      (       a$  [        R                  " [        X5      5      (       d  U$ [        [        R                  " [        X5      5      R                  R                  5       5      SS nU HE  u  pVUS;   a  M  UR                  UR                  UR                  1;   a  M5  UR                  USS9  MG     U$ )a  Build the `MethodMetadataRequest` for a method using its signature.

This method takes all arguments from the method signature and uses
``None`` as their default request value, except ``X``, ``y``, ``Y``,
``Xt``, ``yt``, ``*args``, and ``**kwargs``.

Parameters
----------
router : MetadataRequest
    The parent object for the created `MethodMetadataRequest`.
method : str
    The name of the method.

Returns
-------
method_request : MethodMetadataRequest
    The prepared request using the method's signature.
r   r   N>   XYyXtytr  )rO   r$   r   r-  
isfunctionr   r   	signature
parametersr/   r(  VAR_POSITIONALVAR_KEYWORDr_   )rf  r   r*   r   r(   pnamer]   s          r   _build_request_for_signature/_MetadataRequester._build_request_for_signature  s    ( $#,,vF s##7+=+=gc>R+S+SJg''(<=HHNNPQRSRTU"LE33zze22E4E4EFFOO   # 
r   c           
         [        U R                  S9n[         H  n[        UUU R	                  XS95        M     Sn[        [        R                  " U 5      5       H|  n[        U5      R                  5        H\  u  pVX5;  a  M  XUR                  U5      [        U5      -   S nUR                  5        H  u  px[        X5      R                  XxS9  M     M^     M~     U$ )zCollect default request values.

This method combines the information present in ``__metadata_request__*``
class attributes, as well as determining request keys from method
signatures.
r   )r   r*   __metadata_request__Nr  )r   r$   r   r   rt  reversedr-  getmrovarsr/   indexr   r   r_   )	rf  rT   r*   substr
base_classattrr4   rd   r^   s	            r   ra  (_MetadataRequester._get_default_requests  s     #6$F000P %  ("7>>##67J#J/557% jj03v;>@A#(;;=KD H-999R $1  8 8 r   c                 v    [        U S5      (       a  [        U R                  5      nU$ U R                  5       nU$ )zGet requested metadata for the instance.

Please check :ref:`User Guide <metadata_routing>` on how the routing
mechanism works.

Returns
-------
request : MetadataRequest
    A :class:`~sklearn.utils.metadata_routing.MetadataRequest` instance.
r   )r   r   r   ra  )r<   rT   s     r   r   (_MetadataRequester._get_metadata_request  s>     4,---d.D.DEH  113Hr   c                 "    U R                  5       $ )a  Get metadata routing of this object.

Please check :ref:`User Guide <metadata_routing>` on how the routing
mechanism works.

Returns
-------
routing : MetadataRequest
    A :class:`~sklearn.utils.metadata_routing.MetadataRequest` encapsulating
    routing information.
)r   r;   s    r   r=   '_MetadataRequester.get_metadata_routing  s     ))++r   r   )r$   r?   r@   rA   rB   r   rA  rD  rG  rJ  rM  rQ  rT  rX  r[  r^  rd  classmethodrt  ra  r   r=   rC   __classcell__)r#   s   @r   r>  r>  U  sl      	295;??337?$,L # #J ) )V$, ,r   r>  c                \   U(       d   " S S5      nU" 5       $ [        U S5      (       d9  [        U [        5      (       d$  [        SU R                  R
                  < S35      eU[        ;  a  [        S[         SU S35      e[        U 5      nUR                  X!S	9  UR                  X!S
9nU$ )a  Validate and route metadata.

This function is used inside a :term:`router`'s method, e.g. :term:`fit`,
to validate the metadata and handle the routing.

Assuming this signature of a router's fit method:
``fit(self, X, y, sample_weight=None, **fit_params)``,
a call to this function would be:
``process_routing(self, "fit", sample_weight=sample_weight, **fit_params)``.

Note that if routing is not enabled and ``kwargs`` is empty, then it
returns an empty routing where ``process_routing(...).ANYTHING.ANY_METHOD``
is always an empty dictionary.

.. versionadded:: 1.3

Parameters
----------
_obj : object
    An object implementing ``get_metadata_routing``. Typically a
    :term:`meta-estimator`.

_method : str
    The name of the router's method in which this function is called.

**kwargs : dict
    Metadata to be routed.

Returns
-------
routed_params : Bunch
    A :class:`~utils.Bunch` of the form ``{"object_name": {"method_name":
    {metadata: value}}}`` which can be used to pass the required metadata to
    A :class:`~sklearn.utils.Bunch` of the form ``{"object_name": {"method_name":
    {metadata: value}}}`` which can be used to pass the required metadata to
    corresponding methods or corresponding child objects. The object names
    are those defined in `obj.get_metadata_routing()`.
c                   *    \ rS rSrSS jrS rS rSrg)%process_routing.<locals>.EmptyRequesti@  Nc           
      \    [        S0 [         Vs0 s H  o3[        5       _M     snD6$ s  snf Nr   r   r   rR   )r<   r   r*  r*   s       r   r   )process_routing.<locals>.EmptyRequest.getA  &    FWEW6WEFFE   )c           
      \    [        S0 [         Vs0 s H  o"[        5       _M     snD6$ s  snf r  r  r<   r   r*   s      r   __getitem__1process_routing.<locals>.EmptyRequest.__getitem__D  r  r  c           
      \    [        S0 [         Vs0 s H  o"[        5       _M     snD6$ s  snf r  r  r  s      r   r   1process_routing.<locals>.EmptyRequest.__getattr__G  r  r  r   rQ   )r$   r?   r@   rA   r   r  r   rC   r   r   r   EmptyRequestr  @  s    GGGr   r  r=   zThe given object (zh) needs to either implement the routing method `get_metadata_routing` or be a `MetadataRouter` instance.z3Can only route and process input on these methods: z, while the passed method is: r"   r   )r(   r,   )r   rF   r   r   r#   r$   r   r  r   r	  r  )_obj_methodr2   r  request_routingrv   s         r   process_routingr    s    N 	G 	G ~D011Zn5U5U !8!8 ; <* *
 	

 gA' K++2)16
 	

 -T2O%%V%D#000OMr   rQ   ).rB   r-  collectionsr   copyr   typingr   r   r   warningsr   rq   r
   
exceptionsr   _bunchr   r   rw   r   r%   r   r    r-   r6   r8   r\   rk   r  rE   rK   rM   rO   r   r   r   r   r   r   r5  r7  r8  r  r>  r  r   r   r   <module>r     sI  _H  "  1 1   1 " [)9% 
 4 1 6 6 89
9>&
R
:
 
4 
  	tT648 9.(,e ePl lp 2Y4IJ  x&:;
B BJZ Zz
''b B 
 u ups, s,@Gr   