merge_tile_labels
patchworks.merge_tile_labels(labeled: Union['da.Array', str, Path], write_to: Union[str, Path, None] = None, *, input_component: str = 'labels', output_component: str = 'labels', overlap: int = 0, sequential_labels: bool = False, n_workers: int | None = None, stage_dir: Union[str, Path, None] = None, keep_stage: bool = False, progress: bool = False) -> 'da.Array'
Merge per-tile labels into a globally consistent label array.
Standalone merge step — use this when you already have per-tile labels (from your own segmentation pipeline) and just need the boundary stitching.
Accepts either:
- A dask array of per-tile integer labels (e.g. output of
dask.array.map_blockson your own segmentation function). - A zarr store path whose
input_componentarray already contains per-tile labels written by your own pipeline.
Labels that touch across tile boundaries are merged into a single ID. The merge is zarr-native (boundary scan → scipy connected components → parallel relabel) — no dask task graph, scales to thousands of tiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labeled
|
Union['da.Array', str, Path]
|
Per-tile label array. Either a dask array or a path to a zarr store
that contains per-tile labels in |
required |
write_to
|
Union[str, Path, None]
|
Output zarr store path. When None, an auto-temp store is used. |
None
|
input_component
|
str
|
Array name inside a zarr input store (ignored for dask arrays). |
'labels'
|
output_component
|
str
|
Array name inside |
'labels'
|
overlap
|
int
|
If |
0
|
sequential_labels
|
bool
|
Renumber the merged labels to a contiguous |
False
|
n_workers
|
int | None
|
Parallel workers for the relabel step. Default |
None
|
stage_dir
|
Union[str, Path, None]
|
Directory for the temp stage zarr when labeled is a dask array. Default: a system temp directory. |
None
|
keep_stage
|
bool
|
Keep the temp stage zarr after merging. Default False. |
False
|
progress
|
bool
|
Show a progress bar during the relabel step. |
False
|
Returns:
| Type | Description |
|---|---|
Array
|
Merged label array (int32) backed by |
Examples:
From a dask array of per-tile labels:
>>> import dask.array as da
>>> from patchworks import merge_tile_labels
>>>
>>> # your own tiling + segmentation
>>> image = da.from_zarr("image.zarr").rechunk((1, 1024, 1024))
>>> labeled = image.map_blocks(my_segment_fn, dtype="int32",
... meta=np.empty((0,) * image.ndim, dtype="int32"))
>>>
>>> merged = merge_tile_labels(labeled, write_to="labels.zarr", progress=True)
From a pre-staged zarr store (your pipeline already wrote labels):
>>> merged = merge_tile_labels(
... "my_staged_labels.zarr",
... input_component="raw_labels",
... write_to="merged_labels.zarr",
... sequential_labels=True,
... )
Trim overlap halos before merging:
>>> # if labeled was computed with da.overlap.overlap(depth=20)
>>> merged = merge_tile_labels(labeled, write_to="labels.zarr", overlap=20)
Source code in src/patchworks/_merge.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 | |