U
    Xe                     @   sn  d dl Zd dlZd dlmZ zd 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Zd dlZd dlZd dlmZ d dlmZ d dlZd dlm  mZ  W n& e!k
r   ed e!ddY nX d dl"Z#d dl$Z#d dl%Z#d dl&m'Z' d dl(m)Z)m*Z* d dlm+Z, d dlm-Z-m.Z. d d	l/m0Z0 d d
l1m2Z2m3Z3 d dlm+Z4 d dlmZ ej5j67dej8Z9ej5j67dej:Z;ej5j67dej<Z=ej5j6j7dej>dd ddZ?ej5j67dej@ZAejBjCe9dd ejBjCe;dd ejBjCe=dd ejBjCe?dd ejBjCeAdd dd ZDeEddgdd ZFeEddgdd ZGeEddgdd ZHdddddd d!dd"dd#d!dd"dd$d%d&d'dd(d)d&d'dd*d+d&d'ddddddddddddddddd,	ZIeJd-d.d/d0d1gZKd2d3 ZLd4d5 ZMd6d7 ZNd8d9 ZOd:d; ZPejQd<d=d>d? ZRd@dA ZSdBdC ZTd[dGdHZUd\dIdJZVdKdL Z+d]dMdNZWd^dOdPZXd_dSdTZYd`dVdWZZdadYdZZ[dS )b    N)warna  The umap.plot package requires extra plotting libraries to be installed.
    You can install these via pip using

    pip install umap-learn[plot]

    or via conda using

     conda install pandas matplotlib datashader bokeh holoviews colorcet scikit-image
    ziumap.plot requires pandas matplotlib datashader bokeh holoviews scikit-image and colorcet to be installedPatch)	submatrixaverage_nn_distance)show)output_fileoutput_notebook)column)CustomJS	TextInputfiredarkblue	darkgreendarkred      )colorsNZ
darkpurple)namec                 C   s   dd | D S )Nc                 S   s   g | ]}t j|qS  
matplotlibr   Zto_hex.0cr   r   (lib/python3.8/site-packages/umap/plot.py
<listcomp>J   s     z_to_hex.<locals>.<listcomp>r   )Zarrr   r   r   _to_hexI   s    r   zuint8(uint32)c                 C   s   | d@ d? S )Ni      r   xr   r   r   _redM   s    r"   c                 C   s   | d@ d? S )Ni      r   r    r   r   r   _greenR   s    r$   c                 C   s   | d@ S )N   r   r    r   r   r   _blueW   s    r&   Zrainbowblack)cmapcolor_key_cmap
background	edge_cmapviridisSpectralZgrayinfernoBluesZtab20whitegray_rZRedsZtab20bZGreensZtab20c)	r   r,   r.   ZblueZredZgreenr   r   r   pcaicavq	local_dimneighborhoodc                 C   s,   t | dr| jS t | dr | jS tdd S )N
embedding_	embeddingz1Could not find embedding attribute of umap_object)hasattrr7   r8   
ValueErrorumap_objectr   r   r   _get_embedding   s
    

r=   c                 C   s   t | dr| jS dS d S )Nmetric	euclidean)r9   r>   r;   r   r   r   _get_metric   s    
r@   c                 C   s   t | dr| jS i S d S )N_metric_kwds)r9   rA   r;   r   r   r   _get_metric_kwds   s    
rB   c                 C   s:   | j d d d }tt|t|t|g}|| |S )N)datanpZdstackr&   r$   r"   Zimshow)Zdatashader_imageaxZimg_revZmpl_imgr   r   r   _embed_datashader_in_an_axis   s    
rG   c                 C   s   t | drj| jrjtj| j}t||d d d |f }t|||}t	|}t|||}t|||}n&tj
dtjd}| jj| j|d\}}||fS )N_small_data   Zdtypek)r9   rH   sklearnmetricspairwise_distances	_raw_datarE   Zargpartitionr   Zargsortemptyint64Z_knn_search_indexquery)r<   
nhood_sizeZdmatindicesZdmat_shortenedZindices_sortedZdistsZ	rng_stater   r   r   _nhood_search   s    

