wsi-patching 0.5.3__tar.gz → 0.5.4__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 (113) hide show
  1. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/PKG-INFO +30 -2
  2. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/README.md +27 -0
  3. wsi_patching-0.5.4/docs/auditing.md +229 -0
  4. wsi_patching-0.5.4/examples/audit_and_visualize_demo.ipynb +110 -0
  5. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/mkdocs.yml +1 -0
  6. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/pyproject.toml +2 -2
  7. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/__init__.py +11 -2
  8. wsi_patching-0.5.4/src/wsi_patching/annotations/__init__.py +4 -0
  9. wsi_patching-0.5.4/src/wsi_patching/annotations/add_annotation_from_geojson.py +206 -0
  10. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/core/chunking_and_batching.py +3 -1
  11. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/core/pipeline.py +234 -16
  12. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/core/types/types.py +12 -0
  13. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/core/wsi_grid.py +1 -0
  14. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/custom_component/__init__.py +2 -0
  15. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/filtering/__init__.py +2 -0
  16. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/filtering/low_contrast_background_filter.py +4 -0
  17. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/filtering/otsu_filter.py +6 -0
  18. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/filtering/pen_artifact_filter.py +6 -0
  19. wsi_patching-0.5.4/src/wsi_patching/filtering/remove_edge_tiles.py +85 -0
  20. wsi_patching-0.5.4/src/wsi_patching/utils/__init__.py +3 -0
  21. wsi_patching-0.5.4/src/wsi_patching/utils/audit.py +273 -0
  22. wsi_patching-0.5.4/src/wsi_patching/utils/wsi_thumbnail_visualization.py +711 -0
  23. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/benchmarks/conftest.py +1 -1
  24. wsi_patching-0.5.4/tests/integration/test_audit_pipeline.py +140 -0
  25. wsi_patching-0.5.4/tests/unit/src/wsi_patching/annotations/test_add_annotation_from_geojson.py +166 -0
  26. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/annotations/test_add_cell_annotation_from_csv.py +3 -1
  27. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/filtering/test_cellvit_tissue_classifier_filter.py +1 -1
  28. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/filtering/test_low_contrast_background_filter.py +24 -4
  29. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/filtering/test_otsu_filter.py +6 -2
  30. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/filtering/test_pen_artifact_filter.py +5 -1
  31. wsi_patching-0.5.4/tests/unit/src/wsi_patching/filtering/test_remove_edge_tiles.py +111 -0
  32. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/transforms/test_macenko_normalizer.py +11 -3
  33. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/transforms/test_reinhard_normalizer.py +1 -0
  34. wsi_patching-0.5.4/tests/unit/src/wsi_patching/utils/test_audit.py +240 -0
  35. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/writers/materialize_writers/webdataset/test_webdataset_writer.py +5 -1
  36. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/uv.lock +513 -24
  37. wsi_patching-0.5.3/src/wsi_patching/annotations/__init__.py +0 -3
  38. wsi_patching-0.5.3/src/wsi_patching/utils/__init__.py +0 -3
  39. wsi_patching-0.5.3/src/wsi_patching/utils/wsi_thumbnail_visualization.py +0 -208
  40. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/.github/workflows/docs.yml +0 -0
  41. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/.github/workflows/integration_tests.yaml +0 -0
  42. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/.github/workflows/publish.yaml +0 -0
  43. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/.github/workflows/unit_tests.yaml +0 -0
  44. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/.gitignore +0 -0
  45. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/LICENSE +0 -0
  46. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/docs/concepts.md +0 -0
  47. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/docs/extending.md +0 -0
  48. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/docs/index.md +0 -0
  49. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/docs/installation.md +0 -0
  50. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/docs/quickstart.md +0 -0
  51. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/docs/reference.md +0 -0
  52. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/examples/numpy_stream_writer_demo.py +0 -0
  53. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/examples/torch_stream_writer_demo.py +0 -0
  54. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/examples/webdataset_materialize_writer_demo.py +0 -0
  55. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/annotations/add_cell_annotation_from_csv.py +0 -0
  56. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/assets/models/cellvit_tissue_detector.pt +0 -0
  57. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/backends/__init__.py +0 -0
  58. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/backends/cupy_numpy.py +0 -0
  59. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/backends/fastslide.py +0 -0
  60. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/backends/torch_device.py +0 -0
  61. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/core/__init__.py +0 -0
  62. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/core/types/__init__.py +0 -0
  63. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/core/types/util_types.py +0 -0
  64. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/encoders/__init__.py +0 -0
  65. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/encoders/pngencoder.py +0 -0
  66. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/filtering/cellvit_tissue_classifier_filter.py +0 -0
  67. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/py.typed +0 -0
  68. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/regions_of_interest/__init__.py +0 -0
  69. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/regions_of_interest/attach_rois.py +0 -0
  70. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/regions_of_interest/roi_providers.py +0 -0
  71. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/regions_of_interest/rois.py +0 -0
  72. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/transforms/__init__.py +0 -0
  73. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/transforms/macenko_normalizer.py +0 -0
  74. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/transforms/reinhard_normalizer.py +0 -0
  75. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/utils/logging_config.py +0 -0
  76. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/utils/meta_typing.py +0 -0
  77. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/utils/profiling.py +0 -0
  78. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/writers/__init__.py +0 -0
  79. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/writers/materialize_writers/__init__.py +0 -0
  80. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/writers/materialize_writers/materialize_writer_base.py +0 -0
  81. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/writers/materialize_writers/webdataset/__init__.py +0 -0
  82. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/writers/materialize_writers/webdataset/webdataset_loader.py +0 -0
  83. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/writers/materialize_writers/webdataset/webdataset_writer.py +0 -0
  84. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/writers/stream_writers/__init__.py +0 -0
  85. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/writers/stream_writers/numpy_stream_writer.py +0 -0
  86. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/writers/stream_writers/stream_writer_base.py +0 -0
  87. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/src/wsi_patching/writers/stream_writers/torch_stream_writer.py +0 -0
  88. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/benchmarks/test_benchmark_pipeline.py +0 -0
  89. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/benchmarks/test_benchmark_png_encoding.py +0 -0
  90. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/benchmarks/test_benchmark_tile_planning.py +0 -0
  91. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/integration/conftest.py +0 -0
  92. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/integration/test_materialize_pipeline.py +0 -0
  93. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/integration/test_resample_pipeline.py +0 -0
  94. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/integration/test_roi_pipeline.py +0 -0
  95. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/integration/test_streaming_pipeline.py +0 -0
  96. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/.gitkeep +0 -0
  97. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/backends/test_cupy_numpy.py +0 -0
  98. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/backends/test_fastslide.py +0 -0
  99. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/backends/test_torch_device.py +0 -0
  100. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/core/test_chunking_and_batching.py +0 -0
  101. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/core/test_pipeline.py +0 -0
  102. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/core/test_wsi_grid.py +0 -0
  103. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/regions_of_interest/test_attach_rois.py +0 -0
  104. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/regions_of_interest/test_roi_providers.py +0 -0
  105. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/regions_of_interest/test_rois.py +0 -0
  106. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/utils/test_logging_config.py +0 -0
  107. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/utils/test_meta_typing.py +0 -0
  108. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/utils/test_profiling.py +0 -0
  109. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/writers/materialize_writers/test_materialize_writer_base.py +0 -0
  110. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/writers/materialize_writers/webdataset/test_webdataset_loader.py +0 -0
  111. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/writers/stream_writers/test_numpy_stream_writer.py +0 -0
  112. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/writers/stream_writers/test_stream_writer_base.py +0 -0
  113. {wsi_patching-0.5.3 → wsi_patching-0.5.4}/tests/unit/src/wsi_patching/writers/stream_writers/test_torch_stream_writer.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wsi-patching
