U
    Åmœd®=  ã                   @   s$  d dl Z d dlZ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
 d dlmZmZmZmZmZ d dlmZmZmZ d	d
lm	Z	 d	dl	mZmZmZ d	dlmZ ddddddœZeee ¡ ƒƒD ]\ZZeee< qÈG dd„ deƒZ ee!ee"ee"df f dœdd„Z#G dd„ dƒZ$e$ƒ Z%dS )é    N)Úcontextmanager)ÚIntEnum)ÚPath)Útime)ÚgetLevelName)ÚAnyÚUnionÚOptionalÚIterableÚTextIO)ÚTupleÚListÚContextManageré   )Úlogging)Ú_set_log_levelÚ_set_log_fileÚ_RootLogger)ÚLiteralÚERRORÚWARNINGÚINFOZHINTÚDEBUG)ÚerrorÚwarningÚinfoÚhintÚdebugc                   @   sJ   e Zd ZdZdZdZdZdZee	dœdd„ƒZ
ed ed  d	œd
d„ƒZdS )Ú	Verbosityr   r   é   é   é   ©Úreturnc                 C   s   t t|  ƒS )N)r   Ú_VERBOSITY_TO_LOGLEVEL©Úself© r'   úI/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scanpy/_settings.pyÚlevel"   s    zVerbosity.level)Ú	verbosityr#   c                 c   s   |t _| V  | t _dS )z/        Temporarily override verbosity
        N)Úsettingsr*   )r&   r*   r'   r'   r(   Úoverride'   s    zVerbosity.overrideN)Ú__name__Ú
__module__Ú__qualname__r   Úwarnr   r   r   ÚpropertyÚintr)   r   r   r,   r'   r'   r'   r(   r      s   r   .)ÚvarÚvarnameÚtypesc                 C   sb   t | |ƒrd S t |tƒr |j}n,dd„ |D ƒ}d d |d d… ¡|d ¡}t|› d|› ƒ‚d S )Nc                 S   s   g | ]
}|j ‘qS r'   )r-   )Ú.0Útr'   r'   r(   Ú
<listcomp>7   s     z_type_check.<locals>.<listcomp>z{} or {}z, éÿÿÿÿz must be of type )Ú
isinstanceÚtyper-   ÚformatÚjoinÚ	TypeError)r3   r4   r5   Zpossible_types_strZ
type_namesr'   r'   r(   Ú_type_check1   s    

 ÿr?   c                   @   s´  e Zd ZdZdddddddd	d
dddddddddddœeeeeeeeeef eeef eeef eeef eedf eeedf ee eeedœdd„Z	e
edœdd„ƒZejeeeef dœdd„ƒZe
edœdd„ƒZejedœdd„ƒZe
edœdd „ƒZejed!œd"d „ƒZe
edœd#d$„ƒZejed%œd&d$„ƒZe
edœd'd(„ƒZejed)œd*d(„ƒZe
edœd+d,„ƒZejed-œd.d,„ƒZe
edœd/d0„ƒZejeeef d1œd2d0„ƒZe
edœd3d4„ƒZejeeef d5œd6d4„ƒZe
edœd7d8„ƒZejeeef d9œd:d8„ƒZe
edœd;d<„ƒZejeeef d=œd>d<„ƒZe
ee dœd?d@„ƒZejee dAœdBd@„ƒZe
eeef dœdCdD„ƒZejeeef dEœdFdD„ƒZe
edœdGdH„ƒZejedIœdJdH„ƒZe
ee dœdKdL„ƒZejeeedf dMœdNdL„ƒZe
edœdOdP„ƒZejeeeedf dQœdRdP„ƒZe
e e dœdSdT„ƒZ!e!jee dUœdVdT„ƒZ!e"dW Z#dceeeeeeee ee e#ee eed\œd]d^„Z$e%d_d`„ ƒZ&edœdadb„Z'dS )dÚScanpyConfigz#    Config manager for scanpy.
    r   Ú Úh5adÚpdfFTz./write/z./cache/z./data/z
./figures/Úlzfé   r   N)zN/AZdontknowZno_gateú?é2   )r*   Úplot_suffixÚfile_format_dataÚfile_format_figsÚautosaveÚautoshowÚwritedirÚcachedirÚ
datasetdirÚfigdirÚcache_compressionÚ
max_memoryÚn_jobsÚlogfileÚcategories_to_ignoreÚ_frameonÚ_vector_friendlyÚ_low_resolution_warningÚn_pcs)r*   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rT   rU   rV   rW   rX   c                C   s˜   t tjƒ| _|| _|| _|| _|| _|| _|| _	|| _
|| _|| _|	| _|
| _|| _|| _|| _|| _|| _|| _|| _tƒ | _| j| _d| _|| _d S )Nr9   )r   r   r   Z_root_loggerrT   r*   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rU   rV   rW   rX   r   Ú_startZ_previous_timeZ_previous_memory_usageZN_PCS)r&   r*   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r'   r'   r(   Ú__init__C   s0    zScanpyConfig.__init__r"   c                 C   s   | j S )a-  
        Verbosity level (default `warning`)

        Level 0: only show 'error' messages.
        Level 1: also show 'warning' messages.
        Level 2: also show 'info' messages.
        Level 3: also show 'hint' messages.
        Level 4: also show very detailed progress for 'debug'ging.
        )Ú
