Skip to main content

SpaRank

Transferable spatial omics deconvolution.

SpaRank turns expression profiles into rank-ordered token sequences and runs them through a Transformer encoder to predict cell-type composition at each spatial spot. Trained once on a multi-context single-cell reference, a single model deploys directly to spatial sections spanning contexts without per-slice retraining. The same pipeline handles unimodal (RNA), multimodal (RNA + ADT, …), and context-conditioned setups.

PyPI Docs License

Installation

Requires Python >= 3.10

Install torch

# Install PyTorch matching your CUDA driver.
# Example
pip install torch==2.9.1+cu128 --index-url https://download.pytorch.org/whl/cu128

From PyPI

pip install sparank

From source

git clone https://github.com/XiHuYan/SpaRank.git
cd SpaRank
pip install -e .

Quick start

Unimodal RNA

from sparank import SpaRank, ExpConfig, ModalityConfig, SimulationConfig

cfg = ExpConfig(
    modalities=[ModalityConfig(name="rna", top_k=500)],
    simulation=SimulationConfig(       # controls pseudo-spot generation for training
        total_samples=100_000,
        cells_mean=10,                 # expected cells per spot — match your section resolution
        cells_std=5,
        cell_sample_method="gaussian",
        batch_key="Tissue",            # group cells by Tissue; simulate pseudo-spots within each, then pool (can differ from context_key)
    ),
    celltype_key="cell_type",
    batch_key="batch",                # group cells by batch; detect marker genes within each group
    context_key="Tissue",             # label cells by Tissue; embed the label into the prediction head
    epochs=20,
)

sr = SpaRank(cfg, save_dir="./output")
sr.register_modality("rna", adata_sc_rna)
sr.prepare()                           
# or pass markers directly:
# sr.prepare(marker_features={"rna": prior_gene_list})
sr.fit()
sr.save()

# Deploy to a spatial section — no retraining needed
df = sr.predict({"rna": adata_sp_rna})

Multimodal RNA + ADT

cfg = ExpConfig(
    modalities=[
        ModalityConfig(name="rna", top_k=500),
        ModalityConfig(name="adt", top_k=200),
    ],
    simulation=SimulationConfig(       # controls pseudo-spot generation for training
        total_samples=100_000,
        cells_mean=10,                 # expected cells per spot — match your section resolution
        cells_std=5,
        cell_sample_method="gaussian",
        batch_key="Tissue",            # group cells by Tissue; simulate pseudo-spots within each, then pool (can differ from context_key)
    ),
    celltype_key="cell_type",
    batch_key="batch",           
    context_key="Tissue",          
    fusion_type="sigmoid_gate",    # "concat" | "gate" | "joint_gate" | "sigmoid_gate" | "attention"
    epochs=20,
)

sr = SpaRank(cfg, save_dir="./output")
sr.register_modality("rna", adata_sc_rna)
sr.register_modality("adt", adata_sc_adt)
sr.prepare()
sr.fit()

# Predict proportions
proportions_df = sr.predict(
    {"rna": adata_sp_rna, "adt": adata_sp_adt},
)

Documentation

Full documentation, API reference, and tutorials: https://sparank.readthedocs.io/en/latest.html.

Metadata

Release files for sparank 0.1.7

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

Source distribution (sdist)

Source distribution for sparank 0.1.7
File Size Uploaded
sparank-0.1.7.tar.gz 36.5 kB Details

Built distribution (wheel)

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

Total release size: 76.7 kB

Release files / sparank-0.1.7.tar.gz

Download URL sparank-0.1.7.tar.gz
Size 36.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ffbd6a238df6873c2b06225426367a4c22f92e2bd16ea47e960b4733e0287e84
BLAKE2b-256 checksum
How to use checksums
d681796127cc736f2a52fd5035f7d4ddc869cfcec24e41d27ace6a4e7cec9a21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / sparank-0.1.7-py3-none-any.whl

Download URL sparank-0.1.7-py3-none-any.whl
Size 40.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ef939e66f0a911829cdd97d2f9cdf6bdd6e57376427efe75d8be1690dbc73756
BLAKE2b-256 checksum
How to use checksums
3e3ff9a9c01275f06b904132db2b39100892727eef18659f75fa240e9ef2715c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 release files

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