denspatial
Density-aware spatial gene-set association with detection-matched null models.
At spot resolution (e.g. 10x Visium), a gene-set score is strongly coupled to per-spot sequencing depth / cell density. As a result, the correlation between two gene-set scores is often dominated by this shared density component, and naive spatial-coupling claims are inflated.
denspatial provides a small, dependency-light toolkit to test whether a
gene-set association is specific or merely a density artifact:
signature_score— per-gene z-scored mean gene-set score (recommended);morans_i— spatial autocorrelation with a permutation null;partial_spearman— rank-based partial correlation controlling for density;matched_null_sets— random gene sets matched on cross-gene detection rate (the key control that existing spatial tools do not implement);gene_set_coupling— one call returning raw & partial coupling plus matched-null p-values and Moran's I;density_audit— three-outcome verdict per gene-set pair (true_signal/density_driven/not_beyond_chance) with BH-FDR;audit_single— single-set check of spatial structure against detection-matched nulls;from_h5ad— load.h5adfiles (anndata, or a dependency-light h5py fallback).
Install
pip install -e .
# with the h5ad reader: pip install -e ".[io]"
Quick start
import numpy as np
from denspatial import gene_set_coupling, density_audit
res = gene_set_coupling(X, coords, HYPOXIA_GENES, VESSEL_GENES, gene_names,
n_null=100)
print(res["raw_rho"], res["partial_rho"], res["p_partial_null"])
# three-outcome audit over several gene-set pairs
audit = density_audit(X, coords,
{"hypoxia": HYPOXIA_GENES, "vessel": VESSEL_GENES},
gene_names, n_null=100)
for row in audit["pairs"]:
print(row["pair"], row["verdict"])
X = locations × genes (ideally log1p(CPM)); coords = locations × 2.
A runnable synthetic example: python examples/quickstart.py.
Multiple density sources
Capture depth correlates with several measured confounds at once. The density
argument accepts a single vector, an (n, k) array, a list of vectors, or a dict
of named sources; all sources are residualised jointly:
from denspatial import density_sources_from, gene_set_coupling
# bundle of UMI-derived covariates (log depth, detected genes, neighbourhood depth)
src = density_sources_from(X, coords)
res = gene_set_coupling(X, coords, A, B, gene_names, density=src, n_null=100)
print(res["density_sources"])
# ['log1p_library', 'n_genes', 'neighbour_library', 'neighbour_library_sq']
# add image- or cross-modality confounds as named sources
density = {"umi": lib_z, "he_cellity": he_z, "imc_cd45": imc_z}
res = gene_set_coupling(X, coords, A, B, gene_names, density=density, n_null=100)
Backward compatible: passing a single vector (or nothing) behaves exactly as in
v0.1–v0.2. See examples/multisource_demo.py.
Verdicts
| verdict | meaning |
|---|---|
true_signal |
density-partial coupling exceeds detection-matched nulls (BH q ≤ α) |
density_driven |
raw coupling exceeds nulls, but density-partial coupling does not |
not_beyond_chance |
raw coupling itself does not exceed detection-matched nulls |
Why it matters
In Visium, gene-set scores covary with cell density; any "program coupling" conclusion must be density-residualised and tested against a detection-matched null — otherwise it is largely spurious.
Reproducing the figures
Every panel of the accompanying Application Note can be regenerated
from this checkout; see REPRODUCE.md. Figure 2 uses
the public Visium section GSM9226169 from GSE307534, and the bundled
toy dataset under testdata/ reproduces the qualitative behaviour in
under a minute with no download.
Testing
pytest -q
Citation
If you use denspatial in your work, please cite:
Chen Lihua (2026). denspatial: density-aware spatial gene-set association with detection-matched null models. Zenodo. https://doi.org/10.5281/zenodo.23601594 (all versions) v0.3.0: https://doi.org/10.5281/zenodo.23880399 v0.1.0: https://doi.org/10.5281/zenodo.23601595
A machine-readable entry is provided in CITATION.cff.
License
MIT
Metadata
Release files for denspatial 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| denspatial-0.3.0.tar.gz | 17.8 kB | Without provenance | Details for denspatial-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| denspatial-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for denspatial-0.3.0-py3-none-any.whl |
Total release size: 33.0 kB
Release files / denspatial-0.3.0.tar.gz
| Download URL | denspatial-0.3.0.tar.gz |
|---|---|
| Size | 17.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a619a5bdaba45a9b8a1908dbaace31529bd3ea612e25951e00fdde8ece601706
|
|
BLAKE2b-256 checksum How to use checksums |
b0b68f6283ab2ec34c0a2122f8e2eea46ab0568e3d1ccfd16fc1f29b43d209c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / denspatial-0.3.0-py3-none-any.whl
| Download URL | denspatial-0.3.0-py3-none-any.whl |
|---|---|
| Size | 15.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
47f074333ee510f5e57ca60168dae752229490bd5e9de43b1ab25eea259e3574
|
|
BLAKE2b-256 checksum How to use checksums |
9392e502b89e373b7892d0247dee8850a028894d82fe7356d870251db0266125
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Security for denspatial 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.