_verbosityr%   r'   r'   r(   r*      s    zScanpyConfig.verbosity)r*   c                 C   sœ   dd„ t D ƒ}t|tƒr || _nht|tƒr6t|ƒ| _nRt|tƒrx| ¡ }||krftd|› d|› ƒ‚qˆt| |¡ƒ| _nt	|dttfƒ t
| t | j ƒ d S )Nc                 S   s   g | ]}t |tƒr|‘qS r'   )r:   Ústr)r6   Úvr'   r'   r(   r8      s    
 z*ScanpyConfig.verbosity.<locals>.<listcomp>zCannot set verbosity to z. Accepted string values are: r*   )r$   r:   r   r\   r2   r]   ÚlowerÚ
ValueErrorÚindexr?   r   )r&   r*   Zverbosity_str_optionsr'   r'   r(   r*   Ž   s     ÿ


ÿc                 C   s   | j S )z3Global suffix that is appended to figure filenames.)Ú_plot_suffixr%   r'   r'   r(   rH   ¤   s    zScanpyConfig.plot_suffix)rH   c                 C   s   t |dtƒ || _d S )NrH   )r?   r]   rb   )r&   rH   r'   r'   r(   rH   ©   s    c                 C   s   | j S )z¬File format for saving AnnData objects.

        Allowed are 'txt', 'csv' (comma separated value file) for exporting and 'h5ad'
        (hdf5) for lossless saving.
        )Ú_file_format_datar%   r'   r'   r(   rI   ®   s    zScanpyConfig.file_format_data)Úfile_formatc                 C   s<   t |dtƒ dddh}||kr2td|› d|› ƒ‚|| _d S )NrI   ÚtxtÚcsvrB   zCannot set file_format_data to z. Must be one of )r?   r]   r`   rc   )r&   rd   Zfile_format_optionsr'   r'   r(   rI   ·   s    
ÿc                 C   s   | j S )zŸFile format for saving figures.

        For example 'png', 'pdf' or 'svg'. Many other formats work as well (see
        `matplotlib.pyplot.savefig`).
        )Ú_file_format_figsr%   r'   r'   r(   rJ   Â   s    zScanpyConfig.file_format_figs)Úfigure_formatc                 C   s   t |dtƒ || _d S )NZfigure_format_data)r?   r]   rg   )r&   rh   r'   r'   r(   rJ   Ë   s    c                 C   s   | j S )z¡        Automatically save figures in :attr:`~scanpy._settings.ScanpyConfig.figdir` (default `False`).

        Do not show plots/figures interactively.
        )Ú	_autosaver%   r'   r'   r(   rK   Ð   s    zScanpyConfig.autosave)rK   c                 C   s   t |dtƒ || _d S )NrK   )r?   Úboolri   )r&   rK   r'   r'   r(   rK   Ù   s    c                 C   s   | j S )z        Automatically show figures if `autosave == False` (default `True`).

        There is no need to call the matplotlib pl.show() in this case.
        )Ú	_autoshowr%   r'   r'   r(   rL   Þ   s    zScanpyConfig.autoshow)rL   c                 C   s   t |dtƒ || _d S )NrL   )r?   rj   rk   )r&   rL   r'   r'   r(   rL   ç   s    c                 C   s   | j S )zP        Directory where the function scanpy.write writes to by default.
        )Ú	_writedirr%   r'   r'   r(   rM   ì   s    zScanpyConfig.writedir)rM   c                 C   s   t |dttfƒ t|ƒ| _d S )NrM   )r?   r]   r   rl   )r&   rM   r'   r'   r(   rM   ó   s    c                 C   s   | j S )zB        Directory for cache files (default `'./cache/'`).
        )Ú	_cachedirr%   r'   r'   r(   rN   ø   s    zScanpyConfig.cachedir)rN   c                 C   s   t |dttfƒ t|ƒ| _d S )NrN   )r?   r]   r   rm   )r&   rN   r'   r'   r(   rN   ÿ   s    c                 C   s   | j S )zU        Directory for example :mod:`~scanpy.datasets` (default `'./data/'`).
        )Ú_datasetdirr%   r'   r'   r(   rO     s    zScanpyConfig.datasetdir)rO   c                 C   s"   t |dttfƒ t|ƒ ¡ | _d S )NrO   )r?   r]   r   Úresolvern   )r&   rO   r'   r'   r(   rO     s    c                 C   s   | j S )zG        Directory for saving figures (default `'./figures/'`).
        )Ú_figdirr%   r'   r'   r(   rP     s    zScanpyConfig.figdir)rP   c                 C   s   t |dttfƒ t|ƒ| _d S )NrP   )r?   r]   r   rp   )r&   rP   r'   r'   r(   rP     s    c                 C   s   | j S )z|        Compression for `sc.read(..., cache=True)` (default `'lzf'`).

        May be `'lzf'`, `'gzip'`, or `None`.
        )Ú_cache_compressionr%   r'   r'   r(   rQ     s    zScanpyConfig.cache_compression)rQ   c                 C   s"   |dkrt d|› dƒ‚|| _d S )N>   NÚgziprD   z`cache_compression` (z") must be in {'lzf', 'gzip', None})r`   rq   )r&   rQ   r'   r'   r(   rQ   %  s
    
ÿc                 C   s   | j S )z_        Maximal memory usage in Gigabyte.

        Is currently not well respected....
        )Ú_max_memoryr%   r'   r'   r(   rR   .  s    zScanpyConfig.max_memory)rR   c                 C   s   t |dttfƒ || _d S )NrR   )r?   r2   Úfloatrs   )r&   rR   r'   r'   r(   rR   7  s    c                 C   s   | j S )zL        Default number of jobs/ CPUs to use for parallel computing.
        )Ú_n_jobsr%   r'   r'   r(   rS   <  s    zScanpyConfig.n_jobs)rS   c                 C   s   t |dtƒ || _d S )NrS   )r?   r2   ru   )r&   rS   r'   r'   r(   rS   C  s    c                 C   s   | j S )z4        The file path `logfile` was set to.
        )Ú_logpathr%   r'   r'   r(   ÚlogpathH  s    zScanpyConfig.logpath)rw   c                 C   s.   t |dttfƒ t|ƒ d¡| _t|ƒ| _d S )NrT   Úa)r?   r]   r   ÚopenrT   rv   )r&   rw   r'   r'   r(   rw   O  s    c                 C   s   | j S )aX          The open file to write logs to.

        Set it to a :class:`~pathlib.Path` or :class:`str` to open a new one.
        The default `None` corresponds to :obj:`sys.stdout` in jupyter notebooks
        and to :obj:`sys.stderr` otherwise.

        For backwards compatibility, setting it to `''` behaves like setting it to `None`.
        )Ú_logfiler%   r'   r'   r(   rT   V  s    zScanpyConfig.logfile)rT   c                 C   sF   t |dƒs|r|| _n,|s.|  ¡ r(tjntj}|| _d | _t| ƒ d S )NÚwrite)	Úhasattrrw   Ú_is_run_from_ipythonÚsysÚstdoutÚstderrrz   rv   r   )r&   rT   r'   r'   r(   rT   c  s    c                 C   s   | j S )z=        Categories that are omitted in plotting etc.
        )Ú_categories_to_ignorer%   r'   r'   r(   rU   n  s    z!ScanpyConfig.categories_to_ignore)rU   c                 C   s8   t |ƒ}t|ƒD ]\}}t|d|› dtƒ q|| _d S )Nzcategories_to_ignore[ú])ÚlistÚ	enumerater?   r]   r   )r&   rU   ÚiÚcatr'   r'   r(   rU   u  s    )ZpngZjpgZtifZtiffrC   ZpsZepsÚsvgZsvgzZpgfÚrawZrgbaéP   é–   é   Úpng2x)ÚscanpyÚdpiÚdpi_saveÚframeonÚvector_friendlyÚfontsizeÚfigsizeÚ	color_mapr<   Ú	facecolorÚtransparentÚipython_formatc                 C   sÂ   |   ¡ r,ddl}t|tƒr |g}|jj|Ž  ddlm} || _|	| _	|dk	rT||d< |dk	rd||d< |dk	rt||d< |
