Skip to content

Embeddings

2D embeddings: UMAP, densMAP, t-SNE, den-SNE, PHATE and ForceAtlas2.

Start with umap on the neighbours from bv.graph.find_neighbours. Every function reads a cached embedding (the PCA by default), fits, and writes the coordinates to sc.cache.embeddings under slot_name ("umap", "densmap", "tsne", "densne", "phate", "fa2"), stamped as derived from the input embedding and, when the cached kNN fed it, from the kNN. Nothing is returned. All of them take a SingleCells, SingleCellsSubset or MetaCells; on a SingleCellsMultiModal, modality="wnn" takes the RNA embedding and the WNN kNN.

The numerics come from the manifolds-rs wheel, an optional extra: pip install bixverse[embed]. Importing bv.embed without it raises.

Parameters are the manifolds_rs estimators themselves. The *_defaults() builders return one set to the bixverse defaults, which differ from the estimators' own in places (t-SNE perplexity is 10, not 30). Tweak one with set_params and hand it over as params.

bv.graph.find_neighbours(sc)
bv.embed.umap(sc)
bv.embed.tsne(sc, params=bv.embed.tsne_defaults().set_params(perplexity=20.0))
umap_xy = sc.cache.embeddings["umap"]

UMAP and densMAP take optimiser="sgd_parallel", umap-learn's SGD run lock-free over all threads (Hogwild!). It is fast and not reproducible: the same seed gives a different embedding each run. n_epochs defaults to None, which is 500, or 200 for the SGD optimisers above 10,000 cells.

Gotchas. With use_knn=True (the default for UMAP, densMAP and ForceAtlas2) the cached kNN is reused and the neighbour search settings in params don't apply. A graph wider than n_neighbors is cut to it, with a warning on stderr; a narrower one is used as it is. With no kNN cached you get a warning and a fresh search. t-SNE, den-SNE and PHATE search afresh unless you ask for the cached kNN; if you do, keep its k near three times the perplexity for t-SNE, and at 6k + 5 (35 by default) for PHATE. A stale input raises, and so does a slot_name that is reserved (the PCA, kNN and so on). ForceAtlas2 lays out the fuzzy kNN graph; laying out a graph you supply is not possible.

Worked through in Analysing PBMCs with bixverse.

UMAP and densMAP

2D embeddings: UMAP, densMAP, t-SNE, den-SNE, PHATE and ForceAtlas2.

Start with umap on the neighbours from bv.graph.find_neighbours. Every function reads a cached embedding (the PCA by default), fits, and writes the coordinates to sc.cache.embeddings under slot_name ("umap", "densmap", "tsne", "densne", "phate", "fa2"), stamped as derived from the input embedding and, when the cached kNN fed it, from the kNN. Nothing is returned. All of them take a SingleCells, SingleCellsSubset or MetaCells; on a SingleCellsMultiModal, modality="wnn" takes the RNA embedding and the WNN kNN.

The numerics come from the manifolds-rs wheel, an optional extra: pip install bixverse[embed]. Importing bv.embed without it raises.

Parameters are the manifolds_rs estimators themselves. The *_defaults() builders return one set to the bixverse defaults, which differ from the estimators' own in places (t-SNE perplexity is 10, not 30). Tweak one with set_params and hand it over as params.

bv.graph.find_neighbours(sc)
bv.embed.umap(sc)
bv.embed.tsne(sc, params=bv.embed.tsne_defaults().set_params(perplexity=20.0))
umap_xy = sc.cache.embeddings["umap"]

UMAP and densMAP take optimiser="sgd_parallel", umap-learn's SGD run lock-free over all threads (Hogwild!). It is fast and not reproducible: the same seed gives a different embedding each run. n_epochs defaults to None, which is 500, or 200 for the SGD optimisers above 10,000 cells.

Gotchas. With use_knn=True (the default for UMAP, densMAP and ForceAtlas2) the cached kNN is reused and the neighbour search settings in params don't apply. A graph wider than n_neighbors is cut to it, with a warning on stderr; a narrower one is used as it is. With no kNN cached you get a warning and a fresh search. t-SNE, den-SNE and PHATE search afresh unless you ask for the cached kNN; if you do, keep its k near three times the perplexity for t-SNE, and at 6k + 5 (35 by default) for PHATE. A stale input raises, and so does a slot_name that is reserved (the PCA, kNN and so on). ForceAtlas2 lays out the fuzzy kNN graph; laying out a graph you supply is not possible.

