
    Q3jK                     ^    d dl Z d dlmZ d dlmZmZ d dlmZ  G d d      Zed        Z	d Z
y)	    N)contextmanager)AutoPropagatedCallbackFitCallback)CallbackContextc                        e Zd ZdZd Z	 ddZy)CallbackSupportMixinzMixin class to add callback support to an estimator.

    .. document the private method
    .. automethod:: _init_callback_context
    c                     t        d |D              st        d      |rt        |      | _        | S | j                  j                  dd       | S )a  Set callbacks for the estimator.

        Parameters
        ----------
        *callbacks : callback instances
            The callbacks to set.

        Returns
        -------
        self : estimator instance
            The estimator instance itself.
        c              3   b   K   | ]'  }t        |t              xr t        |t                ) y wN)
isinstancer   type).0callbacks     O/DATA/.local/lib/python3.12/site-packages/sklearn/callback/_callback_support.py	<genexpr>z5CallbackSupportMixin.set_callbacks.<locals>.<genexpr>   s3      
 & x-Pj46P2PP%s   -/z?callbacks must be instances following the FitCallback protocol._skl_callbacksN)all	TypeErrorlistr   __dict__pop)self	callbackss     r   set_callbacksz"CallbackSupportMixin.set_callbacks   sa      
 &	
 
 Q  "&y/D  MM.5    c                 4   t        j                  | ||||      | _        g | _        t	        | dg       D ]W  }t        |t              rt        | d      r | j                  j                  |       |j                  | | j                         Y | j                  S )aP  Initialize the callback context for the estimator and setup its callbacks.

        This method should be called once, at the beginning of the fit method.

        It will only setup callbacks that are not propagated from a meta-estimator.

        Parameters
        ----------
        task_name : str, default="fit"
            The name of the root task.

        task_id : int, default=0
            Identifier for the root task.

        max_subtasks : int or None, default=0
            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, default=True
            Whether the root context has sequential subtasks. If True, children contexts
            of the root context, created via `subcontext`, will have automatically
            assigned consecutive integer task_ids starting from 0.

        Returns
        -------
        callback_fit_ctx : CallbackContext
            The root callback context for the estimator.
        )	estimator	task_nametask_idmax_subtaskssequential_subtasksr   _parent_callback_ctxr   context)
r   _from_estimator_callback_fit_ctx_skl_callbacks_to_teardowngetattrr   r   hasattrappendsetup)r   r   r   r    r!   r   s         r   _init_callback_contextz+CallbackSupportMixin._init_callback_context0   s    @ "1!@!@% 3"
 +-'&6;H 8%;<D"89//66x@t7M7MN < %%%r   N)fitr   r   T)__name__
__module____qualname____doc__r   r,    r   r   r   r      s    > OS5&r   r   c              #   H  K   	 d t        | d      rWg }| j                  D ]   }	 |j                  | | j                         " | `| `t        |      dk(  r|d   |rt        d|      yy# t        $ r}|j                  |       Y d}~id}~ww xY w# t        | d      r~g }| j                  D ]G  }	 |j                  | | j                         "# t        $ r}|j                  |       Y d}~Ad}~ww xY w | `| `t        |      dk(  r|d   |rt        d|      w w xY ww)a  Context manager to manage callback lifecycle around estimator fit.

    The context manager is responsible for calling the callbacks `teardown` hook in a
    `try finally` block, which guarantees that callbacks teardown will always be called,
    whether the estimator's fit exits successfully or not.

    Parameters
    ----------
    estimator : estimator instance
        The estimator being fitted.

    Yields
    ------
    None.
    Nr&   r#      r   z/The following callback teardown errors occurred)r)   r'   teardownr&   	Exceptionr*   lenExceptionGroup)r   teardown_errorsr   excs       r   callback_management_contextr;   h   sj    "912 O%@@0%%"+Y5P5P &  A 4+?#q(%a(($E#   3 ! 0#**3//0 912 O%@@0%%"+Y5P5P &  ! 0#**3//0 A 4+?#q(%a(($E#   3sn   D"B D"A,)D",	B5BD"BD"D2CD	C4	C/	*D/C4	4+DD"c                 B     t        j                          fd       }|S )a6  Decorator to run the method within a callback context manager.

    This decorator is responsible for calling the callbacks `teardown` hooks of
    callbacks in a `try finally` block, which guarantees that callbacks teardown will
    always be called, whether the estimator's method exits successfully or not.

    It will only teardown callbacks that are not propagated from a meta-estimator.

    Parameters
    ----------
    method : method
        The method to decorate.

    Returns
    -------
    decorated_method : method
        The decorated method.
    c                 ^    t        |       5   | g|i |cd d d        S # 1 sw Y   y xY wr   )r;   )r   argskwargsmethods      r   callback_managed_methodz/with_callbacks.<locals>.callback_managed_method   s)    (3)5d5f5 433s   #,)	functoolswraps)r@   rA   s   ` r   with_callbacksrD      s(    ( __V6 6 #"r   )rB   
contextlibr   sklearn.callback._baser   r   "sklearn.callback._callback_contextr   r   r;   rD   r2   r   r   <module>rH      s:     % F >Z& Z&z % %P#r   