
    Mpj                     $   S SK r S SKrS SKJr  S SKrS SKJr  S SKJr  S SK	J
r
JrJrJrJr  S SKJr  S SKJrJr  S SKJrJr  \R.                  R1                  S	S
S/5      \R.                  R1                  SSS/5      \R.                  R1                  S\\\/5      \R.                  R1                  S/ SQ5      S 5       5       5       5       rS rS r\R.                  R8                  \R.                  R1                  SSS/5      S 5       5       r\R.                  R1                  SSS/5      S 5       rS r\R.                  R1                  S\" \" SS9SS9\
" \" SS9SSS/5      \
" SS\" SS9S/5      \
" SSS\" SS9/5      /5      S  5       r S! r!g)"    N)mock)clone)ProgressBar)HeterogeneousMetaEstimatorMaxIterEstimatorMetaEstimatorNoSubtaskEstimatorWhileEstimator)check_rich_support)assert_allclose'assert_run_python_script_without_output)Paralleldelayedn_jobs      preferthreads	processesInnerEstimatormax_propagation_depth)r   r   Nc                    [         R                  " S5        SnSnU" 5       n[        XvXPUS9nUR                  [	        US95        UR                  5         UR                  5       n	[        R                  " SU	R                  5      (       d   e[        U5       H.  n
[        R                  " SU
 3U	R                  5      (       a  M.   e   Ub  US	:  aT  [        U5       HE  n
[        R                  " S
UR                  R                   SU
 3U	R                  5      (       a  ME   e   [        R                  " SU	R                  5      (       d   e[        R                  " SU	R                  5      (       a   eg)z;Check the output of the progress bars and their completion.richr      )n_outern_innerr   r   )r   zMetaEstimator - fitzMetaEstimator - outer #Nr   zMetaEstimator - inner \| z - fit #100%z\b[0-9]{1,2}%)pytestimportorskipr   set_callbacksr   fit
readouterrresearchoutrange	__class____name__)r   r   r   r   capsysr   r   estmeta_estcapturedis              c/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/sklearn/callback/tests/test_progressbar.pytest_progressbarr/      sF    GG

CgVH ;=RSTLLN  "H99+X\\::::7^yy3A37FFFF 
 $(=(AwA99,S]]-C-C,DHQCP      99Whll++++yy)8<<88888    c                       [        S5        [        R                  " S5        g! [         aC    Sn [        R                  " [        U S9   [        5         SSS5         g! , (       d  f        g= ff = f)zDCheck that we raise an informative error when rich is not installed.$test_progressbar_requires_rich_errorz,This test requires rich to not be installed.zProgressbar requires rich)matchN)r   r   skipImportErrorraisesr   )err_msgs    r.   r2   r2   C   sS    ABBC -]];g6M 7666s'   !$ %A1	AA1
A-	(A1-A1c                      [         R                  " S5        [        5       n [        5       R	                  U 5      R                  5       n[        U5      nUR                  S   U L d   eg)zCloning an estimator with a ProgressBar preserves the callback reference.

clone copies _skl_callbacks by reference so that a single callback instance can
track every clone.
r   r   N)r   r   r   r   r    r!   r   _skl_callbacks)pbr*   cloneds      r.   test_clone_after_fitr<   N   sW     	B


*
*2
.
2
2
4C3ZF  #r)))r0   backend	threadinglokyc                    ^ ^ [         R                  " S5        S mU U4S jn[        5       nSnU" [        5       R	                  U5      US9  SSKJnJn  UR                  0 :X  d   eU0 :X  d   eU0 :X  d   eg)	zSanity check for ProgressBar within function not supporting callbacks.

It's hard to check the output from sub-processes so this test only checks that it
doesn't crash and that there are no threads leftover running.
r   c                 6    [        U 5      R                  5         g N)r   r!   )	estimators    r.   clone_and_fit;test_progressbar_no_callback_support.<locals>.clone_and_fitf   s    ir0   c                 P   >^  [        STS9" UU 4S j[        U5       5       5        g )Nr   )r   r=   c              3   F   >#    U  H  n[        T5      " T5      v   M     g 7frB   )r   ).0_rD   rC   s     r.   	<genexpr>Etest_progressbar_no_callback_support.<locals>.func.<locals>.<genexpr>j   s!      ,
7D!GM"9--}s   !)r   r&   )rC   n_fitsr=   rD   s   ` r.   func2test_progressbar_no_callback_support.<locals>.funci   s#    7+ ,
7<V},
 	
