Skip to main content

A pure-Python re-implementation of Bioconductor Statial for spatial cell state analysis

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pystatial-0.1.2.tar.gz (30.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pystatial-0.1.2-py3-none-any.whl (32.1 kB view details)

Uploaded Python 3

File details

Details for the file pystatial-0.1.2.tar.gz.

File metadata

  • Download URL: pystatial-0.1.2.tar.gz
  • Upload date:
  • Size: 30.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for pystatial-0.1.2.tar.gz
Algorithm Hash digest
SHA256 380d8e6a0ad5ec9949817813faddf33b2d8c0821fe5e226de1dbac08e254db99
MD5 a556637646aa9f3e27f9a26f813d2148
BLAKE2b-256 8b1cca2c15b0568601abdcd60265b89828e20eac1964b3740776c465e963debb

See more details on using hashes here.

File details

Details for the file pystatial-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pystatial-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 32.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for pystatial-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bc180801eb6b030555f3247d19090c2b695cfca143b3e70d6676d4a9b66042af
MD5 d25fb7889b28da3ca83f22b8f0d45ade
BLAKE2b-256 c7ad0d35c48cf7e5520d7fe1f07a5b26bd7d12ce44c0da72c3ac54655d88df95

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page