U
    vIÀdÜ#  ã                   @  sŒ  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 d dlmZmZmZmZmZmZ d dlmZ d dlZd d	lmZ d dlZd dlZG d
d„ dƒZeeee ej!ej"f Z#ee#ee#e#f ef Z$ej%Z&ej'Z(d dl)m*Z+ zd dl,m-Z. d dl/m%Z0 W n2 e1k
�r8   G dd„ dƒZ.G dd„ dƒZ0Y nX zd dl2Z2e2j-Z3W n$ e1k
�rp   G dd„ dƒZ3Y nX zd dl4m5Z5 W n$ e1k
�r¦   G dd„ dƒZ5Y nX zd dl6m-Z7 W n$ e1k
�rÜ   G dd„ dƒZ7Y nX eefddddœdd„ƒZ8e8 9ej:¡efd dddœd!d"„ƒZ;d#d$„ Z<d>d&d'd(d&d)œd*d+„Z=d&d&d,œd-d.„Z>d/d0œd1d2„Z?d3d4„ Z@dd5d6d7d8œd9d:„ZAd?d;dœddœd<d=„ZBdS )@é    )Úannotations)Údeepcopy)ÚreduceÚsingledispatchÚwraps©Údecode)Ú	signatureÚ	Parameter)ÚAnyÚTupleÚUnionÚMappingÚMutableMappingÚOptional)ÚwarnN)Úspmatrixc                   @  s   e Zd ZdS )ÚEmptyN)Ú__name__Ú
__module__Ú__qualname__© r   r   úP/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/anndata/compat/__init__.pyr      s   r   ©Úversion)ÚArray)ÚGroupc                   @  s   e Zd Zedd„ ƒZdS )Ú	ZarrArrayc                   C  s   dS )Nzmock zarr.core.Arrayr   r   r   r   r   Ú__repr__#   s    zZarrArray.__repr__N©r   r   r   Ústaticmethodr   r   r   r   r   r   "   s   r   c                   @  s   e Zd Zedd„ ƒZdS )Ú	ZarrGroupc                   C  s   dS )Nzmock zarr.core.Groupr   r   r   r   r   r   (   s    zZarrGroup.__repr__Nr   r   r   r   r   r!   '   s   r!   c                   @  s   e Zd Zedd„ ƒZdS )ÚAwkArrayc                   C  s   dS )Nzmock awkward.highlevel.Arrayr   r   r   r   r   r   5   s    zAwkArray.__repr__Nr   r   r   r   r   r"   4   s   r"   )Ú
ZappyArrayc                   @  s   e Zd Zedd„ ƒZdS )r#   c                   C  s   dS )Nzmock zappy.base.ZappyArrayr   r   r   r   r   r   ?   s    zZappyArray.__repr__Nr   r   r   r   r   r#   >   s   r#   c                   @  s   e Zd Zedd„ ƒZdS )Ú	DaskArrayc                   C  s   dS )Nzmock dask.array.core.Arrayr   r   r   r   r   r   I   s    zDaskArray.__repr__Nr   r   r   r   r   r$   H   s   r$   r   ÚstrzOptional[Any]©ÚattrsÚnameÚdefaultc                 C  s"   |t kr| | S | j||d�S d S )N)r)   )r   Úgetr&   r   r   r   Ú
_read_attrN   s    r+   zh5py.AttributeManagerc                 C  sv   || kr|t k	r|S | | }|  |¡}t |j¡}|dkr>|S |jdkrL|S t|jƒdkrd| d¡S dd„ |D ƒS dS )al  
    Read an HDF5 attribute and perform all necessary conversions.

    At the moment, this only implements conversions for string attributes, other types
    are passed through. String conversion is needed compatibility with other languages.
    For example Julia's HDF5.jl writes string attributes as fixed-size strings, which
    are read as bytes by h5py.
    Nr   úutf-8c                 S  s   g | ]}t |d ƒ‘qS )r,   r   )Ú.0Úsr   r   r   Ú
