U
    mdT                     @   s   d dl mZ d dlmZmZ d dlZd dlmZ ddlm	Z	m
Z
 ddlmZmZmZ ddlmZ dd	lmZ e	eed
dddeee ee eeef eeef eeef e
eee eeee dddZdS )    )version)OptionalUnionN)AnnData   )_doc_params	AnyRandom)_choose_representationdoc_use_rep	doc_n_pcs)settings)logging)r   use_rep        F	euclidean)metric)adatan_pcsr   
perplexityearly_exaggerationlearning_raterandom_stateuse_fast_tsnen_jobscopyr   returnc
             	   C   s  ddl }td}|	r|  n| } t| ||d}|dkr>tjn|}t||tjdk||||
d}|
dkrt	
|j}|t	
dkrd	|d
< ntd|j dt |dkr|sttd |rttd |r0z4ddlm} |f |}td ||d}W n( tk
r.   d}ttd Y nX |dkrdddlm} |f |}td ||}|| jd< ddd |||||
|d D i| jd< tjd|dd |	r| S dS ) a	      t-SNE [Maaten08]_ [Amir13]_ [Pedregosa11]_.

    t-distributed stochastic neighborhood embedding (tSNE) [Maaten08]_ has been
    proposed for visualizating single-cell data by [Amir13]_. Here, by default,
    we use the implementation of *scikit-learn* [Pedregosa11]_. You can achieve
    a huge speedup and better convergence if you install `Multicore-tSNE
    <https://github.com/DmitryUlyanov/Multicore-TSNE>`__ by [Ulyanov16]_, which
    will be automatically detected by Scanpy.

    Parameters
    ----------
    adata
        Annotated data matrix.
    {doc_n_pcs}
    {use_rep}
    perplexity
        The perplexity is related to the number of nearest neighbors that
        is used in other manifold learning algorithms. Larger datasets
        usually require a larger perplexity. Consider selecting a value
        between 5 and 50. The choice is not extremely critical since t-SNE
        is quite insensitive to this parameter.
    metric
        Distance metric calculate neighbors on.
    early_exaggeration
        Controls how tight natural clusters in the original space are in the
        embedded space and how much space will be between them. For larger
        values, the space between natural clusters will be larger in the
        embedded space. Again, the choice of this parameter is not very
        critical. If the cost function increases during initial optimization,
        the early exaggeration factor or the learning rate might be too high.
    learning_rate
        Note that the R-package "Rtsne" uses a default of 200.
        The learning rate can be a critical parameter. It should be
        between 100 and 1000. If the cost function increases during initial
        optimization, the early exaggeration factor or the learning rate
        might be too high. If the cost function gets stuck in a bad local
        minimum increasing the learning rate helps sometimes.
    random_state
        Change this to use different intial states for the optimization.
        If `None`, the initial state is not reproducible.
    n_jobs
        Number of jobs for parallel computation.
        `None` means using :attr:`scanpy._settings.ScanpyConfig.n_jobs`.
    copy
        Return a copy instead of writing to `adata`.

    Returns
    -------
    Depending on `copy`, returns or updates `adata` with the following fields.

    **X_tsne** : `np.ndarray` (`adata.obs`, dtype `float`)
        tSNE coordinates of data.
    r   Nzcomputing tSNE)r   r      )r   r   verboser   r   r   r   r   z0.24.0TZsquare_distanceszQResults for non-euclidean metrics changed in sklearn 0.24.0, while you are using .   a  In previous versions of scanpy, calling tsne with n_jobs > 1 would use MulticoreTSNE. Now this uses the scikit-learn version of TSNE by default. If you'd like the old behaviour (which is deprecated), pass 'use_fast_tsne=True'. Note, MulticoreTSNE is not actually faster anymore.ztArgument `use_fast_tsne` is deprecated, and support for MulticoreTSNE will be dropped in a future version of scanpy.)MulticoreTSNEz7    using the 'MulticoreTSNE' package by Ulyanov (2017)Zfloat64Fz?Could not import 'MulticoreTSNE'. Falling back to scikit-learn.)TSNEz    using sklearn.manifold.TSNEX_tsneparamsc                 S   s   i | ]\}}|d k	r||qS )N ).0kvr&   r&   K/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/scanpy/tools/_tsne.py
<dictcomp>   s    ztsne.<locals>.<dictcomp>)r   r   r   r   r   r   tsnez    finishedz1added
    'X_tsne', tSNE coordinates (adata.obsm))timedeep)sklearnlogginfor   r	   r   r   dict	verbosityr   parse__version__warningswarnUserWarningFutureWarningr"   Zfit_transformZastypeImportErrorZsklearn.manifoldr#   ZobsmitemsZuns)r   r   r   r   r   r   r   r   r   r   r   r/   startXZparams_sklearnZsklearn_versionr#   r,   r$   r&   r&   r*   r,      s    E









 
r,   )	NNr   r   r   r   FNF)	packagingr   typingr   r   r6   Zanndatar   _utilsr   r   Ztools._utilsr	   r
   r   Z	_settingsr    r   r0   intstrfloatboolr,   r&   r&   r&   r*   <module>   sB   
         


