Skip to content

Commit eb22f28

Browse files
committed
docs: πŸ“ document dilate_labels, API and cluster config usage
Adds an API reference page, a cluster-workflow guide section covering both the dilate: N config key and calling dilate_labels() directly, and a DoG-specific example since thin cilia/spot labels are the motivating case.
1 parent 87a467e commit eb22f28

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

β€Ždocs/api/postprocess.mdβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# dilate_labels
2+
3+
::: patchworks.dilate_labels

β€Ždocs/examples/dog.mdβ€Ž

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ result = tile_process(IMAGE, fn, tile_shape=(1, 1024, 1024), overlap=32)
8181
so edge tiles keep enough context (a plain intensity/threshold halo is
8282
too thin).
8383

84+
## Growing the labels afterwards
85+
86+
DoG spots/threads are often thin β€” grow each label by a few pixels with
87+
[`dilate_labels`](../api/postprocess.md):
88+
89+
```python
90+
from patchworks import tile_process, dilate_labels
91+
from patchworks.plugins.dog import dog_label_fn
92+
93+
fn = dog_label_fn(low_sigma=1.0, high_sigma=3.0, threshold=0.02)
94+
fn = dilate_labels(fn, iterations=2)
95+
tile_process(IMAGE, fn, tile_shape=(1, 1024, 1024), overlap=8, write_to=OUTPUT)
96+
```
97+
98+
On the cluster, set `dilate: 2` in the YAML config instead β€” it applies to
99+
`method: "custom"` (this plugin) the same way it does for `cellpose`/
100+
`threshold`, see [Growing labels after segmentation](../guide/snakemake.md#growing-labels-afterwards-dilation).
101+
84102
## Using it in the Snakemake workflow
85103

86104
No dedicated wiring needed β€” `patchworks.plugins.dog` exposes a `segment(tile, **kwargs)`

β€Ždocs/guide/snakemake.mdβ€Ž

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ skip_empty: true # skip background tiles
6262
empty_threshold: null # null β†’ Otsu
6363

6464
# segmentation
65-
method: "cellpose" # "cellpose" (GPU) or "threshold" (no GPU)
65+
method: "cellpose" # "cellpose" (GPU), "threshold" (no GPU), "custom"
6666
label_name: "cellpose" # name under image.zarr/labels/
67+
dilate: 0 # optional: pixels to grow labels by, any method
6768
cellpose:
6869
model: "cyto3"
6970
diameter: 30
@@ -77,6 +78,18 @@ pyramid_downscale: 2
7778
sequential_labels: true # renumber labels to a contiguous 1..N
7879
```
7980
81+
!!! tip "Growing labels after segmentation"
82+
`dilate: N` grows every label by `N` pixels once segmentation finishes,
83+
regardless of `method` (`cellpose`, `threshold`, or `custom`). It runs
84+
per-tile, before the overlap halo is trimmed and tiles are merged, so
85+
dilated labels still stitch correctly across tile boundaries β€” just make
86+
sure `overlap` covers the dilation amount plus the usual object-diameter
87+
halo. `0` (default) disables it. Under the hood this wraps whatever
88+
segmentation function `method` builds with
89+
[`patchworks.dilate_labels`](../api/postprocess.md); see [Custom
90+
segmentation function](#custom-segmentation-function) below for using it
91+
directly from Python instead of via config.
92+
8093
!!! tip "Tile size vs runtime"
8194
`tile_shape: "auto"` sizes each tile to your GPU's VRAM. Smaller tiles =
8295
more (faster) jobs; very large 3-D tiles are slow. Keep `do_3D: false` (2-D
@@ -433,6 +446,29 @@ custom:
433446
sigma: 1.5
434447
```
435448

449+
### Growing labels afterwards (dilation)
450+
451+
To grow every label by a few pixels after segmentation β€” any method, not
452+
just `custom` β€” set `dilate: N` in the config (see the tip above), or wrap
453+
your function directly with
454+
[`patchworks.dilate_labels`](../api/postprocess.md) when calling the API
455+
yourself:
456+
457+
```python
458+
from patchworks import tile_process, dilate_labels
459+
from patchworks.plugins.dog import dog_label_fn
460+
461+
fn = dog_label_fn(low_sigma=1.0, high_sigma=3.0, threshold=0.02)
462+
fn = dilate_labels(fn, iterations=2) # grow each label by 2 px, then run
463+
result = tile_process("image.zarr", fn, tile_shape=(1, 2048, 2048),
464+
overlap=8, write_to="labels.zarr")
465+
```
466+
467+
`dilate_labels` wraps any `(tile) -> labels` function β€” the same contract
468+
described above β€” so it works with `dog_label_fn`, `cellpose_fn`, or your
469+
own `segment`. It dilates each tile's labels before the halo is trimmed and
470+
tiles are merged, so `overlap` must still cover the dilation amount.
471+
436472
### Real example: StarDist 3-D, with model caching
437473

438474
Heavy models must be loaded **once**, not per tile. On SLURM each tile is its

β€Žmkdocs.ymlβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ nav:
5454
- API Reference:
5555
- tile_process: api/tile_process.md
5656
- merge_tile_labels: api/merge_tile_labels.md
57+
- dilate_labels: api/postprocess.md
5758
- Tile sizing: api/chunks.md
5859
- I/O helpers: api/io.md
5960
- Relabelling: api/relabel.md

0 commit comments

Comments
Β (0)