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_threshold from eachother. This enables the kernel estimation to run on sparse matrices.

The second approximation is faster and can be run if approx is 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 the resolution parameter 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 matrix

  • groupby (Optional[str] (default: 'all')) – A categorical column in obs to use for density calculations. If omitted, each cell will contribute equally towards density calculations

  • groups (Union[str, List[str], None] (default: 'all')) – Groups in adata.obs[groupby] that contribute towards density

  • groupname (Optional[str] (default: None)) – Column in adata.obs to assign density values to.

  • basis (Optional[str] (default: 'X_spatial')) – Coordinates in adata.obsm[{basis}] to use. Defaults to spatial

  • bandwidth (Optional[float] (default: 1.0)) – Bandwidth for kernel density estimation

  • approx (Optional[bool] (default: True)) – If true, approximate the kernel density estimation by grouping cells into bins

  • resolution (Optional[float] (default: 1.0)) – How small each gridpoint is. Number of points per row/column = resolution * 10. Used only if approx is True

  • radius_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 if approx is False.

Return type:

Union[str, Dict[str, str]]

Returns:

: The column name adata.obs storing the density values for each cell.