Skip to main content

hs2p

PyPI version Python 3.10+ empty empty HuggingFace Space

hs2p is a Python package for fast, scalable whole-slide tiling and annotation-aware sampling. You can request tiles at any spacing, whether or not that spacing is natively present in the image pyramid. It is designed for computational pathology workflows that need reproducible coordinates, explicit artifacts, and backend-independent physical semantics.

We support two main workflows:

  • a Python API for library-style integration
  • a CLI for batch preprocessing

Demo

Try hs2p interactively: hs2p-demo on HuggingFace Spaces
You can adjust tiling parameters and inspect the resulting grid and mask previews.
You can also upload your own pyramidal WSI (up to 1 GB).

Installation

Base install:

pip install hs2p

Optional backend extras:

pip install "hs2p[openslide]"
pip install "hs2p[asap]"
pip install "hs2p[vips]"
pip install "hs2p[cucim]"
pip install "hs2p[turbojpeg]"
pip install "hs2p[all]"

Tile TAR export uses Pillow (speed.jpeg_backend: pil) by default, so it works with the base install. For higher JPEG encoding throughput, install the turbojpeg extra and explicitly set speed.jpeg_backend: turbojpeg. Explicit encoder choices are authoritative: hs2p reports a missing optional dependency instead of falling back to a different encoder.

Pillow also provides the base-install flat-raster reader (tiling.backend: pil). These two settings have different jobs: tiling.backend chooses the input reader, while speed.jpeg_backend chooses the tile-output JPEG encoder.

The supported input-reader backend set is:

  • auto
  • pil
  • cucim
  • vips
  • openslide
  • asap

For .png, .jpg, and .jpeg inputs (case-insensitive), auto selects only PIL. Corrupt, unsupported, or oversized flat rasters fail through PIL without probing or recommending a native WSI backend. Other inputs retain the cucim -> vips -> openslide -> asap openability chain; PIL is never tried for them. Slide and source-mask paths apply this format policy independently. Because the priority changed, an existing auto configuration can resolve to a different backend; resumed runs may therefore reject and recompute backend-dependent artifacts. Pin an explicit backend when preserving the previous decoder is required.

SAM2-based tissue segmentation requires a separate additional install:

pip install "hs2p[sam2]"
pip install "git+https://github.com/facebookresearch/sam2.git"

Workflows

Tiling

Tiling computes a reproducible grid of tile coordinates for each slide and saves them as explicit named artifacts. When a precomputed tissue mask is not provided, hs2p segments tissue on the fly. If you want to create those masks ahead of time, a standalone script is available.

hs2p tiling workflow

Sampling

Sampling filters or partitions tile coordinates by annotation coverage so you can keep only tiles relevant to a label or tissue class.

hs2p sampling workflow

Python API

Minimal tiling example:

from pathlib import Path

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

result = tile_slide(
    SlideSpec(
        sample_id="slide-1",
        image_path=Path("/data/wsi/slide-1.tif"),
        mask_path=Path("/data/mask/slide-1-tissue-mask.tif"), # optional
    ),
    tiling=TilingConfig(
        backend="openslide",
        requested_spacing_um=0.5,
        requested_tile_size_px=224,
        tolerance=0.07,
        overlap=0.0,
        tissue_threshold=0.1,
    ),
)

# save tiling results to disk
artifacts = save_tiling_result(result, output_dir=Path("output"))

print(artifacts.coordinates_npz_path)   # output/tiles/slide-1.coordinates.npz
print(artifacts.coordinates_meta_path)  # output/tiles/slide-1.coordinates.meta.json

# preview tile grid
tiling_preview_path = write_tiling_preview(
    result=result,
    output_dir=Path("output"),
    downsample=32,
)
print(tiling_preview_path)  # output/preview/tiling/slide-1.jpg

result is a canonical hs2p.preprocessing.TilingResult. Downstream code should use its structured fields such as:

  • x
  • y
  • tissue_fractions
  • tile_index
  • requested_*
  • effective_*
  • min_tissue_fraction

