tmapslide 0.2.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.
- tmapslide-0.2.0/.gitignore +19 -0
- tmapslide-0.2.0/LICENSE +21 -0
- tmapslide-0.2.0/PKG-INFO +142 -0
- tmapslide-0.2.0/README.md +111 -0
- tmapslide-0.2.0/examples/read_region.py +30 -0
- tmapslide-0.2.0/pyproject.toml +78 -0
- tmapslide-0.2.0/src/tmapslide/__init__.py +56 -0
- tmapslide-0.2.0/src/tmapslide/_cache.py +37 -0
- tmapslide-0.2.0/src/tmapslide/_exceptions.py +19 -0
- tmapslide-0.2.0/src/tmapslide/_slide.py +553 -0
- tmapslide-0.2.0/src/tmapslide/_tmapformat.py +654 -0
- tmapslide-0.2.0/tests/test_basic.py +240 -0
- tmapslide-0.2.0/tests/test_synthetic.py +124 -0
- tmapslide-0.2.0/tests/tmap_fixtures.py +167 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# throwaway spike probes
|
|
2
|
+
probe_tmap*.py
|
|
3
|
+
diag*.py
|
|
4
|
+
|
|
5
|
+
# python
|
|
6
|
+
__pycache__/
|
|
7
|
+
*.py[cod]
|
|
8
|
+
*.egg-info/
|
|
9
|
+
build/
|
|
10
|
+
dist/
|
|
11
|
+
.pytest_cache/
|
|
12
|
+
.ruff_cache/
|
|
13
|
+
.mypy_cache/
|
|
14
|
+
|
|
15
|
+
# large sample renders (analysis artifacts)
|
|
16
|
+
cache/
|
|
17
|
+
|
|
18
|
+
# local sample-data symlink
|
|
19
|
+
scce_external_center
|
tmapslide-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yifan Feng
|
|
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.
|
tmapslide-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: tmapslide
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Pure Python UNIC TMAP whole-slide image reader with OpenSlide-compatible API
|
|
5
|
+
Project-URL: Homepage, https://github.com/yifanfeng97/tmapslide
|
|
6
|
+
Project-URL: Documentation, https://github.com/yifanfeng97/tmapslide#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/yifanfeng97/tmapslide
|
|
8
|
+
Project-URL: Issues, https://github.com/yifanfeng97/tmapslide/issues
|
|
9
|
+
Author-email: Yifan Feng <evanfeng97@gmail.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: digital-pathology,openslide,pathology,tmap,tmapslide,unic,whole-slide-image,wsi
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Image Processing
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Requires-Dist: pillow>=9.0.0
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: mypy; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest-cov; extra == 'dev'
|
|
28
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
29
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
# TmapSlide
|
|
33
|
+
|
|
34
|
+

