§
    qŠtjª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dS )
é    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d	S )Ú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                 óê  — |                       | ¦  «        }t          |dg ¦  «        |_        |j        j        |_        ||_        ||_        ||_        ||_	        d|_
        t          j        ¦   «         |_        t          j        t           j        ¦  «        |_        i |_        d|_        d|_        t-          |d¦  «        r,|j        }|                     |¦  «         |j        dz   |_        n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           ú`/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sklearn/callback/_callback_context.pyÚ_from_estimatorzCallbackContext._from_estimatorI   së   € ð6 —+’+˜cÑ"Ô"ˆõ % YÐ0@À"ÑEÔEˆÔØ!*Ô!4Ô!=ˆÔØ%ˆÔØ!ˆŒØ+ˆÔØ&9ˆÔ#ØˆŒÝ œJ™LœLˆÔÝ$œL­¬Ñ6Ô6ˆÔØ "ˆÔØ(,ˆÔ%Ø#'ˆÔ å�9Ð4Ñ5Ô5ð 
	+ð #Ô7ˆJØ×Ò 
Ñ+Ô+Ð+Ø)3Ô)FÈÑ)JˆGÔ&Ð&à)*ˆGÔ&àˆó    c                óp  — |                       | ¦  «        }|j        |_        |j        |_        |j        |_        ||_        ||_        ||_        ||_        |j        |_        t          j
        t          j        ¦  «        |_        d|_        i |_        d|_        d|_        |                     |¦  «         |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ˆÔØ!/Ô!>ˆÔØ%3Ô%FˆÔ"Ø%ˆÔØ!ˆŒØ+ˆÔØ&9ˆÔ#Ø*Ô4ˆÔÝ$œL­¬Ñ6Ô6ˆÔØˆŒØ "ˆÔØ(,ˆÔ%Ø#'ˆÔ ð 	×!Ò! 'Ñ*Ô*Ð*àˆr.   c              #   óZ   K  — | V — | j                              ¦   «         D ]
}|E d{V —† ŒdS )z1Pre-order depth-first traversal of the task tree.N)r"   Úvalues)ÚselfÚcontexts     r,   Ú__iter__zCallbackContext.__iter__³   sR   è è € àˆ
ˆ
ˆ
ØÔ)×0Ò0Ñ2Ô2ð 	ð 	ˆGØÐÐÐÐÐÐÐÐð	ð 	r.   c                 óP   — | 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__¹   sN   € àŒ~Ô&ð 'ð 'Ø"Ô1ð'ð 'àœð'ð 'ð ”|ð'ð 'ð 'ð	
r.   c           	      ó6  — |j         | j        v r(t          d| j        › d| j        › d|j         › d�¦  «        ‚| j        �Et          | j        ¦  «        | j        k    r(t          d| j        › d| j        › d| j        › d�¦  «        ‚|| j        |j         <   | |_        dS )	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Ýð4 D¤Nð 4ð 4ØÔ&ð4ð 4à(Ô0ð4ð 4ð 4ñô ð ð ÔÐ)Ý�DÔ&Ñ'Ô'¨4Ô+<Ò<Ð<åð=¸¼ð =ð =ØÔ&ð=ð =à'+Ô'8ð=ð =ð =ñô ð ð 5BˆÔ˜=Ô0Ñ1Ø#ˆÔÐÐr.   c                 ó  — |j         dk    r0t          d| j        ›d| j        › d|j        ›d|j        › d�	¦  «        ‚|j        | _        |j        | _        |j        | _        | |j        j        | j        <   |j        | _        |j        | _	        dS )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È   € ð Ô%¨Ò*Ð*ÝðU°t´~ð Uð UØÔ&ðUð Uà&Ô0ðUð Uð !Ô/ðUð Uð Uñô ð ð $Ô*ˆŒØ$Ô,ˆŒØ&Ô0ˆŒØ;?ˆÔÔ*¨4¬<Ñ8ð !.Ô 7ˆÔØ%2Ô%AˆÔ"Ð"Ð"r.   Ú Nr   Tc           	      ó  — | j         r%|�#t          d| j        › d|› d| j        › d�¦  «        ‚| j         s%|€#t          d| j        › d|› d| j        › d�¦  «        ‚|€t	          | j        ¦  «        }t                               | ||||¬¦  «        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.
        Nz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 Ô#ð 	¨Ð(;ÝðE˜tÔ2ð Eð E°Yð Eð EØ37´>ðEð Eð Eñô ð ð Ô'ð 	¨G¨OÝðI˜tÔ2ð Ið I°Yð Ið IØ7;´~ðIð Ið Iñô ð ð ˆ?Ý˜$Ô,Ñ-Ô-ˆGå×+Ò+ØØØØ%Ø 3ð ,ñ 
ô 
ð 	
r.   c                 óú  — d}i }| j         D �]m}|t          | dg ¦  «        v rŒt          t          ||¦  «        ¦  «        }d„ |j                             ¦   «         D ¦   «         }t          |¦  «        t          t          ¦  «        z
  x}	r+t          d|› d|j        j	        › d|	› dt          › d�	¦  «        ‚i }
|D ]˜}||vr‡|d	k    rK| 
                    d
d¦  «        }t          |¦  «        r
 |¦   «         n|}|�t          ||¦  «        nd}||d	<   n6| 
                    |d¦  «        }t          |¦  «        r
 |¦   «         n|}|||<   ||         |
|<   Œ™|t           t          ||¦  «        || fi |
¤Ž¦  «        z  }�Œo|S )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_callbacksc                 ó<   — h | ]}|j         |j        k    ¯|j        ’ŒS © )ÚkindÚKEYWORD_ONLYÚname)Ú.0Úps     r,   ú	<setcomp>z.CallbackContext._call_hooks.<locals>.<setcomp>E  s3   € ð ð ð àØ”6˜Qœ^Ò+Ð+ð ”à+Ð+Ð+r.   z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   ÚsetÚVALID_HOOK_PARAMS_OUTÚ	TypeErrorr   r   ÚgetÚcallableÚ_from_reconstruction_attributesÚbool)r5   r)   Ú	hook_nameÚkwargsÚresultÚevaluated_argsÚcallbackÚ	signatureÚparams_namesÚdiffÚargs_to_passÚ
param_nameÚattrsÚnew_estÚvals                  r,   Ú_call_hookszCallbackContext._call_hooks!  s  € ð. ˆð ˆàœð ,	ñ ,	ˆHØ�7 4Ð)@À"ÑEÔEÐEÐEð å)­'°(¸IÑ*FÔ*FÑGÔGˆIðð à"Ô-×4Ò4Ñ6Ô6ðñ ô ˆLõ
 ˜<Ñ(Ô(­3Õ/DÑ+EÔ+EÑEÐEˆtð Ýð5˜Ið 5ð 5¸Ô8JÔ8Sð 5ð 5Ø:>ð5ð 5å1ð5ð 5ð 5ñô ð ð ˆLØ*ð Fð F�
Ø ^Ð3Ð3ð "Ð%7Ò7Ð7Ø &§
¢
Ð+FÈÑ MÔ M˜Ý+3°E©?¬?Ð E  ¡¤ À˜ð  %Ð0õ <¸IÀuÑMÔMÐMà!%ð  ð
 >E˜Ð'9Ñ:Ð:à$Ÿjšj¨°TÑ:Ô:˜Ý'/°¡}¤}Ð=˜c˜c™eœe˜e¸#˜Ø58˜ zÑ2à+9¸*Ô+E�˜ZÑ(Ð(à•dØ,•˜ )Ñ,Ô,¨Y¸ÐMÐMÀÐMÐMñô ñ ˆF‰Fð ˆr.   )r   r   r	   rQ   c                ó<   — |                       |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©r[   r   r   r	   rQ   ©rh   ©r5   r)   r   r   r	   rQ   s         r,   Úcall_on_fit_task_beginz&CallbackContext.call_on_fit_task_beginm  s;   € ð@ 	×ÒØØ)ØØØØ&?ð 	ñ 	
ô 	
ð 	
ð ˆr.   c                ó8   — |                       |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_endrk   rl   rm   s         r,   Úcall_on_fit_task_endz$CallbackContext.call_on_fit_task_end—  s4   € ðL ×ÒØØ'ØØØØ&?ð  ñ 
ô 
ð 	
r.   c           	   #   ó.  ‡ ‡K  — d„ t          |dg ¦  «        D ¦   «         }|r(t          d|j        j        › d‰ j        › d|› d�¦  «        ‚‰ |_        ˆ fd„‰ j        D ¦   «         Š‰r<t          |d¦  «        s,t          j	        d	|j        j        › d
‰ j        › d�¦  «         g Š‰r%‰‰ _
        t          |dg ¦  «        } |j        |‰z   Ž  	 dV — ‰rˆfd„|j        D ¦   «         } |j        |Ž  |`dS # ‰rˆfd„|j        D ¦   «         } |j        |Ž  |`w xY 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.
        c                 óP   — g | ]#}t          |t          ¦  «        ¯|j        j        ‘Œ$S rJ   )Ú
