scanpy.tl.embedding_density#
- scanpy.tl.embedding_density(adata, use='umap', *, groupby=None, components=None, out=False, basis=None, key_added=None)[source]#
Calculate the density of cells in an embedding (per condition).
Gaussian kernel density estimation is used to calculate the density of cells in an embedded space. This can be performed per category over a categorical cell annotation. The cell density can be plotted using the
pl.embedding_densityfunction.Note that density values are scaled to be between 0 and 1. Thus, the density value at each cell is only comparable to densities in the same category.
Beware that the KDE estimate used (
scipy.stats.gaussian_kde) becomes unreliable if you don’t have enough cells in a category. Categories with fewer than 3 cells, or whose cells lie on a single line or point in the embedding, get a density ofNaN.This function was written by Sophie Tritschler and implemented into Scanpy by Malte Luecken.
Array type support# Array type
supported
… experimentally in dask
Array✅
❌
❌
❌
- Parameters:
- adata
AnnData The annotated data matrix.
- use
MultiAcc|str|Default(default:'umap') The embedding over which the density will be calculated, e.g.
A.obsm['umap']. Ifscanpy.settings.presetisScanpyV2Preview,strs areanndata.acc.AdAcc.resolve()d (e.g.'obsm.umap'), otherwise they name an embedding found inadata.obsm['X_[basis]'](oradata.obsm['[basis]']). By default, the UMAP embedding.- groupby
AdRef|str|None(default:None) Categorical observation/cell annotation for which densities are calculated per category. Given by a reference like
A.obs['phase'], or by column name (strs areresolve()d withScanpyV2Preview).- components
str|Sequence[str] |None(default:None) The embedding dimensions over which the density should be calculated. This is limited to two components.
- out
AdRef|str|bool(default:False) Where to write the densities. Given by an accessor, e.g.
A.obs['density'].strs areanndata.acc.AdAcc.resolve()d. Accompanyingunsentries are named after its key. IfTrue, the densities is returned instead of written. IfFalse, it is written to.obs['[basis]_density_[groupby]']and.uns['[basis]_density_[groupby]_params'].- key_added
str|None(default:None) Deprecated since version 1.13.0: Use
outinstead.key_added='k'isout=A.obs['k'].- basis
MultiAcc|str|None(default:None) Deprecated since version 1.13.0: Use
useinstead.The deprecated name of
use.
- adata
- Return type:
- Returns:
Returns the densities if
out=True, elseNone. Sets the following fields forkeyas[basis]_density_[groupby], where[basis]is one ofumap,diffmap,pca,tsne, ordraw_graph_faand[groupby]denotes the parameter input:adata.obs[key]numpy.ndarray(dtypefloat)Embedding density values for each cell.
adata.uns['[key]_params']dictA dict with the values for the parameters
covariate(for thegroupbyparameter) andcomponents.
Examples
import scanpy as sc adata = sc.datasets.pbmc68k_reduced() sc.tl.umap(adata) sc.tl.embedding_density(adata, 'umap', groupby='phase') sc.pl.embedding_density( adata, basis='umap', key='umap_density_phase', group='G1' )
See also