3
- Version: 0.5.3
3
+ Version: 0.5.4
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
@@ -40,7 +40,7 @@ Classifier: Programming Language :: Python :: 3.13
40
40
  Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
41
41
  Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
42
42
  Requires-Python: <3.14,>=3.10
43
- Requires-Dist: fastslide==0.7.3
43
+ Requires-Dist: fastslide>=0.7.5
44
44
  Requires-Dist: numpy>=1.24
45
45
  Requires-Dist: opencv-python-headless>=4.8
46
46
  Requires-Dist: orjson>=3.8
@@ -48,6 +48,7 @@ Requires-Dist: pillow>=9.0
48
48
  Requires-Dist: rtree>=1.0
49
49
  Requires-Dist: webdataset>=0.2.86
50
50
  Provides-Extra: dev
51
+ Requires-Dist: ipykernel; extra == 'dev'
51
52
  Requires-Dist: mkdocs-material; extra == 'dev'
52
53
  Requires-Dist: pytest; extra == 'dev'
53
54
  Requires-Dist: pytest-benchmark; extra == 'dev'
@@ -72,6 +73,7 @@ A pragmatic pipeline for streaming whole-slide image (WSI) patches with region p
72
73
  - Single writer process for continuous writing in the sink (i.e. to a webdataset, or numpy arrays).
