Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

SONIC: Spatial Organization through Nonrandom-pattern Inference and Comparison

SONIC detects spatial patterns within one sample and compares patterns across multiple samples. It works with coordinates, graphs, and regular grids stored in AnnData or SpatialData.

The two main workflows are:

  • Pattern detection: find spatially variable features with the Q-test and spatially co-expressed feature pairs with the R-test.
  • Pattern comparison: find features whose spatial pattern changes between samples or conditions, without registering the samples to one another.

Installation

Install the release candidate from PyPI:

pip install sonic-spatial==1.0.0rc2

The distribution name is sonic-spatial; the Python import is sonic.

For the latest source:

pip install "sonic-spatial @ git+https://github.com/JiayuSuPKU/sonic.git"

Code written for the former quadsv package remains supported through the compatibility distribution:

pip install quadsv==1.0.0rc2

Detect patterns in one sample

Detector reads an AnnData object with coordinates or a graph and tests all features in the expression matrix. Start with NUFFT and a Matérn kernel for two-dimensional spatial coordinates. Set bandwidth to the spatial scale of interest, in the same units as the coordinates.

import anndata as ad
from sonic import Detector

adata = ad.read_h5ad("spatial_data.h5ad")

detector = Detector(
    adata,
    kernel_method="matern",
    backend="nufft",
    bandwidth=2.0,
    nu=1.5,
).setup_data(adata, obsm_key="spatial", min_cells_frac=0.05)

q_results = detector.compute_qstat(return_pval=True)
svgs = q_results[q_results["P_adj"] < 0.05]

top_genes = q_results.nlargest(100, "Q").index.tolist()
r_results = detector.compute_rstat(
    features_x=top_genes,
    features_y=None,
    return_pval=True,
)

Use spatial_q_test and spatial_r_test directly when you already have a kernel and want to test one feature or one feature pair. Regular rasterized grids, including Visium HD bins, use the same Detector interface with a SpatialData object.

Compare patterns across samples

Comparator converts each sample into per-gene spatial spectra and tests for differences between groups. Samples may use different orientations and coordinate systems.

import anndata as ad
import numpy as np
from sonic import Comparator

paths = [
    "control_1.h5ad",
    "control_2.h5ad",
    "control_3.h5ad",
    "case_1.h5ad",
    "case_2.h5ad",
    "case_3.h5ad",
]
samples = [ad.read_h5ad(path) for path in paths]
groups = np.array([0, 0, 0, 1, 1, 1])

comparison = Comparator(samples).compute_spectra(n_jobs=4)

# Optional: remove the sample-wide spectral background before testing.
# comparison.normalize_background()

pattern_results = comparison.test_diff_freq(
    groups,
    statistic="log_l2",
    normalize_shape=True,
)
expression_results = comparison.test_diff_expr(groups)

pattern_results tests the spatial layout of each gene. expression_results tests its sample-level mean expression. Run both when you want to distinguish a pattern change from an expression-level change. Background normalization is optional: use it when sample-wide spectral differences are nuisance variation. It can also remove shared biological signal.

Choose an interface

Q-test defaults depend on the backend: Gaussian, Matérn, CAR and graph-Laplacian kernels use upper-tail Welch on MatrixKernel and upper-tail moment matching on FFTKernel/NUFFTKernel. Moran uses two-sided CLT on every backend. All defaults account for sample standardization. See the kernel calibration table for signed/custom kernels and overrides.

Input Task Interface
AnnData with coordinates or a graph Detect patterns Detector
SpatialData with regular bins Detect patterns on a large grid Detector
A list of AnnData samples Compare patterns Comparator
A list of SpatialData samples Compare patterns on regular grids Comparator
Arrays plus a kernel Run one Q-test or R-test spatial_q_test, spatial_r_test

Documentation

Development

git clone https://github.com/JiayuSuPKU/sonic.git
cd sonic
pip install -e ".[dev,docs]"
pytest -q

See the installation guide for environment troubleshooting.

Citation

Su, Jiayu, et al. “On the consistent and scalable detection of spatial patterns.” arXiv:2602.02825 (2026). Preprint

SONIC is released under the BSD 3-Clause License. Please report bugs and feature requests through GitHub Issues.

Metadata

Release files for sonic-spatial 1.0.0rc2

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

Source distribution (sdist)

Source distribution for sonic-spatial 1.0.0rc2
File Size Uploaded Provenance Actions
sonic_spatial-1.0.0rc2.tar.gz 204.0 kB With provenance Details for sonic_spatial-1.0.0rc2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sonic-spatial 1.0.0rc2
File Interpreter ABI Platform Actions
sonic_spatial-1.0.0rc2-py3-none-any.whl Python 3 none any Details for sonic_spatial-1.0.0rc2-py3-none-any.whl

Total release size: 388.6 kB

Release files / sonic_spatial-1.0.0rc2.tar.gz

Download URL sonic_spatial-1.0.0rc2.tar.gz
Size 204.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1a35ef484cd67c4a1589152a2dc57e3f523801ff62d5a4cd916db586ab9eeb10
BLAKE2b-256 checksum
How to use checksums
207f407cd70015531257b1b68dd7211ab981e806af0a56fba7b4ffbf886f534f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

Release files / sonic_spatial-1.0.0rc2-py3-none-any.whl

Download URL sonic_spatial-1.0.0rc2-py3-none-any.whl
Size 184.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f1ff22e142f2619e515f083ef80f968371d4a4b80a8905fb1579c4c8b1a89b44
BLAKE2b-256 checksum
How to use checksums
d881cc96847de75331f130d234856121465eadd80416fec7d670247f535fbe29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

Security for sonic-spatial 1.0.0rc2

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sonic_spatial-1.0.0rc2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.

Transparency log

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