snapatac2.tl.umap#

snapatac2.tl.umap(adata, n_comps=2, use_dims=None, use_rep='X_spectral', key_added='umap', random_state=0, inplace=True, **kwargs)[source]#

Compute a UMAP embedding from an existing representation.

Use this function after computing a low-dimensional representation such as X_spectral. Pass a NumPy array directly when you want the embedding returned instead of written to an AnnData object.

Anti-Patterns#

  • Do NOT set inplace=True when passing a raw NumPy array; arrays cannot store .obsm results and the embedding is returned instead.

  • Do NOT pass cluster labels through key_added; this key names adata.obsm["X_" + key_added], not adata.obs.

param adata:

Annotated data object containing adata.obsm[use_rep], or a numeric matrix with shape (n_cells, n_features).

type adata:

AnnData | AnnDataSet | ndarray

param n_comps:

Number of UMAP dimensions to compute.

type n_comps:

int

param use_dims:

Dimensions from use_rep to use. If an integer, use the first use_dims columns; if a list, use those column indices; if None, use all columns.

type use_dims:

int | list[int] | None

param use_rep:

Key in adata.obsm containing the input representation.

type use_rep:

str

param key_added:

Suffix for the output key adata.obsm["X_" + key_added].

type key_added:

str

param random_state:

Random seed passed to umap.UMAP.

type random_state:

int | None

param inplace:

If True, store the embedding in adata.obsm; if False, return it.

type inplace:

bool

type **kwargs:

param **kwargs:

Additional keyword arguments passed to umap.UMAP.

returns:

If inplace=True and adata is an AnnData object, stores the embedding in adata.obsm["X_" + key_added] and returns None. Otherwise, returns the embedding with shape (n_cells, n_comps).

rtype:

ndarray | None

Examples

>>> import numpy as np
>>> import snapatac2 as snap
>>> X = np.random.default_rng(0).normal(size=(20, 5))
>>> embedding = snap.tl.umap(X, n_comps=2, inplace=False, n_neighbors=5)
>>> embedding.shape
(20, 2)