
    Mpj7                        S SK r S SKrS SKrS SKJrJrJr  S SKJ	r	J
r
  S SKJrJr  S SKJr  S SKJrJr  \R&                  R)                  \SS9r " S	 S
5      r " S S\5      r " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\	\5      r " S S\	\5      r " S S\	5      r " S S\5      r  " S S \5      r! " S! S"\	\5      r"S# r# " S$ S%\	5      r$ " S& S'\	\5      r%g)(    N)BaseEstimator_fit_contextclone)CallbackSupportMixinwith_callbacks)open_listenersend)_IS_WASM)Paralleldelayedz*callback tests are skipped on WASM/Pyodide)reasonc                   X    \ rS rSrSrS rS rSSSSS.S jrSSSSS.S jrS	 r	S
 r
Srg)RecordingCallback   a  A minimal callback used for smoke testing purposes.

This callback keeps a record of the hooks called for introspection.

This callback doesn't define `max_propagation_depth` and is therefore not an
`AutoPropagatedCallback`: it should not be propagated to sub-estimators.
c                 X    / U l         [        U R                   R                  U S9U l        g )N)owner)recordr   append_listener_handle)selfs    Y/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/sklearn/callback/tests/_utils.py__init__RecordingCallback.__init__   s"     -dkk.@.@ M    c                 6    [        U R                  SXS.5        g )Nsetupname	estimatorcontextr	   r   r   r   r    s      r   r   RecordingCallback.setup"   s    !!9I	
r   NXymetadatafitted_estimatorc                D    [        U R                  SUUUUUUS.S.5        g )Non_fit_task_beginr$   r   r   r    kwargsr!   r   r   r    r%   r&   r'   r(   s          r   r*   #RecordingCallback.on_fit_task_begin(   s4     	!!+&" ((8		
	
r   c                D    [        U R                  SUUUUUUS.S.5        g )Non_fit_task_endr$   r+   r!   r-   s          r   r0   !RecordingCallback.on_fit_task_endA   s4     	!!)&" ((8		
	
r   c                 6    [        U R                  SXS.5        g )Nteardownr   r!   r"   s      r   r3   RecordingCallback.teardownZ   s    !!iL	
r   c                 l    [        U R                   Vs/ s H  o"S   U:X  d  M  UPM     sn5      $ s  snf )Nr   )lenr   )r   	hook_namerecs      r   count_hooksRecordingCallback.count_hooks`   s,    4;;K;Cf+2JC;KLLKs   11)r   r   )__name__
__module____qualname____firstlineno____doc__r   r   r*   r0   r3   r9   __static_attributes__ r   r   r   r      sC    N
 

< 

2
Mr   r   c                       \ rS rSrSrSrSrg)RecordingAutoPropagatedCallbackd   a2  A minimal auto-propagated callback used for smoke testing purposes.

This callback keeps a record of the hooks called for introspection.

This callback defines `max_propagation_depth` and is therefore an
`AutoPropagatedCallback`: it should be set on a top-level estimator and propagated
to sub-estimators.
NrA   )r;   r<   r=   r>   r?   max_propagation_depthr@   rA   r   r   rC   rC   d   s     !r   rC   c                   $    \ rS rSrSrS rS rSrg)NotValidCallbackq   z>Invalid callback since it's missing methods from the protocol.c                     g NrA   r"   s      r   r   NotValidCallback.setupt       r   c                     g rJ   rA   r"   s      r   r0    NotValidCallback.on_fit_task_endw   rL   r   rA   N)r;   r<   r=   r>   r?   r   r0   r@   rA   r   r   rG   rG   q   s    Hr   rG   c                   &    \ rS rSrSrSS.S jrSrg)NotValidHookCallback{   zIInvalid callback since it has invalid parameters in the hooks signatures.N)not_valid_kwargc                    g rJ   rA   )r   r   r    rR   s       r   r*   &NotValidHookCallback.on_fit_task_begin~   rL   r   rA   )r;   r<   r=   r>   r?   r*   r@   rA   r   r   rP   rP   {   s    SGK  r   rP   c                   `   ^  \ rS rSrSrS	U 4S jjrU 4S jrU 4S jrU 4S jrU 4S jr	Sr
U =r$ )
FailingCallback   z.A callback that raises an error at some point.c                 .   > [         TU ]  5         Xl        g rJ   )superr   fail_at)r   rZ   	__class__s     r   r   FailingCallback.__init__   s    r   c                 Z   > [         TU ]  X5        U R                  S:X  a  [        S5      eg )Nr   z Failing callback failed at setup)rY   r   rZ   
ValueErrorr   r   r    r[   s      r   r   FailingCallback.setup   s,    i)<<7"?@@ #r   c                 Z   > [         TU ]  X5        U R                  S:X  a  [        S5      eg )Nr*   z,Failing callback failed at on_fit_task_begin)rY   r*   rZ   r^   r_   s      r   r*   !FailingCallback.on_fit_task_begin   s.    !)5<<..KLL /r   c                 Z   > [         TU ]  X5        U R                  S:X  a  [        S5      eg )Nr0   z*Failing callback failed at on_fit_task_end)rY   r0   rZ   r^   r_   s      r   r0   FailingCallback.on_fit_task_end   s.    	3<<,,IJJ -r   c                 Z   > [         TU ]  X5        U R                  S:X  a  [        S5      eg )Nr3   z#Failing callback failed at teardown)rY   r3   rZ   r^   r_   s      r   r3   FailingCallback.teardown   s-    ,<<:%BCC &r   )rZ   rJ   )r;   r<   r=   r>   r?   r   r   r*   r0   r3   r@   __classcell__r[   s   @r   rV   rV      s)    8A
M
K
D Dr   rV   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )StopFitCallback   z8A callback with a `on_fit_task_end` hook returning True.c                 $   > [         TU ]  X5        g)NTrY   r0   r_   s      r   r0   StopFitCallback.on_fit_task_end   s    	3r   rA   r;   r<   r=   r>   r?   r0   r@   rg   rh   s   @r   rj   rj      s    B r   rj   c                   6   ^  \ rS rSrSrSSS.U 4S jjrSrU =r$ )NotRequiredKwargsCallback   zFA callback with a `on_fit_task_end` not requiring all possible kwargs.Nr%   r&   c                "   > [         TU ]  XX4S9  g )Nrs   rm   )r   r   r    r%   r&   r[   s        r   r0   )NotRequiredKwargsCallback.on_fit_task_end   s    	a=r   rA   ro   rh   s   @r   rq   rq      s    P7;t > >r   rq   c                   `    \ rS rSr% Sr0 r\\S'   SS jr\	" SS9  SSS.S	 jj5       r
S
 rSrg)MaxIterEstimator   a  A class that mimics the behavior of an estimator.

The iterative part uses a loop with a max number of iterations known in advance.

This estimator computes arbitrary predictions by averaging the feature
values and multiplying the result by the number of iterations done
in fit.
_parameter_constraintsc                     Xl         X l        g rJ   max_itercomputation_intensityr   r|   r}   s      r   r   MaxIterEstimator.__init__        %:"r   Fprefer_skip_nested_validationN)sample_weightc          	        ^ U R                  U R                  S9nUb  SU0O0 nUR                  XX%S9  [        U R                  5       Hc  mUR	                  ST 3S9nUR                  XX%S9  [
        R                  " U R                  5        UR                  U UUUU4S jS9(       d  Mc    O   TS-   U l	        UR                  U UUU0 S9  U $ )	Nmax_subtasksr   r   r%   r&   r'   z
iteration 	task_namec                     > ST S-   0$ )Nn_iter_   rA   )is   r   <lambda>&MaxIterEstimator.fit.<locals>.<lambda>   s    9a!e2Dr   )r   r%   r&   r'   reconstruction_attributesr   
_init_callback_contextr|   call_on_fit_task_beginrange
subcontexttimesleepr}   call_on_fit_task_endr   )r   r%   r&   r   callback_ctxr'   r   r   s          @r   fitMaxIterEstimator.fit   s     222N7D7PO]3VX++d1+Xt}}%A%00Zs;K0LJ--q .  JJt112..!*D /    &" 1u))&( 	* 	
 r   c                 F    [         R                  " USS9U R                  -  $ )Nr   )axis)npmeanr   r   r%   s     r   predictMaxIterEstimator.predict   s    wwqq!DLL00r   r}   r|   r      MbP?NN)r;   r<   r=   r>   r?   ry   dict__annotations__r   r   r   r   r@   rA   r   r   rw   rw      sJ     $&D%; 6 
&
 & 7&P1r   rw   c                   N    \ rS rSr% Sr0 r\\S'   S
S jr\	" SS9SS j5       r
S	rg)WhileEstimator   zA class that mimics the behavior of an estimator.

