
    MpjU                         S SK r S SKrS SKrS SKrS SKrS SKJr  S SKJrJr  S SK	J
r
  / SQr\R                  " 5       " \R                  5      r " S S5      rS rS	 rg)
    N)contextmanager)datetimetimezone)AutoPropagatedCallback)Xymetadatafitted_estimatorc                       \ rS rSrSr\S 5       r\S 5       rS rS r	S r
S r SS
 jrS rS	S	S	S	S.S jrS	S	S	S	S.S jr\S 5       rSrg	)CallbackContext   ax  Task level context for the callbacks.

This class is responsible for managing the callbacks and holding the tree structure
of an estimator's tasks. Each instance corresponds to a task of the estimator.

This class should not be instantiated directly, but through the
`_init_callback_context` method of the estimator to create the root context or using
the `subcontext` method of this class to create sub-contexts.

These contexts are passed to the callback hooks to be able to keep track of the
position of a task in the task tree from within the callbacks.

Attributes
----------
task_name : str
    The name of the task this context is responsible for.

task_id : int
    The identifier of the task this context is responsible for. It uniquely
    identifies the task among its siblings.

max_subtasks : int or None
    The maximum number of children tasks for this task. 0 means it's a leaf.
    None means the maximum number of subtasks is not known in advance.

sequential_subtasks : bool
    Whether this context's subtasks are sequential. When True, children contexts'
    have consecutive integer task_ids starting from 0.

estimator_name : str
    The name of the estimator that holds this context.

parent : CallbackContext or None
    The parent context of this context. None if this context is the root.

root_uuid : uuid.UUID instance
    The UUID of the root context. All contexts in the same task tree have the same
    root UUID that is used to identify the task tree itself.

init_time : datetime.datetime
    The time when the context was initialised, in the UTC timezone.

source_estimator_name : str or None
    The name of the estimator that holds the parent task this task was
    merged with. None if it was not merged with another context.

source_task_name : str or None
    The task name of the parent task this task was merged with. None if it
    was not merged with another context.
