U
    f»{jÂ  ã                   @   s¨   d Z ddlmZ ddlZddlmZ ddlZddlZddl	m
Z
 ddlZddlZddlmZ ddlmZ ddlmZ G d	d
„ d
eƒZG dd„ de
ƒZG dd„ deƒZdS )zSome utility functions.é    )ÚIterableN)ÚThreadé   )Ú_check_option)Ú
get_config)Úloggerc                   @   sR   e Zd ZdZddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚProgressBaraï  Generate a command-line progressbar.

    Parameters
    ----------
    iterable : iterable | int | None
        The iterable to use. Can also be an int for backward compatibility
        (acts like ``max_value``).
    initial_value : int
        Initial value of process, useful when resuming process from a specific
        value, defaults to 0.
    mesg : str
        Message to include at end of progress bar.
    max_total_width : int | str
        Maximum total message width. Can use "auto" (default) to try to set
        a sane value based on the current terminal width.
    max_value : int | None
        The max value. If None, the length of ``iterable`` will be used.
    **kwargs : dict
        Additional keyword arguments for tqdm.
    Nr   Úautoc              
   K   s–  dd l }tddƒ}td|d d… ddd� t d|› �¡ |d	kr¢zt|ƒ W n< tk
rŠ }	 ztd
t|ƒ› d|	› �ƒd ‚W 5 d }	~	X Y nX t	|| 
dd¡d ƒ}|j }tdddddd�}
|
 ¡ D ]\}}||krÂ| ||i¡ qÂt|tƒ�r|| _|d k�r
t|ƒ| _n|| _nt|ƒ| _d | _|dk�r0d }tjddd��}|j| _W 5 Q R X ~d | _tjtjk�pn|dk}|f | j|| j|||dœ|—Ž| _d S )Nr   ZMNE_TQDMz	tqdm.autoé   )Útqdmztqdm.ÚoffZ	beginning)ÚextrazUsing ProgressBar with )r   r   zUnknown tqdm backend z, got: Ú.r   Tgü©ñÒMb�?gš™™™™™©?zg{percentage:3.0f}%|{bar}| {desc} : {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt:>11}{postfix}])ZleaveZminintervalZminitersZ	smoothingZ
bar_formatr	   ÚwbZtmp_mne_prog)Úprefixr   )ÚiterableÚdescÚtotalÚinitialZncolsÚdisable)r   r   r   r   ÚdebugÚ
__import__Ú	ExceptionÚ
ValueErrorÚreprÚgetattrÚsplitÚdictÚitemsÚupdateÚ
isinstancer   r   ÚlenÚ	max_valueÚintÚtempfileÚNamedTemporaryFileÚnameÚ_mmap_fnameÚ_mmapÚlevelÚloggingÚINFOÚ_tqdm)Úselfr   Úinitial_valueZmesgZmax_total_widthr"   Úkwargsr   Z
which_tqdmÚexcÚdefaultsÚkeyÚvalÚtfr   © r5   úT/var/www/html/bwave/python/venv/lib/python3.8/site-packages/mne/utils/progressbar.pyÚ__init__,   sf    
ÿÿþ   þ


   ýýzProgressBar.__init__c                 C   s   |   || jj ¡ dS )aE  Update progressbar with current value of process.

        Parameters
        ----------
        cur_value : number
            Current value of process.  Should be <= max_value (but this is not
            enforced).  The percent of the progressbar will be computed as
            ``(cur_value / max_value) * 100``.
        N)Úupdate_with_increment_valuer,   Ún)r-   Z	cur_valuer5   r5   r6   r   [   s    
zProgressBar.updatec                 C   s   | j  |¡ dS )a(  Update progressbar with an increment.

        Parameters
        ----------
        increment_value : int
            Value of the increment of process.  The percent of the progressbar
            will be computed as
            ``(self.cur_value + increment_value / max_value) * 100``.
        N)r,   r   )r-   Zincrement_valuer5   r5   r6   r8   g   s    
z'ProgressBar.update_with_increment_valuec                 c   s   | j D ]
}|V  qdS )z*Iterate to auto-increment the pbar with 1.N)r,   )r-   Úxr5   r5   r6   Ú__iter__s   s    
zProgressBar.__iter__c                 C   s
   t | |ƒS )a  Make a joblib-friendly index subset updater.

        Parameters
        ----------
        idx : ndarray
            List of indices for this subset.

        Returns
        -------
        updater : instance of PBSubsetUpdater
            Class with a ``.update(ii)`` method.
        )Ú_PBSubsetUpdater)r-   Úidxr5   r5   r6   Úsubsetx   s    zProgressBar.subsetc                 C   sX   t  | j¡rt | j¡ tj| jtdt| j	dƒd�| _
|  d¡ t| ƒ| _| j ¡  | S )Nzw+r   )Úshaper   )ÚopÚisfiler'   ÚosÚremoveÚnpZmemmapÚboolÚmaxr"   r(   r   Ú_UpdateThreadÚ_threadÚstart©r-   r5   r5   r6   Ú	__enter__‡   s    
ÿ


zProgressBar.__enter__c                 C   sN   |   | j ¡ ¡ | j ¡  d| j_| j ¡  d | _t 	| j
¡rJt | j
¡ d S )NF)r   r(   Úsumr,   ÚcloserH   Ú_mne_runÚjoinr@   rA   r'   rB   rC   )r-   Útype_ÚvalueÚ	tracebackr5   r5   r6   Ú__exit__•   s    

zProgressBar.__exit__c                 C   s   t | ddƒdk	r| j ¡  dS )zEnsure output completes.r,   N)r   r,   rM   rJ   r5   r5   r6   Ú__del__Ÿ   s    zProgressBar.__del__)Nr   Nr	   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r7   r   r8   r;   r>   rK   rS   rT   r5   r5   r5   r6   r      s       ÿ
/
r   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )rG   c                    s"   t t| ƒjdd� d| _|| _d S )NT)Údaemon)ÚsuperrG   r7   rN   Ú_mne_pb)r-   Úpb©Ú	__class__r5   r6   r7   §   s    z_UpdateThread.__init__c                 C   s*   | j r&| j | jj ¡ ¡ t d¡ q d S )Ng¡?)rN   r[   r   r(   rL   ÚtimeÚsleeprJ   r5   r5   r6   Úrun¬   s    z_UpdateThread.run)rU   rV   rW   r7   ra   Ú__classcell__r5   r5   r]   r6   rG   ¥   s   rG   c                   @   s   e Zd Zdd„ Zdd„ ZdS )r<   c                 C   s   |j | _|| _d S )N)r(   Úmmapr=   )r-   r\   r=   r5   r5   r6   r7   ´   s    z_PBSubsetUpdater.__init__c                 C   s   d| j | j|d  < d S )NTr   )rc   r=   )r-   Úiir5   r5   r6   r   ¸   s    z_PBSubsetUpdater.updateN)rU   rV   rW   r7   r   r5   r5   r5   r6   r<   ²   s   r<   )rX   Úcollections.abcr   rB   Úos.pathÚpathr@   r*   r$   Ú	threadingr   r_   ÚnumpyrD   Úcheckr   Úconfigr   Z_loggingr   Úobjectr   rG   r<   r5   r5   r5   r6   Ú<module>   s    