Optimized slide tiling library for histopathology
Project description
hs2p
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[all]"
The supported backend set is:
autocucimvipsopenslideasap
auto prefers cucim -> vips -> openslide -> asap.
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.
Sampling
Sampling filters or partitions tile coordinates by annotation coverage so you can keep only tiles relevant to a label or tissue class.
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",
target_spacing_um=0.5,
target_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:
xytissue_fractionstile_indexrequested_*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 tiling:
python -m hs2p.cli.tiling --config-file /path/to/config.yaml
Run sampling:
python -m hs2p.cli.sampling --config-file /path/to/config.yaml
For a first run, start from hs2p/configs/default.yaml and edit only the essentials:
csvoutput_dirtiling.backendtiling.params.target_spacing_umtiling.params.target_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.npzandtiles/{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
xfirst, then numericywithin each sharedx
Artifact field reference: docs/artifacts.md
Docker
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hs2p-3.1.3.tar.gz.
File metadata
- Download URL: hs2p-3.1.3.tar.gz
- Upload date:
- Size: 135.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6483f1d4fd0751bb15bf5dd388b1f4cbf8e51e2d6eca955342c0541637e1901
|
|
| MD5 |
62d5b7752f00d4499d23ee4df51d7590
|
|
| BLAKE2b-256 |
84648a67354d7b45c82c5f51c4ac7068e4698c19130b0beffd3e744e659928e6
|
File details
Details for the file hs2p-3.1.3-py3-none-any.whl.
File metadata
- Download URL: hs2p-3.1.3-py3-none-any.whl
- Upload date:
- Size: 92.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ff44421a3c322e6db5636413223bb745042dc3de7a2eb0b729016cec1e16926
|
|
| MD5 |
a15aefcd6b3ad9ee5c9f3ec8e206dc66
|
|
| BLAKE2b-256 |
0802aa7aa2e11a3ff08f26dc89c27f5066688aed8e9a4569eed8e252a8a1e350
|