<listcomp>o   s     z#_read_attr_hdf5.<locals>.<listcomp>)	r   Zget_idÚh5pyÚcheck_string_dtypeÚdtypeÚlengthÚlenÚshaper   )r'   r(   r)   ÚattrZattr_idr2   r   r   r   Ú_read_attr_hdf5V   s    


r7   c                 C  s    g }| j jD ]ˆ}t|ƒ}|d }t|tƒ}|r6|d }tt  |¡jtjƒrfdt	|dd… ƒ› �|d< n |s~t 
t  |¡tj¡r†d|d< | t|ƒ¡ q|  |¡S )zw    Convert from fixed length strings to unicode.

    For backwards compatability with older h5ad and zarr files.
    é   r   ÚUé   NÚO)r2   ÚdescrÚlistÚ
isinstanceÚtupleÚ
issubclassÚnpÚtypeZstring_ÚintZ
issubdtypeZstr_ÚappendÚastype)ÚvalueÚ	new_dtypeÚdtZdt_listÚdt_typeZis_annotatedr   r   r   Ú_from_fixed_length_stringsr   s    
rJ   Fz
np.ndarrayzOptional[np.dtype]Úbool)Úarrr2   ÚcopyÚreturnc                 C  sx   |r|   ¡ } |dkr| j}t dd„ dd¡}|j ¡ D ]<\}\}}t |¡}|dk	r6|jdkr6|| | | | d� q6| S )a´  
    h5py 3.0 now reads all strings as bytes. There is a helper method which can convert these to strings,
    but there isn't anything for fields of structured dtypes.

    Params
    ------
    arr
        An array with structured dtype
    dtype
        dtype of the array. This is checked for h5py string data types.
        Passing this is allowed for cases where array may have been processed by another function before hand.
    Nc                 S  s
   |   d¡S )Nr,   r   )Úxr   r   r   Ú<lambda>ž   ó    z*_decode_structured_array.<locals>.<lambda>r8   r,   )Úout)	rM   r2   rA   Z
frompyfuncÚfieldsÚitemsr0   r1   Úencoding)rL   r2   rM   r   ÚkrH   Ú_Úcheckr   r   r   Ú_decode_structured_arrayŠ   s    
rY   )rF   rN   c                 C  sn   g }| j j ¡ D ]R\}\}}|jdkrTtdd„ |  d|¡D ƒƒ}| |d|ff¡ q| ||f¡ q|  |¡S )z•    Convert variable length strings to fixed length.

    Currently a workaround for
    https://github.com/zarr-developers/zarr-python/pull/422
    r;   c                 s  s   | ]}t | ¡ ƒV  qd S )N)r4   Úencode)r-   rO   r   r   r   Ú	<genexpr>±   s     z+_to_fixed_length_strings.<locals>.<genexpr>r9   )r2   rS   rT   ÚkindÚmaxZgetfieldrD   rE   )rF   rG   Zdt_namerI   Z	dt_offsetÚsizer   r   r   Ú_to_fixed_length_strings¦   s    
r_   z	'AnnData')Úadatac                 C  s²   t ƒ }| j ¡ D ]Š\}}| d¡s$q| dd¡}t|ttfƒrD|g}| j| j	fD ]H}||kr^qP|| j
}t |t|ƒk ¡s|qPtj ||¡||< | |¡ qPq|D ]}| j|= q dS )zå
    Compat function for when categorical keys were stored in uns.
    This used to be buggy because when storing categorical columns in obs and var with
    the same column name, only one `<colname>_categories` is retained.
    Z_categoriesÚ N)ÚsetÚunsrT   ÚendswithÚreplacer>   r%   rC   ZobsÚvarÚvaluesrA   Úallr4   ÚpdZCategoricalZ
