U
    Xe;M                     @   s   d dl Z d dl mZ d dlZ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mZ d dlmZmZ d	i fd
dZd	i ddd fddZd	i dd fddZd	i ddd fddZd	i dddd fddZdS )    N)warn)TruncatedSVD)SpectralEmbedding)pairwise_distances)_VALID_METRICS)pairwise_special_metricSPECIAL_METRICS)SPARSE_SPECIAL_METRICSsparse_named_distancesZ	euclideanc                 C   s<  | dkrt jj||fdd S t j|| jd ft jd}|dkrt j||ft jd}|dd}	|	dkrpt j}	n,|	d	krt j}	n|	d
krt j	}	nt
d|	 t|D ]T}
| ||
k }t|
d |D ]4}|	|dd||kf }|||
|f< ||||
f< qqn
t|D ]}| ||k jdd||< qtj|rDtd | }|tkr^t|||d}n|tkr|t|t| |d}nt|rtj| rdd tttt @ D }z|| }W n tk
r   tdY nX t|fd|i|}nt|fd|i|}t |d  }t|d|d|}||  }|S )a  Provide a layout relating the separate connected components. This is done
    by taking the centroid of each component and then performing a spectral embedding
    of the centroids.

    Parameters
    ----------
    data: array of shape (n_samples, n_features)
        The source data -- required so we can generate centroids for each
        connected component of the graph.

    n_components: int
        The number of distinct components to be layed out.

    component_labels: array of shape (n_samples)
        For each vertex in the graph the label of the component to
        which the vertex belongs.

    dim: int
        The chosen embedding dimension.

    metric: string or callable (optional, default 'euclidean')
        The metric used to measure distances among the source data points.

    metric_kwds: dict (optional, default {})
        Keyword arguments to be passed to the metric function.
        If metric is 'precomputed', 'linkage' keyword can be used to specify
        'average', 'complete', or 'single' linkage. Default is 'average'

    Returns
    -------
    component_embedding: array of shape (n_components, dim)
        The ``dim``-dimensional embedding of the ``n_components``-many
        connected components.
    Nsize      $@   ZdtypeZprecomputedlinkageZaverageZcompleteZsinglezPUnrecognized linkage '%s'. Please choose from 'average', 'complete', or 'single'r   ZaxiszlForcing component centroids to dense; if you are running out of memory then consider increasing n_neighbors.)metrickwdsc                 S   s   i | ]}t | |qS  )r
   ).0kr   r   ,lib/python3.8/site-packages/umap/spectral.py
<dictcomp>r   s    z$component_layout.<locals>.<dictcomp>zPMulticomponent layout for custom sparse metrics is not implemented at this time.r      )n_componentsZaffinityrandom_state)nprandomemptyshapefloat64zerosgetZmeanmaxmin
ValueErrorrangescipysparseZ
isspmatrixr   Ztoarrayr   r   r	   callablesetSKLEARN_PAIRWISE_VALID_METRICSr
   keysKeyErrorNotImplementedErrorr   Zexpr   fit_transform)datar   component_labelsdimr   r   metric_kwdsZcomponent_centroidsZdistance_matrixr   Zc_iZdm_iZc_jZdistlabelZfunction_to_name_mappingZmetric_nameZaffinity_matrixcomponent_embeddingr   r   r   component_layout   s    +



  r6   r           c                 C   s  t j|jd |ft jd}|d| kr>t| ||||||d}nLtt |d }t t |t 	||| fg}t 