rV   F)Znopythonc                 C   s|   t | jd }t| jd D ]X}t j| | || ddjd }t t | | || gjd }t|t| ||< q|S )z*Compute Jaccard index of two neighborhoodsr   T)Zassume_unique)rE   rQ   shaperangeZintersect1duniqueZhstackfloat)Zindices_leftZindices_rightresultiZintersection_sizeZ
union_sizer   r   r   _nhood_compare   s    "r]   c              	   C   s   t | dddf }t | dddf }t | dddf }t | dddf }t |d||   t |d||   t |d||   t |d||   f}|S )z2Compute bounds on a space with appropriate paddingNr      g?)rE   ZnanminZnanmaxround)pointsZmin_xZmax_xZmin_yZmax_yextentr   r   r   _get_extent   s    rb   c                 C   sf   | dkrd}nT|  dr^tdd | dd | dd | dd	 fD }|d
krXd}qbd}nd}|S )Nr'   r0   #c                 S   s   g | ]}t d | qS )Z0x)intr   r   r   r   r      s     z&_select_font_color.<locals>.<listcomp>r^   rI         ~   )
startswithrE   Zmean)r*   
font_colorZmean_valr   r   r   _select_font_color   s    
*rj      Tr%   c                    s"  t | }tj||	|d |d f|d |d fd}tj| dd}d}|dk	rV|jd | jd kr~td	|jd | jd t||d
< |j	|ddt
d
d} dkr|dkrtj|d|d}n dkr*t|}|jd }tt|tdd|  fddt|D }tj| d|d}n( fdd  D }tj| d|d}nz|dk	r|jd | jd krtd|jd | jd t|}|jd dkr:t|t| }}|| d }tt|| | tj|d< |j	|ddt
dd}tt|tddd tj| d|d}nht||d< |j	|ddt
dd}tt|tdd|jd }tt|| tj| d|d}n,|j	|ddt d}tj|t||d}|dk	rt||}|dk	rt|| |
r|dk	r|j|d |S |S dS )zUse datashader to plot pointsr   r^      rI   Z
plot_widthZplot_heightZx_rangeZy_ranger!   ycolumnsN?Labels must have a label for each sample (size mismatch: {} {})labelr!   ro   Zaggeq_hist)howalphac                    s    g | ]\}}t  | |d qS )Z	facecolorrs   r   r   r\   rL   	color_keyr   r   r   +  s   z%_datashade_points.<locals>.<listcomp>)r{   rv   rw   c                    s   g | ]}t  | |d qS rx   r   r   rL   rz   r   r   r   3  s    ?Values must have a value for each sample (size mismatch: {} {})r   g     o@val_cat)r(   rw   Zhandles) rb   dsCanvaspd	DataFramerW   r:   formatCategoricalr`   	count_cattfshaderE   rY   r   pltget_cmaplinspace	enumeratekeysminmaxr_   astypeZint16dictzipcountset_backgroundrG   legend)r`   rF   labelsvaluesr(   r{   r)   r*   widthheightshow_legendrw   ra   canvasrD   legend_elementsZaggregationr[   unique_labels
num_labelsZunique_valuesmin_valZmax_valZbin_sizeZcolor_key_colsr   rz   r   _datashade_points   s    
 



   
   

 
      


r   c                    sj  dt | jd  }d}|dkrLtjd }tj|| |	| fd}|d}|| |dk	r|jd | jd krtd	|jd | jd  dkrt 
|jd }t|t dd|  fd	d
tD }t trt| }t 
| fdd
D }nbt 
|t jd k r<td fddtD } fdd
tD }t||}|j| dddf | dddf |||d n|dk	r|jd | jd krtd	|jd | jd |j| dddf | dddf ||||d n8t|d}|j| dddf | dddf ||d |
rf|dk	rf|j|d |S )zUse matplotlib to plot points      Y@r   N
figure.dpifigsizeo   rr   r^   c                    s$   g | ]\}}t  | | d qS rx   r   ry   r{   r   r   r   r     s   z&_matplotlib_points.<locals>.<listcomp>c                    s   g | ]}t  | |d qS rx   r   r|   rz   r   r   r     s    :Color key must have enough colors for the number of labelsc                    s"   i | ]\}}|t j | qS r   r   ry   rz   r   r   
<dictcomp>  s    z&_matplotlib_points.<locals>.<dictcomp>c                    s    g | ]\}}t  | |d qS rx   r   ry   rz   r   r   r     s   sr   rw   r}   )r   r   r(   rw         ?)r   r   r   )rE   sqrtrW   r   rcParamsfigureadd_subplotZset_facecolorr:   r   rY   r   r   r   
isinstancer   r   Seriesmaplenscatterr   )r`   rF   r   r   r(   r{   r)   r*   r   r   r   rw   
point_sizer   dpifigr   r   new_color_keycolorr   r   r   _matplotlib_pointsk  s|    



 






.
      *r   c                 C   sZ   t | tjrt  nBt | tjr*t|  n,t | tjj	j
rNtt| dd ntddS )zDisplay a plot, either interactive or static.

    Parameters
    ----------
    plot_to_show: Output of a plotting command (matplotlib axis or bokeh figure)
        The plot to show

    Returns
    -------
    None
    bokeh)Zbackendz>The type of ``plot_to_show`` was not valid, or not understood.N)r   r   ZAxesshow_staticbplr   show_interactivehvcoreZspacesZ
DynamicMapZrenderr:   )Zplot_to_showr   r   r   r     s    
r   c                 C   s  |dk	r,t | d }t | d }t | d }|dk	rD|dk	rDtd|dk	rjd|  kr`dksjn td|dkrzt| }|dk	rt||jd	 krtd
t||jd	 || }|dk	r|| }|dk	r|| }|jd dkrtdt|}|dkr*tjd }tj	|	| |
| fd}|
d}|jd	 |	|
 d krbt|||||||||	|
