pytessera 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytessera-0.1.0/LICENSE +21 -0
- pytessera-0.1.0/PKG-INFO +134 -0
- pytessera-0.1.0/README.md +86 -0
- pytessera-0.1.0/pyproject.toml +100 -0
- pytessera-0.1.0/setup.cfg +4 -0
- pytessera-0.1.0/src/pytessera/__init__.py +129 -0
- pytessera-0.1.0/src/pytessera/_internal/__init__.py +8 -0
- pytessera-0.1.0/src/pytessera/_internal/mask.py +44 -0
- pytessera-0.1.0/src/pytessera/_internal/memory.py +40 -0
- pytessera-0.1.0/src/pytessera/annotations/__init__.py +118 -0
- pytessera-0.1.0/src/pytessera/annotations/_resources.py +38 -0
- pytessera-0.1.0/src/pytessera/annotations/asap.py +88 -0
- pytessera-0.1.0/src/pytessera/annotations/cache.py +149 -0
- pytessera-0.1.0/src/pytessera/annotations/create.py +247 -0
- pytessera-0.1.0/src/pytessera/annotations/maskspec.py +146 -0
- pytessera-0.1.0/src/pytessera/annotations/ndpa.py +155 -0
- pytessera-0.1.0/src/pytessera/annotations/parse.py +164 -0
- pytessera-0.1.0/src/pytessera/annotations/render.py +433 -0
- pytessera-0.1.0/src/pytessera/annotations/roles.py +126 -0
- pytessera-0.1.0/src/pytessera/annotations/templates/__init__.py +7 -0
- pytessera-0.1.0/src/pytessera/annotations/templates/annotation_template.dcm +0 -0
- pytessera-0.1.0/src/pytessera/cli/__init__.py +184 -0
- pytessera-0.1.0/src/pytessera/cli/__main__.py +5 -0
- pytessera-0.1.0/src/pytessera/cli/_runtime.py +157 -0
- pytessera-0.1.0/src/pytessera/examples_data/__init__.py +133 -0
- pytessera-0.1.0/src/pytessera/io/__init__.py +62 -0
- pytessera-0.1.0/src/pytessera/io/cache.py +72 -0
- pytessera-0.1.0/src/pytessera/io/case.py +249 -0
- pytessera-0.1.0/src/pytessera/io/dicom.py +304 -0
- pytessera-0.1.0/src/pytessera/io/dicom_pyramid.py +174 -0
- pytessera-0.1.0/src/pytessera/io/fast_decode.py +179 -0
- pytessera-0.1.0/src/pytessera/io/ndpi_pyramid.py +180 -0
- pytessera-0.1.0/src/pytessera/io/open_wsi.py +120 -0
- pytessera-0.1.0/src/pytessera/io/pyramid.py +354 -0
- pytessera-0.1.0/src/pytessera/io/serialize.py +154 -0
- pytessera-0.1.0/src/pytessera/io/tiff_pyramid.py +350 -0
- pytessera-0.1.0/src/pytessera/ml/__init__.py +43 -0
- pytessera-0.1.0/src/pytessera/ml/dataset.py +393 -0
- pytessera-0.1.0/src/pytessera/ml/inference.py +120 -0
- pytessera-0.1.0/src/pytessera/ml/loader.py +136 -0
- pytessera-0.1.0/src/pytessera/ml/sampler.py +239 -0
- pytessera-0.1.0/src/pytessera/ml/transforms.py +80 -0
- pytessera-0.1.0/src/pytessera/models/__init__.py +17 -0
- pytessera-0.1.0/src/pytessera/models/resnet.py +76 -0
- pytessera-0.1.0/src/pytessera/pipeline.py +1197 -0
- pytessera-0.1.0/src/pytessera/segmentation/__init__.py +62 -0
- pytessera-0.1.0/src/pytessera/segmentation/artefacts.py +233 -0
- pytessera-0.1.0/src/pytessera/segmentation/evaluation.py +293 -0
- pytessera-0.1.0/src/pytessera/segmentation/malignancy.py +65 -0
- pytessera-0.1.0/src/pytessera/segmentation/tissue.py +474 -0
- pytessera-0.1.0/src/pytessera/tiles/__init__.py +129 -0
- pytessera-0.1.0/src/pytessera/tiles/build.py +211 -0
- pytessera-0.1.0/src/pytessera/tiles/cohort.py +110 -0
- pytessera-0.1.0/src/pytessera/tiles/context.py +64 -0
- pytessera-0.1.0/src/pytessera/tiles/datatile.py +276 -0
- pytessera-0.1.0/src/pytessera/tiles/labels.py +400 -0
- pytessera-0.1.0/src/pytessera/tiles/lazy.py +288 -0
- pytessera-0.1.0/src/pytessera/tiles/ops.py +242 -0
- pytessera-0.1.0/src/pytessera/tiles/patchbag.py +183 -0
- pytessera-0.1.0/src/pytessera/tiles/populate.py +303 -0
- pytessera-0.1.0/src/pytessera/tiles/quadtree.py +386 -0
- pytessera-0.1.0/src/pytessera/viz/__init__.py +48 -0
- pytessera-0.1.0/src/pytessera/viz/backend.py +26 -0
- pytessera-0.1.0/src/pytessera/viz/overlay.py +61 -0
- pytessera-0.1.0/src/pytessera/viz/slide.py +172 -0
- pytessera-0.1.0/src/pytessera/viz/structure.py +206 -0
- pytessera-0.1.0/src/pytessera/viz/tiles.py +215 -0
- pytessera-0.1.0/src/pytessera/wsi/__init__.py +37 -0
- pytessera-0.1.0/src/pytessera/wsi/grid.py +136 -0
- pytessera-0.1.0/src/pytessera/wsi/magnification.py +457 -0
- pytessera-0.1.0/src/pytessera.egg-info/PKG-INFO +134 -0
- pytessera-0.1.0/src/pytessera.egg-info/SOURCES.txt +74 -0
- pytessera-0.1.0/src/pytessera.egg-info/dependency_links.txt +1 -0
- pytessera-0.1.0/src/pytessera.egg-info/entry_points.txt +2 -0
- pytessera-0.1.0/src/pytessera.egg-info/requires.txt +33 -0
- pytessera-0.1.0/src/pytessera.egg-info/top_level.txt +1 -0
pytessera-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tomás Bordoy García-Carpintero, Lucas Dyssel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pytessera-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytessera
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Turn whole-slide images (DICOM, SVS, NDPI, OME-TIFF) into training-ready tile datasets
|
|
5
|
+
Author: Tomás Bordoy García-Carpintero, Lucas Dyssel
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://gitlab.sdu.dk/ouh/pytessera
|
|
8
|
+
Project-URL: Documentation, https://gitlab.sdu.dk/ouh/pytessera/-/tree/main/docs/tutorials
|
|
9
|
+
Keywords: dicom,whole-slide-image,wsi,digital-pathology,tiling,segmentation,deep-learning
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
|
|
15
|
+
Classifier: Topic :: Scientific/Engineering :: Image Recognition
|
|
16
|
+
Requires-Python: >=3.11
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: numpy>=1.24
|
|
20
|
+
Requires-Dist: scipy>=1.10
|
|
21
|
+
Requires-Dist: opencv-python-headless>=4.8
|
|
22
|
+
Requires-Dist: scikit-image>=0.22
|
|
23
|
+
Requires-Dist: Pillow>=10.0
|
|
24
|
+
Requires-Dist: matplotlib>=3.7
|
|
25
|
+
Requires-Dist: pydicom>=2.4
|
|
26
|
+
Requires-Dist: highdicom>=0.22
|
|
27
|
+
Requires-Dist: pylibjpeg[all]>=2.0
|
|
28
|
+
Requires-Dist: tqdm>=4.65
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
31
|
+
Requires-Dist: pytest-cov>=5.0; extra == "dev"
|
|
32
|
+
Requires-Dist: ruff>=0.6; extra == "dev"
|
|
33
|
+
Requires-Dist: mypy>=1.10; extra == "dev"
|
|
34
|
+
Requires-Dist: psutil>=5.9; extra == "dev"
|
|
35
|
+
Provides-Extra: ml
|
|
36
|
+
Requires-Dist: torch>=2.1; extra == "ml"
|
|
37
|
+
Requires-Dist: torchvision>=0.16; extra == "ml"
|
|
38
|
+
Provides-Extra: tiff
|
|
39
|
+
Requires-Dist: tifffile>=2023.9; extra == "tiff"
|
|
40
|
+
Requires-Dist: imagecodecs; extra == "tiff"
|
|
41
|
+
Requires-Dist: zarr; extra == "tiff"
|
|
42
|
+
Provides-Extra: openslide
|
|
43
|
+
Requires-Dist: openslide-python>=1.3; extra == "openslide"
|
|
44
|
+
Provides-Extra: gpu
|
|
45
|
+
Requires-Dist: torch>=2.1; extra == "gpu"
|
|
46
|
+
Requires-Dist: torchvision>=0.16; extra == "gpu"
|
|
47
|
+
Dynamic: license-file
|
|
48
|
+
|
|
49
|
+
# PyTessera
|
|
50
|
+
|
|
51
|
+
**A modular library for turning whole-slide images into training-ready tile datasets for multi-scale models.**
|
|
52
|
+
|
|
53
|
+
Diagnostic interpretation of whole-slide images (WSIs) relies on integrating cellular
|
|
54
|
+
morphology and tissue architecture across multiple magnifications, so AI training datasets
|
|
55
|
+
must preserve those spatial relationships. PyTessera builds training-ready tile datasets
|
|
56
|
+
from WSIs through modular functions rather than one fixed pipeline. A quadtree tiles only
|
|
57
|
+
tissue, or only annotated regions of interest, into `DataTiles`, the shared unit that each
|
|
58
|
+
later stage enriches: a tile can carry a multi-resolution context chain for multi-scale
|
|
59
|
+
models, or join its neighbours in a patch bag for hierarchical patch-then-region models.
|
|
60
|
+
Encoders and classifiers attach feature embeddings and tissue-type labels through simple
|
|
61
|
+
hooks, so users bring their own models, while a class-balanced sampler draws a balanced
|
|
62
|
+
subset from the same catalog for a standardized data representation.
|
|
63
|
+
|
|
64
|
+

