monkeybread.calc.cell_density
- monkeybread.calc.cell_density(adata, groupby='all', groups='all', groupname=None, basis='X_spatial', bandwidth=1.0, approx=True, resolution=1.0, radius_threshold=250)
Calculates the spatial distribution of cells of a given cell type using kernel density estimation.
This can be computed via two approximations: the first approximation is closest to the true kernel density estimate, however, the kernel between pairs of cells are zeroed out if they are greater than
radius_thresholdfrom eachother. This enables the kernel estimation to run on sparse matrices.The second approximation is faster and can be run if
approxis set to true. A grid is overlayed onto the spatial coordinates, and each cell is assigned to its nearest gridpoint. Each gridpoint is then used in a kernel density estimation calculation. The density value for each gridpoint is then assigned back to each cell. Fineness of the grid can be modified using theresolutionparameter where a high resolution leads to a more accurate calculation of density for each cell. Final density values are scaled between 0 and 1 corresponding to the minimum and maximum density, respectively.- Parameters:
adata (
AnnData) – Annotated data matrixgroupby (
Optional[str] (default:'all')) – A categorical column inobsto use for density calculations. If omitted, each cell will contribute equally towards density calculationsgroups (
Union[str,List[str],None] (default:'all')) – Groups inadata.obs[groupby]that contribute towards densitygroupname (
Optional[str] (default:None)) – Column inadata.obsto assign density values to.basis (
Optional[str] (default:'X_spatial')) – Coordinates inadata.obsm[{basis}]to use. Defaults tospatialbandwidth (
Optional[float] (default:1.0)) – Bandwidth for kernel density estimationapprox (
Optional[bool] (default:True)) – If true, approximate the kernel density estimation by grouping cells into binsresolution (
Optional[float] (default:1.0)) – How small each gridpoint is. Number of points per row/column = resolution * 10. Used only ifapproxisTrueradius_threshold (
Optional[float] (default:250)) – Sparsifies the pairwise distance matrix to only consider cells if they are within this threshold distance from eachother. Only used ifapproxis False.
- Return type:
- Returns:
: The column name
adata.obsstoring the density values for each cell.