monkeybread.calc.kernel_density
- monkeybread.calc.kernel_density(adata, groupby='all', groups='all', groupname=None, basis='spatial', bandwidth=1.0, resolution=1.0, separate_groups=False)
Calculates kernel density estimation on large cell quantities.
Instead of a single-cell resolution for density, a grid is overlayed onto the spatial coordinates, and each cell is assigned to its nearest gridpoint. Each gridpoint is then used to calculate kernel density, and the density for each gridpoint is assigned to each cell at that spot. Fineness of the grid can be modified using the
resolution
parameter. Density will be scaled between 0 and 1 corresponding to the minimum and maximum density, respectively.- Parameters
adata (
AnnData
) – Annotated data matrix.groupby (
Optional
[str
] (default:'all'
)) – A categorical column inobs
to use for density calculations. If omitted, each cell will contribute equally towards density calculations.groups (
Union
[str
,List
[str
],None
] (default:'all'
)) – Groups inadata.obs[groupby]
that contribute towards density.groupname (
Optional
[str
] (default:None
)) – Column inadata.obs
to assign density values to.basis (
Optional
[str
] (default:'spatial'
)) – Coordinates inadata.obsm[X_{basis}]
to use. Defaults tospatial
.bandwidth (
Optional
[float
] (default:1.0
)) – Bandwidth for kernel density estimation.resolution (
Optional
[float
] (default:1.0
)) – How small each gridpoint is. Number of points per row/column = resolution * 10separate_groups – Whether groups should be calculated separately or aggregated.
- Return type
- Returns
If
separate_groups = True
, a dictionary mapping groups to keys inadata.obs
is returned. Ifseparate_groups = False
, a single key inadata.obs
is returned.