
    Q3jU                         d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
 g dZ  ej                         ej                        Z G d d      Zd Zd	 Zy)
    N)contextmanager)datetimetimezone)AutoPropagatedCallback)Xymetadatafitted_estimatorc                       e Zd ZdZed        Zed        Zd Zd Zd Z	d Z
	 dd	Zd
 ZddddddZddddddZed        Zy)CallbackContexta  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                     | j                  |       }t        |dg       |_        |j                  j                  |_        ||_        ||_        ||_        ||_	        d|_
        t        j                         |_        t        j                  t         j"                        |_        i |_        d|_        d|_        t-        |d      r3|j.                  }|j1                  |       |j2                  dz   |_        |S d|_        |S )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           O/DATA/.local/lib/python3.12/site-packages/sklearn/callback/_callback_context.py_from_estimatorzCallbackContext._from_estimatorI   s    6 ++c" %Y0@"E!*!4!4!=!=%!+&9# JJL$LL6 "(,%#' 945 #77J
+)3)F)F)JG&  *+G&    c                   | j                  |       }|j                  |_        |j                  |_        |j                  |_        ||_        ||_        ||_        ||_        |j                  |_        t        j                  t        j                        |_        d|_        i |_        d|_        d|_        |j%                  |       |S )av  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_parentzCallbackContext._from_parent   s    6 ++c"+66!/!>!>%3%F%F"%!+&9#*44$LL6 "(,%#'  	!!'*r.   c              #   j   K   |  | j                   j                         D ]  }|E d{     y7 w)z1Pre-order depth-first traversal of the task tree.N)r"   values)selfcontexts     r,   __iter__zCallbackContext.__iter__   s0     
))002G 3s   '313c                     | j                   j                   d| j                  d| j                  d| j                   dS )Nz(estimator_name=z, task_name=z
, task_id=))r   r   r   r   r   )r5   s    r,   __repr__zCallbackContext.__repr__   sL    ~~&&' ("114 5* +||nA'	
r.   c           	         |j                   | j                  v r3t        d| j                   d| j                   d|j                    d      | j
                  Ut        | j                        | j
                  k\  r3t        d| j                   d| j                   d| j
                   d      || j                  |j                   <   | |_        y)	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   )r5   child_contexts     r,   r0   z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#r.   c                    |j                   dk7  r@t        d| j                  d| j                   d|j                  d|j                   d	      |j                  | _        |j
                  | _        |j                  | _        | |j                  j                  | j
                  <   |j                  | _        |j                  | _	        y)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 r<   z) with callback context (task z#) because the latter is not a leaf.N)
r   r>   r   r   r   r   r   r"   r$   r#   )r5   other_contexts     r,   r&   zCallbackContext._merge_with   s     %%*6t~~6H&&' (&003> //00SU  $**$,,&00;?**4<<8 !. 7 7%2%A%A"r.   Nc           	      @   | j                   r+|)t        d| j                   d| d| j                   d      | j                   s+|)t        d| j                   d| d| j                   d      |t	        | j
                        }t        j                  | ||||      S )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 r=   z6 must be provided if sequential_subtasks is False for )r1   r   r   r   r   )r   r>   r   r   r?   r"   r   r2   )r5   r   r   r   r   s        r,   
subcontextzCallbackContext.subcontext   s    2 ##(;t2231YK @337>>2B!E  ''GOt2231YK @77;~~6FaI  ?$,,-G++% 3 , 
 	
r.   c                    d}i }| j                   D ]`  }|t        | dg       v rt        t        ||            }|j                  j	                         D ch c]'  }|j
                  |j                  k(  r|j                  ) }	}t        |	      t        t              z
  x}
r0t        d| d|j                  j                   d|
 dt         d	      i }|	D ]z  }||vrl|dk(  r<|j                  d	d
      }t        |      r |       n|}|t        ||      nd
}||d<   n+|j                  |d
      }t        |      r |       n|}|||<   ||   ||<   | |t!         t        ||      || fi |      z  }c |S c c}w )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: r=   r
   reconstruction_attributesN)r   r   _cached_signature
parametersr4   kindKEYWORD_ONLYnamesetVALID_HOOK_PARAMS_OUT	TypeErrorr   r   getcallable_from_reconstruction_attributesbool)r5   r)   	hook_namekwargsresultevaluated_argscallback	signaturepparams_namesdiffargs_to_pass
param_nameattrsnew_estvals                   r,   _call_hookszCallbackContext._call_hooks!  s   .  H74)@"EE )'(I*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 <IuM!%  
 >E'9:$jjT:'/}ce#58z2+9*+EZ(' +* d,),YMM FU (\ Ms   ,E7)r   r   r	   rH   c                4    | j                  |d||||       | S )a  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rU   r   r   r	   rH   rc   r5   r)   r   r   r	   rH   s         r,   call_on_fit_task_beginz&CallbackContext.call_on_fit_task_beginm  s1    @ 	)&? 	 	
 r.   c                0    | j                  |d||||      S )ad  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_endrf   rg   rh   s         r,   call_on_fit_task_endz$CallbackContext.call_on_fit_task_end  s/    L '&?   
 	
r.   c           	   #   ~  K   t        |dg       D cg c](  }t        |t              r|j                  j                  * }}|r3t        d|j                  j                   d| j                   d| d      | |_        | j                  D cg c]9  }t        |t              r'|j                  | j                  |j                  k  r|; }}|rHt        |d      s<t        j                  d|j                  j                   d	| j                   d
       g }|r&|| _        t        |dg       } |j                  ||z     	 d |r,|j                   D cg c]  }||vr|
 }} |j                  |  |`yc c}w c c}w c c}w # |r2|j                   D cg c]  }||vr|
 nc c}w }} |j                  |  |`w xY ww)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   rP   r   r   r   max_propagation_depthr'   r%   warningswarnrG   rn   r   )r5   sub_estimatorrY   bad_callbackscallbacks_to_propagatecurr_callbackscbkept_callbackss           r,   propagate_callback_contextz*CallbackContext.propagate_callback_context  s*    ( $M3CRH
H($:; ''H 	 

 %m&=&=&F&F%G H$$($7$7#8 9//<o >AA  .2* !OO"
+($:;..6**X-K-KK + 	 "
 "'-*QMM !8!8!A!A B C88<8K8K7L M34
 &("!)?D&$]4DbIN'M''.;Q*QS
	3% ,::":!77 :  "
 ,++^<2i
("
4" & ,::":!77 :" "
 ,++^<2sM   F=-E3AF=>E8	A4F=>F F=E=#F=F:F! F::F=) Nr   T)r   
__module____qualname____doc__classmethodr-   r2   r7   r:   r0   r&   rE   rc   ri   rl   r   ry    r.   r,   r   r      s    1f 7 7r - -^
$,B: OS,
\J` 
"&(\ 
"&-
^ E3 E3r.   r   c                 z    t        j                   |       }|j                         D ]  \  }}t        |||        |S )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)   rH   new_estimatorkeyrb   s        r,   rS   rS     s;    " IIi(M-335SsC( 6r.   c                 R    | j                   | gS t        | j                         | gz   S )a  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)r6   s    r,   r   r   &  s4      >>! 
	 gnn-	9r.   )r   	functoolsinspectr   rq   
contextlibr   r   r   sklearn.callback._baser   rO   	lru_cacherZ   rI   r   rS   r   r   r.   r,   <module>r      s`         % ' 9 C  *'I'')'*;*;< w3 w3t.r.   