dk	rŒ|
|d< |
|d< |r¨d	d
l
m} |||d |dk	r¸||d< || _dS )u©          Set resolution/size, styling and format of figures.

        Parameters
        ----------
        scanpy
            Init default values for :obj:`matplotlib.rcParams` suited for Scanpy.
        dpi
            Resolution of rendered figures â€“ this influences the size of figures in notebooks.
        dpi_save
            Resolution of saved figures. This should typically be higher to achieve
            publication quality.
        frameon
            Add frames and axes labels to scatter plots.
        vector_friendly
            Plot scatter plots using `png` backend even when exporting as `pdf` or `svg`.
        fontsize
            Set the fontsize for several `rcParams` entries. Ignored if `scanpy=False`.
        figsize
            Set plt.rcParams['figure.figsize'].
        color_map
            Convenience method for setting the default color map. Ignored if `scanpy=False`.
        format
            This sets the default format for saving figures: `file_format_figs`.
        facecolor
            Sets backgrounds via `rcParams['figure.facecolor'] = facecolor` and
            `rcParams['axes.facecolor'] = facecolor`.
        transparent
            Save figures with transparent back ground. Sets
            `rcParams['savefig.transparent']`.
        ipython_format
            Only concerns the notebook/IPython environment; see
            :func:`~IPython.display.set_matplotlib_formats` for details.
        r   N)ÚrcParamsz