|| gd| }t|D ]}| ||kddf  }|dd||kf  }t|| g|}||dk  d }|jd d| k s|jd |d krD|j| ||jd |fd	||  |||k< qtd|||||||	|
d
	}|t t | }||9 }|||  |||k< q|S )ah  Specialised layout algorithm for dealing with graphs with many connected components.
    This will first find relative positions for the components by spectrally embedding
    their centroids, then spectrally embed each individual connected component positioning
    them according to the centroid embeddings. This provides a decent embedding of each
    component while placing the components in good relative positions to one another.

    Parameters
    ----------
    data: array of shape (n_samples, n_features)
        The source data -- required so we can generate centroids for each
        connected component of the graph.

    graph: sparse matrix
        The adjacency matrix of the graph to be embedded.

    n_components: int
        The number of distinct components to be layed out.

    component_labels: array of shape (n_samples)
        For each vertex in the graph the label of the component to
        which the vertex belongs.

    dim: int
        The chosen embedding dimension.

    metric: string or callable (optional, default 'euclidean')
        The metric used to measure distances among the source data points.

    metric_kwds: dict (optional, default {})
        Keyword arguments to be passed to the metric function.

    init: string, either "random" or "tsvd"
        Indicates to initialize the eigensolver. Use "random" (the default) to
        use uniformly distributed random initialization; use "tsvd" to warm-start the
        eigensolver with singular vectors of the Laplacian associated to the largest
        singular values. This latter option also forces usage of the LOBPCG eigensolver;
        with the former, ARPACK's solver ``eigsh`` will be used for smaller Laplacians.

    tol: float, default chosen by implementation
        Stopping tolerance for the numerical algorithm computing the embedding.

    maxiter: int, default chosen by implementation
        Number of iterations the numerical algorithm will go through at most as it
        attempts to compute the embedding.

    Returns
    -------
    embedding: array of shape (n_samples, dim)
        The initial embedding of ``graph``.
    r   r   r   r   r3   g       @Nr7   r   ZlowZhighr   	r0   graphr2   r   r   r3   inittolmaxiter)r   r   r   Zfloat32r6   intZceilZhstackZeyer!   Zvstackr&   ZtocsrZtocscZtocoor   r$   uniform_spectral_layoutr#   abs)r0   r;   r   r1   r2   r   r   r3   r<   r=   r>   resultZmeta_embeddingr   baser4   Zcomponent_graphZ	distancesZ
data_ranger5   Z	expansionr   r   r   multi_component_layout   sX    @
"(	
rE   c                 C   s   t | |||||d||d	S )a  
    Given a graph compute the spectral embedding of the graph. This is
    simply the eigenvectors of the laplacian of the graph. Here we use the
    normalized laplacian.

    Parameters
    ----------
    data: array of shape (n_samples, n_features)
        The source data

    graph: sparse matrix
        The (weighted) adjacency matrix of the graph as a sparse matrix.

    dim: int
        The dimension of the space into which to embed.

    random_state: numpy RandomState or equivalent
        A state capable being used as a numpy random state.

    tol: float, default chosen by implementation
        Stopping tolerance for the numerical algorithm computing the embedding.

    maxiter: int, default chosen by implementation
        Number of iterations the numerical algorithm will go through at most as it
        attempts to compute the embedding.

    Returns
    -------
    embedding: array of shape (n_vertices, dim)
        The spectral embedding of the graph.
    r   r:   rA   )r0   r;   r2   r   r   r3   r=   r>   r   r   r   spectral_layout  s    )rG   c	           	      C   s   t | |||||d|||d
S )a	  Given a graph, compute the spectral embedding of the graph. This is
    simply the eigenvectors of the Laplacian of the graph. Here we use the
    normalized laplacian and a truncated SVD-based guess of the
    eigenvectors to "warm" up the eigensolver. This function should
    give results of similar accuracy to the spectral_layout function, but
    may converge more quickly for graph Laplacians that cause
    spectral_layout to take an excessive amount of time to complete.

    Parameters
    ----------
    data: array of shape (n_samples, n_features)
        The source data

    graph: sparse matrix
        The (weighted) adjacency matrix of the graph as a sparse matrix.

    dim: int
        The dimension of the space into which to embed.

    random_state: numpy RandomState or equivalent
        A state capable being used as a numpy random state.

    metric: string or callable (optional, default 'euclidean')
        The metric used to measure distances among the source data points.
        Used only if the multiple connected components are found in the
        graph.

    metric_kwds: dict (optional, default {})
        Keyword arguments to be passed to the metric function.
        If metric is 'precomputed', 'linkage' keyword can be used to specify
        'average', 'complete', or 'single' linkage. Default is 'average'.
        Used only if the multiple connected components are found in the
        graph.

    method: str (optional, default None, values either 'eigsh' or 'lobpcg')
        Name of the eigenvalue computation method to use to compute the spectral
        embedding. If left to None (or empty string), as by default, the method is
        chosen from the number of vectors in play: larger vector collections are
        handled with lobpcg, smaller collections with eigsh. Method names correspond
        to SciPy routines in scipy.sparse.linalg.

    tol: float, default chosen by implementation
        Stopping tolerance for the numerical algorithm computing the embedding.

    maxiter: int, default chosen by implementation
        Number of iterations the numerical algorithm will go through at most as it
        attempts to compute the embedding.

    Returns
    -------
    embedding: array of shape (n_vertices, dim)
        The spectral embedding of the graph.
    tsvd)
r0   r;   r2   r   r   r3   r<   methodr=   r>   rF   )	r0   r;   r2   r   r   r3   rI   r=   r>   r   r   r   tswspectral_layout=  s    @rJ   c
              
   C   s  |j d }
