Skip to main content

sm-rust — Python bindings

Spatial multiomics primitives from the sm-rust crate (neighbour graphs, Moran's I, Geary's C, local LISA statistics — Local Moran's I with permutation and exact p-values, Local Getis-Ord G_i/G_i*, Local Geary's C — neighbourhood enrichment, Ripley F/G/L, co-occurrence, ligand-receptor, sepal, niche detection, per-cell neighbourhood metrics — local density, distance to nearest cell of a type, diversity bundle (Shannon / Simpson / inverse-Simpson / richness / Pielou) and homophily / mixing fraction), exposed to Python via PyO3 + maturin.

Layout

  • sm_rust._native — the compiled extension module: Cells, Neighbors, every compute_* function returning result dataclass-like objects with numpy arrays. Mirrors the Node/wasm binding surface.
  • sm_rust.gr — squidpy-shaped wrappers that consume AnnData directly: spatial_neighbors, spatial_autocorr, nhood_enrichment, interaction_matrix, centrality_scores, co_occurrence, ripley, ligrec, sepal. The output shape (column names, .uns keys) matches squidpy.gr so existing notebooks can drop this in.

Build (host)

pip install maturin
maturin develop --release --features python-bindings,parallel

The repo's docker-compose.yml ships a pyo3 service that builds a wheel into python/dist/:

docker compose run --rm pyo3

On x86-64, scripts/build-pyo3.sh puts three builds of the extension in the wheel — baseline, x86-64-v3 (AVX2, FMA, BMI2) at sm_rust/_v3/ and x86-64-v4 (AVX-512) at sm_rust/_v4/ — plus a tiny probe library, sm_rust/_cpu_probe. Importing sm_rust asks the probe which levels the CPU supports and imports exactly one build, the best it can run; sm_rust.CPU_VARIANT says which. Set SM_RUST_CPU to baseline, v3 or v4 to require that build instead (the import raises rather than falling back). Results can differ between the builds in the last bits.

maturin develop, like any local cargo build, compiles a single module for this machine's own CPU (target-cpu=native, from .cargo/config.toml) — the fastest option here, and not something to copy to another machine. scripts/build-pyo3.sh is what builds wheels to distribute.

Quick start

Low-level API:

import numpy as np
from sm_rust import _native as sm

cells = sm.cells_from_coords_py(np.random.rand(1000), np.random.rand(1000))
labels = np.random.randint(0, 3, 1000).astype(np.uint8)
neighbors = sm.Neighbors.knn(6)

res = sm.compute_nhood_enrichment_py(cells, labels, neighbors, n_clusters=3)
print(res.count.reshape(3, 3))
print(res.zscore.reshape(3, 3))

Local (LISA) statistics — one score per cell, returned as parallel numpy arrays (mirroring esda.Moran_Local / Geary_Local / G_Local). Pass permutations=0 for the observed map only (conditional-null fields become NaN):

values = np.random.rand(1000)   # a continuous attribute / marker intensity

lm = sm.compute_local_moran_i_py(cells, values, neighbors, permutations=999, seed=42)
print(lm.Is, lm.q, lm.p_sim)         # local I, HH/LH/LL/HL quadrant, pseudo p

lg = sm.compute_local_geary_c_py(cells, values, neighbors, permutations=999)
print(lg.local_g, lg.p_sim)

# Getis-Ord hot-/cold-spot map; star=True selects G_i* (includes the focal cell).
go = sm.compute_local_getis_ord_py(cells, values, neighbors, star=True)
print(go.z, go.p_norm)               # standardised z-score + normal p-value

# Exact Local Moran p-values (Tiefelsdorf & Boots 1995, spdep's
# localmoran.exact): permutation-free, no RNG, so bit-reproducible. Takes no
# permutations/seed/threads. NaN for isolated cells; None on degenerate input.
# Different null from lm.p_sim above — normality, not conditional randomization.
p_exact = sm.compute_local_moran_exact_p_py(cells, values, neighbors)
print(p_exact)

Per-cell neighbourhood metrics — one summary per cell over the supplied Neighbors graph, returned as parallel numpy arrays (cells with no neighbours hold NaN):

# Local cell density: neighbour count + area-normalised density (count / πr²).
den = sm.compute_local_density_py(cells, neighbors)
print(den.count, den.density)

# Diversity of the neighbour cell-type composition (vegan conventions:
# simpson = 1 - Σp², inverse_simpson = 1/Σp², pielou = H / ln(richness)).
div = sm.compute_local_diversity_py(cells, labels, n_clusters=3, neighbors=neighbors)
print(div.richness, div.shannon, div.simpson, div.inverse_simpson, div.pielou)

# Homophily: fraction of neighbours sharing the focal cell's label (+ mixing).
hom = sm.compute_homophily_py(cells, labels, neighbors)
print(hom.homophily, hom.mixing)

# Distance from every cell to the nearest *other* cell of a target type (NaN
# where none exists; None if the target label is absent).
d = sm.compute_distance_to_nearest_type_py(cells, labels, target=1)
print(d)

squidpy-style API:

import sm_rust as smr
import anndata as ad

smr.gr.spatial_neighbors(adata, coord_type="generic", n_neighs=6)
smr.gr.spatial_autocorr(adata, mode="moran", genes=adata.var_names[:50])
smr.gr.nhood_enrichment(adata, cluster_key="leiden")

Metadata

Release files for sm-rust 0.0.6

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

Built distributions (wheels)

