Enhanced stain normalization for histopathology images with batch processing support. Optimized for CPU, GPU (CUDA), and MPS (Apple Silicon) devices.
Project description
StainX
Enhanced stain normalization for histopathology images with batch processing support. Optimized for CPU, GPU (CUDA), and MPS (Apple Silicon) devices.
Features
- Multiple algorithms: Histogram Matching, Reinhard, and Macenko normalization
- Automatic backend selection: PyTorch (CPU/GPU/MPS) or optimized CUDA kernels
- Batch processing: Enhanced normalization through efficient batch processing of multiple images
- Flexible device support: CPU, CUDA, MPS (Apple Silicon)
Installation
Requirements
- Python >=3.11
- PyTorch >=2.0.0
- CUDA (optional, for GPU acceleration)
Install from PyPI
pip install stainx
Install from source
git clone https://github.com/rendeirolab/stainx.git
cd stainx
pip install .
CUDA extensions will be automatically built if CUDA is available.
Quick Start
import torch
from stainx import Reinhard, Macenko, HistogramMatching
# Load your images as torch.Tensor (N, C, H, W) or (N, H, W, C)
reference_image = torch.randn(1, 3, 512, 512) # Reference image
source_images = torch.randn(10, 3, 512, 512) # Images to normalize
# Reinhard normalization
normalizer = Reinhard(device="cuda") # or "cpu"
normalizer.fit(reference_image)
normalized = normalizer.transform(source_images)
# Macenko normalization
normalizer = Macenko(device="cuda")
normalizer.fit(reference_image)
normalized = normalizer.transform(source_images)
# Histogram Matching
normalizer = HistogramMatching(device="cuda", channel_axis=1)
normalizer.fit(reference_image)
normalized = normalizer.transform(source_images)
API
All normalizers follow a scikit-learn-like interface:
fit(reference_images): Compute normalization parameters from reference image(s)transform(images): Apply normalization to imagesfit_transform(images): Fit and transform in one step
Available Normalizers
Reinhard: Reinhard color normalizationMacenko: Macenko stain separation and normalizationHistogramMatching: Histogram matching normalization
Backend Selection
Backends are automatically selected based on device availability:
- CUDA: Used when CUDA is available and device is set to CUDA
- PyTorch: Fallback backend, works on CPU and GPU
You can explicitly specify a backend:
normalizer = Reinhard(device="cuda", backend="torch") # Force Torch backend
Requirements
- Python >=3.11
- PyTorch >=2.0.0
- CUDA Toolkit (optional, for CUDA backend)
License
This project is licensed under the GNU General Public License v3 (GPL-3.0-or-later).
Contributing
See CONTRIBUTING.md for guidelines on contributing to StainX.
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file stainx-0.0.19.tar.gz.
File metadata
- Download URL: stainx-0.0.19.tar.gz
- Upload date:
- Size: 41.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77ee028ebb9b58778388aeaf3404348d23674cc2fc0082f617f43b49b7200217
|
|
| MD5 |
aa93923bf02cae01e5bc986cc8255579
|
|
| BLAKE2b-256 |
c22c20f7e3229a7086b09d4f7b84c307fe3bd507fe719c14e0c9f06d28e5ea76
|
Provenance
The following attestation bundles were made for stainx-0.0.19.tar.gz:
Publisher:
ci.yml on rendeirolab/stainx
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
stainx-0.0.19.tar.gz -
Subject digest:
77ee028ebb9b58778388aeaf3404348d23674cc2fc0082f617f43b49b7200217 - Sigstore transparency entry: 837184673
- Sigstore integration time:
-
Permalink:
rendeirolab/stainx@0facd35877210cc2ac8a6db4366926028fcc8999 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/rendeirolab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@0facd35877210cc2ac8a6db4366926028fcc8999 -
Trigger Event:
push
-
Statement type: