Skip to main content

Enhanced stain normalization for histopathology images with batch processing support. Optimized for CPU, GPU (CUDA), and MPS (Apple Silicon) devices.

Project description

StainX

CI Python

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 images
  • fit_transform(images): Fit and transform in one step

Available Normalizers

  • Reinhard: Reinhard color normalization
  • Macenko: Macenko stain separation and normalization
  • HistogramMatching: 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="pytorch")  # Force PyTorch 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stainx-0.0.18.tar.gz (37.7 kB view details)

Uploaded Source

File details

Details for the file stainx-0.0.18.tar.gz.

File metadata

  • Download URL: stainx-0.0.18.tar.gz
  • Upload date:
  • Size: 37.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for stainx-0.0.18.tar.gz
Algorithm Hash digest
SHA256 3919ec9d05b4fdf50f304898170b006e3845c3b0f7c85efd059411ee10ec434a
MD5 f7b984ce10c97090400c4f5696b45fd7
BLAKE2b-256 570a107b48dd01230d993b2d5d6003db677d2a47acafc7f015405f42d77fc26e

See more details on using hashes here.

Provenance

The following attestation bundles were made for stainx-0.0.18.tar.gz:

Publisher: ci.yml on rendeirolab/stainx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page