r0      )rL   r   )_run_monitors_run_queuesN)	r   r   r   r   r    sklearn.callback._progressbarrP   rQ   _listener_handles)r=   rM   progressbarrL   rP   rQ   rD   s   `     @r.   $test_progressbar_no_callback_supportrU   \   sy     

 -KF			)	)+	6vFH ((B..."Br0   c                 ~    [         R                  " S5        SU  S3nSn[        [        R                  " U5      USS9  g)zHCheck that ProgressBar does not trigger spawn errors outside `__main__`.r   z
    from sklearn.callback import ProgressBar
    from sklearn.callback.tests._utils import MaxIterEstimator, MetaEstimator

    est = MaxIterEstimator()
    meta_est = MetaEstimator(
        est, n_outer=2, n_inner=1, n_jobs=2, prefer='zI'
    )
    meta_est.set_callbacks(ProgressBar())
    meta_est.fit()
    z/An attempt has been made to start a new processx   )patterntimeoutN)r   r   r   textwrapdedent)r   coderX   s      r.   $test_progressbar_outside_main_moduler]   {   sJ     6 7=X >
D @G+wr0   c                    ^^ [         R                  " S5        SSKJn   / mU R                  mUU4S jnSn[
        R                  R                  U SU5         [        US9R                  [        5       5      R                  5         SSS5        [        S	US	-   5       Vs/ s H  o3U-  PM	     snS
/-   n[        TU5        g! , (       d  f       NA= fs  snf )zLCheck that the completion of a bottom-level progressbar increments linearly.r   r   RichProgressMonitorc                 `   > T" X5        TR                  U R                  R                  5        g rB   appendroot_rich_taskprogress)self	task_infoorig_on_task_endrecordss     r.   recording_on_task_end7test_progress_during_fit.<locals>.recording_on_task_end   s#    )t**334r0      _on_task_endmax_iterNr         ?)r   r   rR   r`   rm   r   patchobjectr   r    r   r!   r&   r   )r`   rj   ro   r-   expectedrh   ri   s        @@r.   test_progress_during_fitrt      s    
AG*775 H			.@U	V(+99+-HLLN 
W ',Ax!|&<=&<H&<=EHGX& 
W	V >s   /CC
Cmeta_estimator   rn   rO   )r   c                   ^^	^
 [         R                  " S5        SSKJn  / m
UR                  m	U4S jmUU	U
4S jn[
        R                  R                  USU5         U R                  [        5       5      nUR                  5         SSS5        T
 VVs/ s H  u  pE[        U5      S:X  d  M  UPM     nnn/ S	Qn[        Xg5        g! , (       d  f       NE= fs  snnf )
zECheck the recursive computation of the progress of nested estimators.r   r   r_   c                   > U R                   (       d  SnO`[        S U R                   R                  5        5       5      U R                  -  nU R                   R                  5        H  nT" U5        M     [	        U R
                  U5        g )Nrp   c              3   8   #    U  H  oR                   v   M     g 7frB   )re   )rH   cs     r.   rJ   Otest_progress_during_fit_composition.<locals>.check_progress.<locals>.<genexpr>   s     F/E!::/Es   )childrensumvaluestotalr   re   )taskrs   childcheck_progresss      r.   r   <test_progress_during_fit_composition.<locals>.check_progress   sd     }}HFt}}/C/C/EFFSH--/u% 0x0r0   c                    > US   nT" X5        TR                  X R                  R                  /5        T" U R                  5        g )Npathrb   )rf   rg   r   r   rh   ri   s      r.   rj   Ctest_progress_during_fit_composition.<locals>.recording_on_task_end   s>     )11::;<t**+r0   rm   Nr   )g      ?g      ?g      ?rp   )r   r   rR   r`   rm   r   rq   rr   r    r   r!   lenr   )ru   r`   rj   metar   re   end_of_outer_subtasksexpected_progressr   rh   ri   s           @@@r.   $test_progress_during_fit_compositionr      s     AG*77	1, 
		.@U	V++KM:
 
W =DVG.$s4yTU~XGV.)= 
W	V Ws   *CC-C
Cc                 N   [         R                  " S5        [        5       R                  [	        5       5      R                  5         U R                  5       n[        R                  " SUR                  5      (       d   e[        R                  " SUR                  5      (       d   eg)zICheck that a progress bar is displayed for an estimator without subtasks.r   zNoSubtaskEstimator - fitr   N)
r   r   r	   r    r   r!   r"   r#   r$   r%   )r)   r,   s     r.   test_estimator_without_subtasksr      sq    
&&{}599;  "H990(,,????99Whll++++r0   )"r#   rZ   unittestr   r   sklearn.baser   sklearn.callbackr   sklearn.callback.tests._utilsr   r   r   r	   r
   $sklearn.utils._optional_dependenciesr   sklearn.utils._testingr   r   sklearn.utils.parallelr   r   markparametrizer/   r2   r<   thread_unsaferU   r]   rt   r   r    r0   r.   <module>r      s   
     (  D 5 Aq6*I{#;<'9KL 0,?9 @ = +9D
* [&$9: ; : I{#;< =*'* &2A>"$4a$@$d#ST"D$0@!0Ld#ST"D$6FPQ6R#ST	!>!>H,r0   