from spidepy import build_niches, fit_spide
from spidepy.datasets import toy_spide
adata = build_niches(toy_spide(), sigma=30)
fit = fit_spide(adata, condition="condition", verbose=False)
fit.engine, fit.sigma('slopes', 30.0)
fit_spide(
adata,
condition=None,
engine='slopes',
index=None,
niche=None,
sigma=None,
cell_type='cell_type',
sample_id='sample_id',
section=None,
covariates=(),
depth='loglib',
offset=None,
strata=None,
assay='counts',
name='Niche',
min_cells=10,
min_patients=6,
min_detect=0.1,
genes=None,
tile=None,
min_tiles=5,
n_jobs=1,
verbose=True,
)Fit spiDE’s per-patient niche models (R fitSpiDE).
Fits, for every index cell type, one negative binomial GLM per gene of its expression on the local density of every other (niche) cell type, with an intercept per patient so that each niche effect is a within-patient slope. The slopes are then tested across patients, pooled or between conditions.
Two engines estimate the between-patient error the tests need. "slopes" (default) fits the shared model once, without the condition, then gives every patient a one-step NB estimate of its own niche slopes with a within-patient spatial sandwich variance; its fit never sees the condition. "sandwich" fits the condition-specific model directly and tests its coefficients with a patient-clustered CR2 sandwich and Bell-McCaffrey degrees of freedom; it needs the condition at fit time.
| Name | Type | Description | Default |
|---|---|---|---|
| adata | AnnData | Cells x genes with raw integer counts, obsm["spatial"] and the niche matrices of build_niches. |
required |
| condition | str | The obs column holding a two-level, patient-level condition. The slopes engine only records it; the sandwich engine fits the condition-specific model only when it is given. |
None |
| engine | ('slopes', 'sandwich') | The between-patient error engine. | "slopes" |
| index | str or sequence of str | Index cell types (default: every type with at least min_cells cells in at least min_patients patients). |
None |
| niche | str or sequence of str | Niche columns to test (default all); the others stay in the model as adjustments. An index type is never tested against its own niche (see merge_niches). |
None |
| sigma | float | The niche bandwidth (needed only if adata carries several). |
None |
| cell_type | str | The obs column of the cell type. |
"cell_type" |
| sample_id | str | The obs column of the patient (the replicated unit). |
"sample_id" |
| section | str | The obs column of the tissue section (default sample_id), used to lay the spatial tiles of the variance estimate. |
None |
| covariates | str or sequence of str | Cell-level obs columns to adjust for (centred; patient-level ones are refused, the patient intercepts absorb them). |
() |
| depth | ('loglib', 'nonlinear', 'spatial_spline', 'none') | How sequencing depth enters: the centred log library size as a covariate; a 3-df natural spline of it; a library-size spline within each patient that may vary over the section; or not at all (e.g. when offset carries depth). |
"loglib" |
| offset | str | The name of a layer holding a log-scale offset per cell and gene, added to every linear predictor with its coefficient fixed at 1. | None |
| strata | str | A patient-level obs column (e.g. slide or batch) whose niche slopes are adjusted for in the condition test. Used by the sandwich engine’s condition model here and by the slopes engine at test time; the pooled model never uses it. |
None |
| assay | str | The layer of raw integer counts (X for "counts" when no such layer exists). |
"counts" |
| name | str | The niche obsm prefix. |
"Niche" |
| min_cells | int | Minimum cells of an index type a patient must contribute. | 10 |
| min_patients | int | Minimum patients a type needs. | 6 |
| min_detect | float | Minimum fraction of an index type’s cells in which a gene is detected. | 0.1 |
| genes | sequence of str | Genes to test (default every gene). Library size is always computed from every gene. | None |
| tile | float | Side of the square tiles of the spatial variance estimate (default 3 x sigma). |
None |
| min_tiles | int | Minimum tiles a patient needs for its own spatial variance. | 5 |
| n_jobs | int | Genes are fitted in blocks over this many workers; the result does not depend on it beyond floating-point summation order. | 1 |
| verbose | bool | Report progress on stderr. | True |
| Name | Type | Description |
|---|---|---|
| SpiDEFit | The fitted models, one entry per index cell type. |
build_niches : Build the niche matrices this reads. test_spide : Test a fit across patients. spide : Build, fit and test in one call.
('slopes', 30.0)