scanpy.tl.embedding_density
- scanpy.tl.embedding_density(adata, basis='umap', groupby=None, key_added=None, components=None)
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
sc.pl.embedding_density()function.Note that density values are scaled to be between 0 and 1. Thus, the density value at each cell is only comparable to other densities in the same condition category.
This function was written by Sophie Tritschler and implemented into Scanpy by Malte Luecken.
- Parameters
- adata :
AnnDataAnnData The annotated data matrix.
- basis :
strstr(default:'umap') The embedding over which the density will be calculated. This embedded representation should be found in
adata.obsm['X_[basis]']`.- groupby :
str|NoneOptional[str] (default:None) Keys for categorical observation/cell annotation for which densities are calculated per category. Columns with up to ten categories are accepted.
- key_added :
str|NoneOptional[str] (default:None) Name of the
.obscovariate that will be added with the density estimates.- components :
str|Sequence[str] |NoneUnion[str,Sequence[str],None] (default:None) The embedding dimensions over which the density should be calculated. This is limited to two components.
- adata :
- Return type
- Returns
Updates
adata.obswith an additional field specified by thekey_addedparameter. This parameter defaults to[basis]_density_[groupby], where where[basis]is one ofumap,diffmap,pca,tsne, ordraw_graph_faand[groupby]denotes the parameter input. Updatesadata.unswith an additional field[key_added]_params.
Examples
import scanpy as sc adata = sc.datasets.pbmc68k_reduced() sc.tl.umap(adata) sc.tl.embedding_density(adata, basis='umap', groupby='phase') sc.pl.embedding_density( adata, basis='umap', key='umap_density_phase', group='G1' )
sc.pl.embedding_density( adata, basis='umap', key='umap_density_phase', group='S' )
See also