||}n6|dk	rv|d }nd}t|||||||||	|
||}|jg g d t| dkr|jdddt| | j| j|jd|d n$|jddd| j| j|jd|d |S )a  Plot an embedding as points. Currently this only works
    for 2D embeddings. While there are many optional parameters
    to further control and tailor the plotting, you need only
    pass in the trained/fit umap model to get results. This plot
    utility will attempt to do the hard work of avoiding
    over-plotting issues, and make it easy to automatically
    colour points by a categorical labelling or numeric values.

    This method is intended to be used within a Jupyter
    notebook with ``%matplotlib inline``.

    Parameters
    ----------
    umap_object: trained UMAP object
        A trained UMAP object that has a 2D embedding.

    points: array, shape (n_samples, dim) (optional, default None)
        An array of points to be plotted. Usually this is None
        and so the original embedding points of the umap_object
        are used. However points can be passed explicitly instead
        which is useful for points manually transformed.

    labels: array, shape (n_samples,) (optional, default None)
        An array of labels (assumed integer or categorical),
        one for each data sample.
        This will be used for coloring the points in
        the plot according to their label. Note that
        this option is mutually exclusive to the ``values``
        option.

    values: array, shape (n_samples,) (optional, default None)
        An array of values (assumed float or continuous),
        one for each sample.
        This will be used for coloring the points in
        the plot according to a colorscale associated
        to the total range of values. Note that this
        option is mutually exclusive to the ``labels``
        option.

    theme: string (optional, default None)
        A color theme to use for plotting. A small set of
        predefined themes are provided which have relatively
        good aesthetics. Available themes are:
           * 'blue'
           * 'red'
           * 'green'
           * 'inferno'
           * 'fire'
           * 'viridis'
           * 'darkblue'
           * 'darkred'
           * 'darkgreen'

    cmap: string (optional, default 'Blues')
        The name of a matplotlib colormap to use for coloring
        or shading points. If no labels or values are passed
        this will be used for shading points according to
        density (largely only of relevance for very large
        datasets). If values are passed this will be used for
        shading according the value. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    color_key: dict or array, shape (n_categories) (optional, default None)
        A way to assign colors to categoricals. This can either be
        an explicit dict mapping labels to colors (as strings of form
        '#RRGGBB'), or an array like object providing one color for
        each distinct category being provided in ``labels``. Either
        way this mapping will be used to color points according to
        the label. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    color_key_cmap: string (optional, default 'Spectral')
        The name of a matplotlib colormap to use for categorical coloring.
        If an explicit ``color_key`` is not given a color mapping for
        categories can be generated from the label list and selecting
        a matching list of colors from the given colormap. Note
        that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    background: string (optional, default 'white)
        The color of the background. Usually this will be either
        'white' or 'black', but any color name will work. Ideally
        one wants to match this appropriately to the colors being
        used for points etc. This is one of the things that themes
        handle for you. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    width: int (optional, default 800)
        The desired width of the plot in pixels.

    height: int (optional, default 800)
        The desired height of the plot in pixels

    show_legend: bool (optional, default True)
        Whether to display a legend of the labels

    subset_points: array, shape (n_samples,) (optional, default None)
        A way to select a subset of points based on an array of boolean
        values.

    ax: matplotlib axis (optional, default None)
        The matplotlib axis to draw the plot to, or if None, which is
        the default, a new axis will be created and returned.

    alpha: float (optional, default: None)
        The alpha blending value, between 0 (transparent) and 1 (opaque).

    Returns
    -------
    result: matplotlib axis
        The result is a matplotlib axis with the relevant plot displayed.
        If you are using a notebooks and have ``%matplotlib inline`` set
        then this will simply display inline.
    Nr(   r)   r*   ?Conflicting options; only one of labels or values should be set              ?'Alpha must be between 0 and 1 inclusiver   ESize of subset points ({}) does not match number of input points ({})r^   rl   8Plotting is currently only implemented for 2D embeddingsr   r   r   
   r%   ZxticksZyticksr?   Gz?{Gz?z,UMAP: metric={}, n_neighbors={}, min_dist={}right	transformZhorizontalalignmentr   !UMAP: n_neighbors={}, min_dist={})_themesr:   r=   r   rW   r   rj   r   r   r   r   r   r   setr@   textn_neighborsmin_dist	transAxes)r<   r`   r   r   themer(   r{   r)   r*   r   r   r   subset_pointsrF   rw   ri   r   r   r   r   r   r`     s      




   r`   c                 C   sX  |dk	r8t | d }t | d }	t | d }t | d }
t| }tj|dd}dt|jd	  }|d
krztt|}nd
}|rd}nd}| j	
 }tjt|j|j|jgjdd}|jtj|d< |jtj|d< t|}tj|||d	 |d
 f|d |d fd}|dkr*tj||dd}n2|dkrNtd tj||dd}ntd|tj|j|ddt ddt!"||d}t#||
}|rt$|d|||||	d||d}|d
krtj%|d|d}tj&||dd }n|}t'|
}t!j(d! }t!j)|| || fd"}|*d#}t+|| |j,g g d$ |j-d%d&d'| j.| j/|j0d(|d) |S )*a  Plot connectivity relationships of the underlying UMAP
    simplicial set data structure. Internally UMAP will make
    use of what can be viewed as a weighted graph. This graph
    can be plotted using the layout provided by UMAP as a
    potential diagnostic view of the embedding. Currently this only works
    for 2D embeddings. While there are many optional parameters
    to further control and tailor the plotting, you need only
    pass in the trained/fit umap model to get results. This plot
    utility will attempt to do the hard work of avoiding
    over-plotting issues and provide options for plotting the
    points as well as using edge bundling for graph visualization.

    Parameters
    ----------
    umap_object: trained UMAP object
        A trained UMAP object that has a 2D embedding.

    edge_bundling: string or None (optional, default None)
        The edge bundling method to use. Currently supported
        are None or 'hammer'. See the datashader docs
        on graph visualization for more details.

    edge_cmap: string (default 'gray_r')
        The name of a matplotlib colormap to use for shading/
        coloring the edges of the connectivity graph. Note that
        the ``theme``, if specified, will override this.

    show_points: bool (optional False)
        Whether to display the points over top of the edge
        connectivity. Further options allow for coloring/
        shading the points accordingly.

    labels: array, shape (n_samples,) (optional, default None)
        An array of labels (assumed integer or categorical),
        one for each data sample.
        This will be used for coloring the points in
        the plot according to their label. Note that
        this option is mutually exclusive to the ``values``
        option.

    values: array, shape (n_samples,) (optional, default None)
        An array of values (assumed float or continuous),
        one for each sample.
        This will be used for coloring the points in
        the plot according to a colorscale associated
        to the total range of values. Note that this
        option is mutually exclusive to the ``labels``
        option.

    theme: string (optional, default None)
        A color theme to use for plotting. A small set of
        predefined themes are provided which have relatively
        good aesthetics. Available themes are:
           * 'blue'
           * 'red'
           * 'green'
           * 'inferno'
           * 'fire'
           * 'viridis'
           * 'darkblue'
           * 'darkred'
           * 'darkgreen'

    cmap: string (optional, default 'Blues')
        The name of a matplotlib colormap to use for coloring
        or shading points. If no labels or values are passed
        this will be used for shading points according to
        density (largely only of relevance for very large
        datasets). If values are passed this will be used for
        shading according the value. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    color_key: dict or array, shape (n_categories) (optional, default None)
        A way to assign colors to categoricals. This can either be
        an explicit dict mapping labels to colors (as strings of form
        '#RRGGBB'), or an array like object providing one color for
        each distinct category being provided in ``labels``. Either
        way this mapping will be used to color points according to
        the label. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    color_key_cmap: string (optional, default 'Spectral')
        The name of a matplotlib colormap to use for categorical coloring.
        If an explicit ``color_key`` is not given a color mapping for
        categories can be generated from the label list and selecting
        a matching list of colors from the given colormap. Note
        that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    background: string (optional, default 'white)
        The color of the background. Usually this will be either
        'white' or 'black', but any color name will work. Ideally
        one wants to match this appropriately to the colors being
        used for points etc. This is one of the things that themes
        handle for you. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    width: int (optional, default 800)
        The desired width of the plot in pixels.

    height: int (optional, default 800)
        The desired height of the plot in pixels

    Returns
    -------
    result: matplotlib axis
        The result is a matplotlib axis with the relevant plot displayed.
        If you are using a notebook and have ``%matplotlib inline`` set
        then this will simply display inline.
    Nr(   r)   r+   r*   rn   rp   r   r   r^   logru   )sourcetargetweightr   r   rl   rI   rm   r   )r   ZhammerzYHammer edge bundling is expensive for large graphs!
This may take a long time to compute!z&{} is not a recognised bundling methodr!   ro   rt   )r(   rv   Fr   )Z	thresholdZmax_pxZover)rv   r   r   r   r   r   r   r   r   r   )1r   r=   r   r   rE   r   rW   rd   r_   graph_ZtocooZvstackrowcolrD   Tr   r   int32r   rb   r   r   bdZdirectly_connect_edgesr   Zhammer_bundler:   r   r   r   linesumr   r   r   r   Z	dynspreadstackrj   r   r   r   rG   r   r   r   r   r   )r<   Zedge_bundlingr+   Zshow_pointsr   r   r   r(   r{   r)   r*   r   r   r`   Zpoint_dfr   Zpx_sizeZedge_howZ	coo_graphZedge_dfra   r   ZedgesZedge_imgZ	point_imgr[   ri   r   r   rF   r   r   r   connectivity  s     






 r      皙?c
           "   	   C   s  t | }
|
jd dkrtd|dkr:dt|
jd  }|dkr\tjd }|dkr\|d	9 }t|}|dkr|d
krt }|	d}|dkr2t
jjdd| j}|t|8 }|tj|dd }|j|
dddf |
dddf ||dd |d |jddd| j| j|jd|d |jg g d nn|dkrt
jjdd| j}|t|8 }|tj|dd }|j|
dddf |
dddf ||dd |d |jddd| j| j|jd|d |jg g d n|dkrt
jjdd| j}t
j| j|j}|t|8 }|tj|dd }|j|
dddf |
dddf ||dd |d |jddd| j| j|jd|d |jg g d n|dkrt | |\}}t
j!"|
}|j#|
|d\}}t$|%tj&|%tj&}t'|d }t'|d!}|j|
dddf |
dddf |||||d" |d# |jddd| j| j|jd|d |jg g d t(j)j*||d$}t(j+j,||d%}|-| tj.||d& n|d'krt | | j\}}| j}tj/|jd tj0d(}t1|jd D ]@}t
j |||  }t2t3|j4|kd d ||< qt'|d }t'|d!}|j|
dddf |
dddf |||||d" |d) |jddd| j| j|jd|d |jg g d t(j)j*||d$}t(j+j,||d%}|-| tj.||d& n|d
krt5t6t7d* d }t6t7| d }tj8||d+d,d-\}} | j9} | t6t7d D ]}|:  qJt;| t7D ]\}}!t<| |!||d. d/ qdntd0d1=t>t7 d2 |S )3aJ  Provide a diagnostic plot or plots for a UMAP embedding.
    There are a number of plots that can be helpful for diagnostic
    purposes in understanding your embedding. Currently these are
    restricted to methods of coloring a scatterplot of the
    embedding to show more about how the embedding is representing
    the data. The first class of such plots uses a linear method
    that preserves global structure well to embed the data into
    three dimensions, and then interprets such coordinates as a
    color space -- coloring the points by their location in the
    linear global structure preserving embedding. In such plots
    one should look for discontinuities of colour, and consider
    overall global gradients of color. The diagnostic types here
    are ``'pca'``, ``'ica'``, and ``'vq'`` (vector quantization).

    The second class consider the local neighbor structure. One
    can either look at how well the neighbor structure is
    preserved, or how the estimated local dimension of the data
    varies. Both of these are available, although the local
    dimension estimation is the preferred option. You can
    access these are diagnostic types ``'local_dim'`` and
    ``'neighborhood'``.

    Finally the diagnostic type ``'all'`` will provide a
    grid of diagnostic plots.

    Parameters
    ----------
    umap_object: trained UMAP object
        A trained UMAP object that has a 2D embedding.

    diagnostic_type: str (optional, default 'pca')
        The type of diagnostic plot to show. The options are
           * 'pca'
           * 'ica'
           * 'vq'
           * 'local_dim'
           * 'neighborhood'
           * 'all'

    nhood_size: int (optional, default 15)
        The size of neighborhood to compare for local
        neighborhood preservation estimates.

    local_variance_threshold: float (optional, default 0.8)
        To estimate the local dimension we consider a PCA of
        the local neighborhood and estimate the dimension
        as that which provides ``local_variance_threshold``
        or more of the ``variance_explained_ratio``.

    ax: matplotlib axis (optional, default None)
        A matplotlib axis to plot to, or, if None, a new
        axis will be created and returned.

    cmap: str (optional, default 'viridis')
        The name of a matplotlib colormap to use for coloring
        points if the ``'local_dim'`` or ``'neighborhood'``
        option are selected.

    point_size: int (optional, None)
        If provided this will fix the point size for the
        plot(s). If None then a suitable point size will
        be estimated from the data.

    Returns
    -------
    result: matplotlib axis
        The result is a matplotlib axis with the relevant plot displayed.
        If you are using a notebook and have ``%matplotlib inline`` set
        then this will simply display inline.
    r^   rl   r   Nr   r   r   )r5   r6   g?allr   r2   rI   )Zn_components)axisgQ?r   z&Colored by RGB coords of PCA embeddingr   r   r   r   r   r   r3   z*Colored by RGB coords of FastICA embeddingr4   )Z
