wsi-patching 0.5.4__tar.gz → 0.5.5__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.
Files changed (111) hide show
  1. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/.github/workflows/integration_tests.yaml +1 -1
  2. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/.github/workflows/unit_tests.yaml +1 -1
  3. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/PKG-INFO +6 -6
  4. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/README.md +2 -2
  5. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/docs/concepts.md +4 -2
  6. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/docs/installation.md +1 -1
  7. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/docs/reference.md +4 -3
  8. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/pyproject.toml +2 -2
  9. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/backends/fastslide.py +19 -1
  10. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/core/wsi_grid.py +18 -7
  11. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/integration/test_resample_pipeline.py +5 -2
  12. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/backends/test_fastslide.py +105 -4
  13. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/core/test_wsi_grid.py +17 -2
  14. wsi_patching-0.5.5/uv.lock +2081 -0
  15. wsi_patching-0.5.4/uv.lock +0 -2020
  16. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/.github/workflows/docs.yml +0 -0
  17. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/.github/workflows/publish.yaml +0 -0
  18. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/.gitignore +0 -0
  19. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/LICENSE +0 -0
  20. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/docs/auditing.md +0 -0
  21. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/docs/extending.md +0 -0
  22. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/docs/index.md +0 -0
  23. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/docs/quickstart.md +0 -0
  24. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/examples/audit_and_visualize_demo.ipynb +0 -0
  25. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/examples/numpy_stream_writer_demo.py +0 -0
  26. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/examples/torch_stream_writer_demo.py +0 -0
  27. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/examples/webdataset_materialize_writer_demo.py +0 -0
  28. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/mkdocs.yml +0 -0
  29. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/__init__.py +0 -0
  30. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/annotations/__init__.py +0 -0
  31. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/annotations/add_annotation_from_geojson.py +0 -0
  32. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/annotations/add_cell_annotation_from_csv.py +0 -0
  33. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/assets/models/cellvit_tissue_detector.pt +0 -0
  34. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/backends/__init__.py +0 -0
  35. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/backends/cupy_numpy.py +0 -0
  36. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/backends/torch_device.py +0 -0
  37. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/core/__init__.py +0 -0
  38. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/core/chunking_and_batching.py +0 -0
  39. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/core/pipeline.py +0 -0
  40. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/core/types/__init__.py +0 -0
  41. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/core/types/types.py +0 -0
  42. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/core/types/util_types.py +0 -0
  43. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/custom_component/__init__.py +0 -0
  44. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/encoders/__init__.py +0 -0
  45. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/encoders/pngencoder.py +0 -0
  46. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/filtering/__init__.py +0 -0
  47. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/filtering/cellvit_tissue_classifier_filter.py +0 -0
  48. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/filtering/low_contrast_background_filter.py +0 -0
  49. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/filtering/otsu_filter.py +0 -0
  50. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/filtering/pen_artifact_filter.py +0 -0
  51. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/filtering/remove_edge_tiles.py +0 -0
  52. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/py.typed +0 -0
  53. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/regions_of_interest/__init__.py +0 -0
  54. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/regions_of_interest/attach_rois.py +0 -0
  55. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/regions_of_interest/roi_providers.py +0 -0
  56. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/regions_of_interest/rois.py +0 -0
  57. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/transforms/__init__.py +0 -0
  58. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/transforms/macenko_normalizer.py +0 -0
  59. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/transforms/reinhard_normalizer.py +0 -0
  60. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/utils/__init__.py +0 -0
  61. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/utils/audit.py +0 -0
  62. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/utils/logging_config.py +0 -0
  63. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/utils/meta_typing.py +0 -0
  64. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/utils/profiling.py +0 -0
  65. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/utils/wsi_thumbnail_visualization.py +0 -0
  66. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/writers/__init__.py +0 -0
  67. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/writers/materialize_writers/__init__.py +0 -0
  68. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/writers/materialize_writers/materialize_writer_base.py +0 -0
  69. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/writers/materialize_writers/webdataset/__init__.py +0 -0
  70. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/writers/materialize_writers/webdataset/webdataset_loader.py +0 -0
  71. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/writers/materialize_writers/webdataset/webdataset_writer.py +0 -0
  72. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/writers/stream_writers/__init__.py +0 -0
  73. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/writers/stream_writers/numpy_stream_writer.py +0 -0
  74. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/writers/stream_writers/stream_writer_base.py +0 -0
  75. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/src/wsi_patching/writers/stream_writers/torch_stream_writer.py +0 -0
  76. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/benchmarks/conftest.py +0 -0
  77. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/benchmarks/test_benchmark_pipeline.py +0 -0
  78. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/benchmarks/test_benchmark_png_encoding.py +0 -0
  79. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/benchmarks/test_benchmark_tile_planning.py +0 -0
  80. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/integration/conftest.py +0 -0
  81. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/integration/test_audit_pipeline.py +0 -0
  82. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/integration/test_materialize_pipeline.py +0 -0
  83. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/integration/test_roi_pipeline.py +0 -0
  84. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/integration/test_streaming_pipeline.py +0 -0
  85. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/.gitkeep +0 -0
  86. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/annotations/test_add_annotation_from_geojson.py +0 -0
  87. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/annotations/test_add_cell_annotation_from_csv.py +0 -0
  88. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/backends/test_cupy_numpy.py +0 -0
  89. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/backends/test_torch_device.py +0 -0
  90. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/core/test_chunking_and_batching.py +0 -0
  91. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/core/test_pipeline.py +0 -0
  92. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/filtering/test_cellvit_tissue_classifier_filter.py +0 -0
  93. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/filtering/test_low_contrast_background_filter.py +0 -0
  94. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/filtering/test_otsu_filter.py +0 -0
  95. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/filtering/test_pen_artifact_filter.py +0 -0
  96. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/filtering/test_remove_edge_tiles.py +0 -0
  97. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/regions_of_interest/test_attach_rois.py +0 -0
  98. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/regions_of_interest/test_roi_providers.py +0 -0
  99. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/regions_of_interest/test_rois.py +0 -0
  100. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/transforms/test_macenko_normalizer.py +0 -0
  101. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/transforms/test_reinhard_normalizer.py +0 -0
  102. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/utils/test_audit.py +0 -0
  103. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/utils/test_logging_config.py +0 -0
  104. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/utils/test_meta_typing.py +0 -0
  105. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/utils/test_profiling.py +0 -0
  106. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/writers/materialize_writers/test_materialize_writer_base.py +0 -0
  107. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/writers/materialize_writers/webdataset/test_webdataset_loader.py +0 -0
  108. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/writers/materialize_writers/webdataset/test_webdataset_writer.py +0 -0
  109. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/writers/stream_writers/test_numpy_stream_writer.py +0 -0
  110. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/writers/stream_writers/test_stream_writer_base.py +0 -0
  111. {wsi_patching-0.5.4 → wsi_patching-0.5.5}/tests/unit/src/wsi_patching/writers/stream_writers/test_torch_stream_writer.py +0 -0