73
74
  - Batched GPU steps.
74
75
  - Built-in isolated stage profiling per slide + aggregated stats.
76
+ - Auditing + interactive visualization of what each stage keeps vs. drops (great for tuning).
75
77
 
76
78
 
77
79
  ## 1) Library install
@@ -116,6 +118,32 @@ p = (
116
118
  p.materialize(num_workers=4)
117
119
  ```
118
120
 
121
+ ### Auditing: see what each stage throws out, and tune it live
122
+ Struggling to pick tile sizes and filter thresholds? Audit the pipeline to see, per stage, exactly which patches are kept vs. dropped — then **drag a slider to retune the filters and watch the overlay update instantly**. `dry_run()` runs the filters **without writing any output** (no encoding, no files) so you can iterate fast. See `examples/audit_and_visualize_demo.ipynb`.
123
+ ```python
124
+ from wsi_patching import WSIGrid, PatchExtractor, OtsuFilter, RemoveEdgeTiles, visualize_audit
125
+
126
+ p = (
127
+ WSIGrid(slides=slides, resolution=2, unit="level")
128
+ .then(PatchExtractor(tile_size=224, stride=224))
129
+ .then(OtsuFilter(min_tissue_fraction=0.35))
130
+ .then(RemoveEdgeTiles(depth=1))
131
+ )
132
+ p.dry_run() # audit only — nothing is written
133
+ p.print_audit() # per-stage keep/drop funnel table
134
+ visualize_audit(p, slides[0]) # interactive overlay (renders inline in Jupyter; .save("r.html") too)
135
+ ```
136
+ ```
137
+ === Audit: RBIO-GC072-HE-01 ===
138
+ Stage In Out Dropped Drop %
139
+ PatchExtractor 252 252 0 0.0%
140
+ OtsuFilter 252 85 167 66.3%
141
+ RemoveEdgeTiles 85 42 43 50.6%
142
+ ```
143
+ `visualize_audit()` draws kept patches (green) and dropped patches colored by the stage that removed them, with hover tooltips of the metadata behind each decision — plus a **slider for every tunable filter parameter**. Dragging one re-decides keep/drop for every patch in the browser (no Python re-run, so it works in the saved HTML too) and prints a config you can paste back into your pipeline.
144
+
145
+ You can also audit a real run with `p.materialize(audit=True)` / `p.stream(audit=True)`. See the [auditing docs](docs/auditing.md).
146
+
119
147
  ## 3) Check out the currently available components:
120
148
  #### Core components
121
149
  - `WSIGrid`: Your starter block
@@ -10,6 +10,7 @@ A pragmatic pipeline for streaming whole-slide image (WSI) patches with region p
10
10
  - Single writer process for continuous writing in the sink (i.e. to a webdataset, or numpy arrays).
11
11
  - Batched GPU steps.
12
12
  - Built-in isolated stage profiling per slide + aggregated stats.
13
+ - Auditing + interactive visualization of what each stage keeps vs. drops (great for tuning).
13
14
 
14
15
 
15
16
  ## 1) Library install
@@ -54,6 +55,32 @@ p = (
54
55
  p.materialize(num_workers=4)
55
56
  ```
56
57
 
58
+ ### Auditing: see what each stage throws out, and tune it live
59
+ Struggling to pick tile sizes and filter thresholds? Audit the pipeline to see, per stage, exactly which patches are kept vs. dropped — then **drag a slider to retune the filters and watch the overlay update instantly**. `dry_run()` runs the filters **without writing any output** (no encoding, no files) so you can iterate fast. See `examples/audit_and_visualize_demo.ipynb`.
60
+ ```python
61
+ from wsi_patching import WSIGrid, PatchExtractor, OtsuFilter, RemoveEdgeTiles, visualize_audit
62
+
63
+ p = (
64
+ WSIGrid(slides=slides, resolution=2, unit="level")
65
+ .then(PatchExtractor(tile_size=224, stride=224))
66
+ .then(OtsuFilter(min_tissue_fraction=0.35))
67
+ .then(RemoveEdgeTiles(depth=1))
68
+ )
69
+ p.dry_run() # audit only — nothing is written
70
+ p.print_audit() # per-stage keep/drop funnel table
71
+ visualize_audit(p, slides[0]) # interactive overlay (renders inline in Jupyter; .save("r.html") too)
72
+ ```
73
+ ```
74
+ === Audit: RBIO-GC072-HE-01 ===
75
+ Stage In Out Dropped Drop %
76
+ PatchExtractor 252 252 0 0.0%
77
+ OtsuFilter 252 85 167 66.3%
78
+ RemoveEdgeTiles 85 42 43 50.6%
79
+ ```
80
+ `visualize_audit()` draws kept patches (green) and dropped patches colored by the stage that removed them, with hover tooltips of the metadata behind each decision — plus a **slider for every tunable filter parameter**. Dragging one re-decides keep/drop for every patch in the browser (no Python re-run, so it works in the saved HTML too) and prints a config you can paste back into your pipeline.
81
+
82
+ You can also audit a real run with `p.materialize(audit=True)` / `p.stream(audit=True)`. See the [auditing docs](docs/auditing.md).
83
+
57
84
  ## 3) Check out the currently available components:
58
85
  #### Core components
59
86
  - `WSIGrid`: Your starter block
@@ -0,0 +1,229 @@
1
+ # Auditing & visualization
2
+
3
+ Tuning a patching pipeline means answering one question: **what does each stage
4
+ throw out, and why?** The auditing tools record, per stage per slide, how many
5
+ patches came in and out, *which* patches each stage dropped, and the metadata
6
+ behind each drop — then show it as a funnel table and an interactive overlay.
7
+
8
+ ## Dry run: audit without writing output
9
+
10
+ `dry_run()` runs the pipeline's filters **without a writer and without encoding**,
11
+ purely to collect the audit. It's the fast way to iterate on parameters. No
12
+ writer is required.
13
+
14
+ ```python
15
+ from wsi_patching import PatchExtractor, OtsuFilter, RemoveEdgeTiles, WSIGrid
16
+
17
+ p = (
18
+ WSIGrid(slides=["slide.tiff"], resolution=2, unit="level")
19
+ .then(PatchExtractor(tile_size=224, stride=224))
20
+ .then(OtsuFilter(min_tissue_fraction=0.35))
21
+ .then(RemoveEdgeTiles(depth=1))
22
+ )
23
+
24
+ p.dry_run()
25
+ p.print_audit()
26
+ ```
27
+
28
+ ```
29
+ === Audit: slide ===
30
+ Stage In Out Dropped Drop %
31
+ PatchExtractor 252 252 0 0.0%
32
+ OtsuFilter 252 85 167 66.3%
33
+ RemoveEdgeTiles 85 42 43 50.6%
34
+ ```
35
+
36
+ ## Interactive overlay
37
+
38
+ `visualize_audit(pipeline, slide)` returns a report that renders inline in
39
+ Jupyter (it's the last expression in the cell) and can be saved as a
40
+ self-contained HTML file.
41
+
42
+ ```python
43
+ from wsi_patching import visualize_audit
44
+
45
+ report = visualize_audit(p, "slide.tiff")
46
+ report # renders inline in a notebook
47
+ report.save("report.html") # standalone shareable file
48
+ ```
49
+
50
+ - **Green** patches are kept; each other color is a stage that dropped those patches.
51
+ - **Hover** a patch to see the stage and its metadata (e.g. `tissue_fraction=0.03`).
52
+ - **Sliders** re-tune each filter's threshold live — see below.
53
+ - **Unchecking a stage in the legend removes it from the pipeline**: everything is
54
+ re-filtered without it, so the patches it had dropped are re-judged by the
55
+ remaining stages and recolored (kept, or dropped by a later stage). The funnel
56
+ marks it `(off)` and the config box drops it.
57
+ - **The ▲▼ arrows reorder the filters**, re-running the whole attribution in the
58
+ new order — see below.
59
+
60
+ Every legend checkbox means exactly one thing — *is this stage in the pipeline?*
61
+ `kept` and `unknown` are outcomes rather than stages, so they are shown as plain
62
+ swatches with no checkbox.
63
+
64
+ ## Reordering filters
65
+
66
+ The ▲▼ arrows move a filter up or down the pipeline. The overlay, funnel and config
67
+ box all follow, and the config box is emitted in the new order — paste it straight
68
+ back into your code.
69
+
70
+ **Reordering cannot change which patches survive.** A patch is kept only if it
71
+ passes *every* filter, and that is independent of the order they run in. What it
72
+ changes is:
73
+
74
+ - **Who gets the blame.** With `OtsuFilter` last, it looks responsible for only 57
75
+ drops; move it first and you see it is really rejecting 167 patches — the other
76
+ filters were just getting to some of them first.
77
+ - **How much work each stage does** — which is a real speed decision. Putting the
78
+ cheap filter first means the expensive one sees far fewer patches:
79
+
80
+ ```
81
+ RemoveEdgeTiles 252 -> 190 OtsuFilter 252 -> 85
82
+ LowContrast 190 -> 67 RemoveEdgeTiles 85 -> 42
83
+ OtsuFilter 67 -> 10 LowContrast 42 -> 10
84
+ ```
85
+
86
+ Both keep the same 10 patches, but the left order only runs Otsu on 67 patches
87
+ instead of 252. `RemoveEdgeTiles` is pure arithmetic, so it is nearly free to put
88
+ first.
89
+
90
+ A filter will only swap with an adjacent **filter**. It cannot be moved across the
91
+ source or a pixel-transforming stage (e.g. a normalizer), because the recorded
92
+ metrics were measured on that stage's output — moving past it would invalidate them.
93
+ Those arrows are disabled.
94
+
95
+ Between the two you can answer both tuning questions without ever re-running:
96
+ *what threshold?* (slider) and *do I even need this filter?* (checkbox).
97
+
98
+ !!! note
99
+ Unchecking a filter that has **no slider** (e.g. `CellVitTissueClassifierFilter`)
100
+ turns its patches **grey — "unknown"**, not green. Those patches were dropped in
101
+ Python before the later stages ever measured them, so there is no honest way to
102
+ say what the rest of the pipeline would have done with them. Re-run without that
103
+ stage to find out.
104
+
105
+ ## Live parameter sliders
106
+
107
+ After a `dry_run()`, the report grows a **slider for every tunable filter
108
+ parameter**. Drag one — or type an exact value in the box on the right — and the
109
+ overlay, the funnel and a copy-pasteable config update instantly:
110
+
111
+ ```
112
+ [▲][▼] RemoveEdgeTiles.depth >= [==O=============] [ 1 ]
113
+ [▲][▼] OtsuFilter.min_tissue_fraction >= [======O=========] [ 0.350 ]
114
+
115
+ .then(RemoveEdgeTiles(depth=1))
116
+ .then(OtsuFilter(min_tissue_fraction=0.350))
117
+ ```
118
+
119
+ When you like what you see, paste the config box straight into your pipeline.
120
+
121
+ The slider track spans the observed range of the metric, since nothing outside it
122
+ changes the outcome — but the number box accepts any value, so you can push a
123
+ threshold past the extremes if you want to see it drop everything.
124
+
125
+ **No Python runs while you drag.** Every threshold filter computes a per-patch
126
+ metric that doesn't depend on its threshold (`tissue_fraction`, `pen_fraction`,
127
+ `gray_range`, `edge_distance`), so the report records the metric once and
128
+ re-decides keep/drop in the browser. That's why the sliders keep working in a
129
+ saved `.html` file with no kernel attached.
130
+
131
+ ### Which parameters get a slider
132
+
133
+ | Parameter | Slider |
134
+ | --- | --- |
135
+ | `OtsuFilter.min_tissue_fraction` | ✅ |
136
+ | `PenArtifactFilter.max_pen_fraction` | ✅ |
137
+ | `LowContrastBackgroundFilter.range_threshold` | ✅ |
138
+ | `RemoveEdgeTiles.depth` | ✅ |
139
+ | `OtsuFilter.num_bins` / `tissue_is_darker`, `PenArtifactFilter.diff_thresh` | ❌ changes the metric itself |
140
+ | `tile_size` / `stride` | ❌ changes the patch grid |
141
+
142
+ The ❌ ones can't be recomputed from recorded data — change them in the
143
+ constructor and `dry_run()` again.
144
+
145
+ ### How `dry_run` makes this work
146
+
147
+ A filter only computes its metric for the patches that reach it. With real
148
+ thresholds, a patch dropped by `OtsuFilter` never gets a `pen_fraction`, so a
149
+ slider could only ever *tighten*. To avoid that, `dry_run()` runs every tunable
150
+ filter **permissively** (thresholds neutralized so nothing drops); each patch
151
+ then flows through every stage and carries every metric, and any threshold can be
152
+ re-decided offline in either direction.
153
+
154
+ The funnel is unaffected: the drops are re-derived from the recorded metrics at
155
+ *your* configured thresholds, so `print_audit()` prints the same table it would
156
+ for a real run. The cost is speed — nothing is rejected early, so every filter
157
+ sees every patch. To audit many slides fast, use `dry_run(tune=False)`; the
158
+ report is then static.
159
+
160
+ `materialize(audit=True)` / `stream(audit=True)` are never permissive — a real
161
+ run writes exactly what its real thresholds say — so those reports are static too.
162
+
163
+ ### Tuning knobs on your own stage
164
+
165
+ Any stage whose decision is `metric op threshold` can opt in by declaring a
166
+ `Knob`, as long as the metric does not depend on the threshold:
167
+
168
+ ```python
169
+ from wsi_patching.custom_component import Knob, Stage
170
+
171
+ class MyFilter(Stage):
172
+ AUDIT_KNOBS = (Knob("max_blur", "blur_score", "<=", permissive=1.0),)
173
+
174
+ def __call__(self, it):
175
+ for batch in it:
176
+ batch.add_meta_column("blur_score", scores) # for every row, before filtering
177
+ batch.filter_on_mask(scores <= self.max_blur)
178
+ yield batch
179
+ ```
180
+
181
+ `permissive` is the value at which the stage drops nothing. Attach the metric
182
+ column for **all** rows *before* `filter_on_mask`, or the dropped patches won't
183
+ have it.
184
+
185
+ ## Auditing a real run
186
+
187
+ You don't have to dry-run. Pass `audit=True` to a normal run to get the same
188
+ funnel and overlay alongside your written output:
189
+
190
+ ```python
191
+ p.materialize(audit=True) # or p.stream(audit=True)
192
+ p.print_audit()
193
+ visualize_audit(p, "slide.tiff")
194
+ ```
195
+
196
+ Auditing is opt-in and adds no overhead to runs that don't request it.
197
+
198
+ ## Programmatic access
199
+
200
+ `pipeline.get_audit()` returns the raw data if you want to build your own
201
+ report:
202
+
203
+ ```python
204
+ audit = pipeline.get_audit()["by_slide"]["slide"]
205
+ audit["funnel"] # [{"stage", "in", "out", "dropped"}, ...]
206
+ audit["kept"] # (K, 2) coords surviving the whole pipeline
207
+ audit["dropped"] # [{"stage", "coords": (D, 2)}, ...] per dropping stage
208
+ audit["candidates"] # (N, 2) every coord that entered the pipeline
209
+ audit["patch_meta"] # {(x, y): {col: value}} per-patch metadata
210
+ audit["knobs"] # tunable params of the run, with their configured values
211
+ ```
212
+
213
+ After a `dry_run()` you can also re-derive the funnel for other knob values
214
+ without re-running — the same computation the sliders do, in Python:
215
+
216
+ ```python
217
+ knob = pipeline.get_audit()["by_slide"]["slide"]["knobs"][0]
218
+ for v in (0.1, 0.2, 0.3):
219
+ entry = pipeline.get_audit(values={knob["stage_idx"]: v})["by_slide"]["slide"]
220
+ print(v, len(entry["kept"]))
221
+ ```
222
+
223
+ !!! note
224
+ Attribution is generic: a stage's drops are the patch coordinates present in
225
+ its input but absent from its output. This works for every stage — current or
226
+ custom — with no per-stage code. Per-patch metadata is captured at the single
227
+ drop point (`CollatedPatchBatch.filter_on_mask`) for dropped patches and at the
228
+ last stage for the survivors, so any columns a filter attaches
229
+ (`tissue_fraction`, `pen_fraction`, …) show up automatically.
@@ -0,0 +1,110 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Pipeline auditing & live parameter tuning\n",
8
+ "\n",
9
+ "This notebook shows how to **see what each stage of a patching pipeline throws out**, and then **tune the filters with sliders** until you like the result.\n",
10
+ "\n",
11
+ "The workflow:\n",
12
+ "1. Build a pipeline as usual.\n",
13
+ "2. `p.dry_run()` — runs the filters **without writing any output** (no encoding, no files). Fast to iterate.\n",
14
+ "3. `p.print_audit()` — a per-stage keep/drop funnel table.\n",
15
+ "4. `visualize_audit(p, slide)` — an **interactive overlay**: kept patches in green, dropped patches colored by the stage that removed them, plus a **slider per tunable parameter**.\n",
16
+ "\n",
17
+ "Dragging a slider re-decides keep/drop for every patch *in the browser* — no Python re-run — and prints a config you can paste straight back into your pipeline."
18
+ ]
19
+ },
20
+ {
21
+ "cell_type": "code",
22
+ "execution_count": null,
23
+ "metadata": {},
24
+ "outputs": [],
25
+ "source": [
26
+ "from wsi_patching import OtsuFilter, PatchExtractor, RemoveEdgeTiles, WSIGrid, visualize_audit, LowContrastBackgroundFilter\n",
27
+ "\n",
28
+ "# A small slide shipped in the repo. Swap for your own path.\n",
29
+ "SLIDE = \"../data/RBIO-GC072-HE-01.tiff\""
30
+ ]
31
+ },
32
+ {
33
+ "cell_type": "markdown",
34
+ "metadata": {},
35
+ "source": [
36
+ "## 1. Build a pipeline and dry-run it\n",
37
+ "\n",
38
+ "No writer is needed for a dry run. `resolution=2` patches at a downsampled level so the demo is quick."
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": null,
44
+ "metadata": {},
45
+ "outputs": [],
46
+ "source": [
47
+ "p = (\n",
48
+ " WSIGrid(slides=[SLIDE], resolution=2, unit=\"level\", use_gpu=False)\n",
49
+ " .then(PatchExtractor(tile_size=224, stride=224))\n",
50
+ " .then(RemoveEdgeTiles(depth=1)) # drop the border ring of tiles\n",
51
+ " .then(LowContrastBackgroundFilter()) # drop patches with too little contrast\n",
52
+ " .then(OtsuFilter(min_tissue_fraction=0.35)) # drop patches with too little tissue\n",
53
+ ")\n",
54
+ "\n",
55
+ "p.dry_run(num_workers=1)\n",
56
+ "p.print_audit()"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "markdown",
61
+ "metadata": {},
62
+ "source": [
63
+ "## 2. The interactive overlay\n",
64
+ "\n",
65
+ "Green = kept. Each other color = a stage that dropped those patches. **Hover** a patch to see its stage + metadata (e.g. `tissue_fraction`).\n",
66
+ "\n",
67
+ "Three ways to tune, all live and none of them re-running Python:\n",
68
+ "\n",
69
+ "- **Drag a slider**, or **type an exact value** in the box on the right (`min_tissue_fraction`, `range_threshold`, `depth`).\n",
70
+ "- **Uncheck a stage** to see the pipeline *without* it — the patches it had dropped are re-judged by the remaining stages and recolored.\n",
71
+ "- **Click ▲▼** to reorder the filters.\n",
72
+ "\n",
73
+ "The overlay, the funnel table and the config box all update together. When you're happy, copy the config box straight into the pipeline above — it's emitted in the order you chose.\n",
74
+ "\n",
75
+ "> **Reordering never changes which patches survive** (a patch is kept only if it passes every filter). It changes *who gets the blame* and *how much work each stage does*. Move `OtsuFilter` to the front and watch: it still keeps the same 10 patches, but you'll see it was really rejecting 167 — and that running it last means it only has to process 67 patches instead of 252."
76
+ ]
77
+ },
78
+ {
79
+ "cell_type": "code",
80
+ "execution_count": null,
81
+ "metadata": {},
82
+ "outputs": [],
83
+ "source": [
84
+ "report = visualize_audit(p, SLIDE)\n",
85
+ "report # renders inline"
86
+ ]
87
+ }
88
+ ],
89
+ "metadata": {
90
+ "kernelspec": {
91
+ "display_name": "wsi-patching (3.13.5)",
92
+ "language": "python",
93
+ "name": "python3"
94
+ },
95
+ "language_info": {
96
+ "codemirror_mode": {
97
+ "name": "ipython",
98
+ "version": 3
99
+ },
100
+ "file_extension": ".py",
101
+ "mimetype": "text/x-python",
102
+ "name": "python",
103
+ "nbconvert_exporter": "python",
104
+ "pygments_lexer": "ipython3",
105
+ "version": "3.13.5"
106
+ }
107
+ },
108
+ "nbformat": 4,
109
+ "nbformat_minor": 4
110
+ }
@@ -32,6 +32,7 @@ nav:
32
32
  - Installation: installation.md