Table of built distributions (wheels) for sm-rust 0.0.6
File Actions
sm_rust-0.0.6-cp39-abi3-win_arm64.whl CPython 3.9 abi3 Windows ARM64 Details for sm_rust-0.0.6-cp39-abi3-win_arm64.whl
sm_rust-0.0.6-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for sm_rust-0.0.6-cp39-abi3-win_amd64.whl
sm_rust-0.0.6-cp39-abi3-musllinux_1_2_x86_64.whl CPython 3.9 abi3 Linux musl 1.2+ x86-64 Details for sm_rust-0.0.6-cp39-abi3-musllinux_1_2_x86_64.whl
sm_rust-0.0.6-cp39-abi3-musllinux_1_2_aarch64.whl CPython 3.9 abi3 Linux musl 1.2+ ARM64 Details for sm_rust-0.0.6-cp39-abi3-musllinux_1_2_aarch64.whl
sm_rust-0.0.6-cp39-abi3-manylinux_2_28_x86_64.whl CPython 3.9 abi3 Linux glibc 2.28+ x86-64 Details for sm_rust-0.0.6-cp39-abi3-manylinux_2_28_x86_64.whl
sm_rust-0.0.6-cp39-abi3-manylinux_2_28_aarch64.whl CPython 3.9 abi3 Linux glibc 2.28+ ARM64 Details for sm_rust-0.0.6-cp39-abi3-manylinux_2_28_aarch64.whl
sm_rust-0.0.6-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for sm_rust-0.0.6-cp39-abi3-macosx_11_0_arm64.whl
sm_rust-0.0.6-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details for sm_rust-0.0.6-cp39-abi3-macosx_10_12_x86_64.whl

Total release size: 20.9 MB

Release files / sm_rust-0.0.6-cp39-abi3-win_arm64.whl

Download URL sm_rust-0.0.6-cp39-abi3-win_arm64.whl
Size 840.7 kB
Tags CPython 3.9 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
5983eeffe3cceeb96f4de312a1d98c7657163dd4edfd72783c18d4d23e63ae05
BLAKE2b-256 checksum
How to use checksums
3fe7f60427c3233a5e8159c33b7180928f351044541eb75052339ef982d04362
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / sm_rust-0.0.6-cp39-abi3-win_amd64.whl

Download URL sm_rust-0.0.6-cp39-abi3-win_amd64.whl
Size 3.8 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
f23582d3179630f87ceb0265b8b80297faa0a7f0239e9710a691253492681476
BLAKE2b-256 checksum
How to use checksums
2fa02b5dee8504ab09a6c63d2434f1b72f03ce72437eabb1930ee16ba1f167b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / sm_rust-0.0.6-cp39-abi3-musllinux_1_2_x86_64.whl

Download URL sm_rust-0.0.6-cp39-abi3-musllinux_1_2_x86_64.whl
Size 4.4 MB
Tags CPython 3.9 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
ffbd4c0ec4f71471ed9110e1e3a342ab04f17629a522c564189db0912a459202
BLAKE2b-256 checksum
How to use checksums
562a0394439343748c79d68b4abab53bbcc9a3c5e25b23bdafb4fb1686e4175b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / sm_rust-0.0.6-cp39-abi3-musllinux_1_2_aarch64.whl

Download URL sm_rust-0.0.6-cp39-abi3-musllinux_1_2_aarch64.whl
Size 1.0 MB
Tags CPython 3.9 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
35522a87050dedebcefedaa92682f2f45f719577f08e311f0a150b15b3bf7cff
BLAKE2b-256 checksum
How to use checksums
1af45eeb4ca6eb61a1c3e991c5717eae7423f7b67e13b956cd58730790dcc656
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / sm_rust-0.0.6-cp39-abi3-manylinux_2_28_x86_64.whl

Download URL sm_rust-0.0.6-cp39-abi3-manylinux_2_28_x86_64.whl
Size 4.4 MB
Tags CPython 3.9 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
dc296bfdfd4bad3eec660470cba732fe4e407f50f1be4e0c82c77d2394d4f5e7
BLAKE2b-256 checksum
How to use checksums
efd6dc3d5de1305e89d38ff7ffbac3f7b9b9fd72cc80ce1fce29a3b73b9b56ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / sm_rust-0.0.6-cp39-abi3-manylinux_2_28_aarch64.whl

Download URL sm_rust-0.0.6-cp39-abi3-manylinux_2_28_aarch64.whl
Size 1.0 MB
Tags CPython 3.9 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
a08953f5c0f2f72eddecf80a075ad00377b6db15dd911443d8b6350e92db68db
BLAKE2b-256 checksum
How to use checksums
7115ba2044186f91bc8204e02247c74c629da70bd63b1c076393ce9199d9d20f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / sm_rust-0.0.6-cp39-abi3-macosx_11_0_arm64.whl

Download URL sm_rust-0.0.6-cp39-abi3-macosx_11_0_arm64.whl
Size 966.0 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1d59feb909e83164f98a0e0a73d59769170e49ebdfa6ebf2e6989fdf4a624878
BLAKE2b-256 checksum
How to use checksums
b2385f956fb04622439992a286be53ec24d7d38503292f1ff782fb5865145d3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / sm_rust-0.0.6-cp39-abi3-macosx_10_12_x86_64.whl

Download URL sm_rust-0.0.6-cp39-abi3-macosx_10_12_x86_64.whl
Size 4.5 MB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
ab589eab95f5d61f23512e05dcc588ef18540d24a5290796e50b62e63c59b7ea
BLAKE2b-256 checksum
How to use checksums
c7d43e21ed42c9640811110941adcccdf6658840f8346060d6dc0b3014356120
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Security for sm-rust 0.0.6

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