build_niches

build_niches(
    adata,
    sigma=(10, 30, 50, 70),
    cell_type='cell_type',
    sample_id='sample_id',
    edge=True,
    diggle=True,
    name='Niche',
    n_jobs=1,
    copy=False,
)

Build niche covariate matrices (R buildNiches).

For each sample and bandwidth, the Gaussian kernel density of every cell type is evaluated at each cell’s location (edge-corrected as in spatstat.explore::densityfun) and scaled by pi * sigma**2. The cells x cell types matrix of each bandwidth is stored as the DataFrame adata.obsm[name + sigma] with the same, sorted, set of cell-type columns for every sample (cell types absent from a sample are 0) and the original cell order.

Parameters

Name Type Description Default
adata AnnData Cells x genes with obsm["spatial"], a cell type and a sample id column. required
sigma float or sequence of float Kernel bandwidths in the units of the spatial coordinates. (10, 30, 50, 70)
cell_type str adata.obs column holding the cell type labels. "cell_type"
sample_id str adata.obs column holding the sample identifiers; densities are estimated within each sample. "sample_id"
edge bool Apply the edge correction to the density (see spidepy._kde). True
diggle bool Use Diggle’s edge-correction variant (only used when edge is true). True
name str Prefix of the obsm keys. "Niche"
n_jobs int Number of joblib workers over samples; the result does not depend on it. 1
copy bool Return a modified copy instead of modifying adata in place. False

Returns

Name Type Description
anndata.AnnData adata (or its copy) with one niche matrix per bandwidth.

See Also

merge_niches : Sum niche columns into broader groups. fit_spide : Fit the per-patient niche models on these matrices.

Examples

from spidepy import build_niches
from spidepy.datasets import toy_spide
adata = build_niches(toy_spide(), sigma=[10, 30])
[k for k in adata.obsm if k.startswith("Niche")]
['Niche10', 'Niche30']
adata.obsm["Niche30"].shape
(2400, 3)
adata.obsm["Niche30"].columns.tolist()
['A', 'B', 'C']