U
    mdM	                     @   s   d Z ddlmZmZmZ ddlZddlmZ ddl	m
Z
 ddlZdddeejejef eejejef eej eejdd	d
ZdS )z3
Metrics which don't quite deserve their own file.
    )OptionalSequenceUnionN)is_categorical_dtype)	natsortedT)	normalize)orignewdatar   returnc                C   st  ddl m} |dk	r8t| tr&||  } t|tr8|| }t| t| } }t| t|ksbttt	
| j|jf}|| ||d}|r|jddddt	jf }t	j|||dkd}| jdkrdn| j}|jdkrd	n|j}	tj|tj||d
tj||	d
d}
t| r t| jj}ntt| }t|rHt|jj}ntt|}|
jt	|t	|f }
|
S )a      Given an original and new set of labels, create a labelled confusion matrix.

    Parameters `orig` and `new` can either be entries in data or categorical arrays
    of the same size.

    Params
    ------
    orig
        Original labels.
    new
        New labels.
    data
        Optional dataframe to fill entries from.
    normalize
        Should the confusion matrix be normalized?


    Examples
    --------

    .. plot::

        import scanpy as sc; import seaborn as sns
        pbmc = sc.datasets.pbmc68k_reduced()
        cmtx = sc.metrics.confusion_matrix("bulk_labels", "louvain", pbmc.obs)
        sns.heatmap(cmtx)

    r   )confusion_matrixN)labels   )Zaxis)wherezOriginal labelsz
New Labels)name)indexcolumns)Zsklearn.metricsr   
isinstancestrpdSerieslenAssertionErroruniquenpZconcatenatevaluessumZnewaxisdivider   	DataFrameIndexr   cat
categoriesr   locarray)r   r	   r
   r   Z_confusion_matrixZunique_labelsZmtxZsums	orig_namenew_nameZdfZorig_idxZnew_idx r&   P/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scanpy/metrics/_metrics.pyr      s8    $



r   )N)__doc__typingr   r   r   Zpandasr   Zpandas.api.typesr   Znatsortr   numpyr   r   Zndarrayr   boolr   r&   r&   r&   r'   <module>   s    