More API details: docs/api.md

CLI

The CLI is intended for fast batch processing of multiple slides with the same config.
Both entrypoints read the same public mask_path column, and the command determines whether that path is treated as a tissue mask or an annotation mask:

Tiling csv (mask_path is optional and means a tissue mask here):

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,
...

Sampling csv (mask_path is mandatory and means an annotation mask here):

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

Run:

hs2p /path/to/config.yaml

For a first run, start from hs2p/configs/default.yaml and edit only the essentials:

  • csv
  • output_dir
  • tiling.backend
  • tiling.params.requested_spacing_um
  • tiling.params.requested_tile_size_px

More details about CLI: docs/cli.md

Outputs

hs2p writes explicit named artifacts rather than anonymous coordinate dumps.

  • Tiling writes tiles/{sample_id}.coordinates.npz and tiles/{sample_id}.coordinates.meta.json
  • Sampling writes the same pair under tiles/<annotation>/
  • Batch runs also write process_list.csv
  • Saved coordinate arrays use a deterministic order: numeric x first, then numeric y within each shared x

Artifact field reference: docs/artifacts.md

Docker

Docker Version

If you prefer running hs2p in a container, a published Docker image is available:

docker pull waticlems/hs2p:latest
docker run --rm -it -v /path/to/your/data:/data waticlems/hs2p:latest

Documentation

Download files

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

Source Distribution

hs2p-4.4.2.tar.gz (196.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hs2p-4.4.2-py3-none-any.whl (118.7 kB view details)

Uploaded Python 3

File details

Details for the file hs2p-4.4.2.tar.gz.

File metadata

  • Download URL: hs2p-4.4.2.tar.gz
  • Upload date:
  • Size: 196.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.15

File hashes

Hashes for hs2p-4.4.2.tar.gz
Algorithm Hash digest
SHA256 667042f828435453e5eb9e2aace5e27c9d406e271454f5828503960e7d304cbd
MD5 40a753dde1b0416ead7a16e6afeb873d
BLAKE2b-256 28b9e3cc7949004235deb15898d7115ca26a60cb3a4aa3ca24872acfb63b60bb

See more details on using hashes here.

File details

Details for the file hs2p-4.4.2-py3-none-any.whl.

File metadata

  • Download URL: hs2p-4.4.2-py3-none-any.whl
  • Upload date:
  • Size: 118.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.15

File hashes

Hashes for hs2p-4.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ea7e40516ecd717e0e203ced2ee9764cf67e70ceca5341cfe00e8b8b294d5067
MD5 fa4c96475e2367a8a404baa5e2c55035
BLAKE2b-256 94bdd0aa9f0ae6c6c867a3874e525e4882c98182bc668cf74c2408395c292f14

See more details on using hashes here.

Release history Release notifications | RSS feed

4.4.3

2 files

This release

4.4.2 This release

2 files

4.4.1

2 files

4.4.0

2 files

4.3.0

2 files

4.2.0

2 files

4.1.2

2 files

4.1.1

2 files

4.1.0

2 files

4.0.8

2 files

4.0.7

2 files

4.0.6

2 files

4.0.5

2 files

4.0.4

2 files

4.0.3

2 files

4.0.2

2 files

4.0.1

2 files

4.0.0

2 files

3.3.0

2 files

3.2.1

2 files

3.2.0

2 files

3.1.5

2 files

3.1.4

2 files

3.1.3

2 files

3.1.2

2 files

3.1.1

2 files

3.1.0

2 files

3.0.1

2 files

3.0.0

2 files

2.5.1

2 files

2.5.0

2 files

2.4.1

2 files

2.4.0

2 files

2.3.0

2 files

2.2.1

2 files

2.2.0

2 files

2.1.0

2 files

2.0.0

2 files

1.1.1

2 files

1.1.0

2 files

1.0.1

2 files

1.0.0

2 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