umap

umap(
    sc: ScOrMc,
    *,
    use_knn: bool = True,
    embd_to_use: str = "pca",
    slot_name: str = "umap",
    no_embd_to_use: int | None = None,
    modality: Modality = "rna",
    params: UMAP | None = None,
) -> None

UMAP.

With use_knn the cached kNN (find_neighbours) is reused and the neighbour search knobs in params do not apply; without one cached, a warning and a search over the embedding. A cached kNN wider than n_neighbors is cut to it (manifolds-rs warns on stderr); a narrower one is used as it is.

optimiser="sgd_parallel" runs umap-learn's SGD lock-free over all threads (Hogwild!). Fast, but a fixed seed no longer gives the same embedding twice.

Parameters:

Name Type Description Default
sc ScOrMc

The experiment.

required
use_knn bool

Reuse the cached kNN.

True
embd_to_use str

The input embedding, "pca" or a name in sc.cache.embeddings.

'pca'
slot_name str

The name in sc.cache.embeddings to write.

'umap'
no_embd_to_use int | None

Number of leading dimensions to use. None for all.

None
modality Modality

The kNN and the slot: "rna", or "adt" / "atac" / "wnn" on a SingleCellsMultiModal. "wnn" reads the embedding from RNA and always uses the WNN kNN.

'rna'
params UMAP | None

The estimator. None is umap_defaults().

None

Raises:

Type Description
ValueError

If the embedding or the kNN is stale, the embedding is absent, or slot_name is reserved.

Examples:

>>> bv.embed.umap(sc)
>>> umap_params = bv.embed.umap_defaults().set_params(n_neighbors=30)
>>> bv.embed.umap(sc, slot_name="umap_30", params=umap_params)
>>> hogwild = bv.embed.umap_defaults().set_params(optimiser="sgd_parallel")
>>> bv.embed.umap(sc, slot_name="umap_sgd", params=hogwild)

umap_defaults

umap_defaults() -> UMAP

UMAP set to the package defaults.

n_epochs=None follows the optimiser: 500, or 200 for "sgd" and "sgd_parallel" above 10,000 cells, as umap-learn does.

densmap

densmap(
    sc: ScOrMc,
    *,
    use_knn: bool = True,
    embd_to_use: str = "pca",
    slot_name: str = "densmap",
    no_embd_to_use: int | None = None,
    modality: Modality = "rna",
    params: DensMAP | None = None,
) -> None

densMAP, UMAP with a density-preserving term.

Inputs are handled as in umap; lambda_=0 gives back plain UMAP.

Parameters:

Name Type Description Default
sc ScOrMc

The experiment.

required
use_knn bool

Reuse the cached kNN.

True
embd_to_use str

The input embedding.

'pca'
slot_name str

The name in sc.cache.embeddings to write.

'densmap'
no_embd_to_use int | None

Number of leading dimensions to use. None for all.

None
modality Modality

The kNN and the slot: "rna", or "adt" / "atac" / "wnn" on a SingleCellsMultiModal. "wnn" reads the embedding from RNA and always uses the WNN kNN.

'rna'
params DensMAP | None

The estimator. None is densmap_defaults().

None

Raises:

Type Description
ValueError

As umap.

References

Narayan, Berger & Cho, Nat Biotechnol, 2021.

densmap_defaults

densmap_defaults() -> DensMAP

densMAP set to the package defaults.

t-SNE and den-SNE

2D embeddings: UMAP, densMAP, t-SNE, den-SNE, PHATE and ForceAtlas2.

Start with umap on the neighbours from bv.graph.find_neighbours. Every function reads a cached embedding (the PCA by default), fits, and writes the coordinates to sc.cache.embeddings under slot_name ("umap", "densmap", "tsne", "densne", "phate", "fa2"), stamped as derived from the input embedding and, when the cached kNN fed it, from the kNN. Nothing is returned. All of them take a SingleCells, SingleCellsSubset or MetaCells; on a SingleCellsMultiModal, modality="wnn" takes the RNA embedding and the WNN kNN.

The numerics come from the manifolds-rs wheel, an optional extra: pip install bixverse[embed]. Importing bv.embed without it raises.

Parameters are the manifolds_rs estimators themselves. The *_defaults() builders return one set to the bixverse defaults, which differ from the estimators' own in places (t-SNE perplexity is 10, not 30). Tweak one with set_params and hand it over as params.