from_codesÚadd)r`   Zk_to_deleteZ	cats_nameZcatsr(   ÚannÚcodesr   r   r   Ú
_clean_uns½   s"    

rm   c                 C  s‚   |   di ¡  di ¡}|  di ¡}dD ]X}||kr$t|| ttjfƒr$t|| jƒdkr$td|› d|› d�t	ƒ | 
|¡||< q$d	S )
zF
    Read-time fix for moving adjacency matrices from uns to obsp
    rc   Z	neighborsÚobsp)Z	distancesZconnectivitiesr:   z'Moving element from .uns['neighbors']['z'] to .obsp['z4'].

This is where adjacency matrices should go now.N)r*   Ú
setdefaultr>   r   rA   Úndarrayr4   r5   r   ÚFutureWarningÚpop)ÚdÚnrn   rV   r   r   r   Ú_move_adj_mtxØ   s    ÿþýýru   rC   r?   r=   )rs   rt   ÚkeysÚpathsc                 C  s^   |   ¡ D ]P\}}t|tƒr0t||||f˜|ƒ qt|tƒr|j||fkr| ||f˜¡ q|S )z0Find paths to sparse matrices with shape (n, n).)rT   r>   r   Ú_find_sparse_matricesr   r5   rD   )rs   rt   rv   rw   rV   Úvr   r   r   rx   í   s    
rx   z1.0 (renaming of 0.25)c                  s    ‡ fdd„}| dk	r|| ƒS |S )av  Decorator for methods that issues warnings for positional arguments.
    Using the keyword-only argument syntax in pep 3102, arguments after the
    * will issue a warning when passed as a positional argument.

    Parameters
    ----------
    func
        Function to check arguments on.
    version
        The version when positional arguments will result in error.
    c                   sr   t ˆƒ‰g ‰g ‰ ˆj ¡ D ]6\}}|jtjkr:ˆ  |¡ q|jtjkrˆ |¡ qtˆƒ‡ ‡‡‡‡fdd„ƒ}|S )Nc                    s†   t | ƒt ˆ ƒ }|dkr"ˆ| |ŽS dd„ tˆd |… | | d … ƒD ƒ}d |¡}td|› dˆ› d�tƒ | tˆj| ƒ¡ ˆf |ŽS )Nr   c                 S  s   g | ]\}}d   ||¡‘qS )z{}={})Úformat)r-   r(   Úargr   r   r   r/     s   ÿzi_deprecate_positional_args.<locals>._inner_deprecate_positional_args.<locals>.inner_f.<locals>.<listcomp>z, zPass z as keyword args. From version z> passing these as positional arguments will result in an error)r4   ÚzipÚjoinr   rq   ÚupdateÚ
parameters)ÚargsÚkwargsÚ
extra_argsZargs_msg)Úall_argsÚfÚkwonly_argsÚsigr   r   r   Úinner_f  s    
þ
ýzU_deprecate_positional_args.<locals>._inner_deprecate_positional_args.<locals>.inner_f)	r	   r   rT   r\   r
   ÚPOSITIONAL_OR_KEYWORDrD   ÚKEYWORD_ONLYr   )r„   r(   Úparamr‡   r   )rƒ   r„   r…   r†   r   Ú _inner_deprecate_positional_args  s    zD_deprecate_positional_args.<locals>._inner_deprecate_positional_argsNr   )Úfuncr   r‹   r   r   r   Ú_deprecate_positional_argsù   s    !r�   )NF)N)CÚ
__future__r   rM   r   Ú	functoolsr   r   r   Úcodecsr   Úinspectr	   r
   Útypingr   r   r   r   r   r   Úwarningsr   r0   Zscipy.sparser   ÚnumpyrA   Zpandasri   r   ÚslicerC   r%   Zint64rp   ZIndex1DÚIndexr   ZH5GroupZDatasetZH5ArrayÚ	packagingr   Ú_vZ	zarr.corer   r   Zzarr.hierarchyr!   ÚImportErrorZawkwardr"   Z
zappy.baser#   Z
dask.arrayr$   r+   ÚregisterZAttributeManagerr7   rJ   rY   r_   rm   ru   rx   r�   r   r   r   r   Ú<module>   sb    

ÿ   ÿ