Alterlab histolab

From AlterLab-IEU/AlterLab-Academic-Skills · View interactive version

Alterlab histolab is an AI agent skill from the GitHub repository AlterLab-IEU/AlterLab-Academic-Skills. Extract and preprocess tiles from whole-slide images (WSI) with histolab — OpenSlide-backed slide loading, tissue detection and masks, Random/Grid/Score tile extraction, and image/morphological filters for H&E preprocessing. Use when the user needs lightweight WSI slide preprocessing — building tile

How to use this skill

Skill file

Histolab

Overview

Histolab is a Python library for processing whole slide images (WSI) in digital pathology. It automates tissue detection, extracts informative tiles from gigapixel images, and prepares datasets for deep learning pipelines. The library handles multiple WSI formats, implements sophisticated tissue segmentation, and provides flexible tile extraction strategies.

When to Use This Skill

Use histolab for lightweight WSI tile pipelines: tissue detection, building tile datasets for ML training, H&E stain handling, and quick tile-based analysis of histopathology slides. For advanced spatial proteomics, multiplexed imaging, or full deep-learning pathology pipelines, use alterlab-pathml instead.

Does NOT Trigger

| Scenario | Use Instead |

|----------|-------------|

| End-to-end computational pathology, nucleus segmentation, multiplexed imaging (CODEX/Vectra) | alterlab-pathml |

| Training the downstream classifier on the extracted tiles | alterlab-pytorch-lightning |

| DICOM whole-slide or radiology images rather than SVS/NDPI | alterlab-pydicom, alterlab-imaging-data-commons |

| Spatial transcriptomics on tissue sections | alterlab-squidpy-spatial |

| Managing/versioning the slide collection itself | alterlab-omero, alterlab-lamindb |

| Single-cell RNA-seq analysis of an .h5ad (QC, normalisation, Leiden clustering, marker genes) | alterlab-scanpy |

Hand-off rule. When the request matches a row above, open your answer by naming that skill by its exact id (for example alterlab-pathml) and saying in one line why it fits better, then stop applying this skill's procedure. Name it even when you also need to ask for missing input. If only part of the request is out of scope, name the skill for that part and do only the in-scope part. A brief general pointer is fine; the named skill does the work.

Installation

# histolab 0.7.0 needs Python >= 3.8, < 3.12 and numpy 1.x — give it its own env
uv venv --python 3.11 .venv-histolab
uv pip install --python .venv-histolab "histolab==0.7.0" openslide-bin

Two constraints that bite in a 2026 toolchain:

  1. Pinned scientific stack. 0.7.0 (Feb 2024, still the latest release) requires

numpy <= 1.24.4, scipy < 1.10.1, scikit-image < 0.19.4 and

openslide-python 1.3.1. It cannot share an environment with a numpy 2.x / Python

3.12+ project — resolve this with a separate venv rather than by loosening pins.

  1. OpenSlide is a C library, not bundled with the wheel. The simplest fix is the

openslide-bin wheel (prebuilt binaries); otherwise brew install openslide on macOS

or apt install libopenslide0 on Debian/Ubuntu. Without it, import histolab.slide

fails with Couldn't locate OpenSlide dylib.

The examples below target histolab 0.7.0; the API differs in older releases.

Core Workflow

  1. Load the slide with Slide(path, processed_path=...) and inspect dimensions/levels.
  2. Detect tissue with a mask (TissueMask or BiggestTissueBoxMask).
  3. Preview tile locations with tiler.locate_tiles(slide) before committing.
  4. Extract tiles with one of three tilers (Random/Grid/Score).

Minimal example:

from histolab.slide import Slide
from histolab.tiler import RandomTiler

slide = Slide("slide.svs", processed_path="output/")
# n_tiles, level, seed are CONSTRUCTOR args — not args to locate_tiles/extract.
tiler = RandomTiler(tile_size=(512, 512), n_tiles=100, level=0, seed=42)
tiler.locate_tiles(slide)   # preview locations on the thumbnail first
tiler.extract(slide)        # writes PNGs into processed_path

API gotcha (histolab 0.7.0): locate_tiles() and extract() take only

slide, an optional extraction_mask, and logging/styling kwargs — they do

not accept n_tiles. Set n_tiles (and seed, level, tile_size,

check_tissue, tissue_percent) on the tiler constructor. The

extraction_mask is passed to extract()/locate_tiles(), never to the

constructor.

Full copy-pasteable pipelines (quick start, 5 end-to-end workflows, and per-capability examples) live in references/workflows.md.

Core Capabilities

1. Slide Management

Load, inspect, and work with WSI files (SVS, TIFF, NDPI, etc.): access metadata

(dimensions, magnification, properties), generate thumbnails, and work with

pyramidal/multi-level structures. Key class: Slide.

See references/slide_management.md for slide initialization, built-in sample

datasets (prostate_tissue, ovarian_tissue, breast_tissue, heart_tissue,

aorta_tissue, plus pen-marked and IHC samples), pyramid levels, and

multi-slide processing.

2. Tissue Detection and Masks

Automatically identify tissue regions and filter background/artifacts. Key

classes: TissueMask (all tissue regions), BiggestTissueBoxMask (bounding box

of largest region — the default), and BinaryMask (base class for custom masks).

Choosing a mask:

See references/tissue_masks.md for how detection filters work, visualizing

masks with locate_mask(), and custom rectangular / annotation-exclusion masks.

3. Tile Extraction

Extract smaller regions from large WSI using one of three strategies:

diverse regions, exploration, training data. Key params: n_tiles, seed.

spatial analysis, reconstruction. Key param: pixel_overlap.

regions, quality-driven selection. Key param: scorer (NucleiScorer,

CellularityScorer, custom).

Common parameters: tile_size, level (0 = highest res), check_tissue,

tissue_percent (default 80%), extraction_mask. **Always preview with

locate_tiles() before extracting.**

See references/tile_extraction.md for scorers, reporting, and advanced

(multi-level, hierarchical) extraction patterns.

4. Filters and Preprocessing

Apply image-processing filters for tissue detection, QC, and preprocessing:

Invert, StretchContrast, HistogramEqualization, Lambda.

BinaryClosing, RemoveSmallObjects, RemoveSmallHoles.

filters into pipelines. Pass custom filters to a mask as positional varargs:

TissueMask(RgbToGrayscale(), OtsuThreshold(), ...).

See references/filters_preprocessing.md for filter chaining, common pipelines

(tissue detection, pen removal, nuclei enhancement), and QC filters.

5. Visualization

Display slides, masks, tile locations, and extraction quality: thumbnails, mask

overlays via locate_mask(), tile-location previews via locate_tiles(), tile

mosaics, and score distributions.

See references/visualization.md for mosaics, quality-assessment plots,

multi-slide comparison, and exporting high-resolution figures / PDF reports.

Reference Index

end-to-end worked workflows (exploratory, grid, score-driven, multi-slide,

custom tissue detection).

pyramid levels, multi-slide processing.

custom masks, mask visualization and integration.

CSV reporting, advanced extraction patterns.

composition, preprocessing pipelines, QC filters.

quality plots, figure export.

troubleshooting (no tiles, background tiles, slow extraction, artifacts).

Load the specific reference file you need for detailed implementation guidance,

troubleshooting, or advanced features.

Part of the AlterLab Academic Skills suite.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.