bv.graph.find_neighbours(sc)
bv.embed.umap(sc)
bv.embed.tsne(sc, params=bv.embed.tsne_defaults().set_params(perplexity=20.0))
umap_xy = sc.cache.embeddings["umap"]

UMAP and densMAP take optimiser="sgd_parallel", umap-learn's SGD run lock-free over all threads (Hogwild!). It is fast and not reproducible: the same seed gives a different embedding each run. n_epochs defaults to None, which is 500, or 200 for the SGD optimisers above 10,000 cells.

Gotchas. With use_knn=True (the default for UMAP, densMAP and ForceAtlas2) the cached kNN is reused and the neighbour search settings in params don't apply. A graph wider than n_neighbors is cut to it, with a warning on stderr; a narrower one is used as it is. With no kNN cached you get a warning and a fresh search. t-SNE, den-SNE and PHATE search afresh unless you ask for the cached kNN; if you do, keep its k near three times the perplexity for t-SNE, and at 6k + 5 (35 by default) for PHATE. A stale input raises, and so does a slot_name that is reserved (the PCA, kNN and so on). ForceAtlas2 lays out the fuzzy kNN graph; laying out a graph you supply is not possible.

tsne

tsne(
    sc: ScOrMc,
    *,
    use_knn: bool = False,
    embd_to_use: str = "pca",
    slot_name: str = "tsne",
    no_embd_to_use: int | None = None,
    modality: Modality = "rna",
    params: TSNE | None = None,
) -> None

t-SNE.

Off by default, use_knn reuses the cached kNN; perplexity then only shapes the affinities over it, so keep its k near 3 * perplexity.

Parameters:

Name Type Description Default
sc ScOrMc

The experiment.

required
use_knn bool

Reuse the cached kNN.

False
embd_to_use str

The input embedding.

'pca'
slot_name str

The name in sc.cache.embeddings to write.

'tsne'
no_embd_to_use int | None

Number of leading dimensions to use. None for all.

None
modality Modality

The kNN and the slot: "rna", or "adt" / "atac" / "wnn" on a SingleCellsMultiModal. "wnn" reads the embedding from RNA and always uses the WNN kNN.

'rna'
params TSNE | None

The estimator. None is tsne_defaults().

None

Raises:

Type Description
ValueError

As umap.

Examples:

>>> tsne_params = bv.embed.tsne_defaults().set_params(perplexity=20.0)
>>> bv.embed.tsne(sc, params=tsne_params)

tsne_defaults

tsne_defaults() -> TSNE

t-SNE set to the package defaults.

densne

densne(
    sc: ScOrMc,
    *,
    use_knn: bool = False,
    embd_to_use: str = "pca",
    slot_name: str = "densne",
    no_embd_to_use: int | None = None,
    modality: Modality = "rna",
    params: DensNE | None = None,
) -> None

den-SNE, t-SNE with a density-preserving term.

Inputs are handled as in tsne; lambda_=0 gives back plain t-SNE.

Parameters:

Name Type Description Default
sc ScOrMc

The experiment.

required
use_knn bool

Reuse the cached kNN.

False
embd_to_use str

The input embedding.

'pca'
slot_name str

The name in sc.cache.embeddings to write.

'densne'
no_embd_to_use int | None

Number of leading dimensions to use. None for all.

None
modality Modality

The kNN and the slot: "rna", or "adt" / "atac" / "wnn" on a SingleCellsMultiModal. "wnn" reads the embedding from RNA and always uses the WNN kNN.

'rna'
params DensNE | None

The estimator. None is densne_defaults().

None

Raises:

Type Description
ValueError

As umap.

References

Narayan, Berger & Cho, Nat Biotechnol, 2021.

densne_defaults

densne_defaults() -> DensNE

den-SNE set to the package defaults.

Trajectory and graph layouts

2D embeddings: UMAP, densMAP, t-SNE, den-SNE, PHATE and ForceAtlas2.

Start with umap on the neighbours from bv.graph.find_neighbours. Every function reads a cached embedding (the PCA by default), fits, and writes the coordinates to sc.cache.embeddings under slot_name ("umap", "densmap", "tsne", "densne", "phate", "fa2"), stamped as derived from the input embedding and, when the cached kNN fed it, from the kNN. Nothing is returned. All of them take a SingleCells, SingleCellsSubset or MetaCells; on a SingleCellsMultiModal, modality="wnn" takes the RNA embedding and the WNN kNN.

The numerics come from the manifolds-rs wheel, an optional extra: pip install bixverse[embed]. Importing bv.embed without it raises.

