
    Q3j                     B   d dl Z d dlZd dlmZ d dlZd dlmZ d dlmZ d dl	m
Z
mZmZmZmZ d dlmZ d dlmZmZ d dlmZmZ ej.                  j1                  d	d
dg      ej.                  j1                  dddg      ej.                  j1                  deeeg      ej.                  j1                  dg d      d                             Zd Zd Zej.                  j8                  ej.                  j1                  dddg      d               Zej.                  j1                  dddg      d        Zd Zej.                  j1                  d e ed      d       e
 ed      dddg       e
dd ed      dg       e
ddd ed      g      g      d         Z d! Z!y)"    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                    t        j                  d       d}d} |       }t        |||| |      }|j                  t	        |             |j                          |j                         }	t        j                  d|	j                        sJ t        |      D ]'  }
t        j                  d|
 |	j                        r'J  ||d	kD  rLt        |      D ]>  }
t        j                  d
|j                  j                   d|
 |	j                        r>J  t        j                  d|	j                        sJ t        j                  d|	j                        rJ y)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              T/DATA/.local/lib/python3.12/site-packages/sklearn/callback/tests/test_progressbar.pytest_progressbarr/      s9    GG

CWgfVH ;=RSTLLN  "H99+X\\:::7^yy3A37FFF 
 $(=(AwA99,S]]-C-C,DHQCP     99Whll+++yy)8<<8888    c                      	 t        d       t        j                  d       y# t        $ r@ d} t        j                  t        |       5  t                ddd       Y y# 1 sw Y   Y yxY ww xY w)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   sP    ABBC -]];g6M 766s'    # &A,	AA,A(	#A,(A,c                      t        j                  d       t               } t               j	                  |       j                         }t        |      }|j                  d   | u sJ y)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                      t        j                  d       d  fd}t               }d} |t               j	                  |      |       ddlm}m} |j                  i k(  sJ |i k(  sJ |i k(  sJ y)	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    t        |       j                          y N)r   r!   )	estimators    r.   clone_and_fitz;test_progressbar_no_callback_support.<locals>.clone_and_fitf   s    ir0   c                 T      t        d       fdt        |      D               y )Nr   )r   r=   c              3   @   K   | ]  } t                      y wrB   )r   ).0_rD   rC   s     r.   	<genexpr>zEtest_progressbar_no_callback_support.<locals>.func.<locals>.<genexpr>j   s"      ,
7D!"GM"9-}s   )r   r&   )rC   n_fitsr=   rD   s   ` r.   funcz2test_progressbar_no_callback_support.<locals>.funci   s%    +7+ ,
7<V},
 	
r0      )rJ   r   )_run_monitors_run_queuesN)	r   r   r   r   r    sklearn.callback._progressbarrM   rN   _listener_handles)r=   rK   progressbarrJ   rM   rN   rD   s   `     @r.   $test_progressbar_no_callback_supportrR   \   sy     

 -KF			)	)+	6vFH ((B..."Br0   c                     t        j                  d       d|  d}d}t        t        j                  |      |d       y)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   coderU   s      r.   $test_progressbar_outside_main_modulerZ   {   sJ     6 7=X >
D @G+wr0   c                     t        j                  d       ddlm}  g | j                  fd}d}t
        j                  j                  | d|      5  t        |      j                  t                     j                          ddd       t        d	|d	z         D cg c]  }||z  	 c}d
gz   }t        |       y# 1 sw Y   :xY wc c}w )zLCheck that the completion of a bottom-level progressbar increments linearly.r   r   RichProgressMonitorc                 b     | |       j                  | j                  j                         y rB   appendroot_rich_taskprogress)self	task_infoorig_on_task_endrecordss     r.   recording_on_task_endz7test_progress_during_fit.<locals>.recording_on_task_end   s%    y)t**334r0      _on_task_endmax_iterNr         ?)r   r   rO   r]   ri   r   patchobjectr   r    r   r!   r&   r   )r]   rg   rk   r-   expectedre   rf   s        @@r.   test_progress_during_fitrp      s    
AG*775 H			.@U	V(+99+-HLLN 
W ',Ax!|&<=&<H&<=EHGX& 
W	V >s   2C  C C	meta_estimator   rj   rL   )r   c                   	
 t        j                  d       ddlm} g 
|j                  	fd	
fd}t
        j                  j                  |d|      5  | j                  t                     }|j                          ddd       
D cg c]  \  }}t        |      dk(  s| }}}g d	}t        ||       y# 1 sw Y   =xY wc c}}w )
zECheck the recursive computation of the progress of nested estimators.r   r   r\   c                    | j                   sd}n^t        d | j                   j                         D              | j                  z  }| j                   j                         D ]
  } |        t	        | j
                  |       y )Nrl   c              3   4   K   | ]  }|j                     y wrB   )rb   )rG   cs     r.   rI   zOtest_progress_during_fit_composition.<locals>.check_progress.<locals>.<genexpr>   s     F/E!1::/Es   )childrensumvaluestotalr   rb   )taskro   childcheck_progresss      r.   r}   z<test_progress_during_fit_composition.<locals>.check_progress   sd     }}HFt}}/C/C/EFFSH--/u% 0x0r0   c                     |d   } | |       j                  || j                  j                  g        | j                         y )Npathr_   )rc   rd   r   r}   re   rf   s      r.   rg   zCtest_progress_during_fit_composition.<locals>.recording_on_task_end   sB     y)d11::;<t**+r0   ri   Nr   )g      ?g      ?g      ?rl   )r   r   rO   r]   ri   r   rm   rn   r    r   r!   lenr   )rq   r]   rg   metar   rb   end_of_outer_subtasksexpected_progressr}   re   rf   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                 4   t        j                  d       t               j                  t	                     j                          | j                         }t        j                  d|j                        sJ t        j                  d|j                        sJ y)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      sk    
&&{}599;  "H990(,,???99Whll+++r0   )"r#   rW   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_unsaferR   rZ   rp   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   