n_clustersz,Colored by RGB coords of Vector Quantizationr6   rK   re   _   )r   r   r(   vminvmaxz%Colored by neighborhood Jaccard index)r   r   )normr(   )rF   r5   rJ   z!Colored by approx local dimensionr   )r   r   T)r   Zconstrained_layoutg      @)diagnostic_typerF   r   z%Unknown diagnostic; should be one of z, z	 or "all")?r=   rW   r:   rE   r   r   r   rj   r   r   rM   ZdecompositionZPCAZfit_transformrP   r   r   r   Z	set_titler   r   r   r   r   r   ZFastICAZclusterZKMeansZfitrN   rO   Zcluster_centers_rV   Z	neighborsZKDTreerS   r]   r   r   Z
percentiler   r   Z	NormalizecmZScalarMappableZ	set_arrayZcolorbarrQ   rR   rX   whereZcumsumZexplained_variance_ratio_rd   r   _diagnostic_typesZsubplotsZflatremover   
diagnosticjoinlist)"r<   r   rT   Zlocal_variance_thresholdrF   r(   r   r*   r   r   r`   r   ri   r   Z
color_projZcolor_projectorZhighd_indicesZhighd_distsZtreeZ
lowd_distsZlowd_indicesZaccuracyr   r   r   ZmappablerD   r5   r\   r2   ZcolsZrowsZaxsZplt_typer   r   r   r     s`   S


,
 

,
 

 ,
 


 
	
 


	
 


r   ffffff?c           %         s6  |dk	r,t | d }t | d }t | d }	|dk	rD|dk	rDtd|dk	rjd|  kr`dksjn tdt| }|dk	rt||jd	 krtd
t||jd	 || }|jd dkrtd|dkrdt|jd	  }tj	t| dd}|dk	rt
||d<  dkrDt|}|jd	 }tt|td	d| t trft| |d< nPt|}t |jd	 k rtd fddt|D }t|||d< d}nj|dk	rt
||d< tt|td	dd}tjd|t|t|d}ntjt|d}|dk	rJ|| }|dk	rJ|| }|jd	 |
| d krd}d}|dk	ri }|D ]"}|| ||< d| d ||< q|t| }|dk	r|D ]}|jjdkrd} qڐq|dk	r||d < nd|d < t !|}t j"|
||sdn||dk	r"|nd!|	d"}|j#d#d$|||d d% d|j$_%d|j&_%|r2t'd&d'd(} |dkrg }|dk	r|(|j) |dk	r|*d t|d	krt+d) n2t,t||d| |d*d+d,}!| -d|! t.| |}nH|dk	rt+d- |r
t+d. |dk	rt+d/ t/0d0 t/j1d1d2 t/j23t/j2j4|	ddd3 |dk	rt/j5|d#d$gd4}"t6j7|"t89d t||
|d5}n|dk	r|j: }#|j: |# }$t;|j:|# |$d  |d6< t/j5|d#d$gd6gd7}"t6j7|"t89d6t||
|d8}n0t/j5|d#d$gd4}"t6j7|"t8< t||
|d8}|S )9a  Create an interactive bokeh plot of a UMAP embedding.
    While static plots are useful, sometimes a plot that
    supports interactive zooming, and hover tooltips for
    individual points is much more desirable. This function
    provides a simple interface for creating such plots. The
    result is a bokeh plot that will be displayed in a notebook.

    Note that more complex tooltips etc. will require custom
    code -- this is merely meant to provide fast and easy
    access to interactive plotting.

    Parameters
    ----------
    umap_object: trained UMAP object
        A trained UMAP object that has a 2D embedding.

    labels: array, shape (n_samples,) (optional, default None)
        An array of labels (assumed integer or categorical),
        one for each data sample.
        This will be used for coloring the points in
        the plot according to their label. Note that
        this option is mutually exclusive to the ``values``
        option.

    values: array, shape (n_samples,) (optional, default None)
        An array of values (assumed float or continuous),
        one for each sample.
        This will be used for coloring the points in
        the plot according to a colorscale associated
        to the total range of values. Note that this
        option is mutually exclusive to the ``labels``
        option.

    hover_data: DataFrame, shape (n_samples, n_tooltip_features)
    (optional, default None)
        A dataframe of tooltip data. Each column of the dataframe
        should be a Series of length ``n_samples`` providing a value
        for each data point. Column names will be used for
        identifying information within the tooltip.

    tools: List (optional, default None),
        Defines the tools to be configured for interactive plots.
        The list can be mixed type of string and tools objects defined by
        Bokeh like HoverTool. Default tool list Bokeh uses is
        ["pan","wheel_zoom","box_zoom","save","reset","help",].
        When tools are specified, and includes hovertool, automatic tooltip
        based on hover_data is not created.

    theme: string (optional, default None)
        A color theme to use for plotting. A small set of
        predefined themes are provided which have relatively
        good aesthetics. Available themes are:
           * 'blue'
           * 'red'
           * 'green'
           * 'inferno'
           * 'fire'
           * 'viridis'
           * 'darkblue'
           * 'darkred'
           * 'darkgreen'

    cmap: string (optional, default 'Blues')
        The name of a matplotlib colormap to use for coloring
        or shading points. If no labels or values are passed
        this will be used for shading points according to
        density (largely only of relevance for very large
        datasets). If values are passed this will be used for
        shading according the value. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    color_key: dict or array, shape (n_categories) (optional, default None)
        A way to assign colors to categoricals. This can either be
        an explicit dict mapping labels to colors (as strings of form
        '#RRGGBB'), or an array like object providing one color for
        each distinct category being provided in ``labels``. Either
        way this mapping will be used to color points according to
        the label. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    color_key_cmap: string (optional, default 'Spectral')
        The name of a matplotlib colormap to use for categorical coloring.
        If an explicit ``color_key`` is not given a color mapping for
        categories can be generated from the label list and selecting
        a matching list of colors from the given colormap. Note
        that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    background: string (optional, default 'white')
        The color of the background. Usually this will be either
        'white' or 'black', but any color name will work. Ideally
        one wants to match this appropriately to the colors being
        used for points etc. This is one of the things that themes
        handle for you. Note that if theme
        is passed then this value will be overridden by the
        corresponding option of the theme.

    width: int (optional, default 800)
        The desired width of the plot in pixels.

    height: int (optional, default 800)
        The desired height of the plot in pixels

    point_size: int (optional, default None)
        The size of each point marker

    subset_points: array, shape (n_samples,) (optional, default None)
        A way to select a subset of points based on an array of boolean
        values.

    interactive_text_search: bool (optional, default False)
        Whether to include a text search widget above the interactive plot

    interactive_text_search_columns: list (optional, default None)
        Columns of data source to search. Searches labels and hover_data by default.

    interactive_text_search_alpha_contrast: float (optional, default 0.95)
        Alpha value for points matching text search. Alpha value for points
        not matching text search will be 1 - interactive_text_search_alpha_contrast

    alpha: float (optional, default: None)
        The alpha blending value, between 0 (transparent) and 1 (opaque).

    Returns
    -------

    Nr(   r)   r*   r   r   r   r   r   r   r^   rl   r   r   rn   rp   rs   r   r   c                    s   i | ]\}}| | qS r   r   ry   rz   r   r   r     s      zinteractive.<locals>.<dictcomp>valuer   )ZlowZhighr   r   Tz@{}Z	HoverToolFrw   z'pan,wheel_zoom,box_zoom,save,reset,help)r   r   tooltipstoolsZbackground_fill_colorr!   ro   )r!   ro   r   r   sizerw    zSearch:)r   titlezinteractive_text_search_columns set to True, but no hover_data or labels provided.Please provide hover_data or labels to use interactive text search.)r   Zmatching_alphaZnon_matching_alphaZsearch_columnsa  
                    var data = source.data;
                    var text_search = cb_obj.value;
                    
                    var search_columns_dict = {}
                    for (var col in search_columns){
                        search_columns_dict[col] = search_columns[col]
                    }
                    
                    // Loop over columns and values
                    // If there is no match for any column for a given row, change the alpha value
                    var string_match = false;
                    for (var i = 0; i < data.x.length; i++) {
                        string_match = false
                        for (var j in search_columns_dict) {
                            if (String(data[search_columns_dict[j]][i]).includes(text_search) ) {
                                string_match = true
                            }
                        }
                        if (string_match){
                            data['alpha'][i] = matching_alpha
                        }else{
                            data['alpha'][i] = non_matching_alpha
                        }
                    }
                    source.change.emit();
                )argscodezbToo many points for hover data -- tooltips will notbe displayed. Sorry; try subsampling your data.zAToo many points for text search.Sorry; try subsampling your data.z6Alpha parameter will not be applied on holoviews plotsr   i,  )r   )ZbgcolorZxaxisZyaxis)kdims)