tjj|\}}|dkr<t| |||||||dS tt|j	dd
 }tjj|j d tjd}tjd| d|j d |j d }||| |  }tj|st|}|d }td| d tt|j d }t|tjjtjjfr|ntjj|d}|s(|j d d	k r$d
nd}zN|dkrN|j|j d |fd}n.|dkrlt||d|}ntd| d|tj| |dddf< |d
krtjjj||d||pdt|j d |	p|j d d d\}}np|dkrNt N tj t!ddd tjjj"|t|d|p&d|	p8d|j d  d\}}W 5 Q R X ntdt#|d| }|dd|f W S  tjjj$t!fk
r   t%d |j&dd|j d |fd  Y S X dS )!a	  General implementation of the spectral embedding of the graph, derived as
    a subset of the eigenvectors of the normalized Laplacian of the graph. The numerical
    method for computing the eigendecomposition is chosen through heuristics.

    Parameters
    ----------
    data: array of shape (n_samples, n_features)
        The source data

    graph: sparse matrix
        The (weighted) adjacency matrix of the graph as a sparse matrix.

    dim: int
        The dimension of the space into which to embed.

    random_state: numpy RandomState or equivalent
        A state capable being used as a numpy random state.

    metric: string or callable (optional, default 'euclidean')
        The metric used to measure distances among the source data points.
        Used only if the multiple connected components are found in the
        graph.

    metric_kwds: dict (optional, default {})
        Keyword arguments to be passed to the metric function.
        If metric is 'precomputed', 'linkage' keyword can be used to specify
        'average', 'complete', or 'single' linkage. Default is 'average'.
        Used only if the multiple connected components are found in the
        graph.

    init: string, either "random" or "tsvd"
        Indicates to initialize the eigensolver. Use "random" (the default) to
        use uniformly distributed random initialization; use "tsvd" to warm-start the
        eigensolver with singular vectors of the Laplacian associated to the largest
        singular values. This latter option also forces usage of the LOBPCG eigensolver;
        with the former, ARPACK's solver ``eigsh`` will be used for smaller Laplacians.

    method: string -- either "eigsh" or "lobpcg" -- or None
        Name of the eigenvalue computation method to use to compute the spectral
        embedding. If left to None (or empty string), as by default, the method is
        chosen from the number of vectors in play: larger vector collections are
        handled with lobpcg, smaller collections with eigsh. Method names correspond
        to SciPy routines in scipy.sparse.linalg.

    tol: float, default chosen by implementation
        Stopping tolerance for the numerical algorithm computing the embedding.

    maxiter: int, default chosen by implementation
        Number of iterations the numerical algorithm will go through at most as it
        attempts to compute the embedding.

    Returns
    -------
    embedding: array of shape (n_vertices, dim)
        The spectral embedding of the graph.
    r   r   r8   r   r   g      ?r   )Zseedi eigshlobpcgr   r   rH   )r   r   z6The init parameter must be either 'random' or 'tsvd': z is invalid.NZSMg-C6?   )whichZncvr=   Zv0r>   z+(?ms).*not reaching the requested toleranceerror)categorymessageactionF)Zlargestr=   r>   z1Method should either be None, 'eigsh' or 'lobpcg'zSpectral initialisation failed! The eigenvector solver
failed. This is likely due to too small an eigengap. Consider
adding some noise or jitter to your data.

Falling back to random initialisation!g      $r   r9   )'r   r'   r(   ZcsgraphZconnected_componentsrE   r   ZsqrtZasarraysumZsqueezeZidentityr    ZspdiagsZissparser#   r?   
isinstancer   	GeneratorZRandomStateZdefault_rngZnormalr   r/   r%   ZlinalgZnormrK   Zoneswarningscatch_warningsfilterwarningsUserWarningrL   ZargsortZArpackErrorr   r@   )r0   r;   r2   r   r   r3   r<   rI   r=   r>   Z	n_samplesr   labelsZsqrt_degIDLr   Znum_lanczos_vectorsgenXZeigenvaluesZeigenvectorsorderr   r   r   rA     s    D
   
"



	

rA   )rV   r   Znumpyr   Zscipy.sparser'   Zscipy.sparse.csgraphZsklearn.decompositionr   Zsklearn.manifoldr   Zsklearn.metricsr   Zsklearn.metrics.pairwiser   r+   Zumap.distancesr   r   Zumap.sparser	   r
   r6   rE   rG   rJ   rA   r   r   r   r   <module>   sL   	
 
{
;
S