Skip to main content

hs2p

PyPI version Python 3.10+ Code style: Black GitHub stars Hugging Face demo

hs2p tiles whole-slide images at a requested physical resolution (microns per pixel), including resolutions absent from the image pyramid. Use the Python API in your own pipeline or the CLI for batch preprocessing. Both produce reproducible tile coordinates and can select tiles by tissue or annotation coverage.

Try the interactive demo to adjust tiling parameters and inspect grids and mask previews, including on your own pyramidal WSI (up to 1 GB).

Installation

Python 3.10 or newer is required. For whole-slide images read with OpenSlide:

pip install "hs2p[openslide]"

For flat PNG/JPEG slides, pip install hs2p is sufficient; supply their physical spacing with spacing_at_level_0. Flat masks need no spacing. See the backend guide for other readers, optional JPEG encoding, and SAM2 installation.

Workflows

Tiling computes a grid over tissue, using a supplied mask or segmenting tissue on the fly. Masks can have a different resolution from the slide. To prepare masks in advance, use the tissue-mask generation script.

hs2p tiling workflow

Annotation sampling selects tiles by label coverage and writes per-class or merged coordinates.

hs2p sampling workflow

Python quick start

from pathlib import Path

from hs2p import (
    SegmentationConfig,
    SlideSpec,
    TilingConfig,
    save_tiling_result,
    tile_slide,
    write_tiling_preview,
)

result = tile_slide(
    SlideSpec(
        sample_id="slide-1",
        image_path=Path("/data/wsi/slide-1.tif"),
        # Optional: omit to segment tissue on the fly.
        mask_path=Path("/data/mask/slide-1-tissue-mask.tif"),
    ),
    tiling=TilingConfig(
        backend="openslide",
        mask_backend="openslide",
        requested_spacing_um=0.5,
        requested_tile_size_px=224,
        tolerance=0.07,
        overlap=0.0,
        min_coverage={"tissue": 0.1},
    ),
    segmentation=SegmentationConfig(method="hsv", downsample=64),
)

artifacts = save_tiling_result(result, output_dir=Path("output"))
print(artifacts.coordinates_meta_path)  # output/tiles/slide-1.coordinates.meta.json

if result.x.size:
    print(artifacts.coordinates_npz_path)  # output/tiles/slide-1.coordinates.npz
    write_tiling_preview(result=result, output_dir=Path("output"), downsample=32)

tile_slide() returns an in-memory TilingResult; tile_slides() processes a batch and saves its results. See the API guide for result fields, batch processing, and artifact loading.

CLI quick start

Create slides.csv with a unique sample_id and an image_path for each slide. mask_path is optional for tissue tiling:

sample_id,image_path,mask_path
slide-1,/data/wsi/slide-1.tif,/data/mask/slide-1-tissue-mask.tif
slide-2,/data/wsi/slide-2.tif,

Save this as config.yaml; omitted settings inherit the default config:

csv: slides.csv
output_dir: output
tiling:
  backend: openslide
  params:
    requested_spacing_um: 0.5
    requested_tile_size_px: 224

Run:

hs2p config.yaml

Results go into a dated subdirectory of output. Annotation sampling uses the same command and CSV schema, with a required annotation mask_path and label settings under tiling.masks. See the CLI guide for sampling, previews, tile export, and resume.

Outputs

Each non-empty result saves .coordinates.npz arrays and .coordinates.meta.json metadata. Empty results save metadata only. Coordinates are in level-0 pixels, sorted by numeric x, then y.

Tissue tiling writes under tiles/; annotation sampling can write under tiles/<annotation>/ or merge results under tiles/. Batch runs also write process_list.csv. The artifact reference describes paths, fields, and reuse compatibility.

Docker

Docker Version

Run the CLI in the published container:

docker pull waticlems/hs2p:latest
docker run --rm -v /path/to/your/data:/data \
  waticlems/hs2p:latest hs2p /data/config.yaml

Use paths inside the container in the CSV and config, and set output_dir under /data to retain results on the host. The mounted output directory must be writable by the container user.

Continue with the documentation index for guides, benchmarks, and release notes.

Metadata

Release files for hs2p 5.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hs2p 5.0.2
File Size Uploaded
hs2p-5.0.2.tar.gz 220.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hs2p 5.0.2
File Interpreter ABI Platform
hs2p-5.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 343.5 kB

Release files / hs2p-5.0.2.tar.gz

Download URL hs2p-5.0.2.tar.gz
Size 220.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a348ae7e6609ee8105d83fa91ba64954c3331e6f90f361da4614dbb3553c2e11
BLAKE2b-256 checksum
How to use checksums
77622dfeaa3660c116ead0189a5477fa945a3d5567dcfe65384119e7239c6385
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release files / hs2p-5.0.2-py3-none-any.whl

Download URL hs2p-5.0.2-py3-none-any.whl
Size 123.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2af3a6d710e3d930195c009bbe835ae9f569091219f125c119321c981a9ec006
BLAKE2b-256 checksum
How to use checksums
ce71a526ace3480c5e0f658c87271153531776cbfd2d16a867bff23165954504
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release history Release notifications | RSS feed

6.1.0

2 release files

6.0.0

2 release files

5.1.0

2 release files

This release

5.0.2 This release

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.5.0

2 release files

4.4.3

2 release files

4.4.2

2 release files

4.4.1

2 release files

4.4.0

2 release files

4.3.0

2 release files

4.2.0

2 release files

4.1.2

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.8

2 release files

4.0.7

2 release files

4.0.6

2 release files

4.0.5

2 release files

4.0.4

2 release files

4.0.3

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.3.0

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.5

2 release files

3.1.4

2 release files

3.1.3

2 release files

3.1.2

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.5.1

2 release files

2.5.0

2 release files

2.4.1

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page