aggregatorr{   r(   r   r   r~   )r   Zvdims)r   r(   r   r   )=r   r:   r=   r   rW   r   rE   r   r   r   ZasarrayrY   r   r   r   r   r   r   r   r   r   btrZlinear_cmapr   r   r   r   Zrgb2hexr   items	__class____name__r   ZColumnDataSourcer   ZcircleZgridZvisibler   r   extendrq   appendr   r   Zjs_on_changer
   r   	extensionoutputZoptsdefaultsZRGBZPointshdZ	datashader   r   r   r   r   )%r<   r   r   Z
hover_datar   r   r(   r{   r)   r*   r   r   r   r   Zinteractive_text_searchZinteractive_text_search_columnsZ&interactive_text_search_alpha_contrastrw   r`   rD   r   r   r   r   Zpaletter   Ztooltip_neededZtooltip_dictZcol_nameZ_toolZdata_sourceZplotZ
text_inputcallbackZ
point_plotr   Z	val_ranger   rz   r   interactive  sF     





   







	



$





r     c                 C   sJ   t | j}|dkr$t }|d}|d |d |j||d |S )a  Create a histogram of the average distance to each points
    nearest neighbors.

    Parameters
    ----------
    umap_object: trained UMAP object
        A trained UMAP object that has an embedding.

    bins: int (optional, default 25)
        Number of bins to put the points into

    ax: matplotlib axis (optional, default None)
        A matplotlib axis to plot to, or, if None, a new
        axis will be created and returned.

    Returns
    -------

    Nr   z%Average distance to nearest neighborsZ	Frequency)bins)r   r   r   r   r   Z
set_xlabelZ
set_ylabelZhist)r<   r  rF   Znn_distancesr   r   r   r   nearest_neighbour_distribution:  s    



r  )NNNr/   Nr-   r0   rk   rk   Tr%   )NNNr/   Nr-   r0   rk   rk   TN)NNNNr/   Nr-   r0   rk   rk   TNNN)Nr1   FNNNr/   Nr-   r0   rk   rk   )	r2   r   r   Nr,   Nr0   rk   rk   )NNNNNr/   Nr-   r0   rk   rk   NNFNr   N)r  N)\ZnumpyrE   Znumbawarningsr   Zpandasr   Z
datashaderr   Zdatashader.transfer_functionsZtransfer_functionsr   Zdatashader.bundlingZbundlingr   Zmatplotlib.pyplotZpyplotr   ZcolorcetZmatplotlib.colorsr   Zmatplotlib.cmZbokeh.plottingZplottingr   Zbokeh.transformr   r  Z	holoviewsr   Zholoviews.operation.datashaderZ	operationr
  ImportErrorZsklearn.decompositionrM   Zsklearn.clusterZsklearn.neighborsZmatplotlib.patchesr   Z
umap.utilsr   r   r   r   r   r	   Zbokeh.layoutsr
   Zbokeh.modelsr   r   r   r   ZLinearSegmentedColormap	from_listr   Z	fire_cmapZkbcZdarkblue_cmapZkgyZdarkgreen_cmapZlinear_kry_5_95_c72Zdarkred_cmapZlinear_bmw_5_95_c89Zdarkpurple_cmapZ	colormapsregisterr   Z	vectorizer"   r$   r&   r   Zarrayr   r=   r@   rB   rG   rV   Zjitr]   rb   rj   r   r   r`   r   r   r  r  r   r   r   r   <module>   s       


9	

           
q           
^              
 x            
 e         
                   
  y