U
    Xe'                     @   s   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	ej
dddd Ze
dd	d
 Ze
ddd Ze
 dd Zej
dddd Zdd ZdddZdd Zdd ZdS )    N)warn)check_is_fittedT)Zparallelc                 C   sX   t j| jd |ft jd}t| jd D ](}| | jdd}|d| }|||< q*|S )a  A fast computation of knn indices.

    Parameters
    ----------
    X: array of shape (n_samples, n_features)
        The input data to compute the k-neighbor indices of.

    n_neighbors: int
        The number of nearest neighbors to compute for each sample in ``X``.

    Returns
    -------
    knn_indices: array of shape (n_samples, n_neighbors)
        The indices on the ``n_neighbors`` closest points in the dataset.
    r   dtypeZ	quicksort)ZkindN)npemptyshapeZint32numbaprangeZargsort)Xn_neighborsZknn_indicesrowv r   )lib/python3.8/site-packages/umap/utils.pyfast_knn_indices   s    
r   z	i4(i8[:])c                 C   s   | d d@ d> d@ | d d> d@ | d A d? A | d< | d d@ d	> d@ | d d
> d@ | d A d? A | d< | d
 d@ d> d@ | d
 d> d@ | d
 A d? A | d
< | d | d A | d
 A S )zA fast (pseudo)-random number generator.

    Parameters
    ----------
    state: array of int64, shape (3,)
        The internal state of the rng

    Returns
    -------
    A (pseudo)-random int32 value
    r   l       l             l             l             r   )stater   r   r   tau_rand_int(   s    r   z	f4(i8[:])c                 C   s   t | }tt|d S )a  A fast (pseudo)-random number generator for floats in the range [0,1]

    Parameters
    ----------
    state: array of int64, shape (3,)
        The internal state of the rng

    Returns
    -------
    A (pseudo)-random float32 in the interval [0, 1]
    i)r   absfloat)r   Zintegerr   r   r   tau_randB   s    r    c                 C   s2   d}t | jd D ]}|| | d 7 }qt|S )zCompute the (standard l2) norm of a vector.

    Parameters
    ----------
    vec: array of shape (dim,)

    Returns
    -------
    The l2 norm of vec.
    g        r   r   )ranger   r   Zsqrt)Zvecresultir   r   r   normS   s    r$   c                 C   s^   | j \}}tj||f| jd}t|D ]0}t|D ] }| ||||f f |||f< q6q(|S )a  Return a submatrix given an orginal matrix and the indices to keep.

    Parameters
    ----------
    dmat: array, shape (n_samples, n_samples)
        Original matrix.

    indices_col: array, shape (n_samples, n_neighbors)
        Indices to keep. Each row consists of the indices of the columns.

    n_neighbors: int
        Number of neighbors.

    Returns
    -------
    submat: array, shape (n_samples, n_neighbors)
        The corresponding submatrix.
    r   )r   r   Zzerosr   r	   r
   )ZdmatZindices_colr   Zn_samples_transformZn_samples_fitZsubmatr#   jr   r   r   	submatrixe   s    
 r&   c                   C   s   t t   S )N)timectimer   r   r   r   ts   s    r)   c                 C   sT   |   }tjdd t|j|jD td}|| | }tj||||dd|d  S )a  Find the unique elements of a sparse csr matrix.
    We don't explicitly construct the unique matrix leaving that to the user
    who may not want to duplicate a massive array in memory.
    Returns the indices of the input array that give the unique values.
    Returns the indices of the unique array that reconstructs the input array.
    Returns the number of times each unique row appears in the input matrix.

    matrix: a csr matrix
    return_index = bool, optional
        If true, return the row indices of 'matrix'
    return_inverse: bool, optional
        If true, return the indices of the unique array that can be
           used to reconstruct 'matrix'.
    return_counts = bool, optional
        If true, returns the number of times each unique item appears in 'matrix'

    The unique matrix can computed via
    unique_matrix = matrix[index]
    and the original matrix reconstructed via
    unique_matrix[inverse]
    c                 S   s   g | ]\}}t || qS r   )tuple).0xyr   r   r   
<listcomp>   s     zcsr_unique.<locals>.<listcomp>r   )return_indexreturn_inversereturn_countsr   )Ztolilr   Zasarrayziprowsdataobjectunique)Zmatrixr/   r0   r1   Z
lil_matrixr3   Zreturn_valuesr   r   r   
csr_unique   s    " r7   c                 C   sT   t | d | jr4t| j| j jdd dk}nt| jjdd dk}|S )aB  
    Returns a boolean vector indicating which vertices are disconnected from the umap graph.
    These vertices will often be scattered across the space and make it difficult to focus on the main
    manifold.  They can either be filtered and have UMAP re-run or simply filtered from the interactive plotting tool
    via the subset_points parameter.
    Use ~disconnected_vertices(model) to only plot the connected points.
    Parameters
    ----------
    model: a trained UMAP model

    Returns
    -------
    A boolean vector indicating which points are disconnected
    graph_r   )Zaxisr   )r   r6   r   Zarrayr8   Z_unique_inverse_sumZflatten)ZmodelZvertices_disconnectedr   r   r   disconnected_vertices   s    
 r:   c                 C   sL   t j| \}}}t|}tj||d}|| }tt|rHtd |S )a  Calculate the average distance to each points nearest neighbors.

    Parameters
    ----------
    dist_matrix: a csr_matrix
        A distance matrix (usually umap_model.graph_)

    Returns
    -------
    An array with the average distance to each points nearest neighbors

    )ZweightszwEmbedding contains disconnected vertices which will be ignored.Use umap.utils.disconnected_vertices() to identify them.)scipyZsparsefindr   ZbincountanyZisnanr   )Zdist_matrixZrow_idxZcol_idxvalZcount_non_zero_elemsZsum_non_zero_elemsZaveragesr   r   r   average_nn_distance   s    
r?   )TTT)r'   warningsr   Znumpyr   r	   Zsklearn.utils.validationr   Zscipy.sparser;   Znjitr   r   r    r$   r&   r)   r7   r:   r?   r   r   r   r   <module>   s&   







!