|
|
35
|
+
|
|
36
|
+
Pure Python reader for **UNIC TMAP** whole-slide images (WSI) with an
|
|
37
|
+
[OpenSlide](https://openslide.org/)-compatible API — the companion library to
|
|
38
|
+
[kfbslide](https://github.com/yifanfeng97/kfbslide) (KFB format).
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
import tmapslide
|
|
42
|
+
|
|
43
|
+
slide = tmapslide.OpenSlide("sample.TMAP")
|
|
44
|
+
|
|
45
|
+
print(slide.dimensions) # (71424, 72704)
|
|
46
|
+
print(slide.level_count) # 10
|
|
47
|
+
print(slide.level_downsamples) # (1.0, 2.0, 4.0, ...)
|
|
48
|
+
|
|
49
|
+
region = slide.read_region((512, 512), 0, (1024, 1024)) # RGBA PIL image
|
|
50
|
+
thumb = slide.get_thumbnail((512, 512))
|
|
51
|
+
macro = slide.associated_images["macro"]
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Features
|
|
55
|
+
|
|
56
|
+
- **Pure Python** — no vendor SDK, no native dependencies; only Pillow.
|
|
57
|
+
- **OpenSlide-compatible API** — drop-in for code written against
|
|
58
|
+
`openslide`/`kfbslide`: `read_region`, `get_thumbnail`, `dimensions`,
|
|
59
|
+
`level_count`, `level_dimensions`, `level_downsamples`, `properties`,
|
|
60
|
+
`associated_images`.
|
|
61
|
+
- **Both known TMAP variants**: `TMAP06` (single pixel level, 612x512
|
|
62
|
+
overlapping tiles) and `TMAP07` (full pyramid, up to 10 levels, 256x256
|
|
63
|
+
tiles).
|
|
64
|
+
- **Fork-safe file handles** — safe to use from PyTorch `DataLoader` workers.
|
|
65
|
+
- LRU tile cache for fast repeated reads.
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pip install tmapslide
|
|
71
|
+
# or from source
|
|
72
|
+
pip install git+https://github.com/yifanfeng97/tmapslide.git
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Supported formats
|
|
76
|
+
|
|
77
|
+
| Format | Extension | Vendor | Backend |
|
|
78
|
+
| ------- | --------- | ------------------ | ----------- |
|
|
79
|
+
| TMAP 06 | `.TMAP` | UNIC (United Imaging) | Pure Python |
|
|
80
|
+
| TMAP 07 | `.TMAP` | UNIC (United Imaging) | Pure Python |
|
|
81
|
+
|
|
82
|
+
## API
|
|
83
|
+
|
|
84
|
+
### `tmapslide.OpenSlide(filename)`
|
|
85
|
+
|
|
86
|
+
| Member | Description |
|
|
87
|
+
| --------------------- | -------------------------------------------------- |
|
|
88
|
+
| `dimensions` | `(width, height)` at level 0 |
|
|
89
|
+
| `level_count` | number of pyramid levels |
|
|
90
|
+
| `level_dimensions` | `(w, h)` per level |
|
|
91
|
+
| `level_downsamples` | downsample factor per level |
|
|
92
|
+
| `properties` | read-only metadata mapping (`openslide.vendor=unic`, `tmap.*`) |
|
|
93
|
+
| `associated_images` | lazy mapping, typically `macro` / `label` |
|
|
94
|
+
| `read_region(loc, level, size)` | `PIL.Image` (RGBA) of the region |
|
|
95
|
+
| `get_thumbnail(size)` | thumbnail from the lowest resolution level |
|
|
96
|
+
| `get_best_level_for_downsample(ds)` | best level for a downsample factor |
|
|
97
|
+
| `close()` / context manager | release resources |
|
|
98
|
+
|
|
99
|
+
### `tmapslide.open_slide(filename)`
|
|
100
|
+
|
|
101
|
+
Alias of `OpenSlide(filename)`.
|
|
102
|
+
|
|
103
|
+
## Format notes (reverse-engineered)
|
|
104
|
+
|
|
105
|
+
TMAP is an undocumented proprietary format. This reader is built from
|
|
106
|
+
binary analysis of real scanner output, cross-validated against
|
|
107
|
+
[ASlide](https://github.com/MrPeterJin/ASlide). In short: both variants
|
|
108
|
+
store plain JPEG tiles with a small binary header and index tables; there
|
|
109
|
+
is no encryption.
|
|
110
|
+
|
|
111
|
+
- TMAP06 stores 3 pyramid levels (40x / 10x / 2.5x); TMAP07 stores up to
|
|
112
|
+
10 levels (40x down to 0.078x, halving each level).
|
|
113
|
+
- TMAP06 level-2 previews come from pre-rendered `ShrinkTile` entries and
|
|
114
|
+
are JPEG-compressed at that scale (slightly softer than downsampling
|
|
115
|
+
level 0 yourself).
|
|
116
|
+
- `iter_tiles()` exposes the stored tile grid directly, useful for
|
|
117
|
+
tile-based ML pipelines.
|
|
118
|
+
|
|
119
|
+
## Testing
|
|
120
|
+
|
|
121
|
+
Tests run against real TMAP samples when the `scce_external_center` data
|
|
122
|
+
directory (or `TAPSLIDE_TEST_DATA`) is present next to the repo, and are
|
|
123
|
+
skipped otherwise.
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
pip install -e .[dev]
|
|
127
|
+
pytest
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
MIT — see [LICENSE](LICENSE).
|
|
133
|
+
|
|
134
|
+
## Acknowledgments
|
|
135
|
+
|
|
136
|
+
- [kfbslide](https://github.com/yifanfeng97/kfbslide) — the KFB reader this
|
|
137
|
+
project is modelled after.
|
|
138
|
+
- [OpenSlide](https://openslide.org/) — the API this library mimics.
|
|
139
|
+
- [ASlide](https://github.com/MrPeterJin/ASlide) — its independent
|
|
140
|
+
reverse-engineering of the TMAP06 layer/block structures (from decompiled
|
|
141
|
+
vendor SDK) was used to cross-validate this implementation. tmapslide is
|
|
142
|
+
an independent MIT-licensed implementation and ships no ASlide code.
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# TmapSlide
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
Pure Python reader for **UNIC TMAP** whole-slide images (WSI) with an
|
|
6
|
+
[OpenSlide](https://openslide.org/)-compatible API — the companion library to
|
|
7
|
+
[kfbslide](https://github.com/yifanfeng97/kfbslide) (KFB format).
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
import tmapslide
|
|
11
|
+
|
|
12
|
+
slide = tmapslide.OpenSlide("sample.TMAP")
|
|
13
|
+
|
|
14
|
+
print(slide.dimensions) # (71424, 72704)
|
|
15
|
+
print(slide.level_count) # 10
|
|
16
|
+
print(slide.level_downsamples) # (1.0, 2.0, 4.0, ...)
|
|
17
|
+
|
|
18
|
+
region = slide.read_region((512, 512), 0, (1024, 1024)) # RGBA PIL image
|
|
19
|
+
thumb = slide.get_thumbnail((512, 512))
|
|
20
|
+
macro = slide.associated_images["macro"]
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- **Pure Python** — no vendor SDK, no native dependencies; only Pillow.
|
|
26
|
+
- **OpenSlide-compatible API** — drop-in for code written against
|
|
27
|
+
`openslide`/`kfbslide`: `read_region`, `get_thumbnail`, `dimensions`,
|
|
28
|
+
`level_count`, `level_dimensions`, `level_downsamples`, `properties`,
|
|
29
|
+
`associated_images`.
|
|
30
|
+
- **Both known TMAP variants**: `TMAP06` (single pixel level, 612x512
|
|
31
|
+
overlapping tiles) and `TMAP07` (full pyramid, up to 10 levels, 256x256
|
|
32
|
+
tiles).
|
|
33
|
+
- **Fork-safe file handles** — safe to use from PyTorch `DataLoader` workers.
|
|
34
|
+
- LRU tile cache for fast repeated reads.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install tmapslide
|
|
40
|
+
# or from source
|
|
41
|
+
pip install git+https://github.com/yifanfeng97/tmapslide.git
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Supported formats
|
|
45
|
+
|
|
46
|
+
| Format | Extension | Vendor | Backend |
|
|
47
|
+
| ------- | --------- | ------------------ | ----------- |
|
|
48
|
+
| TMAP 06 | `.TMAP` | UNIC (United Imaging) | Pure Python |
|
|
49
|
+
| TMAP 07 | `.TMAP` | UNIC (United Imaging) | Pure Python |
|
|
50
|
+
|
|
51
|
+
## API
|
|
52
|
+
|
|
53
|
+
### `tmapslide.OpenSlide(filename)`
|
|
54
|
+
|
|
55
|
+
| Member | Description |
|
|
56
|
+
| --------------------- | -------------------------------------------------- |
|
|
57
|
+
| `dimensions` | `(width, height)` at level 0 |
|
|
58
|
+
| `level_count` | number of pyramid levels |
|
|
59
|
+
| `level_dimensions` | `(w, h)` per level |
|
|
60
|
+
| `level_downsamples` | downsample factor per level |
|
|
61
|
+
| `properties` | read-only metadata mapping (`openslide.vendor=unic`, `tmap.*`) |
|
|
62
|
+
| `associated_images` | lazy mapping, typically `macro` / `label` |
|
|
63
|
+
| `read_region(loc, level, size)` | `PIL.Image` (RGBA) of the region |
|
|
64
|
+
| `get_thumbnail(size)` | thumbnail from the lowest resolution level |
|
|
65
|
+
| `get_best_level_for_downsample(ds)` | best level for a downsample factor |
|
|
66
|
+
| `close()` / context manager | release resources |
|
|
67
|
+
|
|
68
|
+
### `tmapslide.open_slide(filename)`
|
|
69
|
+
|
|
70
|
+
Alias of `OpenSlide(filename)`.
|
|
71
|
+
|
|
72
|
+
## Format notes (reverse-engineered)
|
|
73
|
+
|
|
74
|
+
TMAP is an undocumented proprietary format. This reader is built from
|
|
75
|
+
binary analysis of real scanner output, cross-validated against
|
|
76
|
+
[ASlide](https://github.com/MrPeterJin/ASlide). In short: both variants
|
|
77
|
+
store plain JPEG tiles with a small binary header and index tables; there
|
|
78
|
+
is no encryption.
|
|
79
|
+
|
|
80
|
+
- TMAP06 stores 3 pyramid levels (40x / 10x / 2.5x); TMAP07 stores up to
|
|
81
|
+
10 levels (40x down to 0.078x, halving each level).
|
|
82
|
+
- TMAP06 level-2 previews come from pre-rendered `ShrinkTile` entries and
|
|
83
|
+
are JPEG-compressed at that scale (slightly softer than downsampling
|
|
84
|
+
level 0 yourself).
|
|
85
|
+
- `iter_tiles()` exposes the stored tile grid directly, useful for
|
|
86
|
+
tile-based ML pipelines.
|
|
87
|
+
|
|
88
|
+
## Testing
|
|
89
|
+
|
|
90
|
+
Tests run against real TMAP samples when the `scce_external_center` data
|
|
91
|
+
directory (or `TAPSLIDE_TEST_DATA`) is present next to the repo, and are
|
|
92
|
+
skipped otherwise.
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
pip install -e .[dev]
|
|
96
|
+
pytest
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
MIT — see [LICENSE](LICENSE).
|
|
102
|
+
|
|
103
|
+
## Acknowledgments
|
|
104
|
+
|
|
105
|
+
- [kfbslide](https://github.com/yifanfeng97/kfbslide) — the KFB reader this
|
|
106
|
+
project is modelled after.
|
|
107
|
+
- [OpenSlide](https://openslide.org/) — the API this library mimics.
|
|
108
|
+
- [ASlide](https://github.com/MrPeterJin/ASlide) — its independent
|
|
109
|
+
reverse-engineering of the TMAP06 layer/block structures (from decompiled
|
|
110
|
+
vendor SDK) was used to cross-validate this implementation. tmapslide is
|
|
111
|
+
an independent MIT-licensed implementation and ships no ASlide code.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""Read regions and metadata from a TMAP whole-slide image."""
|
|
2
|
+
|
|
3
|
+
import tmapslide
|
|
4
|
+
|
|
5
|
+
slide = tmapslide.OpenSlide("sample.TMAP")
|
|
6
|
+
|
|
7
|
+
print("dimensions: ", slide.dimensions)
|
|
8
|
+
print("level count: ", slide.level_count)
|
|
9
|
+
print("level dimensions:", slide.level_dimensions)
|
|
10
|
+
print("level downsamples:", slide.level_downsamples)
|
|
11
|
+
print("vendor: ", slide.properties["openslide.vendor"])
|
|
12
|
+
print("associated images:", list(slide.associated_images))
|
|
13
|
+
|
|
14
|
+
# Read a 1024x1024 region at the highest resolution.
|
|
15
|
+
region = slide.read_region((1000, 1000), 0, (1024, 1024))
|
|
16
|
+
region.save("region.png")
|
|
17
|
+
|
|
18
|
+
# Read from a lower pyramid level (4x downsampled).
|
|
19
|
+
region_l2 = slide.read_region((0, 0), 2, (1024, 1024))
|
|
20
|
+
region_l2.save("region_l2.png")
|
|
21
|
+
|
|
22
|
+
# Whole-slide thumbnail.
|
|
23
|
+
thumb = slide.get_thumbnail((512, 512))
|
|
24
|
+
thumb.save("thumbnail.png")
|
|
25
|
+
|
|
26
|
+
# Associated images (macro photo, label).
|
|
27
|
+
for name in slide.associated_images:
|
|
28
|
+
slide.associated_images[name].save(f"assoc_{name}.png")
|
|
29
|
+
|
|
30
|
+
slide.close()
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tmapslide"
|
|
7
|
+
version = "0.2.0"
|
|
8
|
+
description = "Pure Python UNIC TMAP whole-slide image reader with OpenSlide-compatible API"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Yifan Feng", email = "evanfeng97@gmail.com" },
|
|
14
|
+
]
|
|
15
|
+
keywords = [
|
|
16
|
+
"tmap",
|
|
17
|
+
"tmapslide",
|
|
18
|
+
"unic",
|
|
19
|
+
"whole-slide-image",
|
|
20
|
+
"openslide",
|
|
21
|
+
"pathology",
|
|
22
|
+
"wsi",
|
|
23
|
+
"digital-pathology",
|
|
24
|
+
]
|
|
25
|
+
classifiers = [
|
|
26
|
+
"Development Status :: 3 - Alpha",
|
|
27
|
+
"Intended Audience :: Science/Research",
|
|
28
|
+
"License :: OSI Approved :: MIT License",
|
|
29
|
+
"Programming Language :: Python :: 3",
|
|
30
|
+
"Programming Language :: Python :: 3.10",
|
|
31
|
+
"Programming Language :: Python :: 3.11",
|
|
32
|
+
"Programming Language :: Python :: 3.12",
|
|
33
|
+
"Programming Language :: Python :: 3.13",
|
|
34
|
+
"Topic :: Scientific/Engineering :: Image Processing",
|
|
35
|
+
"Topic :: Scientific/Engineering :: Medical Science Apps.",
|
|
36
|
+
]
|
|
37
|
+
dependencies = [
|
|
38
|
+
"Pillow>=9.0.0",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.optional-dependencies]
|
|
42
|
+
dev = [
|
|
43
|
+
"pytest>=7.0",
|
|
44
|
+
"pytest-cov",
|
|
45
|
+
"ruff",
|
|
46
|
+
"mypy",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[project.urls]
|
|
50
|
+
Homepage = "https://github.com/yifanfeng97/tmapslide"
|
|
51
|
+
Documentation = "https://github.com/yifanfeng97/tmapslide#readme"
|
|
52
|
+
Repository = "https://github.com/yifanfeng97/tmapslide"
|
|
53
|
+
Issues = "https://github.com/yifanfeng97/tmapslide/issues"
|
|
54
|
+
|
|
55
|
+
[tool.hatch.build.targets.wheel]
|
|
56
|
+
packages = ["src/tmapslide"]
|
|
57
|
+
|
|
58
|
+
[tool.hatch.build.targets.sdist]
|
|
59
|
+
include = [
|
|
60
|
+
"/src",
|
|
61
|
+
"/tests",
|
|
62
|
+
"/examples",
|
|
63
|
+
"/README.md",
|
|
64
|
+
"/LICENSE",
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
[tool.ruff]
|
|
68
|
+
line-length = 100
|
|
69
|
+
select = ["E", "F", "I", "W"]
|
|
70
|
+
|
|
71
|
+
[tool.mypy]
|
|
72
|
+
python_version = "3.10"
|
|
73
|
+
warn_return_any = true
|
|
74
|
+
warn_unused_configs = true
|
|
75
|
+
disallow_untyped_defs = false
|
|
76
|
+
|
|
77
|
+
[tool.pytest.ini_options]
|
|
78
|
+
pythonpath = ["src"]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""tmapslide — Pure Python TMAP whole-slide image reader.
|
|
2
|
+
|
|
3
|
+
A minimal, cross-platform reader for UNIC TMAP whole-slide images
|
|
4
|
+
with an OpenSlide-compatible API.
|
|
5
|
+
|
|
6
|
+
Author: Yifan Feng <evanfeng97@gmail.com>
|
|
7
|
+
License: MIT
|
|
8
|
+
|
|
9
|
+
Drop-in usage:
|
|
10
|
+
import tmapslide
|
|
11
|
+
slide = tmapslide.OpenSlide("sample.TMAP")
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from ._exceptions import (
|
|
15
|
+
OpenSlideError,
|
|
16
|
+
OpenSlideUnsupportedFormatError,
|
|
17
|
+
TmapError,
|
|
18
|
+
TmapOpenError,
|
|
19
|
+
TmapUnsupportedFormatError,
|
|
20
|
+
)
|
|
21
|
+
from ._slide import OpenSlide, TmapSlide, open_slide
|
|
22
|
+
|
|
23
|
+
__version__ = "0.2.0"
|
|
24
|
+
|
|
25
|
+
# Standard OpenSlide property name constants
|
|
26
|
+
PROPERTY_NAME_VENDOR = "openslide.vendor"
|
|
27
|
+
PROPERTY_NAME_QUICKHASH1 = "openslide.quickhash-1"
|
|
28
|
+
PROPERTY_NAME_BACKGROUND_COLOR = "openslide.background-color"
|
|
29
|
+
PROPERTY_NAME_OBJECTIVE_POWER = "openslide.objective-power"
|
|
30
|
+
PROPERTY_NAME_MPP_X = "openslide.mpp-x"
|
|
31
|
+
PROPERTY_NAME_MPP_Y = "openslide.mpp-y"
|
|
32
|
+
PROPERTY_NAME_BOUNDS_X = "openslide.bounds-x"
|
|
33
|
+
PROPERTY_NAME_BOUNDS_Y = "openslide.bounds-y"
|
|
34
|
+
PROPERTY_NAME_BOUNDS_WIDTH = "openslide.bounds-width"
|
|
35
|
+
PROPERTY_NAME_BOUNDS_HEIGHT = "openslide.bounds-height"
|
|
36
|
+
|
|
37
|
+
__all__ = [
|
|
38
|
+
"OpenSlide",
|
|
39
|
+
"OpenSlideError",
|
|
40
|
+
"OpenSlideUnsupportedFormatError",
|
|
41
|
+
"TmapError",
|
|
42
|
+
"TmapOpenError",
|
|
43
|
+
"TmapUnsupportedFormatError",
|
|
44
|
+
"PROPERTY_NAME_VENDOR",
|
|
45
|
+
"PROPERTY_NAME_QUICKHASH1",
|
|
46
|
+
"PROPERTY_NAME_BACKGROUND_COLOR",
|
|
47
|
+
"PROPERTY_NAME_OBJECTIVE_POWER",
|
|
48
|
+
"PROPERTY_NAME_MPP_X",
|
|
49
|
+
"PROPERTY_NAME_MPP_Y",
|
|
50
|
+
"PROPERTY_NAME_BOUNDS_X",
|
|
51
|
+
"PROPERTY_NAME_BOUNDS_Y",
|
|
52
|
+
"PROPERTY_NAME_BOUNDS_WIDTH",
|
|
53
|
+
"PROPERTY_NAME_BOUNDS_HEIGHT",
|
|
54
|
+
"TmapSlide",
|
|
55
|
+
"open_slide",
|
|
56
|
+
]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""LRU cache for decoded TMAP tiles."""
|
|
2
|
+
|
|
3
|
+
from collections import OrderedDict
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
from PIL import Image
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class _LRUCache:
|
|
10
|
+
"""OrderedDict-backed LRU cache for decoded tiles (O(1) ops)."""
|
|
11
|
+
|
|
12
|
+
__slots__ = ("capacity", "_cache")
|
|
13
|
+
|
|
14
|
+
def __init__(self, capacity: int):
|
|
15
|
+
self.capacity = max(0, capacity)
|
|
16
|
+
self._cache: OrderedDict[int, Image.Image] = OrderedDict()
|
|
17
|
+
|
|
18
|
+
def get(self, key: int) -> Optional[Image.Image]:
|
|
19
|
+
if key not in self._cache:
|
|
20
|
+
return None
|
|
21
|
+
self._cache.move_to_end(key)
|
|
22
|
+
return self._cache[key]
|
|
23
|
+
|
|
24
|
+
def put(self, key: int, value: Image.Image) -> None:
|
|
25
|
+
if self.capacity <= 0:
|
|
26
|
+
return
|
|
27
|
+
if key in self._cache:
|
|
28
|
+
self._cache.move_to_end(key)
|
|
29
|
+
elif len(self._cache) >= self.capacity:
|
|
30
|
+
self._cache.popitem(last=False)
|
|
31
|
+
self._cache[key] = value
|
|
32
|
+
|
|
33
|
+
def clear(self) -> None:
|
|
34
|
+
self._cache.clear()
|
|
35
|
+
|
|
36
|
+
def __len__(self) -> int:
|
|
37
|
+
return len(self._cache)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""tmapslide / OpenSlide-compatible exceptions."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class OpenSlideError(Exception):
|
|
5
|
+
"""Base exception for OpenSlide errors."""
|
|
6
|
+
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class OpenSlideUnsupportedFormatError(OpenSlideError):
|
|
11
|
+
"""File format not supported or file is corrupted."""
|
|
12
|
+
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# Backward/short aliases
|
|
17
|
+
TmapError = OpenSlideError
|
|
18
|
+
TmapUnsupportedFormatError = OpenSlideUnsupportedFormatError
|
|
19
|
+
TmapOpenError = OpenSlideError
|