The iterative part uses a while loop with a number of iterations unknown in
advance.
ry   c                     Xl         g rJ   r}   )r   r}   s     r   r   WhileEstimator.__init__   s    %:"r   Fr   Nc                 2   U R                  S S9nUR                  XUS9  Sn UR                  5       nUR                  XUS9  [        R                  " U R
                  5        UR                  XUS9(       a  OUS:X  a  OUS-  nMd  UR                  XUS9  U $ )Nr   r   r%   r&   r   r   r   )r   r   r   r   r   r}   r   r   r%   r&   r   r   r   s         r   r   WhileEstimator.fit   s    222E++d1+E%002J--Q-GJJt112..a.HBwFA  	))D)Cr   r   )r   r   r;   r<   r=   r>   r?   ry   r   r   r   r   r   r@   rA   r   r   r   r      s2     $&D%; 6 7r   r   c                   6    \ rS rSrSrSS jr\SS j5       rSrg)	ThirdPartyEstimatori  zYA class that mimics a third-party estimator with callback support only using
public API.
c                     Xl         X l        g rJ   r{   r~   s      r   r   ThirdPartyEstimator.__init__  r   r   Nc                 r   U R                  U R                  S9nUR                  XUS9  [        U R                  5       HZ  nUR	                  5       nUR                  XUS9  [
        R                  " U R                  5        UR                  XUS9(       d  MZ    O   UR                  XUS9  WS-   U l	        U $ )Nr   r   r   r   r   s         r   r   ThirdPartyEstimator.fit  s    222N++d1+Et}}%A%002J--Q-GJJt112..a.HH & 	))D)C1ur   r   r   r   	r;   r<   r=   r>   r?   r   r   r   r@   rA   r   r   r   r     s     ;  r   r   c                   b   ^  \ rS rSr% Sr0 r\\S'   S	U 4S jjr\	" SS9S
U 4S jj5       r
SrU =r$ )ParentFitEstimatori-  z=A class that mimics an estimator using its parent fit method.ry   c                 $   > [         TU ]  X5        g rJ   )rY   r   )r   r|   r}   r[   s      r   r   ParentFitEstimator.__init__2  s    9r   Fr   c                 "   > [         TU ]  X5      $ rJ   )rY   r   )r   r%   r&   r[   s      r   r   ParentFitEstimator.fit5  s    w{1  r   rA   r   r   )r;   r<   r=   r>   r?   ry   r   r   r   r   r   r@   rg   rh   s   @r   r   r   -  s/    G#%D%: 6! 7!r   r   c                   2    \ rS rSrSrSS jrS	S jrS rSrg)
NoCallbackEstimatori:  z:A class that mimics an estimator without callback support.c                     Xl         X l        g rJ   r{   r~   s      r   r   NoCallbackEstimator.__init__=  r   r   Nc                 ~    [        U R                  5       H#  n[        R                  " U R                  5        M%     U $ rJ   )r   r|   r   r   r}   )r   r%   r&   r   s       r   r   NoCallbackEstimator.fitA  s.    t}}%AJJt112 & r   c                 H    [         R                  " UR                  S   5      $ )Nr   )r   zerosshaper   s     r   r   NoCallbackEstimator.predictG  s    xx
##r   )r}   r|   r   r   )	r;   r<   r=   r>   r?   r   r   r   r@   rA   r   r   r   r   :  s    D;$r   r   c                   P    \ rS rSr% Sr0 r\\S'    S
S jr\	" SS9SS j5       r
S	rg)MetaEstimatoriK  a   A class that mimics the behavior of a meta-estimator.

It has two levels of iterations. The outer level uses parallelism and the inner
level is done in a function that is not a method of the class. That function must
therefore receive the estimator and the callback context as arguments.
ry   Nc                 @    Xl         X l        X0l        X@l        XPl        g rJ   )r   n_outern_innern_jobsprefer)r   r   r   r   r   r   s         r   r   MetaEstimator.__init__U  s     #r   Fr   c           	        ^ ^^^^ T R                  T R                  SS9nUb  SU0O0 mUR                  T TTTS9  [        T R                  5       Vs/ s H  nUR	                  SUT R
                  S9PM      snm[        T R                  T R                  S9" UUUU U4S j[        T R                  5       5       5        UR                  T TTTS9  T $ s  snf )	NF)r   sequential_subtasksr   r   outer)r   task_idr   )r   r   c           
   3   n   >#    U  H*  n[        [        5      " TTR                  TTTTU   S 9v   M,     g7f))r%   r&   r'   outer_callback_ctxN)r   _fit_subestimatorr   ).0r   r%   r'   outer_callback_contextsr   r&   s     r   	<genexpr>$MetaEstimator.fit.<locals>.<genexpr>m  sB      
9
 ) %&!#:1#= )s   25)
