tile_process
blockbuster.tile_process(image: Union[da.Array, str, Path], fn: Callable[[np.ndarray], np.ndarray], *, tile_shape: Union[tuple[int, ...], Callable[[tuple, Any], tuple], str, None] = None, overlap: int = 0, channel: int | None = 0, level: int = 0, use_gpu: bool = False, compute: bool = False, progress: bool = False, write_to: Union[str, Path, None] = None, output_component: str = 'labels', sequential_labels: bool = False, skip_empty: bool = False, empty_threshold: float | None = None, stage_dir: Union[str, Path, None] = None, keep_stage: bool = False, verbose: bool = False) -> Union[da.Array, np.ndarray]
Apply fn to every tile of image and merge labels globally.
The core workhorse of blockbuster. fn can be any callable that takes a
NumPy array and returns an integer label array of the same shape — Cellpose,
StarDist, Otsu threshold, your own model, anything.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Union[Array, str, Path]
|
Dask array or path to an OME-ZARR store. |
required |
fn
|
Callable[[ndarray], ndarray]
|
|
required |
tile_shape
|
Union[tuple[int, ...], Callable[[tuple, Any], tuple], str, None]
|
Controls tiling before calling fn. Accepted values:
.. code-block:: python |
None
|
overlap
|
int
|
Voxels of overlap (halo) added to each tile before fn is called, so
objects near tile boundaries have enough spatial context to be
segmented correctly (Cellpose, StarDist, …). The halo is trimmed off
before merging — the output has the original shape. Merging is always touching-label based: after the halo is trimmed, labels that touch across a tile boundary are merged into one object. |
0
|
channel
|
int | None
|
Channel index when image is a path. Ignored for arrays. |
0
|
level
|
int
|
Pyramid level when image is a path (0 = full resolution). |
0
|
use_gpu
|
bool
|
When |
False
|
compute
|
bool
|
Compute and return the result immediately as a NumPy array. |
False
|
progress
|
bool
|
Show a progress bar while computing. Requires |
False
|
write_to
|
Union[str, Path, None]
|
Zarr store path to stream-write labels while computing (avoids loading
the full result into RAM). Implies |
None
|
output_component
|
str
|
Array name inside |
'labels'
|
sequential_labels
|
bool
|
Renumber merged labels to a contiguous |
False
|
skip_empty
|
bool
|
Skip fn on background tiles. A tile whose max signal is <=
|
False
|
empty_threshold
|
float | None
|
Intensity at or below which a tile is empty ( |
None
|
stage_dir
|
Union[str, Path, None]
|
Where to put the temporary stage store. |
None
|
keep_stage
|
bool
|
Keep the temp stage store after merging (default: delete it). Useful for debugging or resuming an interrupted run. |
False
|
verbose
|
bool
|
Log each tile's location and shape as it is processed. |
False
|
Returns:
| Type | Description |
|---|---|
Array or ndarray
|
Globally relabeled array (int32). Returns a lazy |
Examples:
Any threshold function:
>>> from skimage.filters import threshold_otsu
>>> from skimage.measure import label
>>>
>>> def my_fn(tile):
... return label(tile > threshold_otsu(tile)).astype("int32")
>>>
>>> result = tile_process("image.zarr", my_fn, compute=True)
Cellpose (via the plugin):
>>> from blockbuster.plugins.cellpose import cellpose_fn
>>>
>>> fn = cellpose_fn("cyto3", gpu=True, diameter=30)
>>> result = tile_process(
... "image.zarr", fn,
... tile_shape=(1, 2048, 2048),
... overlap=20,
... write_to="labels.zarr",
... progress=True,
... )
StarDist:
>>> from stardist.models import StarDist2D
>>> model = StarDist2D.from_pretrained("2D_versatile_fluo")
>>>
>>> def stardist_fn(tile):
... norm = tile.astype("float32") / tile.max()
... labels, _ = model.predict_instances(norm)
... return labels.astype("int32")
>>>
>>> result = tile_process("image.zarr", stardist_fn,
... tile_shape=(1, 1024, 1024), overlap=32)
Write directly to zarr (no RAM accumulation):
Source code in src/blockbuster/_core.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |