Skip to content

AnnData functions

Need the anndata extra. See AnnData / scanpy.

umap

umap(
    adata: AnnData,
    *,
    n_components: int = 2,
    min_dist: float = 0.5,
    spread: float = 1.0,
    neighbors_key: str = "neighbors",
    key_added: str | None = None,
    device: str = "cpu",
    seed: int = 42,
    verbose: int = 0,
    copy: bool = False,
    **kwargs: Any,
) -> AnnData | None

UMAP on the sc.pp.neighbors graph, stored the way sc.tl.umap does.

The neighbour graph is taken from obsp rather than searched again, so n_neighbors and the metric are whatever sc.pp.neighbors used. The representation (use_rep / n_pcs) is read from the same uns[neighbors_key]["params"]; it feeds the initialisation.

Parameters:

Name Type Description Default
adata AnnData

Annotated data matrix, after sc.pp.neighbors.

required
n_components int

Output dimensionality.

2
min_dist float

How tightly points may pack. Fits the repulsion curve with spread.

0.5
spread float

Scale of the embedding relative to min_dist.

1.0
neighbors_key str

Where sc.pp.neighbors stored its results in uns.

'neighbors'
key_added str | None

Store under obsm[key_added] and uns[key_added] instead of obsm["X_umap"] and uns["umap"].

None
device str

"cpu" for manifolds_rs.UMAP, "gpu" for manifolds_rs.UMAPGpu.

'cpu'
seed int

Fixes the initialisation and the negative sampling.

42
verbose int

0 silent, 1 normal, 2 detailed.

0
copy bool

Return a modified copy instead of writing into adata.

False
**kwargs Any

Any other constructor argument of the chosen estimator, e.g. n_epochs, init, optim_params. Unknown names raise.

{}

Returns:

Type Description
AnnData | None

The copy if copy, else None. Sets obsm["X_umap" | key_added]

AnnData | None

and uns["umap" | key_added]["params"].

Raises:

Type Description
ValueError

If sc.pp.neighbors has not run, its graph is ragged, or the representation is missing or sparse.

tsne

tsne(
    adata: AnnData,
    n_pcs: int | None = None,
    *,
    use_rep: str | None = None,
    perplexity: float = 30.0,
    approx: str = "barnes_hut",
    key_added: str | None = None,
    device: str = "cpu",
    seed: int = 42,
    verbose: int = 0,
    copy: bool = False,
    **kwargs: Any,
) -> AnnData | None

t-SNE on a representation, stored the way sc.tl.tsne does.

Searches its own 3 * perplexity neighbours; sc.pp.neighbors is not used.

Parameters:

Name Type Description Default
adata AnnData

Annotated data matrix.

required
n_pcs int | None

Leading columns of the representation to use. None uses all.

None
use_rep str | None

None for obsm["X_pca"], "X" for adata.X, or any other obsm key. Must be dense.

None
perplexity float

Effective neighbourhood size.

30.0
approx str

"barnes_hut", "qd", "fft", "fft_3k", or "fft_3k_gpu" with device="gpu".

'barnes_hut'
key_added str | None

Store under obsm[key_added] and uns[key_added] instead of obsm["X_tsne"] and uns["tsne"].

None
device str

"cpu" for manifolds_rs.TSNE, "gpu" for manifolds_rs.TSNEGpu.

'cpu'
seed int

Fixes the initialisation.

42
verbose int

0 silent, 1 normal, 2 detailed.

0
copy bool

Return a modified copy instead of writing into adata.

False
**kwargs Any

Any other constructor argument of the chosen estimator, e.g. n_epochs, learning_rate, ann, metric. Unknown names raise.

{}

Returns:

Type Description
AnnData | None

The copy if copy, else None. Sets obsm["X_tsne" | key_added]

AnnData | None

and uns["tsne" | key_added]["params"].

Raises:

Type Description
ValueError

If the representation is missing or sparse, or approx is not available on device.