Ë
    ÜÍ:jªU  ã                   ó¾   — 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           úw/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/sklearn/callback/_callback_context.pyÚ_from_estimatorzCallbackContext._from_estimatorI   sê   € ð6 —+‘+˜cÓ"ˆô % YÐ0@À"ÓEˆÔØ!*×!4Ñ!4×!=Ñ!=ˆÔØ%ˆÔØ!ˆŒØ+ˆÔØ&9ˆÔ#ØˆŒÜ ŸJ™J›LˆÔÜ$ŸL™L¬¯©Ó6ˆÔØ "ˆÔØ(,ˆÔ%Ø#'ˆÔ ä�9Ð4Ô5ð #×7Ñ7ˆJØ×Ñ 
Ô+Ø)3×)FÑ)FÈÑ)JˆGÔ&ð ˆð *+ˆ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ˆÔÜ$ŸL™L¬¯©Ó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__³   s5   è ø€ àŠ
Ø×)Ñ)×0Ñ0Ó2ò 	ˆGØ×Ññ	Øú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ØŸ™Ð*ð +Ø—|‘|�n Að'ð	
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ÜØ# D§N¡NÐ#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Â   € ð ×%Ñ%¨Ò*ÜØ6°t·~±~Ð6HÈØ×&Ñ&Ð'ð (Ø&×0Ñ0Ð3°>Ø ×/Ñ/Ð0Ð0Sð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Ð3°1°Y°Kð @3Ø37·>±>Ð2BÀ!ðEóð ð ×'Ò'¨G¨OÜØ˜t×2Ñ2Ð3°1°Y°Kð @7Ø7;·~±~Ð6FÀað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Øœ7 4Ð)@À"ÓEÑEð ä)¬'°(¸IÓ*FÓGˆIð #×-Ñ-×4Ñ4Ó6öàØ—6‘6˜QŸ^™^Ò+ð —“ðˆLð ô
 ˜<Ó(¬3Ô/DÓ+EÑEÐEˆtÐEÜØ˜I˜;Ð&7¸×8JÑ8J×8SÑ8SÐ7Tð U:Ø:>¸ð @Ü1Ð2°!ð5óð ð ˆLØ*ò F�
Ø ^Ñ3ð "Ð%7Ò7Ø &§
¡
Ð+FÈÓ M˜Ü+3°E¬?¡¤À˜ð  %Ð0ô <¸IÀuÔMà!%ð  ð
 >E˜Ð'9Ò:à$Ÿj™j¨°TÓ:˜Ü'/°¤}™cœe¸#˜Ø58˜ zÑ2à+9¸*Ñ+E�˜ZÒ(ð'Fð* ”dØ,”˜ )Ó,¨Y¸ÑMÀÑMóñ ŠFðU,	ð\ ˆùò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"  è ø€ ô( $ MÐ3CÀRÓHö
àÜ˜(Ô$:Ô;ð ×Ñ×'Ó'ð
ˆð 
ñ
 ÜØ% m×&=Ñ&=×&FÑ&FÐ%Gð H$Ø$(×$7Ñ$7Ð#8ð 9/Ø/<¨oð >AðAóð ð .2ˆÔ*ð !ŸO™Oö"
àÜ˜(Ô$:Ô;à×.Ñ.Ð6Ø×*Ñ*¨X×-KÑ-KÒKò ð"
Ðð "
ñ "¬'°-ÀÔ*QÜ�M‰MØ  ×!8Ñ!8×!AÑ!AÐ Bð C8Ø8<×8KÑ8KÐ7Lð M3ð4ôð
 &(Ð"á!Ø)?ˆDÔ&Ü$ ]Ð4DÀbÓIˆNØ'ˆM×'Ñ'¨.Ð;QÑ*QÑSð
	3Ûá%ð ,×:Ñ:ö"àØÐ!7Ñ7ò ð"�ð "ð
 ,�×+Ñ+¨^Ñ<ØÑ2ùòi
ùò("
ùò4"øñ &ð ,×:Ñ:ö"àØÐ!7Ñ7ò ñ"ùò "�ð "ð
 ,�×+Ñ+¨^Ñ<ØÑ2üsM   ‚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@   € ô" —I‘I˜iÓ(€MØ-×3Ñ3Ó5ò )‰ˆˆSÜ�˜s CÕ(ð)àÐ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   € ð  �>‰>Ð!ð 
ˆ	ðô ˜gŸn™nÓ-°°	Ñ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3òtó.r.   