Relabelling
patchworks.relabel_sequential_array(labels: np.ndarray) -> np.ndarray
Remap labels to a contiguous 0, 1, … N range.
Background (0) stays 0. Runs in one np.unique + a lookup-table gather,
i.e. O(voxels) — unlike dask's relabel_sequential which is O(n_chunks²).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
ndarray
|
Integer label array (may have gappy ids). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Labels remapped to a contiguous |
Examples:
Source code in src/patchworks/_relabel.py
patchworks.relabel_sequential_zarr(store_path: str, component: str = 'labels') -> int
Relabel a written label zarr to contiguous ids, in place.
Two-pass streaming algorithm — safe for arrays far larger than RAM.
Pass 1 collects unique ids (bounded memory: a Python set, not the
voxels themselves). Pass 2 applies the lookup-table remap chunk by
chunk, writing back into the same store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store_path
|
str
|
Path to the zarr store containing the label array. |
required |
component
|
str
|
Array name inside the store to relabel in place (default
|
'labels'
|
Returns:
| Type | Description |
|---|---|
int
|
Number of distinct objects ( |
Examples:
>>> import zarr
>>> root = zarr.open_group("staged.zarr", mode="w")
>>> root.create_array(
... "labels", shape=(4, 4), chunks=(4, 4), dtype="int32"
... )[:] = [
... [0, 500000, 500000, 0],
... [0, 0, 0, 7],
... [0, 0, 0, 0],
... [0, 0, 0, 0],
... ]
>>> relabel_sequential_zarr("staged.zarr")
2