@@ -18,7 +18,7 @@ jobs:
18
18
  runs-on: ubuntu-latest
19
19
  strategy:
20
20
  matrix:
21
- python-version: ${{ fromJson( ((github.ref == 'refs/heads/main') || startsWith(github.ref, 'refs/tags/')) && '["3.10","3.11","3.12","3.13"]' || '["3.10","3.13"]' ) }}
21
+ python-version: ${{ fromJson( ((github.ref == 'refs/heads/main') || startsWith(github.ref, 'refs/tags/')) && '["3.11","3.12","3.13","3.14"]' || '["3.11","3.14"]' ) }}
22
22
 
23
23
  steps:
24
24
  - name: Checkout
@@ -18,7 +18,7 @@ jobs:
18
18
  runs-on: ubuntu-latest
19
19
  strategy:
20
20
  matrix:
21
- python-version: ${{ fromJson( ((github.ref == 'refs/heads/main') || startsWith(github.ref, 'refs/tags/')) && '["3.10","3.11","3.12","3.13"]' || '["3.10","3.13"]' ) }}
21
+ python-version: ${{ fromJson( ((github.ref == 'refs/heads/main') || startsWith(github.ref, 'refs/tags/')) && '["3.11","3.12","3.13","3.14"]' || '["3.11","3.14"]' ) }}
22
22
 
23
23
  steps:
24
24
  - name: Checkout
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: wsi-patching
3
- Version: 0.5.4
3
+ Version: 0.5.5
4
4
  Summary: A High Performance Patching Library for Digital Pathology AI dataset creation.