figure.dpizsavefig.dpizsavefig.transparentzfigure.facecolorzaxes.facecolorr   )Úset_rcParams_scanpy)r’   r”   zfigure.figsize)r}   ÚIPythonr:   r]   ÚdisplayZset_matplotlib_formatsZ
matplotlibr˜   rW   rJ   Zplotting._rcmodr™   rV   )r&   r   rŽ   r   r   r‘   r’   r“   r”   r<   r•   r–   r—   rš   r˜   r™   r'   r'   r(   Úset_figure_params‰  s.    1
zScanpyConfig.set_figure_paramsc                  C   s   ddl } t| ddƒS )z.Determines whether we're currently in IPython.r   NZ__IPYTHON__F)ÚbuiltinsÚgetattr)r   r'   r'   r(   r}   Ö  s    z!ScanpyConfig._is_run_from_ipythonc                 C   s   d  dd„ t | ¡D ƒ¡S )NÚ
c                 s   s2   | ]*\}}|  d ¡s|dks|› d|›V  qdS )Ú_Úgetdocz = N)Ú
startswith)r6   Úkr^   r'   r'   r(   Ú	<genexpr>Þ  s   
 þz'ScanpyConfig.__str__.<locals>.<genexpr>)r=   ÚinspectÚ
getmembersr%   r'   r'   r(   Ú__str__Ý  s    
þzScanpyConfig.__str__)Tr‰   rŠ   TTr‹   NNrC   NFrŒ   )(r-   r.   r/   Ú__doc__r]   rj   r   r   r
   r[   r1   r   r*   Úsetterr2   rH   rI   rJ   rK   rL   rM   rN   rO   rP   r	   rQ   rt   rR   rS   rw   r   rT   r   rU   r   Z_Formatrœ   Ústaticmethodr}   r§   r'   r'   r'   r(   r@   >   s  ë




ì>

ÿ	            óóM
r@   )&r¥   r~   Ú
contextlibr   Úenumr   Úpathlibr   r   r   r   Útypingr   r   r	   r
   r   r   r   r   rA   r   r   r   Z_compatr   r$   r„   rƒ   Úvaluesr^   r)   r   r]   r;   r?   r@   r+   r'   r'   r'   r(   Ú<module>   s4   û
"   *