|
| 1 | +"""Generic post-segmentation wrappers for patchworks. |
| 2 | +
|
| 3 | +These wrap any ``fn(tile) -> labels`` callable (a plugin, a custom function, |
| 4 | +whatever ``method`` in the Snakemake workflow builds) so the same |
| 5 | +post-processing applies regardless of which segmentation method produced the |
| 6 | +labels. |
| 7 | +
|
| 8 | +Usage |
| 9 | +----- |
| 10 | +>>> from patchworks import tile_process, dilate_labels |
| 11 | +>>> from patchworks.plugins.dog import dog_label_fn |
| 12 | +>>> |
| 13 | +>>> fn = dog_label_fn(low_sigma=1.0, high_sigma=3.0, threshold=0.02) |
| 14 | +>>> fn = dilate_labels(fn, iterations=2) |
| 15 | +>>> result = tile_process("image.zarr", fn, tile_shape=(1, 2048, 2048), |
| 16 | +... overlap=8, write_to="labels.zarr") |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +from functools import partial |
| 22 | +from typing import Callable |
| 23 | + |
| 24 | +import numpy as np |
| 25 | + |
| 26 | + |
| 27 | +def dilate_labels( |
| 28 | + fn: Callable[[np.ndarray], np.ndarray], |
| 29 | + iterations: int = 1, |
| 30 | + *, |
| 31 | + use_gpu: bool = False, |
| 32 | +) -> Callable[[np.ndarray], np.ndarray]: |
| 33 | + """Wrap a segmentation callable to grow its labels after each tile. |
| 34 | +
|
| 35 | + Applies a single-pass grey dilation to whatever ``fn`` returns, before |
| 36 | + ``tile_process``/``stage_tile`` trim the overlap halo and merge across |
| 37 | + tile boundaries — so dilated labels still stitch correctly at tile |
| 38 | + edges. |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + fn : Callable[[np.ndarray], np.ndarray] |
| 43 | + Any segmentation function with the ``tile_process``/``stage_tile`` |
| 44 | + contract (one tile in, integer label array out). |
| 45 | + iterations : int, optional |
| 46 | + Pixels to grow each label by (grey-dilation footprint size |
| 47 | + ``2 * iterations + 1``, single pass). Default 1. Values ``<= 0`` |
| 48 | + disable dilation — ``fn`` is returned unwrapped. |
| 49 | + use_gpu : bool, optional |
| 50 | + Dilate via cupyx instead of scipy. Independent of whatever backend |
| 51 | + ``fn`` itself uses internally. |
| 52 | +
|
| 53 | + Returns |
| 54 | + ------- |
| 55 | + Callable[[np.ndarray], np.ndarray] |
| 56 | + Picklable function ready for ``tile_process``/``stage_tile``. If |
| 57 | + ``iterations <= 0``, this is ``fn`` itself. |
| 58 | + """ |
| 59 | + if iterations <= 0: |
| 60 | + return fn |
| 61 | + return partial(_run, fn=fn, iterations=iterations, use_gpu=use_gpu) |
| 62 | + |
| 63 | + |
| 64 | +def _run( |
| 65 | + block: np.ndarray, |
| 66 | + fn: Callable[[np.ndarray], np.ndarray], |
| 67 | + iterations: int, |
| 68 | + use_gpu: bool, |
| 69 | +) -> np.ndarray: |
| 70 | + """Run ``fn`` on ``block``, then grow the resulting labels. |
| 71 | +
|
| 72 | + Parameters |
| 73 | + ---------- |
| 74 | + block : np.ndarray |
| 75 | + One image tile. |
| 76 | + fn : Callable[[np.ndarray], np.ndarray] |
| 77 | + Segmentation function to run first. |
| 78 | + iterations : int |
| 79 | + Pixels to grow each label by. |
| 80 | + use_gpu : bool |
| 81 | + Dilate via cupyx instead of scipy. |
| 82 | +
|
| 83 | + Returns |
| 84 | + ------- |
| 85 | + np.ndarray |
| 86 | + Dilated integer label array, same shape as ``fn``'s output. |
| 87 | + """ |
| 88 | + labels = fn(block) |
| 89 | + size = 2 * iterations + 1 |
| 90 | + |
| 91 | + if use_gpu: |
| 92 | + import cupy as cp |
| 93 | + from cupyx.scipy.ndimage import grey_dilation |
| 94 | + |
| 95 | + labels = cp.asnumpy(grey_dilation(cp.asarray(labels), size=size)) |
| 96 | + else: |
| 97 | + from scipy.ndimage import grey_dilation |
| 98 | + |
| 99 | + labels = grey_dilation(labels, size=size) |
| 100 | + |
| 101 | + return labels |
0 commit comments