|
|
65
|
+
|
|
66
|
+
## Features
|
|
67
|
+
|
|
68
|
+
- **Adaptive quadtree tiling**: background is pruned as the tree descends, so tiles follow
|
|
69
|
+
the tissue, and the whole layout is built as a pixel-free skeleton before anything is read.
|
|
70
|
+
- **Aligned multi-zoom context**: every tile can carry a multi-resolution context chain,
|
|
71
|
+
and neighbouring tiles regroup into patch bags, both off the same tiling.
|
|
72
|
+
- **Bring your own models**: encoders and classifiers attach through hooks; PyTessera
|
|
73
|
+
bundles and validates none of them.
|
|
74
|
+
- **Class-balanced datasets**: draw a materialised balanced subset or an exact per-class
|
|
75
|
+
epoch, over either the annotation-derived category or a hooked tissue-type label.
|
|
76
|
+
- **Pure Python** (≥3.11), with a native DICOM reader in the core. PyTorch (`pytessera[ml]`) and the SVS/NDPI/OME-TIFF readers
|
|
77
|
+
(`pytessera[tiff]`) are optional extras, so `import pytessera` never pulls PyTorch.
|
|
78
|
+
- **Format-agnostic**: a native Sectra DICOM backend, plus Aperio **SVS**, Hamamatsu
|
|
79
|
+
**NDPI** and OME-TIFF through `tifffile` + `zarr`.
|
|
80
|
+
|
|
81
|
+
## Install
|
|
82
|
+
|
|
83
|
+
The core is a light, pure-Python install; the slide readers and the PyTorch loading stack
|
|
84
|
+
are optional extras you add only when your workflow needs them.
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
pip install pytessera # core: tissue segmentation, quadtree tiling, DICOM
|
|
88
|
+
pip install "pytessera[tiff]" # + Aperio SVS / Hamamatsu NDPI / OME-TIFF readers
|
|
89
|
+
pip install "pytessera[ml]" # + PyTorch loading stack (TileDataset, samplers)
|
|
90
|
+
pip install "pytessera[tiff,ml]" # both extras at once (what the tutorials use)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## From the command line
|
|
94
|
+
|
|
95
|
+
The same builders run as a `pytessera` command, so a cohort can be tiled from a shell script
|
|
96
|
+
or a cluster job and the catalog handed to Python later with `load_datatile_dict`:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
pytessera build --slides cohort/*.svs --magnification 20 --save cohort.npz # tissue-driven
|
|
100
|
+
pytessera build-annotations --slides cohort/*.tif --labels Tumor --save tumour.npz
|
|
101
|
+
pytessera build -h # every flag
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Slides can be files of any supported format, or DICOM case folders (`--data-root DIR --cases ID...`).
|
|
105
|
+
The command stops at the catalog; embeddings, labels and balancing are Python calls.
|
|
106
|
+
|
|
107
|
+
## Tutorials
|
|
108
|
+
|
|
109
|
+
**Start here:** **[Train a tumour segmentation model on a cohort](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/07_end_to_end_training.ipynb)**
|
|
110
|
+
takes ten annotated CAMELYON16 slides to a trained model, scores it on held-out slides, and
|
|
111
|
+
paints a prediction map over an unseen CAMELYON17 slide. PyTessera calls do the data side, the
|
|
112
|
+
scoring and the map; only the model and the training loop are plain PyTorch.
|
|
113
|
+
|
|
114
|
+
The other notebooks in [`docs/tutorials/`](https://gitlab.sdu.dk/ouh/pytessera/-/tree/main/docs/tutorials) take one idea each:
|
|
115
|
+
|
|
116
|
+
1. **[Slide-level MIL on patch bags](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/01_walkthrough.ipynb)** — group tiles into patch bags, encode them with a foundation model, and feed the bags to an attention-MIL model.
|
|
117
|
+
2. **[Tissue mask hook](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/02_tissue_mask_hook.ipynb)** — swap in your own tissue segmentation via the `mask_fn` hook.
|
|
118
|
+
3. **[Embeddings](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/03_embeddings.ipynb)** — leaf, chain, and merged tile embeddings, plus a classifier hook.
|
|
119
|
+
4. **[Patch bags](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/04_patch_bags_hipt.ipynb)** — group tiles into HIPT-style region grids and encode each into a vector.
|
|
120
|
+
5. **[ROI-balanced tiling](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/05_roi_balanced.ipynb)** — label tiles from real ASAP annotations and balance classes before materialising pixels.
|
|
121
|
+
6. **[Label-balanced skeleton](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/06_label_balanced_skeleton.ipynb)** — enrich with pixels, then drop them and keep only the labelled, embedded skeleton.
|
|
122
|
+
7. **[End-to-end training](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/07_end_to_end_training.ipynb)** — the cohort training tutorial above.
|
|
123
|
+
|
|
124
|
+
## Acknowledgements
|
|
125
|
+
|
|
126
|
+
We thank all paper authors for their contributions: Tomás Bordoy García-Carpintero, Lucas A. D. T. Dyssel, Isabell Seyfried
|
|
127
|
+
If PyTessera is useful in your work, please cite it; see [`CITATION.cff`](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/CITATION.cff).
|
|
128
|
+
|
|
129
|
+
## Contact
|
|
130
|
+
|
|
131
|
+
Tomás Bordoy García-Carpintero and Lucas A. D. T. Dyssel (equal contribution),
|
|
132
|
+
University of Southern Denmark & Odense University Hospital.
|
|
133
|
+
Corresponding author: [tobor@imada.sdu.dk](mailto:tobor@imada.sdu.dk). Issues and questions
|
|
134
|
+
are welcome on the [GitLab repository](https://gitlab.sdu.dk/ouh/pytessera).
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# PyTessera
|
|
2
|
+
|
|
3
|
+
**A modular library for turning whole-slide images into training-ready tile datasets for multi-scale models.**
|
|
4
|
+
|
|
5
|
+
Diagnostic interpretation of whole-slide images (WSIs) relies on integrating cellular
|
|
6
|
+
morphology and tissue architecture across multiple magnifications, so AI training datasets
|
|
7
|
+
must preserve those spatial relationships. PyTessera builds training-ready tile datasets
|
|
8
|
+
from WSIs through modular functions rather than one fixed pipeline. A quadtree tiles only
|
|
9
|
+
tissue, or only annotated regions of interest, into `DataTiles`, the shared unit that each
|
|
10
|
+
later stage enriches: a tile can carry a multi-resolution context chain for multi-scale
|
|
11
|
+
models, or join its neighbours in a patch bag for hierarchical patch-then-region models.
|
|
12
|
+
Encoders and classifiers attach feature embeddings and tissue-type labels through simple
|
|
13
|
+
hooks, so users bring their own models, while a class-balanced sampler draws a balanced
|
|
14
|
+
subset from the same catalog for a standardized data representation.
|
|
15
|
+
|
|
16
|
+

|
|
17
|
+
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
- **Adaptive quadtree tiling**: background is pruned as the tree descends, so tiles follow
|
|
21
|
+
the tissue, and the whole layout is built as a pixel-free skeleton before anything is read.
|
|
22
|
+
- **Aligned multi-zoom context**: every tile can carry a multi-resolution context chain,
|
|
23
|
+
and neighbouring tiles regroup into patch bags, both off the same tiling.
|
|
24
|
+
- **Bring your own models**: encoders and classifiers attach through hooks; PyTessera
|
|
25
|
+
bundles and validates none of them.
|
|
26
|
+
- **Class-balanced datasets**: draw a materialised balanced subset or an exact per-class
|
|
27
|
+
epoch, over either the annotation-derived category or a hooked tissue-type label.
|
|
28
|
+
- **Pure Python** (≥3.11), with a native DICOM reader in the core. PyTorch (`pytessera[ml]`) and the SVS/NDPI/OME-TIFF readers
|
|
29
|
+
(`pytessera[tiff]`) are optional extras, so `import pytessera` never pulls PyTorch.
|
|
30
|
+
- **Format-agnostic**: a native Sectra DICOM backend, plus Aperio **SVS**, Hamamatsu
|
|
31
|
+
**NDPI** and OME-TIFF through `tifffile` + `zarr`.
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
The core is a light, pure-Python install; the slide readers and the PyTorch loading stack
|
|
36
|
+
are optional extras you add only when your workflow needs them.
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install pytessera # core: tissue segmentation, quadtree tiling, DICOM
|
|
40
|
+
pip install "pytessera[tiff]" # + Aperio SVS / Hamamatsu NDPI / OME-TIFF readers
|
|
41
|
+
pip install "pytessera[ml]" # + PyTorch loading stack (TileDataset, samplers)
|
|
42
|
+
pip install "pytessera[tiff,ml]" # both extras at once (what the tutorials use)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## From the command line
|
|
46
|
+
|
|
47
|
+
The same builders run as a `pytessera` command, so a cohort can be tiled from a shell script
|
|
48
|
+
or a cluster job and the catalog handed to Python later with `load_datatile_dict`:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pytessera build --slides cohort/*.svs --magnification 20 --save cohort.npz # tissue-driven
|
|
52
|
+
pytessera build-annotations --slides cohort/*.tif --labels Tumor --save tumour.npz
|
|
53
|
+
pytessera build -h # every flag
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Slides can be files of any supported format, or DICOM case folders (`--data-root DIR --cases ID...`).
|
|
57
|
+
The command stops at the catalog; embeddings, labels and balancing are Python calls.
|
|
58
|
+
|
|
59
|
+
## Tutorials
|
|
60
|
+
|
|
61
|
+
**Start here:** **[Train a tumour segmentation model on a cohort](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/07_end_to_end_training.ipynb)**
|
|
62
|
+
takes ten annotated CAMELYON16 slides to a trained model, scores it on held-out slides, and
|
|
63
|
+
paints a prediction map over an unseen CAMELYON17 slide. PyTessera calls do the data side, the
|
|
64
|
+
scoring and the map; only the model and the training loop are plain PyTorch.
|
|
65
|
+
|
|
66
|
+
The other notebooks in [`docs/tutorials/`](https://gitlab.sdu.dk/ouh/pytessera/-/tree/main/docs/tutorials) take one idea each:
|
|
67
|
+
|
|
68
|
+
1. **[Slide-level MIL on patch bags](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/01_walkthrough.ipynb)** — group tiles into patch bags, encode them with a foundation model, and feed the bags to an attention-MIL model.
|
|
69
|
+
2. **[Tissue mask hook](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/02_tissue_mask_hook.ipynb)** — swap in your own tissue segmentation via the `mask_fn` hook.
|
|
70
|
+
3. **[Embeddings](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/03_embeddings.ipynb)** — leaf, chain, and merged tile embeddings, plus a classifier hook.
|
|
71
|
+
4. **[Patch bags](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/04_patch_bags_hipt.ipynb)** — group tiles into HIPT-style region grids and encode each into a vector.
|
|
72
|
+
5. **[ROI-balanced tiling](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/05_roi_balanced.ipynb)** — label tiles from real ASAP annotations and balance classes before materialising pixels.
|
|
73
|
+
6. **[Label-balanced skeleton](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/06_label_balanced_skeleton.ipynb)** — enrich with pixels, then drop them and keep only the labelled, embedded skeleton.
|
|
74
|
+
7. **[End-to-end training](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/docs/tutorials/07_end_to_end_training.ipynb)** — the cohort training tutorial above.
|
|
75
|
+
|
|
76
|
+
## Acknowledgements
|
|
77
|
+
|
|
78
|
+
We thank all paper authors for their contributions: Tomás Bordoy García-Carpintero, Lucas A. D. T. Dyssel, Isabell Seyfried
|
|
79
|
+
If PyTessera is useful in your work, please cite it; see [`CITATION.cff`](https://gitlab.sdu.dk/ouh/pytessera/-/blob/main/CITATION.cff).
|
|
80
|
+
|
|
81
|
+
## Contact
|
|
82
|
+
|
|
83
|
+
Tomás Bordoy García-Carpintero and Lucas A. D. T. Dyssel (equal contribution),
|
|
84
|
+
University of Southern Denmark & Odense University Hospital.
|
|
85
|
+
Corresponding author: [tobor@imada.sdu.dk](mailto:tobor@imada.sdu.dk). Issues and questions
|
|
86
|
+
are welcome on the [GitLab repository](https://gitlab.sdu.dk/ouh/pytessera).
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pytessera"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Turn whole-slide images (DICOM, SVS, NDPI, OME-TIFF) into training-ready tile datasets"
|
|
9
|
+
authors = [
|
|
10
|
+
{ name = "Tomás Bordoy García-Carpintero" },
|
|
11
|
+
{ name = "Lucas Dyssel" },
|
|
12
|
+
]
|
|
13
|
+
readme = "README.md"
|
|
14
|
+
requires-python = ">=3.11"
|
|
15
|
+
license = "MIT"
|
|
16
|
+
license-files = ["LICENSE"]
|
|
17
|
+
keywords = [
|
|
18
|
+
"dicom", "whole-slide-image", "wsi", "digital-pathology",
|
|
19
|
+
"tiling", "segmentation", "deep-learning",
|
|
20
|
+
]
|
|
21
|
+
classifiers = [
|
|
22
|
+
"Development Status :: 4 - Beta",
|
|
23
|
+
"Intended Audience :: Science/Research",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Topic :: Scientific/Engineering :: Medical Science Apps.",
|
|
27
|
+
"Topic :: Scientific/Engineering :: Image Recognition",
|
|
28
|
+
]
|
|
29
|
+
dependencies = [
|
|
30
|
+
"numpy>=1.24",
|
|
31
|
+
"scipy>=1.10",
|
|
32
|
+
"opencv-python-headless>=4.8",
|
|
33
|
+
"scikit-image>=0.22",
|
|
34
|
+
"Pillow>=10.0",
|
|
35
|
+
"matplotlib>=3.7",
|
|
36
|
+
"pydicom>=2.4",
|
|
37
|
+
"highdicom>=0.22",
|
|
38
|
+
"pylibjpeg[all]>=2.0",
|
|
39
|
+
"tqdm>=4.65",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[project.optional-dependencies]
|
|
43
|
+
dev = ["pytest>=8.0", "pytest-cov>=5.0", "ruff>=0.6", "mypy>=1.10", "psutil>=5.9"]
|
|
44
|
+
# PyTorch loading stack: pytessera.ml (TileDataset, BalancedTileSampler) and
|
|
45
|
+
# the bundled reference encoders in pytessera.models (torchvision ResNet).
|
|
46
|
+
ml = ["torch>=2.1", "torchvision>=0.16"]
|
|
47
|
+
# Optional WSI readers (v0.2). open_tiff_slide / open_wsi for SVS/NDPI/OME-TIFF.
|
|
48
|
+
tiff = ["tifffile>=2023.9", "imagecodecs", "zarr"]
|
|
49
|
+
openslide = ["openslide-python>=1.3"]
|
|
50
|
+
# Experimental GPU JPEG decode (nvJPEG via torchvision). Requires an NVIDIA CUDA
|
|
51
|
+
# GPU + a CUDA build of torch/torchvision; falls back to CPU when unavailable.
|
|
52
|
+
gpu = ["torch>=2.1", "torchvision>=0.16"]
|
|
53
|
+
|
|
54
|
+
# Console entry point: `pip install` puts a `pytessera` command on PATH.
|
|
55
|
+
[project.urls]
|
|
56
|
+
Repository = "https://gitlab.sdu.dk/ouh/pytessera"
|
|
57
|
+
Documentation = "https://gitlab.sdu.dk/ouh/pytessera/-/tree/main/docs/tutorials"
|
|
58
|
+
|
|
59
|
+
[project.scripts]
|
|
60
|
+
pytessera = "pytessera.cli:main"
|
|
61
|
+
|
|
62
|
+
[tool.setuptools]
|
|
63
|
+
package-dir = { "" = "src" }
|
|
64
|
+
|
|
65
|
+
[tool.setuptools.packages.find]
|
|
66
|
+
where = ["src"]
|
|
67
|
+
|
|
68
|
+
# The DICOM annotation template is code-adjacent data, not a source file, so it
|
|
69
|
+
# is not picked up by packages.find on its own. Without this it is missing from
|
|
70
|
+
# the wheel and annotations.template_path() fails after a pip install -- while
|
|
71
|
+
# still passing every test, because the tests run against the source tree.
|
|
72
|
+
[tool.setuptools.package-data]
|
|
73
|
+
"pytessera.annotations.templates" = ["*.dcm"]
|
|
74
|
+
|
|
75
|
+
[tool.pytest.ini_options]
|
|
76
|
+
testpaths = ["tests"]
|
|
77
|
+
addopts = ["-ra"]
|
|
78
|
+
|
|
79
|
+
[tool.ruff]
|
|
80
|
+
line-length = 100
|
|
81
|
+
target-version = "py311"
|
|
82
|
+
src = ["src", "tests"]
|
|
83
|
+
extend-exclude = ["legacy", "benchmarks/results"]
|
|
84
|
+
|
|
85
|
+
[tool.ruff.lint]
|
|
86
|
+
select = ["E", "F", "I", "W", "UP", "B"]
|
|
87
|
+
ignore = [
|
|
88
|
+
"E741", # ambiguous names (l/I/O) are fine in tight numeric code
|
|
89
|
+
"B905", # zip(strict=) — opinionated; equal-length by construction here
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
[tool.mypy]
|
|
93
|
+
# numpy's own stub file requires 3.12+ syntax (PEP 695 `type` statements) to
|
|
94
|
+
# even parse, so this must track the newest supported version, not the
|
|
95
|
+
# oldest (requires-python's floor of 3.11 is still enforced by actually
|
|
96
|
+
# running the test suite on 3.11 in CI, which would raise a real SyntaxError
|
|
97
|
+
# on any 3.12-only code that slipped through).
|
|
98
|
+
python_version = "3.12"
|
|
99
|
+
ignore_missing_imports = true
|
|
100
|
+
files = ["src/pytessera"]
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"""pytessera, WSI tiling utilities for digital pathology pipelines.
|
|
2
|
+
|
|
3
|
+
Top-level convenience
|
|
4
|
+
---------------------
|
|
5
|
+
:func:`build_dataset` runs the full skeleton-then-populate pipeline for many
|
|
6
|
+
slides in one call, any format, opened via :func:`open_wsi` internally::
|
|
7
|
+
|
|
8
|
+
from pytessera import build_dataset
|
|
9
|
+
|
|
10
|
+
dataset = build_dataset([9, 40], data_root="/path/to/data", magnification=10)
|
|
11
|
+
|
|
12
|
+
:func:`build_datatiles` is the single-slide core it's built on -- take an
|
|
13
|
+
already-opened :class:`~pytessera.io.Pyramid` and one ``slide_id``:
|
|
14
|
+
|
|
15
|
+
build_datatiles one call: skeleton + pixels, one slide
|
|
16
|
+
build_dataset the same, batched over many slides
|
|
17
|
+
build_skeleton skeleton only (no pixels)
|
|
18
|
+
populate_datatiles fill .img / .mask / .category on a skeleton
|
|
19
|
+
classify_masks fill .mask / .category only, no pixel reader opened
|
|
20
|
+
build_patch_bags hierarchical patch-bag grids (no pixels)
|
|
21
|
+
populate_patch_bags fill .img on a patch-bag dataset
|
|
22
|
+
build_datatiles_from_annotations annotated-region set, one slide (from
|
|
23
|
+
pyramid.annotations, format-agnostic),
|
|
24
|
+
grouped by connected region (region_id);
|
|
25
|
+
each leaf's source-polygon label is
|
|
26
|
+
written to DataTile.tissue_class
|
|
27
|
+
build_dataset_from_annotations the same, batched over many slides
|
|
28
|
+
|
|
29
|
+
group_by_region regroup an annotation dataset
|
|
30
|
+
into {slide: {region_id: {...}}}
|
|
31
|
+
|
|
32
|
+
On a built dataset, pixel-free:
|
|
33
|
+
|
|
34
|
+
summarize tile counts per slide and label, as a table
|
|
35
|
+
split_slides train / validation split by whole slide
|
|
36
|
+
|
|
37
|
+
DICOM case-batch convenience, resolving magnification-ladder zoom levels
|
|
38
|
+
(z1=40x, z2=20x, ...) per case via Pyramid.resolve_level:
|
|
39
|
+
|
|
40
|
+
build_datatiles_for_cases DICOM case batch, one call per case,
|
|
41
|
+
thread-pooled over slide_workers
|
|
42
|
+
|
|
43
|
+
Unused (no callers in this package or in ALIVE; kept importable, not called
|
|
44
|
+
by anything above -- see each function's own docstring):
|
|
45
|
+
|
|
46
|
+
build_skeleton_for_cases DICOM-only, separate skeleton implementation
|
|
47
|
+
populate_datatiles_for_cases DICOM-only, separate populate implementation
|
|
48
|
+
|
|
49
|
+
Optional, post-catalog hooks for a caller's own third-party tools -- neither
|
|
50
|
+
bundled nor validated by PyTessera itself, just wired onto existing tiles:
|
|
51
|
+
|
|
52
|
+
attach_tile_labels run a caller-supplied classifier,
|
|
53
|
+
write DataTile.tissue_class
|
|
54
|
+
attach_tile_embeddings run a caller-supplied encoder,
|
|
55
|
+
write DataTile.embedding
|
|
56
|
+
|
|
57
|
+
For finer control, the subpackages are public:
|
|
58
|
+
|
|
59
|
+
:mod:`pytessera.io` load a pyramid, any format
|
|
60
|
+
:mod:`pytessera.wsi` pyramid geometry helpers
|
|
61
|
+
:mod:`pytessera.annotations` parse / render / create / cache
|
|
62
|
+
:mod:`pytessera.segmentation` tissue mask + artefact removal
|
|
63
|
+
:mod:`pytessera.tiles` quadtree, DataTile, patch ops,
|
|
64
|
+
build_skeleton_for_cases,
|
|
65
|
+
populate_datatiles_for_cases,
|
|
66
|
+
group_by_region, attach_tile_labels,
|
|
67
|
+
attach_tile_embeddings
|
|
68
|
+
:mod:`pytessera.pipeline` top-level convenience entry
|
|
69
|
+
:mod:`pytessera.ml` ``[ml]`` extra: TileDataset,
|
|
70
|
+
BalancedTileSampler, build_dataloader
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
from __future__ import annotations
|
|
74
|
+
|
|
75
|
+
from .io.open_wsi import open_wsi
|
|
76
|
+
from .pipeline import (
|
|
77
|
+
build_dataset,
|
|
78
|
+
build_dataset_from_annotations,
|
|
79
|
+
build_datatiles,
|
|
80
|
+
build_datatiles_for_cases,
|
|
81
|
+
build_datatiles_from_annotations,
|
|
82
|
+
build_patch_bags,
|
|
83
|
+
build_skeleton,
|
|
84
|
+
classify_masks,
|
|
85
|
+
populate_datatiles,
|
|
86
|
+
populate_patch_bags,
|
|
87
|
+
)
|
|
88
|
+
from .tiles import (
|
|
89
|
+
PatchBag,
|
|
90
|
+
attach_bag_embeddings,
|
|
91
|
+
attach_context_chain,
|
|
92
|
+
attach_region_labels,
|
|
93
|
+
attach_tile_embeddings,
|
|
94
|
+
attach_tile_labels,
|
|
95
|
+
build_skeleton_for_cases,
|
|
96
|
+
group_by_region,
|
|
97
|
+
populate_datatiles_for_cases,
|
|
98
|
+
select_balanced_subset,
|
|
99
|
+
split_slides,
|
|
100
|
+
summarize,
|
|
101
|
+
)
|
|
102
|
+
from .tiles.datatile import DataTile
|
|
103
|
+
|
|
104
|
+
__all__ = [
|
|
105
|
+
"DataTile",
|
|
106
|
+
"PatchBag",
|
|
107
|
+
"attach_bag_embeddings",
|
|
108
|
+
"attach_context_chain",
|
|
109
|
+
"attach_region_labels",
|
|
110
|
+
"attach_tile_embeddings",
|
|
111
|
+
"attach_tile_labels",
|
|
112
|
+
"build_dataset",
|
|
113
|
+
"build_dataset_from_annotations",
|
|
114
|
+
"build_datatiles",
|
|
115
|
+
"build_datatiles_for_cases",
|
|
116
|
+
"build_datatiles_from_annotations",
|
|
117
|
+
"build_patch_bags",
|
|
118
|
+
"build_skeleton",
|
|
119
|
+
"build_skeleton_for_cases",
|
|
120
|
+
"classify_masks",
|
|
121
|
+
"group_by_region",
|
|
122
|
+
"open_wsi",
|
|
123
|
+
"populate_datatiles",
|
|
124
|
+
"populate_datatiles_for_cases",
|
|
125
|
+
"populate_patch_bags",
|
|
126
|
+
"select_balanced_subset",
|
|
127
|
+
"split_slides",
|
|
128
|
+
"summarize",
|
|
129
|
+
]
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Resolve the tissue mask a builder tiles from: the caller's own segmentation
|
|
2
|
+
if supplied, else PyTessera's built-in :func:`~pytessera.segmentation.tissue_mask`.
|
|
3
|
+
|
|
4
|
+
This is the bring-your-own-segmentation hook, the mask-side counterpart to the
|
|
5
|
+
encoder/classifier hooks. A ``mask_fn`` maps a coarse RGB slide image to a tissue
|
|
6
|
+
mask; any truthy pixel is treated as tissue.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from collections.abc import Callable
|
|
12
|
+
from typing import TYPE_CHECKING
|
|
13
|
+
|
|
14
|
+
import numpy as np
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from ..segmentation.tissue import TissueType
|
|
18
|
+
|
|
19
|
+
#: ``coarse_rgb (H, W, 3) uint8 -> mask (H, W)``. The mask may be boolean, 0/1, or
|
|
20
|
+
#: 0/255; any value ``> 0`` counts as tissue.
|
|
21
|
+
MaskFn = Callable[[np.ndarray], np.ndarray]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def resolve_mask_u8(
|
|
25
|
+
rgb: np.ndarray,
|
|
26
|
+
mask_fn: MaskFn | None,
|
|
27
|
+
*,
|
|
28
|
+
tissue_type: TissueType = "dense",
|
|
29
|
+
smooth_sigma: float = 0.30,
|
|
30
|
+
) -> np.ndarray:
|
|
31
|
+
"""Return a ``(H, W)`` 0/255 uint8 tissue mask for ``rgb``.
|
|
32
|
+
|
|
33
|
+
``mask_fn(rgb)`` when a caller supplies one, otherwise the built-in
|
|
34
|
+
``tissue_mask(rgb, tissue_type=..., smooth_sigma=...)``. The result is
|
|
35
|
+
normalised to 0/255 so it feeds :func:`~pytessera.tiles.build_quadtree_from_mask`
|
|
36
|
+
regardless of whether the mask came back boolean, 0/1, or 0/255.
|
|
37
|
+
"""
|
|
38
|
+
if mask_fn is not None:
|
|
39
|
+
mask = mask_fn(rgb)
|
|
40
|
+
else:
|
|
41
|
+
from ..segmentation import tissue_mask
|
|
42
|
+
|
|
43
|
+
mask = tissue_mask(rgb, tissue_type=tissue_type, smooth_sigma=smooth_sigma)
|
|
44
|
+
return (np.asarray(mask) > 0).astype(np.uint8) * 255
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Process-level resource limits.
|
|
2
|
+
|
|
3
|
+
Private helper used at the very start of long-running scripts to cap
|
|
4
|
+
how much memory the Python interpreter is allowed to allocate. This
|
|
5
|
+
is the kind of guard you set once in a launcher script, not something
|
|
6
|
+
the library calls implicitly.
|
|
7
|
+
|
|
8
|
+
Linux-only at the moment (Windows does not expose RLIMIT_AS through
|
|
9
|
+
the stdlib). The call is a no-op on platforms that lack ``resource``.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
try:
|
|
15
|
+
import resource # type: ignore[import-not-found]
|
|
16
|
+
except ImportError:
|
|
17
|
+
resource = None # type: ignore[assignment]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
__all__ = ["set_memory_limit"]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def set_memory_limit(max_memory_in_gb: float) -> None:
|
|
24
|
+
"""Cap the process's virtual address space.
|
|
25
|
+
|
|
26
|
+
Parameters
|
|
27
|
+
----------
|
|
28
|
+
max_memory_in_gb : float
|
|
29
|
+
Soft + hard limit in gigabytes. Allocations past the limit
|
|
30
|
+
raise :class:`MemoryError`.
|
|
31
|
+
|
|
32
|
+
Notes
|
|
33
|
+
-----
|
|
34
|
+
No-op on platforms without ``resource`` (notably Windows). Use
|
|
35
|
+
a Job object via pywin32 if you need an equivalent guard there.
|
|
36
|
+
"""
|
|
37
|
+
if resource is None:
|
|
38
|
+
return
|
|
39
|
+
n_bytes = int(max_memory_in_gb * 1024 * 1024 * 1024)
|
|
40
|
+
resource.setrlimit(resource.RLIMIT_AS, (n_bytes, n_bytes))
|