StainX
Torch-first stain normalization for histopathology images with batch processing, training transforms, and optional CUDA kernels.
0.1.0 migration: CuPy backends are removed. Pin
stainx<0.1to stay on the CuPy stack, or switch to Torch tensors withbackend="torch"/"torch_cuda".
Features
- Multiple algorithms: Histogram Matching, Reinhard, and Macenko normalization
- Torch backends:
torch(CPU / CUDA / MPS) and optionaltorch_cudacompiled kernels - Training-ready:
StainNormalizerTransformfor DataLoader / torchvision pipelines
Installation
Requirements
- Python >= 3.11
- PyTorch >= 2.0.0
- CUDA Toolkit + nvcc (optional; builds
stainx_cuda_torchwhen available)
Supported platforms
| Platform | Support |
|---|---|
| Linux + CUDA | Primary (Torch + optional CUDA extension) |
| Linux CPU | Primary (Torch backend) |
| macOS (MPS / CPU) | Best-effort Torch path (no CUDA extension) |
| Windows | Best-effort Torch path (CUDA extension not guaranteed) |
Install from PyPI
pip install stainx
Install from source (recommended: Makefile)
git clone https://github.com/rendeirolab/stainx.git
cd stainx
make install # editable + best-effort CUDA build
# or
make install-dev # + test/docs tooling
Plain pip also works:
pip install .
# CUDA extension builds automatically when CUDA/nvcc are present; otherwise Torch-only.
Quick Start
import torch
from stainx import Reinhard, Macenko, HistogramMatching, StainNormalizerTransform
reference_image = torch.randn(1, 3, 512, 512)
source_images = torch.randn(10, 3, 512, 512)
normalizer = Reinhard(device="cuda") # or "cpu" / "mps"
normalizer.fit(reference_image)
normalized = normalizer.transform(source_images)
# Training transform (fit once on a reference — preferred for supervised training)
transform = StainNormalizerTransform(
method="macenko",
mode="reference",
reference=reference_image,
device="cuda",
# normalize_to_0_1 defaults to True for Macenko (float [0,1] pipelines)
)
batch_out = transform(source_images)
Modes
| Mode | Behavior | When to use |
|---|---|---|
reference |
Fit once on a fixed reference, then transform | Default for training |
batch |
Fit on the current batch every forward | Exploratory / domain-shift checks; usually unsafe for reproducible supervised training |
API
fit(reference_images)/transform(images)/fit_transform(images)StainNormalizerTransform—nn.Modulefor pipelines- Backends:
"torch"(default) or"torch_cuda"when the extension is built
Documentation
See the documentation site for installation details, training usage, and examples.
License
GPL-3.0-or-later
Release files for stainx 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| stainx-0.1.3.tar.gz | 40.7 kB | Details |
Release files / stainx-0.1.3.tar.gz
| Download URL | stainx-0.1.3.tar.gz |
|---|---|
| Size | 40.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7232f3ce5bbc1f120318a5b2584105b0d2033a3f51b6c137a02c6542202767a3
|
|
BLAKE2b-256 checksum How to use checksums |
45713554a1130098d0b3a84eb14bbe961062907054d3512299aded669bfe908a
|
| 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 Jul 30, 2026.
Transparency log