Parameters are the manifolds_rs estimators themselves. The *_defaults() builders return one set to the bixverse defaults, which differ from the estimators' own in places (t-SNE perplexity is 10, not 30). Tweak one with set_params and hand it over as params.

bv.graph.find_neighbours(sc)
bv.embed.umap(sc)
bv.embed.tsne(sc, params=bv.embed.tsne_defaults().set_params(perplexity=20.0))
umap_xy = sc.cache.embeddings["umap"]

UMAP and densMAP take optimiser="sgd_parallel", umap-learn's SGD run lock-free over all threads (Hogwild!). It is fast and not reproducible: the same seed gives a different embedding each run. n_epochs defaults to None, which is 500, or 200 for the SGD optimisers above 10,000 cells.

Gotchas. With use_knn=True (the default for UMAP, densMAP and ForceAtlas2) the cached kNN is reused and the neighbour search settings in params don't apply. A graph wider than n_neighbors is cut to it, with a warning on stderr; a narrower one is used as it is. With no kNN cached you get a warning and a fresh search. t-SNE, den-SNE and PHATE search afresh unless you ask for the cached kNN; if you do, keep its k near three times the perplexity for t-SNE, and at 6k + 5 (35 by default) for PHATE. A stale input raises, and so does a slot_name that is reserved (the PCA, kNN and so on). ForceAtlas2 lays out the fuzzy kNN graph; laying out a graph you supply is not possible.

phate

phate(
    sc: ScOrMc,
    *,
    use_knn: bool = False,
    embd_to_use: str = "pca",
    slot_name: str = "phate",
    no_embd_to_use: int | None = None,
    modality: Modality = "rna",
    params: PHATE | None = None,
) -> None

PHATE.

Off by default, use_knn reuses the cached kNN. PHATE searches 6k + 5 neighbours (35 at the default k=5) because its kernel keeps points past the k-th; a cached graph narrower than that is used as it is, so keep its k at that width if you reuse it.

Parameters:

Name Type Description Default
sc ScOrMc

The experiment.

required
use_knn bool

Reuse the cached kNN.

False
embd_to_use str

The input embedding.

'pca'
slot_name str

The name in sc.cache.embeddings to write.

'phate'
no_embd_to_use int | None

Number of leading dimensions to use. None for all.

None
modality Modality

The kNN and the slot: "rna", or "adt" / "atac" / "wnn" on a SingleCellsMultiModal. "wnn" reads the embedding from RNA and always uses the WNN kNN.

'rna'
params PHATE | None

The estimator. None is phate_defaults().

None

Raises:

Type Description
ValueError

As umap.

References

Moon, et al., Nat Biotechnol, 2019.

Examples:

>>> bv.embed.phate(sc, slot_name="phate")

phate_defaults

phate_defaults() -> PHATE

PHATE set to the package defaults.

randomised, mds_iter and thresh are not set here; the estimator's own apply.

forceatlas2

forceatlas2(
    sc: ScOrMc,
    *,
    use_knn: bool = True,
    embd_to_use: str = "pca",
    slot_name: str = "fa2",
    no_embd_to_use: int | None = None,
    modality: Modality = "rna",
    params: ForceAtlas2 | None = None,
) -> None

ForceAtlas2 on the fuzzy kNN graph.

The kNN (cached with use_knn, else searched) becomes UMAP's fuzzy union graph first, as in scanpy's draw_graph. Laying out a given graph is not supported: the manifolds-rs wheel cannot do it.

Parameters:

Name Type Description Default
sc ScOrMc

The experiment.

required
use_knn bool

Reuse the cached kNN.

True
embd_to_use str

The input embedding.

'pca'
slot_name str

The name in sc.cache.embeddings to write.

'fa2'
no_embd_to_use int | None

Number of leading dimensions to use. None for all.

None
modality Modality

The kNN and the slot: "rna", or "adt" / "atac" / "wnn" on a SingleCellsMultiModal. "wnn" reads the embedding from RNA and always uses the WNN kNN.

'rna'
params ForceAtlas2 | None

The estimator. None is forceatlas2_defaults().

None

Raises:

Type Description
ValueError

As umap.

References

Jacomy, et al., PLoS ONE, 2014.

Examples:

>>> bv.embed.forceatlas2(sc, slot_name="fa2")

forceatlas2_defaults

forceatlas2_defaults() -> ForceAtlas2

ForceAtlas2 set to the package defaults.