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 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: 'spatial')) – Coordinates in adata.obsm[X_{basis}] to use. Defaults to spatial.

  • 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 * 10

  • separate_groups – Whether groups should be calculated separately or aggregated.

Return type

Union[str, Dict[str, str]]

Returns

If separate_groups = True, a dictionary mapping groups to keys in adata.obs is returned. If separate_groups = False, a single key in adata.obs is returned.