Skip to main content

py-statial

A pure-Python re-implementation of Bioconductor Statial for identifying changes in cell state relative to spatial associations.

Installation

pip install py-statial

Quickstart

import statial
import anndata as ad
import pandas as pd

# Load spatial cell data (x, y coordinates + cellType + imageID)
cells = pd.read_csv("cell_metadata.csv")
adata = ad.AnnData(obs=cells)

# Pairwise distances between cell types
adata = statial.get_distances(adata, max_dist=200)

# Cell type abundances (K-function)
adata = statial.get_abundances(adata, r=200)

# Kontextual: conditional spatial relationships
result = statial.Kontextual(
    cells=cells,
    r=50,
    from_types="Macrophages",
    to_types="Keratin_Tumour",
    parent=["Macrophages", "CD4_Cell"],
    image=["6"],
)

API Reference

Python R Description
statial.get_distances() Statial::getDistances() Euclidean distance to nearest cell of each type
statial.get_abundances() Statial::getAbundances() Cell count within radius (K-function)
statial.calc_contamination() Statial::calcContamination() Random-forest contamination scores
statial.Kontextual() Statial::Kontextual() L-function conditional on a parent population
statial.kontext_curve() Statial::kontextCurve() Kontextual over a range of radii
statial.kontext_plot() Statial::kontextPlot() Plot Kontextual results
statial.calc_state_changes() Statial::calcStateChanges() Linear models for marker ~ distance
statial.make_window() Statial::makeWindow() Observation window (square / convex / concave)
statial.parent_combinations() Statial::parentCombinations() All pairwise parent-child cell type combos
statial.get_parent_phylo() Statial::getParentPhylo() Extract parent-children from phylo tree
statial.prep_matrix() Statial::prepMatrix() Pivot results into a matrix
statial.get_marker_means() Statial::getMarkerMeans() Mean marker expression per cell type
statial.relabel() Statial::relabel() Permute cell type labels
statial.relabel_kontextual() Statial::relabelKontextual() Permutation-based significance testing
statial.is_kontextual() Statial::isKontextual() Check if object is a kontextual result

Benchmark

Dataset: Keren et al. 2018 MIBI-TOF breast cancer (patient 6) — 57,811 cells, 10 images, 17 cell types.

Environments: Python 3.9.13 / scipy 1.8 / scikit-learn 1.0 vs R 4.5.2 / Statial 1.11.6. Timings are 3-run means.

benchmark

Numerical parity vs R

Function Metric Value Threshold
get_distances max abs error 1.33e-11 1e-8 pass
get_abundances max abs error 0.0 1e-8 pass
Kontextual (L-function) max abs error 7.11e-15 1e-8 pass
Kontextual (value) relative error 5.14% 10% pass

Core functions match R at machine precision. The ~5% gap in Kontextual values comes from scipy.spatial.cKDTree vs spatstat.geom::closepairs spatial indexing.

Speed

Function Python R Speedup
get_distances 0.44 s 7.48 s 16.9x
get_abundances 0.59 s 7.25 s 12.4x
Kontextual (image 6) 0.07 s 0.20 s 2.9x
Total 1.10 s 14.93 s 13.6x

Citation

@article{ameen2022statial,
  title={Statial: A package to identify changes in cell state relative to spatial associations},
  author={Ameen, Farhan and Iyengar, Sourish and Qin, Alex and Ghazanfar, Shila and Patrick, Ellis},
  year={2022}
}

License

GPL-3.0 (matching upstream R package)

Metadata

Release files for pystatial 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pystatial 0.1.2
File Size Uploaded Provenance Actions
pystatial-0.1.2.tar.gz 30.8 kB Without provenance Details for pystatial-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pystatial 0.1.2
File Interpreter ABI Platform Actions
pystatial-0.1.2-py3-none-any.whl Python 3 none any Details for pystatial-0.1.2-py3-none-any.whl

Total release size: 62.9 kB

Release files / pystatial-0.1.2.tar.gz

Download URL pystatial-0.1.2.tar.gz
Size 30.8 kB
Tags Source
SHA-256 checksum
How to use checksums
380d8e6a0ad5ec9949817813faddf33b2d8c0821fe5e226de1dbac08e254db99
BLAKE2b-256 checksum
How to use checksums
8b1cca2c15b0568601abdcd60265b89828e20eac1964b3740776c465e963debb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.13

Release files / pystatial-0.1.2-py3-none-any.whl

Download URL pystatial-0.1.2-py3-none-any.whl
Size 32.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc180801eb6b030555f3247d19090c2b695cfca143b3e70d6676d4a9b66042af
BLAKE2b-256 checksum
How to use checksums
c7ad0d35c48cf7e5520d7fe1f07a5b26bd7d12ce44c0da72c3ac54655d88df95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.13

Security for pystatial 0.1.2

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page