ZarrNii is a Python library for working with OME-Zarr, NIfTI, and Imaris formats. ZarrNii bridges the gap between these popular formats, enabling seamless data transformation, metadata preservation, and efficient processing of biomedical images. The motivating application is for whole brain lightsheet microscopy and ultra-high field MRI, but it can generally be used for any 3D+[channel,time] datasets.
ZarrNii allows you to:
- Read and write OME-Zarr, NIfTI, and Imaris datasets
- Perform transformations like cropping, downsampling, and interpolation.
- Preserve and manipulate metadata from OME-Zarr (e.g., axes, coordinate transformations, OME annotations).
pip install zarrniiFor additional format support:
# For Imaris (.ims) file support
pip install zarrnii[imaris]For contributing or development, clone the repository and install with uv:
git clone https://github.com/khanlab/zarrnii.git
cd zarrnii
uv sync --dev- Seamless Format Conversion: Easily convert between OME-Zarr, NIfTI, and Imaris while preserving spatial metadata.
- ZipStore Support: Read and write OME-Zarr files in compressed ZIP format (.ome.zarr.zip) for efficient storage and sharing.
- Transformations: Apply common operations like affine transformations, downsampling, and upsampling.
- Multiscale Support: Work with multiscale OME-Zarr pyramids.
- Metadata Handling: Access and modify OME-Zarr metadata like axes and transformations.
- Lazy Loading: Leverage Dask arrays for efficient processing of large datasets.
- Segmentation Plugins: Extensible plugin architecture for image segmentation algorithms.
Starting from version 0.2.0, ZarrNii implements improved orientation metadata handling with backwards compatibility for existing OME-Zarr files:
- Metadata Key:
xyz_orientation - Axis Order: Always in XYZ axes order for consistency
- Example:
"RAS"means Right-to-left, Anterior-to-posterior, Superior-to-inferior in XYZ space
- Metadata Key:
orientation - Axis Order: ZYX axes order (reversed from XYZ)
- Example:
"SAR"in ZYX order is equivalent to"RAS"in XYZ order
ZarrNii automatically handles both formats when loading OME-Zarr files:
# Loading prioritizes xyz_orientation, falls back to orientation (with reversal)
znimg = ZarrNii.from_ome_zarr("legacy_file.zarr") # Works with both formats
# New files always use xyz_orientation format
znimg.to_ome_zarr("new_file.zarr") # Saves with xyz_orientation- Existing files: Continue to work without modification
- New files: Use the improved
xyz_orientationformat automatically - API: The
orientationproperty maintains backwards compatibility
ZarrNii includes a plugin architecture for image segmentation algorithms, starting with Otsu thresholding:
from zarrnii import ZarrNii, OtsuSegmentation
# Load your image
znimg = ZarrNii.from_ome_zarr("image.ome.zarr")
# Apply Otsu thresholding segmentation
segmented = znimg.segment_otsu(nbins=256)
# Or use the generic plugin interface
plugin = OtsuSegmentation(nbins=128)
segmented = znimg.segment(plugin)
# Save segmented results
segmented.to_ome_zarr("segmented_image.ome.zarr")Create your own segmentation algorithms by extending the SegmentationPlugin base class:
from zarrnii.plugins.segmentation import SegmentationPlugin
class CustomSegmentation(SegmentationPlugin):
def segment(self, image, metadata=None):
# Your segmentation logic here
return binary_mask.astype(np.uint8)
@property
def name(self):
return "Custom Algorithm"
@property
def description(self):
return "Description of your algorithm"from zarrnii import ZarrNii
# Load an OME-Zarr dataset
znimg = ZarrNii.from_ome_zarr("path/to/zarr_dataset.ome.zarr")
<<<<<<< HEAD
# Or load from Imaris (requires zarrnii[imaris])
# znimg = ZarrNii.from_imaris("path/to/microscopy_data.ims")
=======
# Load from compressed ZIP format
znimg_zip = ZarrNii.from_ome_zarr("path/to/dataset.ome.zarr.zip")
>>>>>>> main
# Perform a transformation (e.g., downsample)
downsampled_znimg = znimg.downsample(level=2)
# Save as NIfTI
downsampled_znimg.to_nifti("output_dataset.nii")
# Save as compressed OME-Zarr ZIP file
downsampled_znimg.to_ome_zarr("compressed_output.ome.zarr.zip")For development, this project uses:
- uv for fast dependency management
- pytest for testing
- black for code formatting
- mkdocs for documentation
# Run tests
uv run pytest
# Format code
uv run black .
# Build documentation
uv run mkdocs build
# Serve docs locally
uv run mkdocs serveIf you have just installed:
# See all available tasks
just help
# Run tests
just test
# Format and lint
just format
just lintExplore the documentation to get started.
Contributions are welcome! Please read our contributing guidelines and ensure all tests pass before submitting pull requests.