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, label_counts: 'Mapping[int, int] | Sequence[int] | None' = None, output_chunks: 'Sequence[int] | None' = None, shard_output: bool = False, halo_dir: 'str | Path | None' = None, iou_threshold: float = 0.5, connectivity: int = 1, parts_dir: 'str | Path | None' = None, min_voxels: 'int | None' = None, max_voxels: 'int | None' = None, objects_out: 'dict | None' = None) -> 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
|
label_counts
|
'Mapping[int, int] | Sequence[int] | None'
|
Labels written per tile, when the producer recorded them (see
:func: |
None
|
output_chunks
|
'Sequence[int] | None'
|
Chunking for the merged store; must divide the staged chunk shape.
Lets the merge write straight into a store you will keep (e.g. an
OME-ZARR label group's level 0) instead of a scratch store that then
has to be copied. See :func: |
None
|
shard_output
|
bool
|
Store the output in shards of one staged tile each (zarr v3; the chunks above are inside them). Each relabel worker writes one tile, so it writes whole shards, and the result is sharded with no second pass over it. Ignored when relabeling in place. |
False
|
halo_dir
|
'str | Path | None'
|
IoU stitching: the halo strips :func: |
None
|
iou_threshold
|
float
|
Minimum IoU for that join (default 0.5). |
0.5
|
connectivity
|
int
|
Neighbourhood the tiles were labelled with: 1 (default) faces, 2
also edges, 3 also corners ( |
1
|
parts_dir
|
'str | Path | None'
|
Object sums saved per tile by :func: |
None
|
min_voxels
|
'str | Path | None'
|
Object sums saved per tile by :func: |
None
|
max_voxels
|
'str | Path | None'
|
Object sums saved per tile by :func: |
None
|
objects_out
|
'str | Path | None'
|
Object sums saved per tile by :func: |
None
|
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 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
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 | |