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, return_count: bool = False) -> Union['da.Array', tuple['da.Array', Union[int, None]]]
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
|
return_count
|
bool
|
Also return the exact object count. Only meaningful (non- |
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
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 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 | |