U
    Åmœd3  ã                   @   sx   d dl m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	Zd
Zddd„Zdee dœdd„Zddd„ZdS )é    )ÚOptionalNé   )Úloggingé   )Úpca)Úsettings)Ú_choose_graphu  use_rep
    Use the indicated representation. `'X'` or any key for `.obsm` is valid.
    If `None`, the representation is chosen automatically:
    For `.n_vars` < 50, `.X` is used, otherwise 'X_pca' is used.
    If 'X_pca' is not present, itâ€™s computed with default parameters.zIn_pcs
    Use this many PCs. If `n_pcs==0` use `.X` if `use_rep is None`.Fc                 C   sœ  t j}|rt jdkrdt _|d kr.|dkr.d}|d krò| jt jkràd| j ¡ kr¦|d k	rt|| jd jd krttdƒ‚| jd d d …d |…f }t 	d|jd › ¡ qðt 
d| j› d¡ t| jƒ}|d d …d |…f | jd< nt 	d	¡ | j}n || j ¡ krL|d k	rL|| j| jd kr0t|› d
ƒ‚| j| d d …d |…f }nF|| j ¡ krr|d krr| j| }n |dkr„| j}ntd |¡ƒ‚|t _|S )Nr   r   ÚXÚX_pcazL`X_pca` does not have enough PCs. Rerun `sc.pp.pca` with adjusted `n_comps`.ú    using 'X_pca' with n_pcs = u   Youâ€™re trying to run this on zŒ dimensions of `.X`, if you really want this, set `use_rep='X'`.
         Falling back to preprocessing with `sc.pp.pca` and default params.z     using data matrix X directlyzv does not have enough Dimensions. Provide a Representation with equal or more dimensions than`n_pcs` or lower `n_pcs` z@Did not find {} in `.obsm.keys()`. You need to compute it first.)r   Ú	verbosityZn_varsÚN_PCSÚobsmÚkeysÚshapeÚ
ValueErrorÚloggÚinfoÚwarningr   r	   Úformat)ÚadataZuse_repÚn_pcsZsilentr   r	   © r   úL/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scanpy/tools/_utils.pyÚ_choose_representation   sL    ÿÿ

ÿ
ÿÿr   )r   c                 C   s
  |dkrt  d¡ | jS |dkrRd|  ¡ krRt  d| jd jd › ¡ | jd S d|  ¡ krœ| jd jd |krœt  d|› ¡ | jd dd…d|…f S |dkrªtjn|}| jjd |kröt  d|› ¡ t  d¡ t	| j||d	}|| jd< |S t  d¡ | jS dS )
zË
    Parameters
    ----------
    n_pcs
        If `n_pcs=0`, do not preprocess with PCA.
        If `None` and there is a PCA version of the data, use this.
        If an integer, compute the PCA.
    r   z)    using data matrix X directly (no PCA)Nr
   r   r   z#    computing 'X_pca' with n_pcs = zavoid this by setting n_pcs = 0)Zn_compsÚrandom_state)
r   r   r	   Z	obsm_keysr   r   r   r   Úhintr   )r   r   r   r	   r   r   r   Úpreprocess_with_pcaG   s$    	

 


r   c                 C   s<  t j |¡ |d kr t| ||ƒ}d| jkr0d| jd kr0| j| jd d  }| jd d }| jd d }t  |jd df¡}t|ƒD ]¤\}	}
||j	j
|	 kj}||	  ¡ }t|d ƒdkr$||	 | }|d t  |¡ }t j t|| ƒdf¡}||	 ||  }|| }|
d|  | ||< qˆ|
||< qˆntd	ƒ‚|S )
NZpagaÚposÚgroupsÚconnectivitiesr   r   r   g      à?z9Plot PAGA first, so that adata.uns['paga']with key 'pos'.)ÚnpÚrandomÚseedr   ZunsZobsZonesr   Ú	enumerateÚcatÚ
categoriesÚvaluesZnonzeroÚlenZargmaxr   )r   Z	adjacencyr   Zneighbors_keyZobspr   r   Zconnectivities_coarseZinit_posÚiZ	group_posZsubsetZ	neighborsr    Znearest_neighborÚnoiseÚdistr   r   r   Úget_init_pos_from_pagaf   s.    ÿr,   )NNF)Nr   )Nr   NN)Útypingr   Únumpyr!   Ú r   r   Z_pcar   r   Ú_utilsr   Zdoc_use_repZ	doc_n_pcsr   Úintr   r,   r   r   r   r   Ú<module>   s   
0        ÿ