5
5
  Project-URL: Homepage, https://github.com/amspath/wsi-patching-pipeline
6
6
  Project-URL: Repository, https://github.com/amspath/wsi-patching-pipeline
@@ -33,13 +33,13 @@ Classifier: Intended Audience :: Science/Research
33
33
  Classifier: License :: OSI Approved :: MIT License
34
34
  Classifier: Operating System :: OS Independent
35
35
  Classifier: Programming Language :: Python :: 3
36
- Classifier: Programming Language :: Python :: 3.10
37
36
  Classifier: Programming Language :: Python :: 3.11
38
37
  Classifier: Programming Language :: Python :: 3.12
39
38
  Classifier: Programming Language :: Python :: 3.13
39
+ Classifier: Programming Language :: Python :: 3.14
40
40
  Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
41
41
  Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
42
- Requires-Python: <3.14,>=3.10
42
+ Requires-Python: >=3.11
43
43
  Requires-Dist: fastslide>=0.7.5
44
44
  Requires-Dist: numpy>=1.24
45
45
  Requires-Dist: opencv-python-headless>=4.8
@@ -78,7 +78,7 @@ A pragmatic pipeline for streaming whole-slide image (WSI) patches with region p
78
78
 
79
79
  ## 1) Library install
80
80
 
81
- Python ≥3.10 <3.14 is recommended. 3.14 is yet unsupported.
81
+ Python ≥3.11 is supported, and 3.11 through 3.14 are tested in CI.
82
82
  ```
83
83
  # CPU install
84
84
  pip install wsi-patching
@@ -261,7 +261,7 @@ PNGEncoder.isolated 640 1.440s 2.412ms
261
261
 
262
262
  ## 5) Development install
263
263
 
264
- Python ≥3.10 is recommended. UV is also recommended as the package manager
264
+ Python ≥3.11 is recommended. UV is also recommended as the package manager
265
265
  ```
266
266
  git clone https://github.com/amspath/wsi-patching-pipeline.git
267
267
  cd wsi-patching-pipeline
@@ -15,7 +15,7 @@ A pragmatic pipeline for streaming whole-slide image (WSI) patches with region p
15
15
 
16
16
  ## 1) Library install
17
17
 
18
- Python ≥3.10 <3.14 is recommended. 3.14 is yet unsupported.
18
+ Python ≥3.11 is supported, and 3.11 through 3.14 are tested in CI.
19
19
  ```
20
20
  # CPU install
21
21
  pip install wsi-patching
@@ -198,7 +198,7 @@ PNGEncoder.isolated 640 1.440s 2.412ms
198
198
 
199
199
  ## 5) Development install
200
200
 
201
- Python ≥3.10 is recommended. UV is also recommended as the package manager
201
+ Python ≥3.11 is recommended. UV is also recommended as the package manager
202
202
  ```
203
203
  git clone https://github.com/amspath/wsi-patching-pipeline.git
204
204
  cd wsi-patching-pipeline