33
33
  - Quickstart: quickstart.md
34
34
  - Concepts: concepts.md
35
+ - Auditing & visualization: auditing.md
35
36
  - API Reference: reference.md
36
37
  - Extending: extending.md
37
38
 
@@ -28,7 +28,7 @@ dependencies = [
28
28
  "numpy>=1.24",
29
29
  "webdataset>=0.2.86",
30
30
  "pillow>=9.0",
31
- "fastslide==0.7.3",
31
+ "fastslide>=0.7.5",
32
32
  "orjson>=3.8",
33
33
  "opencv-python-headless>=4.8",
34
34
  "rtree>=1.0",
@@ -40,7 +40,7 @@ Repository = "https://github.com/amspath/wsi-patching-pipeline"
40
40
 
41
41
  [project.optional-dependencies]
42
42
  gpu = ['cupy-cuda12x; platform_system != "Darwin"', "torch", "torchvision"]
43
- dev = ["ruff", "pytest", "tifffile", "pytest-benchmark", "ty", "mkdocs-material"]
43
+ dev = ["ruff", "pytest", "tifffile", "pytest-benchmark", "ty", "mkdocs-material", "ipykernel"]
44
44
 
45
45
  # --- Hatch VCS versioning (default-ish behavior) ---
46
46
  [tool.hatch.version]
@@ -1,10 +1,12 @@
1
1
  # src/wsi_patching/__init__.py
2
2
  from importlib.metadata import version as _version
3
3
 
4
+ from .annotations import AddAnnotationFromGeoJSON, AddCellAnnotationFromCSV
4
5
  from .core import PatchExtractor, ReadWindowChunker, RegionReadAndBatch, TilePlanner, WSIGrid
5
6
  from .encoders import PNGEncoder
6
- from .filtering import LowContrastBackgroundFilter, OtsuFilter, PenArtifactFilter
7
+ from .filtering import LowContrastBackgroundFilter, OtsuFilter, PenArtifactFilter, RemoveEdgeTiles
7
8
  from .regions_of_interest import AttachROIs, RectROIfromXMLProvider, RectROIProvider, WholeSlideProvider
9
+ from .utils import visualize_audit, visualize_selected_patches
8
10
  from .writers import NumpyStreamWriter, TorchStreamWriter, WebDatasetWriter
9
11
 
10
12
  __all__ = [
@@ -19,20 +21,27 @@ __all__ = [
19
21
  "RectROIProvider",
20
22
  "RectROIfromXMLProvider",
21
23
  "WholeSlideProvider",
24
+ # Annotations
25
+ "AddCellAnnotationFromCSV",
26
+ "AddAnnotationFromGeoJSON",
22
27
  # Encoders
23
28
  "PNGEncoder",
24
29
  # Filters
25
30
  "LowContrastBackgroundFilter",
26
31
  "OtsuFilter",
27
32
  "PenArtifactFilter",
33
+ "RemoveEdgeTiles",
28
34
  # Writers
29
35
  "NumpyStreamWriter",
30
36
  "WebDatasetWriter",
31
37
  "TorchStreamWriter",
38
+ # Visualization / auditing
39
+ "visualize_audit",
40
+ "visualize_selected_patches",
32
41
  ]
33
42
 
34
43
  # version (from your installed package metadata)
35
44
  try:
36
45
  __version__ = _version("wsi_patching")
37
46
  except Exception: # during editable installs before metadata exists
38
- __version__ = "0.1.0"
47
+ __version__ = "0.1.0"
@@ -0,0 +1,4 @@
1
+ from .add_annotation_from_geojson import AddAnnotationFromGeoJSON
2
+ from .add_cell_annotation_from_csv import AddCellAnnotationFromCSV
3
+
4
+ __all__ = ["AddCellAnnotationFromCSV", "AddAnnotationFromGeoJSON"]