r   r   r   r   r   r   r   r   r   r   )r   r%   r&   r   r   r   r'   r   s   ```   @@r   r   MetaEstimator.fit^  s    225 3 
 8E7PO]3VX++da1x+X 4<<(	#
 ) ##!14<< $  )	#
 	DKK8 
9
 
9
 4<<(
9
 
	
 	))DAX)V+#
s   %C)r   r   r   r   r   )      N	processes)NNNr   rA   r   r   r   r   K  s9     $&D% DO 6 7r   r   c          	      p   UR                  XX4S9  [        U R                  5       Hk  n[        U5      nUR	                  SS9nUR                  U5         UR                  XX4S9  UR                  " SX#S.UD6  UR                  XX4S9  S S S 5        Mm     UR                  XX4S9  g ! , (       d  f       M  = f)Nr   innerr   rs   rA   )r   r   r   r   r   propagate_callback_contextr   r   )	meta_estimatorinner_estimatorr%   r&   r'   r   r   est	inner_ctxs	            r   r   r   ~  s     --  .  >))*O$&11G1D	11#6,,( -  GG)a))**( +  76	 + ++  ,  76s   5B&&
B5	c                   2    \ rS rSrSrS r\SS j5       rSrg)HeterogeneousMetaEstimatori  z9A meta-estimator that fits a list of estimators in order.c                     Xl         g rJ   
estimators)r   r   s     r   r   #HeterogeneousMetaEstimator.__init__  s    $r   Nc                 H   U R                  [        U R                  5      S9nUR                  XUS9  [	        U R                  5       H  u  pEU(       a  SUR
                  R                   3OSU 3nUR                  US9nUbX  [        U5      nUR                  U5         UR                  XUS9  UR                  X5        UR                  XUS9  S S S 5        M  UR                  XUS9  UR                  XUS9  M     UR                  XUS9  U $ ! , (       d  f       M  = f)Nr   r   zfit zskip r   )r   r6   r   r   	enumerater[   r;   r   r   r   r   r   )r   r%   r&   r   r   r   r   r   s           r   r   HeterogeneousMetaEstimator.fit  s   22DOO@T2U++d1+E0FA;>$s}}5567eA3KI%0090EJCj::3?55Q5OGGAM33d13M @?
 11D1K//$q/I 1 	))D)C @?s    2D
D!	r   r   r   rA   r   r   r   r     s    C%  r   r   c                   ,    \ rS rSrSr\SS j5       rSrg)NoSubtaskEstimatori  z7A class mimicking an estimator without subtasks in fit.Nc                 b    U R                  5       R                  XUS9nUR                  XUS9  U $ )Nr   )r   r   r   )r   r%   r&   r   s       r   r   NoSubtaskEstimator.fit  s@    224KK1 L 
 	))D)Cr   rA   r   )r;   r<   r=   r>   r?   r   r   r@   rA   r   r   r   r     s    A	 	r   r   )&r   numpyr   pytestsklearn.baser   r   r   sklearn.callbackr   r   sklearn.callback._transportr   r	   sklearn.utils.fixesr
   sklearn.utils.parallelr   r   markskipifskip_callback_test_if_wasmr   rC   rG   rP   rV   rj   rq   rw   r   r   r   r   r   r   r   r   rA   r   r   <module>r     s      ; ; A ; ( 4#[[//7 0  LM LM^
!&7 
! , D' D8' > 1 >:1+] :1z")= "J. <
!) 
!$- $"0(- 0f6!5 :-} r   