OME-ZARR conversion plugin
Write any array or image file to a pyramidal OME-ZARR store, add resolution
levels to an existing store, or store a label image inside an OME-ZARR under
the NGFF labels/ group. Uses only the core dependencies for arrays and
.zarr inputs; reading other file formats needs the optional bioio extra
(pip install "patchworks[bioio]").
Pyramids downsample X and Y only — Z (and channel/time) are kept at full
resolution, matching anisotropic microscopy stacks.
to_ome_zarr
patchworks.plugins.ome_zarr.to_ome_zarr(source: Union[da.Array, np.ndarray, str, Path], out_path: Union[str, Path], *, axes: Union[str, None] = None, pixel_size: Union[PixelSize, tuple, None] = None, scene: int = 0, sequence_pattern: Union[str, None] = None, n_levels: int = 5, downscale: int = 2, chunks: Union[tuple[int, ...], None] = None, shard: ShardSpec = False, reuse_pyramid: bool = False, progress: bool = True, overwrite: bool = False) -> str
Write source as a pyramidal, calibrated OME-ZARR store.
source may be a dask/NumPy array, a .zarr store, an Imaris .ims
file, any image format readable by bioio (CZI, LIF, ND2, OME-TIFF, …), or
(with sequence_pattern set) a glob over a folder of single-plane TIFFs.
File inputs are read lazily; the pyramid is built level-by-level from disk
with bounded chunks, so the full volume never needs to fit in RAM. Only
x/y are downsampled; z (and channel/time) stay full-resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
(Array, ndarray, str or Path)
|
Array or path to convert. |
required |
out_path
|
str or Path
|
Destination |
required |
axes
|
str
|
One character per array dimension, e.g. |
None
|
pixel_size
|
(dict, tuple or None)
|
Physical voxel size in micrometers, as |
None
|
scene
|
int
|
Scene index for multi-scene bioio files. |
0
|
sequence_pattern
|
str
|
When given, source is treated as a glob pattern over a folder of
single-plane TIFFs (e.g. |
None
|
n_levels
|
int
|
Maximum number of pyramid levels including full resolution. |
5
|
downscale
|
int
|
Per-level X/Y downsampling factor (default 2). |
2
|
chunks
|
tuple of int
|
Chunk shape for the written levels. |
None
|
shard
|
bool or tuple of int
|
Pack many chunks into one shard file (zarr v3), cutting the file count
~100× on huge arrays. |
False
|
progress
|
bool
|
Show a per-level dask progress bar (default |
True
|
reuse_pyramid
|
bool
|
Imaris |
False
|
overwrite
|
bool
|
Overwrite an existing store at out_path. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The path to the written store ( |
Examples:
>>> from patchworks.plugins.ome_zarr import to_ome_zarr
>>> to_ome_zarr("scan.ims", "scan.zarr", n_levels=4)
'scan.zarr'
>>> to_ome_zarr(
... "ZT18_Male4_Left/*.tif",
... "ZT18_Male4_Left.zarr",
... sequence_pattern=r"_T(?P<T>\d+)_Z(?P<Z>\d+)_C(?P<C>\d+)_V\d+",
... shard=True,
... )
'ZT18_Male4_Left.zarr'
Source code in src/patchworks/plugins/ome_zarr.py
1408 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 | |
add_pyramid
patchworks.plugins.ome_zarr.add_pyramid(group_path: Union[str, Path], *, base: str = '0', axes: Union[str, None] = None, pixel_size: Union[PixelSize, tuple, None] = None, n_levels: int = 5, downscale: int = 2, chunks: Union[tuple[int, ...], None] = None, shard: ShardSpec = False, progress: bool = True) -> str
Add downsampled pyramid levels to an existing single-resolution zarr.
Reads the full-resolution array already at group_path/base, writes the
missing levels next to it (lazily, from disk), and (re)writes the NGFF
multiscales metadata. Existing calibration is preserved; pass
pixel_size to set it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_path
|
str or Path
|
Zarr group containing the full-resolution array at base. |
required |
base
|
str
|
Component name of the existing full-resolution level (default
|
'0'
|
axes
|
str
|
One letter per axis, e.g. |
None
|
pixel_size
|
(dict, tuple or None)
|
Physical voxel size in micrometers. |
None
|
n_levels
|
int
|
Maximum number of levels including the existing full-resolution one (default 5). |
5
|
downscale
|
int
|
Per-level X/Y downsampling factor (default 2). |
2
|
chunks
|
tuple of int
|
Chunk shape for the written levels. |
None
|
shard
|
bool or tuple of int
|
Sharding request (see :func: |
False
|
progress
|
bool
|
Show a per-level dask progress bar (default |
True
|
Returns:
| Type | Description |
|---|---|
str
|
The path to the updated group. |
Examples:
Source code in src/patchworks/plugins/ome_zarr.py
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 1645 1646 1647 1648 1649 1650 1651 1652 1653 | |
write_labels
patchworks.plugins.ome_zarr.write_labels(image_store: Union[str, Path], labels: Union[da.Array, np.ndarray], *, name: str = 'labels', axes: Union[str, None] = None, pixel_size: Union[PixelSize, tuple, None] = None, n_levels: int = 5, downscale: int = 2, chunks: Union[tuple[int, ...], None] = None, shard: ShardSpec = False, progress: bool = True, overwrite: bool = False, n_objects: Union[int, None] = None) -> str
Store labels inside image_store under the NGFF labels/ group.
The labels are written as their own multi-scale pyramid at
image_store/labels/<name>/ and registered in
image_store/labels/.zattrs, so the image and its segmentation live in a
single OME-ZARR store. Calibration is inherited from the parent image
unless pixel_size is given.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_store
|
str or Path
|
OME-ZARR store this label image belongs to. |
required |
labels
|
Array or ndarray
|
Integer label array (0 = background), same spatial shape as the image. |
required |
name
|
str
|
Label image name under |
'labels'
|
axes
|
str
|
One letter per axis. |
None
|
pixel_size
|
(dict, tuple or None)
|
Physical voxel size in micrometers. |
None
|
n_levels
|
int
|
Maximum number of pyramid levels including full resolution (default 5). |
5
|
downscale
|
int
|
Per-level X/Y downsampling factor (default 2). |
2
|
chunks
|
tuple of int
|
Chunk shape for the written levels. |
None
|
shard
|
bool or tuple of int
|
Sharding request (see :func: |
False
|
progress
|
bool
|
Show a per-level dask progress bar (default |
True
|
overwrite
|
bool
|
Replace an existing label image of the same name (default
|
False
|
n_objects
|
int or None
|
Exact non-background object count, if known — forwarded to
:func: |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Path to the written label group ( |
Examples:
>>> from patchworks import merge_tile_labels
>>> merged, n = merge_tile_labels(
... "stage.zarr",
... input_component="staged",
... write_to="merged.zarr",
... sequential_labels=True,
... return_count=True,
... )
>>> write_labels(
... "scan.zarr", merged, name="cells", n_objects=n
... )
'scan.zarr/labels/cells'
Source code in src/patchworks/plugins/ome_zarr.py
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 | |
register_labels
patchworks.plugins.ome_zarr.register_labels(image_store: Union[str, Path], name: str = 'labels', *, axes: Union[str, None] = None, pixel_size: Union[PixelSize, tuple, None] = None, n_levels: int = 5, downscale: int = 2, chunks: Union[tuple[int, ...], None] = None, shard: ShardSpec = False, progress: bool = True, n_objects: Union[int, None] = None) -> str
Pyramidalise and register an existing labels/<name>/0 base level.
Assumes the full-resolution label array already exists at
image_store/labels/<name>/0. Adds the downsampled levels, tags the
group with NGFF image-label metadata, lists name in
labels/.zattrs, and inherits the parent image's pixel calibration
(unless pixel_size is given).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_store
|
str or Path
|
OME-ZARR store path containing the image this label belongs to. |
required |
name
|
str
|
Label image name under |
'labels'
|
axes
|
str
|
One letter per axis. |
None
|
pixel_size
|
(dict, tuple or None)
|
Physical voxel size in micrometers. |
None
|
n_levels
|
int
|
Maximum number of pyramid levels including full resolution (default 5). |
5
|
downscale
|
int
|
Per-level X/Y downsampling factor (default 2). |
2
|
chunks
|
tuple of int
|
Chunk shape for the written levels. |
None
|
shard
|
bool or tuple of int
|
Sharding request (see :func: |
False
|
progress
|
bool
|
Show a per-level dask progress bar (default |
True
|
n_objects
|
int or None
|
Exact non-background object count, if known (e.g. from
:func: |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Path to the label group ( |
Examples:
Source code in src/patchworks/plugins/ome_zarr.py
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 | |
read_pixel_size
patchworks.plugins.ome_zarr.read_pixel_size(store: Union[str, Path]) -> PixelSize
Physical voxel size recorded in an OME-ZARR's level-0 metadata.
The calibration the conversion carried over from the source file, as
{"z": .., "y": .., "x": ..} in micrometers. Axes left at scale 1.0
(uncalibrated) are omitted, so an empty dict means the store carries no
usable calibration.
Use this instead of retyping voxel sizes into a config: a deconvolution told the wrong voxel size produces a plausible-looking but wrong result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
str or Path
|
Path of the OME-ZARR group. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
|
Examples:
Source code in src/patchworks/plugins/ome_zarr.py
NGFF metadata layout
NGFF 0.4 is defined over zarr v2 and puts its keys at the top level; 0.5 is
the zarr-v3 revision and nests them under ome. patchworks writes whichever
matches the store, and reads both.
patchworks.plugins.ome_zarr.ngff_version() -> str
patchworks.plugins.ome_zarr.read_ngff_attr(attrs, key: str, default=None)
Read an NGFF key from either layout.
Accepts both the 0.4 top-level placement and the 0.5 ome nesting, so
stores written by any patchworks version (or another tool) still load.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs
|
Mapping
|
A zarr group's attributes. |
required |
key
|
str
|
NGFF key, e.g. |
required |
default
|
Any
|
Returned when the key is absent from both layouts. |
None
|
Source code in src/patchworks/plugins/ome_zarr.py
patchworks.plugins.ome_zarr.write_ngff_attrs(group, **entries) -> None
Write NGFF keys in the layout matching the store's zarr version.
On zarr v3 the keys are merged into the ome attribute (0.5); on v2
they go to the top level (0.4). Merging matters because several keys are
written at different times onto the same group -- multiscales by the
pyramid, then image-label when the labels are registered.