@@ -90,12 +90,14 @@ If the slide does not have an exact match for the requested resolution, `fallbac
90
90
 
91
91
  | `fallback_mode` | Behaviour |
92
92
  |---|---|
93
- | `"error"` *(default)* | Raises an error if no exact match |
94
- | `"nearest"` | Picks the closest available level |
93
+ | `"error"` | Raises an error if no exact match |
94
+ | `"nearest"` *(default)* | Picks the closest available level, provided it is within `max_relative_deviation` of the request |
95
95
  | `"floor"` | Picks the highest resolution level that is ≤ the request |
96
96
  | `"ceil"` | Picks the lowest resolution level that is ≥ the request |
97
97
  | `"resample"` | Reads from a finer level, then downsamples with `cv2.resize` to hit the exact target. Not valid with `unit="level"`. |
98
98
 
99
+ `max_relative_deviation` guards `fallback_mode="nearest"`: if the closest level deviates from the requested resolution by more than this fraction (default `0.05`, i.e. 5%), an error is raised instead of silently patching at the wrong resolution. Pass `None` to disable the check. The other fallback modes ignore it.
100
+
99
101
  `resample_interpolation` controls the interpolation method when `fallback_mode="resample"` (default `"lanczos"`; options: `"nearest"`, `"linear"`, `"cubic"`, `"area"`, `"lanczos"`).
100
102
 
101
103
  ---
@@ -1,6 +1,6 @@
1
1
  # Installation
2
2
 
3
- Python **≥ 3.10, < 3.14** is required. Python 3.14 is not yet supported.
3
+ Python **≥ 3.11** is required. 3.11 through 3.14 are tested in CI.
4
4
 
5
5
  ## CPU
6
6
 
@@ -17,11 +17,12 @@ from wsi_patching import WSIGrid
17
17
  | Parameter | Type | Default | Description |
18
18
  |---|---|---|---|
19
19
  | `slides` | `List[str]` | — | Paths to WSI files |
20
- | `resolution` | `float` | — | Desired resolution value |
21
- | `unit` | `"level" \| "mpp" \| "downsample"` | — | Unit of `resolution` |
20
+ | `resolution` | `float` | `0.25` | Desired resolution value |
21
+ | `unit` | `"level" \| "mpp" \| "downsample"` | `"mpp"` | Unit of `resolution` |
22
22
  | `use_gpu` | `bool` | `False` | Enable GPU-accelerated backends (CuPy) |
23
- | `fallback_mode` | `"nearest" \| "floor" \| "ceil" \| "error" \| "resample"` | `"error"` | What to do when the exact resolution is unavailable |
23
+ | `fallback_mode` | `"nearest" \| "floor" \| "ceil" \| "error" \| "resample"` | `"nearest"` | What to do when the exact resolution is unavailable |
24
24
  | `resample_interpolation` | `"nearest" \| "linear" \| "cubic" \| "area" \| "lanczos"` | `"lanczos"` | Interpolation used when `fallback_mode="resample"` |
25
+ | `max_relative_deviation` | `float \| None` | `0.05` | Max relative deviation between the request and the selected level for `fallback_mode="nearest"`; `None` disables the check |
25
26
 
26
27
  ---
27
28
 
@@ -8,7 +8,7 @@ dynamic = ["version"]
8
8
  description = "A High Performance Patching Library for Digital Pathology AI dataset creation."
9
9
  readme = {file = "README.md", content-type = "text/markdown"}
10
10
  license = {file = "LICENSE"}
11
- requires-python = ">=3.10,<3.14"
11
+ requires-python = ">=3.11"
12
12
  authors = [{ name = "Robin van Hoorn", email = "r.d.vanhoorn@amsterdamumc.nl" }]
13
13
  keywords = ["digital pathology", "whole slide image", "WSI", "patching", "deep learning", "medical imaging"]
14
14
  classifiers = [
@@ -17,10 +17,10 @@ classifiers = [
17
17
  "Topic :: Scientific/Engineering :: Medical Science Apps.",
18
18
  "Topic :: Scientific/Engineering :: Artificial Intelligence",
19
19
  "Programming Language :: Python :: 3",
20
- "Programming Language :: Python :: 3.10",
21
20
  "Programming Language :: Python :: 3.11",
22
21
  "Programming Language :: Python :: 3.12",
23
22
  "Programming Language :: Python :: 3.13",
23
+ "Programming Language :: Python :: 3.14",
24
24
  "License :: OSI Approved :: MIT License",
25
25
  "Operating System :: OS Independent",
26
26
  ]
@@ -140,6 +140,7 @@ def get_level_for_resolution(
140
140
  resolution: float,
141
141
  unit: Literal["level", "mpp", "downsample"],
142
142
  fallback_mode: Literal["nearest", "floor", "ceil", "error", "resample"],
143
+ max_relative_deviation: float | None = None,
143
144
  ) -> int:
144
145
  """
145
146
  Determine which pyramid level to use for a given resolution specification.
@@ -152,7 +153,8 @@ def get_level_for_resolution(
152
153
  - If unit == "downsample": Downsample factor relative to level 0.
153
154
  unit: Resolution unit ("level", "mpp", or "downsample").
154
155
  fallback_mode:
155
- - "nearest": pick level whose value is closest to 'resolution'.
156
+ - "nearest": pick level whose value is closest to 'resolution';
157
+ see 'max_relative_deviation'.
156
158
  - "floor": pick coarsest level with value >= 'resolution'
157
159
  (if none, use coarsest level).
158
160
  - "ceil": pick finest level with value <= 'resolution'
@@ -160,6 +162,9 @@ def get_level_for_resolution(
160
162
  - "resample": same selection as "ceil"; caller must then resample
161
163
  the read pixels to the exact requested resolution.
162
164
  Raises ValueError when unit == "level".
165
+ max_relative_deviation: Maximum allowed |level_value - resolution| / resolution
166
+ for fallback_mode="nearest". Raises ValueError when the closest level lies
167
+ outside that band. None disables the check. Ignored by the other modes.
163
168
 
164
169
  Returns:
165
170
  level_idx: int
@@ -169,6 +174,9 @@ def get_level_for_resolution(
169
174
  if fallback_mode == "resample" and unit == "level":
170
175
  raise ValueError("fallback_mode='resample' is not meaningful for unit='level'.")
171
176
 
177
+ if max_relative_deviation is not None and max_relative_deviation < 0:
178
+ raise ValueError(f"max_relative_deviation must be non-negative, got {max_relative_deviation}.")
179
+
172
180
  # --- Trivial case: unit == "level" ---------------------------------
173
181
  if unit == "level":
174
182
  if not float(resolution).is_integer():
@@ -240,4 +248,14 @@ def get_level_for_resolution(
240
248
  else:
241
249
  raise ValueError(f"Unknown fallback_mode: {fallback_mode}")
242
250
 
251
+ if fallback_mode == "nearest" and max_relative_deviation is not None:
252
+ deviation = abs(values[level_idx] - requested) / requested
253
+ if deviation > max_relative_deviation:
254
+ raise ValueError(
255
+ f"Nearest level {level_idx} has {unit} {values[level_idx]}, deviating "
256
+ f"{deviation:.2%} from the requested {requested} "
257
+ f"(max_relative_deviation={max_relative_deviation:.2%}). "
258
+ f"Available values: {values}. Widen max_relative_deviation, or pass None to disable."
259
+ )
260
+
243
261
  return level_idx
@@ -21,11 +21,12 @@ class WSIGrid(Stage):
21
21
  def __init__(
22
22
  self,
23
23
  slides: List[str],
24
- resolution: float,
25
- unit: Literal["level", "mpp", "downsample"],
24
+ resolution: float = 0.25,
25
+ unit: Literal["level", "mpp", "downsample"] = "mpp",
26
26
  use_gpu: bool = False,
27
- fallback_mode: Literal["nearest", "floor", "ceil", "error", "resample"] = "error",
27
+ fallback_mode: Literal["nearest", "floor", "ceil", "error", "resample"] = "nearest",
28
28
  resample_interpolation: Literal["nearest", "linear", "cubic", "area", "lanczos"] = "lanczos",
29
+ max_relative_deviation: float | None = 0.05,
29
30
  ):
30
31
  """
31
32
  Initializes the WSIGrid stage, the starting point of a WSI patching pipeline.
@@ -33,14 +34,18 @@ class WSIGrid(Stage):
33
34
  Args:
34
35
  slides: List of file paths to whole slide images (WSIs).
35
36
  use_gpu: Whether to use GPU-accelerated backends when possible (e.g., cuCIM, CuPy).
36
- resolution: Desired resolution for patch extraction.
37
- unit: Unit of the resolution ("level", "mpp", or "downsample").
38
- fallback_mode: Strategy for selecting resolution if exact match is unavailable (default is "error").
37
+ resolution: Desired resolution for patch extraction (default is 0.25).
38
+ unit: Unit of the resolution ("level", "mpp", or "downsample"). Default is "mpp".
39
+ fallback_mode: Strategy for selecting resolution if exact match is unavailable (default is "nearest").
39
40
  Options are ("nearest", "floor", "ceil", "error", "resample").
40
41
  - "resample": selects the finest level with resolution <= requested, reads extra pixels,
41
42
  then downsamples to the exact target resolution using cv2.resize. Not valid for unit="level".
42
43
  resample_interpolation: Interpolation method used when fallback_mode="resample".
43
44
  Options are ("nearest", "linear", "cubic", "area", "lanczos"). Default is "lanczos".
45
+ max_relative_deviation: Maximum allowed relative deviation between the requested
46
+ resolution and the selected level when fallback_mode="nearest" (default is 0.05,
47
+ i.e. 5%). Raises if the closest level lies outside that band. Pass None to disable.
48
+ Ignored by the other fallback modes.
44
49
  """
45
50
  self.slides = list(slides)
46
51
  self.use_gpu = use_gpu
@@ -48,6 +53,7 @@ class WSIGrid(Stage):
48
53
  self.unit = unit
49
54
  self.fallback_mode = fallback_mode
50
55
  self.resample_interpolation = resample_interpolation
56
+ self.max_relative_deviation = max_relative_deviation
51
57
 
52
58
  def export_context(self, ctx: "PipelineContext") -> None:
53
59
  # Seed/override global grid parameters for other stages to read.
@@ -56,6 +62,7 @@ class WSIGrid(Stage):
56
62
  ctx["fallback_mode"] = self.fallback_mode
57
63
  ctx["use_gpu"] = self.use_gpu
58
64
  ctx["resample_interpolation"] = self.resample_interpolation
65
+ ctx["max_relative_deviation"] = self.max_relative_deviation
59
66
 
60
67
  def validate(self) -> None:
61
68
  validate_xp_backend(self.use_gpu)
@@ -68,6 +75,7 @@ class WSIGrid(Stage):
68
75
  unit=self.unit,
69
76
  fallback_mode=self.fallback_mode,
70
77
  resample_interpolation=self.resample_interpolation,
78
+ max_relative_deviation=self.max_relative_deviation,
71
79
  )
72
80
 
73
81
  def __call__(self, it: Iterable[Any]) -> Iterable[Slide]:
@@ -77,7 +85,9 @@ class WSIGrid(Stage):
77
85
  raise FileNotFoundError(f"Slide path does not exist: {path}")
78
86
 
79
87
  wsi_id = Path(path).stem
80
- selected_level = get_level_for_resolution(path, self.resolution, self.unit, self.fallback_mode)
88
+ selected_level = get_level_for_resolution(
89
+ path, self.resolution, self.unit, self.fallback_mode, max_relative_deviation=self.max_relative_deviation
90
+ )
81
91
  W, H = get_dimensions_for_level(path, selected_level)
82
92
  level_downsamples = get_level_downsamples(path)
83
93
  downsample = level_downsamples[selected_level]
@@ -110,6 +120,7 @@ class WSIGrid(Stage):
110
120
  "slide.requested_resolution": self.resolution,
111
121
  "slide.requested_unit": self.unit,
112
122
  "slide.requested_fallback_mode": self.fallback_mode,
123
+ "slide.requested_max_relative_deviation": self.max_relative_deviation,
113
124
  "slide.selected_level": selected_level,
114
125
  "slide.downsample": slide_downsample,
115
126
  "slide.path": path,
@@ -30,8 +30,11 @@ class TestResampleFallbackMode:
30
30
  resample_batches = _stream_patches(pyramid_slide, resolution=4.0, unit="downsample", fallback_mode="resample")
31
31
  resample_total = sum(b.shape[0] for b in resample_batches)
32
32
 
33
- # nearest: picks level 1 (ds=2.0, closest to 4.0); actual dims 256×256
34
- nearest_batches = _stream_patches(pyramid_slide, resolution=4.0, unit="downsample", fallback_mode="nearest")
33
+ # nearest: picks level 1 (ds=2.0, closest to 4.0); actual dims 256×256.
34
+ # 2.0 is 50% off the request, so the deviation guard has to be disabled here.
35
+ nearest_batches = _stream_patches(
36
+ pyramid_slide, resolution=4.0, unit="downsample", fallback_mode="nearest", max_relative_deviation=None
37
+ )
35
38
  nearest_total = sum(b.shape[0] for b in nearest_batches)
36
39
 
37
40
  assert resample_total == 4, f"expected 4 patches (2×2 virtual grid), got {resample_total}"
@@ -267,6 +267,110 @@ class TestGetLevelForResolutionUnitMpp:
267
267
  mod.get_level_for_resolution("dummy.svs", resolution=0.5, unit="mpp", fallback_mode="nearest")
268
268
 
269
269
 
270
+ # ---------------------------------------------------------------------------
271
+ # get_level_for_resolution — max_relative_deviation
272
+ # ---------------------------------------------------------------------------
273
+
274
+
275
+ class TestGetLevelForResolutionMaxRelativeDeviation:
276
+ # level_downsamples = [1.0, 2.0, 4.0]
277
+
278
+ @pytest.fixture(autouse=True)
279
+ def _patch(self, monkeypatch):
280
+ fake = FakeFastSlide("dummy.svs")
281
+ monkeypatch.setattr(mod, "_new_slide", _make_patcher(fake))
282
+
283
+ def test_within_tolerance_passes(self):
284
+ # 2.02 is 1% off level 2.0
285
+ assert (
286
+ mod.get_level_for_resolution(
287
+ "dummy.svs", resolution=2.02, unit="downsample", fallback_mode="nearest", max_relative_deviation=0.05
288
+ )
289
+ == 1
290
+ )
291
+
292
+ def test_outside_tolerance_raises(self):
293
+ # 1.3 → nearest is 1.0, 23% off
294
+ with pytest.raises(ValueError, match="max_relative_deviation"):
295
+ mod.get_level_for_resolution(
296
+ "dummy.svs", resolution=1.3, unit="downsample", fallback_mode="nearest", max_relative_deviation=0.01
297
+ )
298
+
299
+ def test_none_disables_check(self):
300
+ assert (
301
+ mod.get_level_for_resolution(
302
+ "dummy.svs", resolution=1.3, unit="downsample", fallback_mode="nearest", max_relative_deviation=None
303
+ )
304
+ == 0
305
+ )
306
+
307
+ @pytest.mark.parametrize("fallback_mode", ["floor", "ceil", "resample", "error"])
308
+ def test_ignored_by_other_modes(self, fallback_mode):
309
+ # 2.0 is an exact match, so every mode selects level 1 regardless of the deviation band
310
+ assert (
311
+ mod.get_level_for_resolution(
312
+ "dummy.svs",
313
+ resolution=2.0,
314
+ unit="downsample",
315
+ fallback_mode=cast(Literal["nearest", "floor", "ceil", "error", "resample"], fallback_mode),
316
+ max_relative_deviation=0.0,
317
+ )
318
+ == 1
319
+ )
320
+
321
+ def test_floor_ignores_deliberate_deviation(self):
322
+ # floor at 1.1 correctly returns 2.0 (82% off) — the band must not interfere
323
+ assert (
324
+ mod.get_level_for_resolution(
325
+ "dummy.svs", resolution=1.1, unit="downsample", fallback_mode="floor", max_relative_deviation=0.01
326
+ )
327
+ == 1
328
+ )
329
+
330
+ def test_negative_tolerance_raises(self):
331
+ with pytest.raises(ValueError, match="non-negative"):
332
+ mod.get_level_for_resolution(
333
+ "dummy.svs", resolution=2.0, unit="downsample", fallback_mode="nearest", max_relative_deviation=-0.1
334
+ )
335
+
336
+ def test_unit_level_is_unaffected(self):
337
+ assert (
338
+ mod.get_level_for_resolution(
339
+ "dummy.svs", resolution=2, unit="level", fallback_mode="nearest", max_relative_deviation=0.0
340
+ )
341
+ == 2
342
+ )
343
+
344
+
345
+ class TestGetLevelForResolutionMaxRelativeDeviationMpp:
346
+ """The slide from issue #84: base mpp 0.253, level mpps [0.253, 1.012, 4.048, 8.097]."""
347
+
348
+ @pytest.fixture(autouse=True)
349
+ def _patch(self, monkeypatch):
350
+ fake = FakeFastSlide(
351
+ "aperio.svs",
352
+ level_dimensions=[(4000, 3000), (1000, 750), (250, 188), (125, 94)],
353
+ level_downsamples=[1.0, 4.0, 16.0, 32.0],
354
+ mpp=(0.253, 0.253),
355
+ )
356
+ monkeypatch.setattr(mod, "_new_slide", _make_patcher(fake))
357
+
358
+ def test_nearest_selects_level_1_within_five_percent(self):
359
+ # level 1 is 1.012 mpp, 1.2% above the requested 1.0
360
+ assert (
361
+ mod.get_level_for_resolution(
362
+ "aperio.svs", resolution=1.0, unit="mpp", fallback_mode="nearest", max_relative_deviation=0.05
363
+ )
364
+ == 1
365
+ )
366
+
367
+ def test_nearest_raises_at_one_percent(self):
368
+ with pytest.raises(ValueError, match="deviating"):
369
+ mod.get_level_for_resolution(
370
+ "aperio.svs", resolution=1.0, unit="mpp", fallback_mode="nearest", max_relative_deviation=0.01
371
+ )
372
+
373
+
270
374
  # ---------------------------------------------------------------------------
271
375
  # get_level_for_resolution — invalid inputs
272
376
  # ---------------------------------------------------------------------------
@@ -434,10 +538,7 @@ class TestGetResampleFactor:
434
538
  def test_unknown_unit_raises(self):
435
539
  with pytest.raises(ValueError, match="Unknown unit"):
436
540
  mod.get_resample_factor(
437
- "dummy.svs",
438
- resolution=1.0,
439
- unit=cast(Literal["mpp", "downsample"], "level"),
440
- selected_level=0,
541
+ "dummy.svs", resolution=1.0, unit=cast(Literal["mpp", "downsample"], "level"), selected_level=0
441
542
  )
442
543
 
443
544
  def test_missing_mpp_raises(self, monkeypatch):
@@ -14,6 +14,7 @@ def test_export_context_sets_expected_keys():
14
14
  assert ctx["fallback_mode"] == "nearest"
15
15
  assert ctx["use_gpu"] is True
16
16
  assert ctx["resample_interpolation"] == "lanczos"
17
+ assert ctx["max_relative_deviation"] == 0.05
17
18
 
18
19
 
19
20
  def test_export_context_resample_interpolation_custom():
@@ -50,6 +51,7 @@ def test_for_slide_returns_single_slide_clone():
50
51
  unit="mpp",
51
52
  fallback_mode="nearest",
52
53
  resample_interpolation="cubic",
54
+ max_relative_deviation=0.5,
53
55
  )
54
56
 
55
57
  g2 = grid.for_slide("/data/C.svs")
@@ -60,6 +62,7 @@ def test_for_slide_returns_single_slide_clone():
60
62
  assert g2.unit == "mpp"
61
63
  assert g2.fallback_mode == "nearest"
62
64
  assert g2.resample_interpolation == "cubic"
65
+ assert g2.max_relative_deviation == 0.5
63
66
 
64
67
 
65
68
  @patch("wsi_patching.core.wsi_grid.Path.exists", return_value=True)
@@ -68,7 +71,7 @@ def test_for_slide_returns_single_slide_clone():
68
71
  @patch("wsi_patching.core.wsi_grid.get_level_for_resolution")
69
72
  def test_call_yields_slide_objects_with_dims(mock_get_level, mock_dims, mock_downsamples, mock_exists):
70
73
  # Mock per-path selected levels
71
- def _get_level(path, resolution, unit, fallback_mode):
74
+ def _get_level(path, resolution, unit, fallback_mode, max_relative_deviation=None):
72
75
  if path.endswith("A.svs"):
73
76
  return 1
74
77
  return 2
@@ -120,7 +123,10 @@ def test_call_yields_slide_objects_with_dims(mock_get_level, mock_dims, mock_dow
120
123
 
121
124
  # get_level_for_resolution called with our resolution/unit/fallback_mode
122
125
  mock_get_level.assert_has_calls(
123
- [call("/slides/A.svs", 0.5, "mpp", "nearest"), call("/slides/B.svs", 0.5, "mpp", "nearest")]
126
+ [
127
+ call("/slides/A.svs", 0.5, "mpp", "nearest", max_relative_deviation=0.05),
128
+ call("/slides/B.svs", 0.5, "mpp", "nearest", max_relative_deviation=0.05),
129
+ ]
124
130
  )
125
131
 
126
132
  # get_dimensions_for_level called with (path, level)
@@ -173,3 +179,12 @@ def test_non_resample_fallback_has_factor_one(mock_get_level, mock_dims, mock_do
173
179
  assert s.dims == (150, 100)
174
180
  assert s.downsample == 2.0
175
181
  assert s.resample_factor == 1.0
182
+
183
+
184
+ def test_defaults_are_quarter_mpp_nearest_with_five_percent_band():
185
+ grid = WSIGrid(slides=["/data/A.svs"])
186
+
187
+ assert grid.resolution == 0.25
188
+ assert grid.unit == "mpp"
189
+ assert grid.fallback_mode == "nearest"
190
+ assert grid.max_relative_deviation == 0.05