c                    U R                  U 5      n[        US/ 5      Ul        UR                  R                  Ul        X&l        X6l        XFl        XVl	        SUl
        [        R                  " 5       Ul        [        R                  " [         R"                  5      Ul        0 Ul        SUl        SUl        [-        US5      (       a3  UR.                  nUR1                  U5        UR2                  S-   Ul        U$ SUl        U$ )a  Private constructor to create a root context.

Parameters
----------
estimator : estimator instance
    The estimator this context is responsible for.

task_name : str
    The name of the root task.

task_id : int
    Identifier for the root task.

max_subtasks : int or None
    The maximum number of subtasks that can be children of the root task. None
    means the maximum number of subtasks is not known in advance. 0 means it's a
    leaf.

sequential_subtasks : bool
    Whether the root context has sequential subtasks. If True, children contexts
    created via `subcontext` will have automatically assigned consecutive
    integer task_ids starting from 0.
_skl_callbacksN_parent_callback_ctx   r   )__new__getattr
_callbacks	__class____name__estimator_name	task_nametask_idmax_subtaskssequential_subtasksparentuuiduuid4	root_uuidr   nowr   utc	init_time_children_mapsource_estimator_namesource_task_namehasattrr   _merge_with_propagation_depth)cls	estimatorr   r   r   r   new_ctx
parent_ctxs           ^/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/sklearn/callback/_callback_context.py_from_estimatorCallbackContext._from_estimatorI   s    6 ++c" %Y0@"E!*!4!4!=!=%!+&9# JJL$LL6 "(,%#' 9455 #77J
+)3)F)F)JG&  *+G&    c                   U R                  U 5      nUR                  Ul        UR                  Ul        UR                  Ul        X&l        X6l        XFl        XVl        UR                  Ul        [        R                  " [        R                  5      Ul        SUl        0 Ul        SUl        SUl        UR%                  U5        U$ )a  Private constructor to create a sub-context.

Parameters
----------
parent_context : `CallbackContext` instance
    The parent context of the new context.

task_name : str
    The name of the task this context is responsible for.

task_id : int
    The identifier of the task this context is responsible for.

max_subtasks : int or None
    The maximum number of tasks that can be children of the task this context is
    responsible for. 0 means it's a leaf. None means the maximum number of
    subtasks is not known in advance.

sequential_subtasks : bool
    Whether this context's subtasks are sequential. If True, children contexts
    created via `subcontext` will have automatically assigned consecutive
    integer task_ids starting from 0.
N)r   r   r   r(   r   r   r   r   r   r   r    r   r!   r"   r   r#   r$   r%   
_add_child)r)   parent_contextr   r   r   r   r+   s          r-   _from_parentCallbackContext._from_parent   s    6 ++c"+66!/!>!>%3%F%F"%!+&9#*44$LL6 "(,%#'  	!!'*r0   c              #   n   #    U v   U R                   R                  5        H  nU Sh  vN   M     g N	7f)z1Pre-order depth-first traversal of the task tree.N)r#   values)selfcontexts     r-   __iter__CallbackContext.__iter__   s/     
))002G 3s   '53
5c                     U R                   R                   SU R                  < SU R                  < SU R                   S3$ )Nz(estimator_name=z, task_name=z
, task_id=))r   r   r   r   r   )r8   s    r-   __repr__CallbackContext.__repr__   sL    ~~&&' ("114 5* +||nA'	
r0   c           	         UR                   U R                  ;   a3  [        SU R                   SU R                   SUR                    S35      eU R
                  bV  [        U R                  5      U R
                  :  a3  [        SU R                   SU R                   SU R
                   S35      eXR                  UR                   '   Xl        g)	z/Add `child_context` as a child of this context.zCallback context  of estimator z" already has a child with task_id=.Nz%Cannot add child to callback context z8 because it already has its maximum number of children (z).)r   r#   
ValueErrorr   r   r   lenr   )r8   child_contexts     r-   r2   CallbackContext._add_child   s      D$6$66#DNN#3>&&' ((0014  )D&&'4+<+<<77G~&&' (''+'8'8&9=  5B=001#r0   c                    UR                   S:w  aB  [        SU R                  < SU R                   SUR                  < SUR                   S3	5      eUR                  U l        UR
                  U l        UR                  U l        XR                  R                  U R
                  '   UR                  U l        UR                  U l	        g)a  Merge this context with `other_context`.

This method is called on a sub-estimator's root task to merge it with a
meta-estimator's leaf task. The sub-estimator's task tree is therefore attached
to the meta-estimator's task tree. The root node of the sub-estimator's task
tree and the leaf node of the meta-estimator's task tree are both represented
by a single node in this combined task tree.
r   z$Cannot merge callback context (task rA   z) with callback context (task z#) because the latter is not a leaf.N)
r   rC   r   r   r   r   r   r#   r%   r$   )r8   other_contexts     r-   r'   CallbackContext._merge_with   s     %%*6t~~6H&&' (&003> //00SU  $**$,,&00;?**4<<8 !. 7 7%2%A%A"r0   Nc           	      T   U R                   (       a,  Ub)  [        SU R                   SU SU R                   S35      eU R                   (       d,  Uc)  [        SU R                   SU SU R                   S35      eUc  [	        U R
                  5      n[        R                  U UUUUS9$ )a$  Create a context for a subtask of the current task.

Parameters
----------
task_name : str, default=""
    The name of the subtask.

task_id : int or None, default=None
    An identifier of the subtask. It must be distinct from the task_ids of its
    siblings. If None, task_id is automatically set to the next available
    integer task_id.

max_subtasks : int or None, default=0
    The maximum number of tasks that can be children of the subtask. 0 means
    it's a leaf. None means the maximum number of subtasks is not known in
    advance.

sequential_subtasks : bool, default=True
    Whether the new context's subtasks are sequential. If True, children
    contexts of the new context, created via `subcontext`, will have
    automatically assigned consecutive integer task_ids starting from 0.
ztask_id for  z1 must be None if sequential_subtasks is True for rB   z6 must be provided if sequential_subtasks is False for )r3   r   r   r   r   )r   rC   r   r   rD   r#   r   r4   )r8   r   r   r   r   s        r-   
subcontextCallbackContext.subcontext   s    2 ##(;t2231YK @337>>2B!E  ''GOt2231YK @77;~~6FaI  ?$,,-G++% 3 , 
 	
r0   c                 &   Sn0 nU R                    GHv  nU[        U S/ 5      ;   a  M  [        [        Xb5      5      nUR                  R	                  5        Vs1 s H+  nUR
                  UR                  :X  d  M  UR                  iM-     n	n[        U	5      [        [        5      -
  =n
(       a0  [        SU SUR                  R                   SU
 S[         S3	5      e0 nU	 H  nX;  au  US:X  a@  UR                  S	S
5      n[        U5      (       a  U" 5       OUnUb  [        X5      OS
nXS'   O/UR                  US
5      n[        U5      (       a  U" 5       OUnXU'   X\   X'   M     U[!        [        Xb5      " X40 UD65      -  nGMy     U$ s  snf )a;  Helper to call the hook of all callbacks with their respective arguments.

Provide the right arguments to each hook by inspecting their signatures. Any
value that is a callable is replaced by what it returns to allow lazy loading of
the arguments.

Parameters
----------
estimator : estimator instance
    The estimator calling the callback hook.

hook_name : str
    Name of the callback hook to call.

**kwargs: dict
    Optional keyword arguments passed to the callback context.

Returns
-------
result : bool
    True if any hook call returned True. False otherwise.
F_propagated_callbackszHook z of the callback z$ has parameters that are not valid: z. The valid parameters are: rB   r
   reconstruction_attributesN)r   r   _cached_signature
parametersr7   kindKEYWORD_ONLYnamesetVALID_HOOK_PARAMS_OUT	TypeErrorr   r   getcallable_from_reconstruction_attributesbool)r8   r*   	hook_namekwargsresultevaluated_argscallback	signaturepparams_namesdiffargs_to_pass
param_nameattrsnew_estvals                   r-   _call_hooksCallbackContext._call_hooks!  s   .  H74)@"EE )'(*FGI #--4466A66Q^^+ 6  
 <(3/D+EEEtEI;&78J8J8S8S7T U::> @12!5  L*
3 "%77 &

+F M+3E??  %0 <IM!%  
 >E'9:$jjT:'/}}ce#58z2+9+E(' +* d,YMM FU (\ Ms   F:F)r   r   r	   rP   c          	      .    U R                  USUUUUS9  U $ )aG  Call the `on_fit_task_begin` hook of the callbacks.

Parameters
----------
estimator : estimator instance
    The estimator calling the callback hook.

X : array-like or None, default=None
    The training data of the current task.

y : array-like or None, default=None
    The training targets of the current task.

metadata : dict or None, default=None
    A dictionary containing training metadata for the current task.

reconstruction_attributes : dict or None, default=None
    A dictionary of the sufficient fitted attributes needed to construct a
    `fitted_estimator` from the current state of the estimator, i.e. an
    estimator instance ready to predict, transform, etc ... as if the fit had
    stopped at the beginning of this task. The `fitted_estimator` is the
    object that will be passed to the callbacks, if required.
on_fit_task_beginr]   r   r   r	   rP   rk   r8   r*   r   r   r	   rP   s         r-   call_on_fit_task_begin&CallbackContext.call_on_fit_task_beginm  s1    @ 	)&? 	 	
 r0   c          	      *    U R                  USUUUUS9$ )a  Call the `on_fit_task_end` hook of the callbacks.

Parameters
----------
estimator : estimator instance
    The estimator calling the callback hook.

X : array-like or None, default=None
    The training data of the current task.

y : array-like or None, default=None
    The training targets of the current task.

metadata : dict or None, default=None
    A dictionary containing training metadata of the current task.

reconstruction_attributes : dict or None, default=None
    A dictionary of the sufficient fitted attributes needed to construct a
    `fitted_estimator` from the current state of the estimator, i.e. an
    estimator instance ready to predict, transform, etc ... as if the fit had
    stopped at the end of this task. The `fitted_estimator` is the object
    that will be passed to the callbacks, if required.

Returns
-------
stop : bool
    Whether or not to stop the current level of iterations at this end of this
    task.
on_fit_task_endro   rp   rq   s         r-   call_on_fit_task_end$CallbackContext.call_on_fit_task_end  s/    L '&?   
 	
r0   c           	   #     #    [        US/ 5       Vs/ s H0  n[        U[        5      (       d  M  UR                  R                  PM2     nnU(       a3  [        SUR                  R                   SU R                   SU S35      eXl        U R                   Vs/ s HE  n[        U[        5      (       d  M  UR                  b  U R                  UR                  :  d  MC  UPMG     nnU(       aN  [        US5      (       d=  [        R                  " SUR                  R                   S	U R                   S
35        / nU(       a$  X@l        [        US/ 5      nUR                  " XT-   6    Sv   U(       a2  UR                    Vs/ s H  nXd;  d  M
  UPM     nnUR                  " U6   U?gs  snf s  snf s  snf ! U(       a8  UR                    Vs/ s H  nXd;  d  M
  UPM     Os  snf nnUR                  " U6   U?f = f7f)a.  Propagate the context and callbacks to a sub-estimator.

Clear the propagated callbacks from the sub-estimator on exit.

Only auto-propagated callbacks are propagated to the sub-estimator. An error is
raised if the sub-estimator already holds auto-propagated callbacks.

The sub-estimator receives this context as an attribute named
`_parent_callback_ctx` so that the meta-estimator's task tree can be merged with
the sub-estimator's one.

Parameters
----------
sub_estimator : estimator instance
    The estimator to propagate the callbacks and context to.
r   zThe sub-estimator (z) of a meta-estimator (z() can't have auto-propagated callbacks (z0). Register them directly on the meta-estimator.Nset_callbackszThe estimator z7 does not support callbacks. The callbacks attached to z* will not be propagated to this estimator.)r   
isinstancer   r   r   rX   r   r   r   max_propagation_depthr(   r&   warningswarnrO   ry   r   )r8   sub_estimatorra   bad_callbackscallbacks_to_propagatecurr_callbackscbkept_callbackss           r-   propagate_callback_context*CallbackContext.propagate_callback_context  s"    ( $M3CRH
H($:; (H''H 	 

 %m&=&=&F&F%G H$$($7$7#8 9//<o >AA  .2* !OO"
+($:;  ..6**X-K-KK + 	 "
 "'-*Q*QMM !8!8!A!A B C88<8K8K7L M34
 &("!)?&$]4DbIN''.*QS
	3% ,::":7 :  "
 ++^<2i
("
4" & ,::":7 :" "
 ++^<2sk   G=F(F(	AG=F-6%F-F-%BG=(F7 ,G=	F2F2#G=7G:	G!G! G::G=)rO   r   r   r$   r%   r   ) Nr   T)r   
__module____qualname____firstlineno____doc__classmethodr.   r4   r:   r>   r2   r'   rL   rk   rr   rv   r   r   __static_attributes__ r0   r-   r   r      s    1f 7 7r - -^
$,B: OS,
\J` 
"&(\ 
"&-
^ E3 E3r0   r   c                 |    [         R                   " U 5      nUR                  5        H  u  p4[        X#U5        M     U$ )a  Return a copy of the estimator as if it was fitted.

Parameters
----------
estimator : estimator instance
    The estimator from which to make a ready-to-be-evaluated copy.

reconstruction_attributes : dict
    A dictionary containing the necessary attributes to create a working
    fitted estimator from this instance.

Returns
-------
fitted_estimator : estimator instance
    The fitted copy of this estimator.
)copyitemssetattr)r*   rP   new_estimatorkeyrj   s        r-   r[   r[     s7    " IIi(M-335C( 6r0   c                 T    U R                   c  U /$ [        U R                   5      U /-   $ )an  Helper function to get the path from the root context down to a given context.

Parameters
----------
context : `CallbackContext` instance
    The context to get the path to.

Returns
-------
list of `CallbackContext` instances
    The list of the ancestors (itself included) of the given context. The list is
    ordered from the root context to the given context.
)r   get_context_path)r9   s    r-   r   r   &  s4      >>! 
	 gnn-	9r0   )r   	functoolsinspectr   r|   
contextlibr   r   r   sklearn.callback._baser   rW   	lru_cacherb   rQ   r   r[   r   r   r0   r-   <module>r      s[         % ' 9 C  '')'*;*;< w3 w3t.r0   