isinstancer   r   r   )rN   r_   s     r,   ú
<listcomp>z>CallbackContext.propagate_callback_context.<locals>.<listcomp>Ø  s?   € ð 
ð 
ð 
àÝ˜(Õ$:Ñ;Ô;ð
ØÔÔ'ð
ð 
ð 
r.   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.c                 ól   •— g | ]0}t          |t          ¦  «        r|j        �‰j        |j        k     ¯.|‘Œ1S )N)rt   r   Úmax_propagation_depthr'   )rN   r_   r5   s     €r,   ru   z>CallbackContext.propagate_callback_context.<locals>.<listcomp>ì  sU   ø€ ð "
ð "
ð "
àÝ˜(Õ$:Ñ;Ô;ð"
ð
 Ô.Ð6ØÔ*¨XÔ-KÒKÐKð ð
 LÐKÐKr.   Úset_callbackszThe estimator z7 does not support callbacks. The callbacks attached to z* will not be propagated to this estimator.Nc                 ó   •— g | ]}|‰v¯|‘Œ	S rJ   rJ   )rN   ÚcbÚcallbacks_to_propagates     €r,   ru   z>CallbackContext.propagate_callback_context.<locals>.<listcomp>  s/   ø€ ð "ð "ð "àØÐ!7Ð7Ð7ð à7Ð7Ð7r.   )r   rV   r   r   r   r   r   r%   ÚwarningsÚwarnrH   rx   r   )r5   Úsub_estimatorÚbad_callbacksÚcurr_callbacksÚkept_callbacksr{   s   `    @r,   Úpropagate_callback_contextz*CallbackContext.propagate_callback_contextÆ  s  øøè è € ð$
