sctransform
Pure-python implementation of R sctransform — variance-stabilizing transformation for single-cell UMI count data. No R, no rpy2 required.
Implements both model flavors of R sctransform:
- v2 model (
vst.flavor="v2"in R): negative-binomial GLM with library-size offset (slope fixed atlog(10)), per-gene intercept + overdispersion estimation, Poisson-gene exclusion. - v1 model (original sctransform): per-gene Poisson GLM with free slope on
log10_umi, theta via a port ofMASS::theta.ml.
Both use kernel-smoothed regularization of parameters — all validated end-to-end against the R reference implementation (with glmGamPoi).
Why
Existing python ports either wrap R via rpy2 (defeats the purpose) or delegate the v2 model to R's glmGamPoi. This package re-implements everything natively and numerically matches R:
| Component | Validation vs R |
|---|---|
| Step-1 NB offset GLM (theta, intercept) | log10 θ corr 0.994, intercept corr 0.9994 |
bw.SJ bandwidth selector |
exact to 7 significant digits |
ksmooth (Nadaraya-Watson, normal kernel) |
max diff 2e-4 |
is_outlier |
identical flags on real data |
| v2 end-to-end Pearson residuals (pbmc3k) | corr 0.99999 |
| v2 UMAP workflow vs Seurat | HVG Jaccard 0.993, KNN label transfer 0.97 |
| v1 end-to-end Pearson residuals (pbmc3k) | corr 1.000000 (regularized log10 θ corr 1.0000) |
See benchmark/ for the full executed notebook and reproduction scripts.
Installation
pip install sctransform
# or with the scanpy interface:
pip install sctransform[scanpy]
Quickstart
import scanpy as sc
from sctransform import SCTransform
# adata with raw counts in adata.layers["counts"]
residuals = SCTransform(adata, layer="counts", vst_flavor="v2", var_features_n=3000)
# downstream, Seurat-style: PCA on Pearson residuals of variable features
adata.obsm["X_pca"] = sc.pp.pca(residuals.values, n_comps=30)
sc.pp.neighbors(adata, use_rep="X_pca")
sc.tl.umap(adata)
Low-level API (R-like, genes x cells matrix in, full vst output out):
from sctransform import vst
vst_out = vst(umi_matrix, gene_names=genes, vst_flavor="v2")
vst_out["y"] # residuals (genes x cells DataFrame)
vst_out["model_parameters_fit"] # regularized per-gene theta / intercept / slope
vst_out["gene_attr"] # residual_variance etc. (for HVG ranking)
Scope
- v2 model (
glmGamPoi_offset): fixed slope, intercept + theta learned,exclude_poisson - v1 model (
poisson/theta_ml, free slope) — exact port ofqpois_regIRLS +MASS::theta.ml - Pearson & deviance residuals,
min_variance("umi_median"), residual clipping - Cell/gene subsampling (density-based gene sampling, as in R)
-
batch_varsupport — roadmap - corrected counts (
return_corrected_umi) — roadmap
Relationship to Seurat
SCTransform(adata, vst_flavor="v2") replaces NormalizeData + FindVariableFeatures + ScaleData:
use the returned residuals as scale.data for PCA (as shown above). HVG flags and
regularized model parameters are stored in adata.var / adata.uns["sct"].
Tutorials
tutorials/pbmc3k_sctransform_vs_lognormalize.ipynb- end-to-end pbmc3k analysis, side-by-side vs the traditional log-normalize workflow (variance-stabilization QC, UMAP, cluster agreement, markers, depth effects).
License & attribution
GPL-3.0. Contains code ported from R (GPL-2+, R Core Team) and algorithms re-implemented from R sctransform (GPL-3, Christoph Hafemeister / Rahul Satija lab).
If you use this package, please cite the original publications:
- Hafemeister & Satija, Normalization and variance stabilization of single-cell RNA-seq data using regularized negative binomial regression, Genome Biology 2019
- Choudhary & Satija, Comparison and evaluation of statistical error models for scRNA-seq, Genome Biology 2022
- Ahlmann-Eltze & Huber, glmGamPoi: Fitting Gamma-Poisson Generalized Linear Models on Single Cell Count Data, Bioinformatics 2021
Metadata
Release files for sctransform 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sctransform-0.2.0.tar.gz | 7.4 MB | With provenance | Details for sctransform-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sctransform-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for sctransform-0.2.0-py3-none-any.whl |
Total release size: 7.4 MB
Release files / sctransform-0.2.0.tar.gz
| Download URL | sctransform-0.2.0.tar.gz |
|---|---|
| Size | 7.4 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3fe3dbdc1731fc94a8aefeb7be0319b5d6747171c21f1138d2a179d803413315
|
|
BLAKE2b-256 checksum How to use checksums |
7c25c14386a6dff0d8b358aa5752ea832a03562c775cbf5b3e94fd8d22b5cb11
|
| 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 Aug 25, 2026.
Transparency logRelease files / sctransform-0.2.0-py3-none-any.whl
| Download URL | sctransform-0.2.0-py3-none-any.whl |
|---|---|
| Size | 30.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a99141b779d2c4d80c828416b24c901f1a89b4b59092d752efb6f7b362bee574
|
|
BLAKE2b-256 checksum How to use checksums |
ef78b8271e47454f06618a872d4fe9c0eb19f85ae22dcd6d22103bb35279cff8
|
| 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 Aug 25, 2026.
Transparency logSecurity for sctransform 0.2.0
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
All 2 files in this release have been uploaded with provenance metadata.
Attestation for sctransform-0.2.0.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 Aug 25, 2026.
Transparency logA 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.