
    Q3j*,                     @   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZmZ i Zi Z G d d	      Z	 d d
lmZmZmZmZ d dlmZ  G d de      Z G d d      Z G d dee      Z G d dee      Z G d de      Z  G d d      Z!y# e$ r Y w xY w)    )Integral)Queue)Thread)get_context_path)close_listeneropen_listenersend)check_rich_support)Intervalvalidate_paramsc            	       f    e Zd ZdZ ed eeddd      dgid      dd	       Zd
 Zd Z	d Z
d Zy)ProgressBarau  Callback that displays progress bars for each iterative step of an estimator.

    Parameters
    ----------
    max_propagation_depth : int, default=1
        The maximum depth of nested levels of estimators to display progress bars for.
        0 means that the progress of only the outermost estimator is displayed.
        If set to None, all levels are displayed.
    max_propagation_depthr   Nleft)closedT)prefer_skip_nested_validationc                 6    t        d       || _        i | _        y )NProgressbar)r
   r   _listener_handles)selfr   s     J/DATA/.local/lib/python3.12/site-packages/sklearn/callback/_progressbar.py__init__zProgressBar.__init__   s    
 	=)%:" "$    c                     t               }t        |j                        | j                  |j                  <   t        |      }|j                          |t        |j                  <   |t        |j                  <   y )N)queue)	r   r   putr   	root_uuidRichProgressMonitorstart_run_queues_run_monitors)r   	estimatorcontextr   progress_monitors        r   setupzProgressBar.setup+   sd      5B%))4Lw001.U; ).G%%&+;g''(r   c                 r   |j                   dk7  s|j                  |j                  t        |      D cg c]  }|j                   }}t        | j                  |j                     d||j                  |j                  |j                   |j                  |j                  |j                  d       y y c c}w )Nr   begin)eventpath	task_nametask_idmax_subtasksestimator_namesource_estimator_namesource_task_name)r,   parentr.   r   r+   r	   r   r   r*   r-   r/   )r   r"   r#   ctxr)   s        r   on_fit_task_beginzProgressBar.on_fit_task_begin>   s       A%~~%,,8 ,<G+DE+DCCKK+DDE&&w'8'89$ !(!2!2&$+$8$8&-&<&<-4-J-J(/(@(@	 9 Fs   B4c                     t        | j                  |j                     dt        |      D cg c]  }|j                   c}d       y c c}w )Nend)r(   r)   )r	   r   r   r   r+   )r   r"   r#   r1   s       r   on_fit_task_endzProgressBar.on_fit_task_endW   sI    ""7#4#450@0IJ0I0IJ	
 Ks   A	c                    t         j                  |j                        j                  d        t        j                  |j                        j                          t        | j                  j                  |j                               y N)r    popr   r   r!   joinr   r   )r   r"   r#   s      r   teardownzProgressBar.teardowna   s]     	))*..t4'++,113t--11'2C2CDEr   )   )__name__
__module____qualname____doc__r   r   r   r   r%   r2   r5   r:    r   r   r   r      sP     	 8Haf#Mt"TU&*$	$<&2
Fr   r   )	BarColumnProgress
TextColumnTimeRemainingColumn)Stylec                       e Zd Zd Zy)	_Progressc              #   H   K   | j                  t        | dg              y w)N_ordered_tasks)make_tasks_tablegetattrr   s    r   get_renderablesz_Progress.get_renderablesq   s!     ''6F(KLLs    "N)r<   r=   r>   rM   r@   r   r   rG   rG   m   s    	Mr   rG   c                   "     e Zd ZdZ fdZ xZS )_StyledColumnMixinz8Apply finished/in-progress color style to rendered text.c                 ^    t         |   |      }|j                  r	d|_        |S d|_        |S )N#29ABE2#F7931E)superrenderfinishedstyle)r   tasktext	__class__s      r   rT   z_StyledColumnMixin.renderw   s2    7>$'D&*mmDJK :CDJKr   )r<   r=   r>   r?   rT   __classcell__rY   s   @r   rO   rO   t   s    F	 	r   rO   c                       e Zd ZdZy)_StyledTimeRemainingColumnzTime column with color styling.Nr<   r=   r>   r?   r@   r   r   r]   r]   |   s    -r   r]   c                       e Zd ZdZy)_StyledPercentageColumnz%Percentage column with color styling.Nr^   r@   r   r   r`   r`      s    3r   r`   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )r   aW  Thread monitoring the progress of an estimator with rich based display.

    The display is a list of nested rich tasks using `rich.Progress`. There is one for
    each non-leaf node in the task tree of the estimator.

    Parameters
    ----------
    queue : `queue.Queue` instance
        This thread will run until the queue is empty.
    c                0    t         |           || _        y r7   )rS   r   r   )r   r   rY   s     r   r   zRichProgressMonitor.__init__   s    
r   c           
         t        t        d      t        t        d      t        d      t        d            t	        d      t        d            | _        d | _        | j                  5  | j                  j                         x}rT|j                  d	      d
k(  r| j                  |       n| j                  |       | j                  j                         x}rT| j                  j                          d d d        y # 1 sw Y   y xY w)Nz([progress.description]{task.description}rR   )colorrQ   )complete_stylefinished_stylepulse_stylez{task.percentage:>3.0f}%T)elapsed_when_finishedr(   r'   )rG   rC   rA   rE   r`   r]   progress_ctxroot_rich_taskr   getr8   _on_task_begin_on_task_endrefresh)r   	task_infos     r   runzRichProgressMonitor.run   s    %AB$95$95!	2
 $$>?&TB	
 ##zz~~//)/==)W4''	2%%i0	  $zz~~//)/ %%' s   -A0DDD