ð 
å# MÐ3CÀRÑHÔHð
ñ 
ô 
ˆð
 ð 	ÝðA mÔ&=Ô&Fð Að AØ$(Ô$7ðAð Aà/<ðAð Að Añô ð ð .2ˆÔ*ð"
ð "
ð "
ð "
à œOð"
ñ "
ô "
Ðð "ð 	(­'°-ÀÑ*QÔ*Qð 	(ÝŒMð4 Ô!8Ô!Að 4ð 4Ø8<Ô8Kð4ð 4ð 4ñô ð ð
 &(Ð"à!ð 	TØ)?ˆDÔ&Ý$ ]Ð4DÀbÑIÔIˆNØ'ˆMÔ'¨.Ð;QÑ*QÐSÐSð
	3ØˆEˆEˆEà%ð =ð"ð "ð "ð "à+Ô:ð"ñ "ô "�ð
 ,�Ô+¨^Ð<Ð<ØÐ2Ð2Ð2øð &ð =ð"ð "ð "ð "à+Ô:ð"ñ "ô "�ð
 ,�Ô+¨^Ð<Ð<ØÐ2Ð2Ð2Ð2Ð2s   Ã
C1 Ã1#D)rC   Nr   T)r   Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr-   r2   r7   r:   r0   r&   rF   rh   rn   rq   r   r‚   rJ   r.   r,   r   r      s5  € € € € € ð1ð 1ðf ð7ð 7ñ „[ð7ðr ð-ð -ñ „[ð-ð^ð ð ð
ð 
ð 
ð$ð $ð $ð,Bð Bð Bð: OSð,
ð ,
ð ,
ð ,
ð\Jð Jð Jð` Ø
ØØ"&ð(ð (ð (ð (ð (ð\ Ø
ØØ"&ð-
ð -
ð -
ð -
ð -
ð^ ðE3ð E3ñ „^ðE3ð E3ð E3r.   r   c                 ó„   — t          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)   rQ   Únew_estimatorÚkeyrg   s        r,   rY   rY     sM   € õ" ”I˜iÑ(Ô(€MØ-×3Ò3Ñ5Ô5ð )ð )‰ˆˆSÝ�˜s CÑ(Ô(Ð(Ð(ØÐr.   c                 óF   — | j         €| gn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Ž   &  s.   € ð  Œ>Ð!ð 
ˆ	ˆ	å˜gœnÑ-Ô-°°	Ñ9ðr.   )rˆ   Ú	functoolsÚinspectr   r|   Ú
contextlibr   r   r   Úsklearn.callback._baser   rU   Ú	lru_cacher`   rR   r   rY   rŽ   rJ   r.   r,   ú<module>r”      sü   ðð €€€Ø Ð Ð Ð Ø €€€Ø €€€Ø €€€Ø %Ð %Ð %Ð %Ð %Ð %Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'à 9Ð 9Ð 9Ð 9Ð 9Ð 9ð CÐBÐBÐ ð *Ð'�IÔ'Ñ)Ô)¨'Ô*;Ñ<Ô<Ð ðw3ð w3ð w3ð w3ð w3ñ w3ô w3ð w3ðtð ð ð.ð ð ð ð r.   