Skip to main content

rctd-py logo

rctd-py

Spatial transcriptomics deconvolution, 10–57x faster than R spacexr on a GPU (doublet mode)

CI PyPI Python Documentation License: GPL v3

New in v0.4.0: rctd-py now gives the same results as R spacexr wherever both use the same sigma. Getting there aligned a few details of the fit with R (bulk platform-effect fit, sigma sampling, singlet labels, cell-type order, QP solver), so results can differ from 0.3.9, mostly on sequencing-based data such as Visium. On imaging data (Xenium, CosMx, Atera) spot classes are almost unchanged. Numbers are in the changelog; rctd-py==0.3.9 stays available to reproduce earlier results.


A Python reimplementation of the spacexr RCTD algorithm (Cable et al., Nature Biotechnology 2022) with GPU acceleration via PyTorch.

Deconvolve spatial transcriptomics spots (Visium, Xenium, MERFISH, Slide-seq, …) into cell type proportions using a scRNA-seq reference — in minutes instead of hours.

Documentation: rctd-py.readthedocs.io (installation, tutorials, CLI, API reference, coming from R spacexr)

✨ Highlights

⚡ 10–57x faster than R spacexr Doublet mode on 19 datasets, one GPU vs 8 R cores. Xenium Prime 5K, 503k cells fitted: 34 min vs 31 hours in R
🎯 Matches R spacexr Same calls as R wherever both pick the same sigma (11 of 19 datasets); 99.0–99.9% of spot classes on the rest, where sigma differs by 0.01–0.02, as it does between two R runs (details)
🔧 Familiar to spacexr users Same algorithm and, with a few exceptions, the same parameter names (mapping)
📦 pip install rctd-py Pure Python, works on CPU out of the box (install rctd-py[cpu] for Numba CPU kernels; 3.7–14x faster than R in doublet mode on v0.4.0, faster since v0.4.5), uses a GPU when there is one
🧬 Whole-transcriptome ready 10x Atera WTA (18,028-gene panel): 714k cells deconvolved in 9 min on GPU (doublet mode, 7 cell types)

Installation

uv pip install rctd-py          # or: pip install rctd-py
uv pip install "rctd-py[cpu]"   # recommended on CPU: adds Numba kernels

GPU builds of PyTorch, older NVIDIA drivers and offline / HPC installs (the 404 MB Q-matrices file) are covered in the installation guide.

Quick Start

from rctd import Reference, run_rctd
import anndata

reference = Reference(anndata.read_h5ad("reference.h5ad"), cell_type_col="cell_type")
spatial = anndata.read_h5ad("spatial.h5ad")

# normalisation, sigma estimation and deconvolution in one call
result = run_rctd(spatial, reference, mode="doublet")

# or write the results into the AnnData (obs["rctd_first_type"], obsm["rctd_weights"], ...)
from rctd import deconvolve
deconvolve(spatial, reference, mode="doublet")

scverse style, AnnData in and out (layer= for raw counts kept in a layer, key_added= for the key prefix). Both steps need raw counts:

import rctd

ref = rctd.pp.reference(ref_adata, cell_type_key="cell_type", layer="counts")
rctd.tl.deconvolve(spatial, ref, mode="doublet", layer="counts")
Mode What it does Best for
full Weights for all K cell types per pixel Visium spots, continuous mixtures
doublet Singlet or doublet call per pixel, top 1–2 types Slide-seq, Visium HD bins, segmented cells (Xenium, MERFISH, CosMx)
multi Greedy selection of up to 4 types per pixel Spots expected to hold more than 2 types (an alternative to full)

📓 Tutorial (marimo notebook) · 🤖 Claude Code skill (copy the .claude/skills/rctd-py/ folder into ~/.claude/skills/ for assisted coding with rctd-py)

Command Line

rctd info                                                # versions, GPU availability
rctd validate spatial.h5ad reference.h5ad                # fast input checks
rctd run spatial.h5ad reference.h5ad --mode doublet -o results.h5ad
rctd run experiment.zarr reference.h5ad --table cell_table   # SpatialData stores work too

All options and the output layout: command line reference.

Benchmarks

Doublet mode wall time of R spacexr, rctd-py CPU and rctd-py GPU on 19 datasets

rctd-py on one NVIDIA L40S or 8 CPU threads, R spacexr 2.2.1 on 8 cores; rctd-py points are the median of 3 runs. Full and multi mode, per-dataset tables and memory: benchmarks.

The plots above were measured on v0.4.0. Subsequent releases sped up CPU runs substantially (6-10x in v0.4.5 with Numba kernels and array bookkeeping, plus another 1.61x total / 2.16x mode geomean speedup in v0.4.6 across full, doublet, and multi) as well as large-K GPU runs (1.38x total geomean on L40S in v0.4.6, reaching 2.02x at K=78 and up to 2.74x total / 4.33x mode).

Citation

If you use rctd-py, please cite the original RCTD paper:

@article{cable2022robust,
  title={Robust decomposition of cell type mixtures in spatial transcriptomics},
  author={Cable, Dylan M and Murray, Evan and Zou, Luli S and Goeva, Aleksandrina and Macosko, Evan Z and Chen, Fei and Irizarry, Rafael A},
  journal={Nature Biotechnology},
  volume={40},
  number={4},
  pages={517--526},
  year={2022},
  doi={10.1038/s41587-021-00830-w}
}

Contributing

Contributions welcome! See CONTRIBUTING.md for setup instructions, or open an issue.

License

GNU General Public License v3.0

Metadata

Release files for rctd-py 0.4.6

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

Source distribution (sdist)

Source distribution for rctd-py 0.4.6
File Size Uploaded Provenance Actions
rctd_py-0.4.6.tar.gz 642.3 kB Without provenance Details for rctd_py-0.4.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rctd-py 0.4.6
File Interpreter ABI Platform Actions
rctd_py-0.4.6-py3-none-any.whl Python 3 none any Details for rctd_py-0.4.6-py3-none-any.whl

Total release size: 733.4 kB

Release files / rctd_py-0.4.6.tar.gz

Download URL rctd_py-0.4.6.tar.gz
Size 642.3 kB
Tags Source
SHA-256 checksum
How to use checksums
91cfe2fdf5fabf83ab4f87a7c2bd240c3facf9afef06bfbcf8b2920acb3d4606
BLAKE2b-256 checksum
How to use checksums
1d0061ff7e4f607c3d407f60f903e8ad92067393f3dfaa3536387ed11bd8ab6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / rctd_py-0.4.6-py3-none-any.whl

Download URL rctd_py-0.4.6-py3-none-any.whl
Size 91.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e4d86c1b9fa139d6d495f448239631b358cca1a473305ed796e45c4c2511b457
BLAKE2b-256 checksum
How to use checksums
3347d26a8c402f9d41cbbc7bf7bf8896b6511b7d210e392cbc6254c5da57104b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for rctd-py 0.4.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