
    MpjK                     ^    S SK r S SKJr  S SKJrJr  S SKJr   " S S5      r\S 5       r	S r
g)	    N)contextmanager)AutoPropagatedCallbackFitCallback)CallbackContextc                   *    \ rS rSrSrS r SS jrSrg)CallbackSupportMixin   z|Mixin class to add callback support to an estimator.

.. document the private method
.. automethod:: _init_callback_context
c                     [        S U 5       5      (       d  [        S5      eU(       a  [        U5      U l        U $ U R                  R                  SS5        U $ )zSet callbacks for the estimator.

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

Returns
-------
self : estimator instance
    The estimator instance itself.
c              3   z   #    U  H1  n[        U[        5      =(       a    [        U[        5      (       + v   M3     g 7fN)
isinstancer   type).0callbacks     ^/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/sklearn/callback/_callback_support.py	<genexpr>5CallbackSupportMixin.set_callbacks.<locals>.<genexpr>   s2      
 & x-Pj46P2PP%s   9;z?callbacks must be instances following the FitCallback protocol._skl_callbacksN)all	TypeErrorlistr   __dict__pop)self	callbackss     r   set_callbacks"CallbackSupportMixin.set_callbacks   sf      
 &	
 
 
 Q  "&y/D  MM.5    c                 B   [         R                  " U UUUUS9U l        / U l        [	        U S/ 5       H_  n[        U[        5      (       a  [        U S5      (       a  M+  U R                  R                  U5        UR                  X R                  S9  Ma     U R                  $ )a  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_context+CallbackSupportMixin._init_callback_context0   s    @ "1!@!@% 3"
 +-'&6;H 8%;<<D"899//66x@7M7MN < %%%r   )r)   r   r*   N)fitr   r   T)__name__
__module____qualname____firstlineno____doc__r   r/   __static_attributes__ r   r   r   r      s    > OS5&r   r   c              #   t  #     Sv   [        U S5      (       a[  / nU R                   H  n UR                  X R                  S9  M     U ?U ?[        U5      S:X  a  US   eU(       a  [        SU5      egg! [         a  nUR                  U5         SnAMo  SnAff = f! [        U S5      (       a  / nU R                   HF  n UR                  X R                  S9  M  ! [         a  nUR                  U5         SnAM@  SnAff = f   U ?U ?[        U5      S:X  a  US   eU(       a  [        SU5      ef f = f7f)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_contextrA   h   sk    "9122 O%@@0%%"+5P5P &  A 4+?#q(%a(($E#   3 ! 0#**3//0 9122 O%@@0%%"+5P5P &  ! 0#**3//0 A 4+?#q(%a(($E#   3sn   D8B "D8A51D85
B?BD8BD8#D5CD5
D	'C>	8D5>D	2D55D8c                 F   ^  [         R                  " T 5      U 4S j5       nU$ )a  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                 h   > [        U 5         T" U /UQ70 UD6sS S S 5        $ ! , (       d  f       g = fr   )rA   )r    argskwargsmethods      r   callback_managed_method/with_callbacks.<locals>.callback_managed_method   s)    (3)5d5f5 433s   #
1)	functoolswraps)rF   rG   s   ` r   with_callbacksrK      s(    ( __V6 6 #"r   )rI   
contextlibr   sklearn.callback._baser   r   "sklearn.callback._callback_contextr   r   rA   rK   r8   r   r   <module>rO      s:     % F >Z& Z&z % %P#r   