c                    |j                  d      }t        | j                  |t        |      dz
        }|j                  dk(  r|| _        n0| j
                  j                  |      d   }||j                  |d   <   | j
                  D cg c]%  }| j                  j                  |j                     ' c}| j                  _
        yc c}w )zHCreate a progress bar for the task and update the list of ordered tasks.r)   r;   )ri   ro   depthr   N)r8   RichTaskri   lenrr   rj   get_descendantschildrentasksidrI   )r   ro   r)   	rich_taskr0   rW   s         r   rl   z"RichProgressMonitor._on_task_begin   s    }}V$**is4yST}
	 ??a"+D((88>rBF(1FOODH% :>9L9L,
9LD##DGG,9L,
( ,
s   *Cc                     |j                  d      }| j                  j                  |      ^ }}|7t        | j                  |t        |            }||d   j                  |d   <   n9|j                  |_        | j                  j                  |j                  dd       t        |      D ]O  }|j                  |j                  |j                  z  }| j                  j                  |j                  |       Q y)z>Update the progress of the task and its ancestors recursively.r)   Nrr   rt   r;   )	completedtotal)r~   )r8   rj   rw   ru   ri   rv   rx   r   r~   updaterz   reversedprogress)r   ro   r)   	ancestorsrW   ancestorr~   s          r   rm   z RichProgressMonitor._on_task_end   s    }}V$..>>tDD<D--yIOD/3IbM""48, "ZZDN$$TWW$C +H~~% ))HNN:I$$X[[I$F ,r   )	r<   r=   r>   r?   r   rp   rl   rm   rZ   r[   s   @r   r   r      s    	(2
"Gr   r   c                   8    e Zd ZdZd Zed        Zd Zd Zd Z	y)ru   a  A task, i.e. progressbar, in the tree of rich tasks.

    There is a rich task for each non-leaf task in the context tree of the estimator.

    Parameters
    ----------
    progress_ctx : `rich.Progress` instance
        The progress context to which this task belongs.

    task_info : dict
        Information about the task for which this rich task is created.

    depth : int
        The depth of the task in the tree of rich tasks.

    Attributes
    ----------
    completed : int
        The number of completed subtasks.

    total : int or None
        The total number of subtasks. None if the total number of subtasks is not known.

    progress : float
        The fraction, between 0 and 1, of the task that is completed.

    id : int
        The ID of the task in the Progress context.

    children : dict
        A mapping from the index of a child to the child node `{idx: RichTask}`.
        For a leaf, it's an empty dictionary.
    c                    i | _         || _        d| _        |j                  dd      | _        |r4| j                  |      }|j                  || j                        | _        y y )Nr   r,   )r   )rx   rr   r~   r8   r   _format_task_descriptionadd_taskrz   )r   ri   ro   rr   descriptions        r   r   zRichTask.__init__   s\    
]]>15
77	BK"++Ktzz+JDG r   c                     | j                   | j                  k(  ry| j                  yt        d | j                  j	                         D              | j                  z  S )z2Return the fraction of the task that is completed.g      ?g        c              3   4   K   | ]  }|j                     y wr7   )r   ).0childs     r   	<genexpr>z$RichTask.progress.<locals>.<genexpr>  s     F/Ee5>>/Es   )r~   r   sumrx   valuesrL   s    r   r   zRichTask.progress  sK     >>TZZ'::Ft}}/C/C/EFFSSr   c                     d| j                   z   }|d    d|d    }| j                   dkD  rd|d    nd}|d	   |d	    d|d
    dnd}| | | | S )z,Return a formatted description for the task.z  r-   z - r*   r   z #r+    r.   r/   z | r}   )r   ro   indent	task_descid_marksource_task_descs         r   r   z!RichTask._format_task_description  s    4::%& !1233y7M6NO	15aBy+,-R 01= 012#i@R6S5TTWX 	
 *+I;wi@@r   c                     t        |      dk(  r| gS |d   | j                  vr| dgS | g| j                  |d      j                  |dd       z   S )z;Return the descendants from this task along the given path.r;   N)rv   rx   rw   )r   r)   s     r   rw   zRichTask.get_descendants  sY    t9>6M7$--'$<vd1g.>>tABxHHHr   c              #      K   t        | d      r.|  | j                  j                         D ]  }|E d{     yy7 w)zJPre-order depth-first traversal, only including tasks with a progress bar.rz   N)hasattrrx   r   )r   r   s     r   __iter__zRichTask.__iter__!  s?     4J--/    0  !s   3A >	A N)
r<   r=   r>   r?   r   propertyr   r   rw   r   r@   r   r   ru   ru      s5     DK T T
AI!r   ru   N)"numbersr   r   r   	threadingr   "sklearn.callback._callback_contextr   sklearn.callback._transportr   r   r	   $sklearn.utils._optional_dependenciesr
   sklearn.utils._param_validationr   r   r    r!   r   rich.progressrA   rB   rC   rD   
rich.stylerE   rG   rO   r]   r`   ImportErrorr   ru   r@   r   r   <module>r      s       ? K K C E
 RF RFj	RR MH M .%79L .4"4j 4NG& NGbO! O!k  		s    ?B BB