Package for working with OME-Zarr and NIFTI images in a unified manner, with a focus on spatial transformations
Project description
zarrnii
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).
Installation
Using pip (recommended)
pip install zarrnii
Optional Dependencies
For additional format support:
# For Imaris (.ims) file support
pip install zarrnii[imaris]
Development installation
For contributing or development, clone the repository and install with uv:
git clone https://github.com/khanlab/zarrnii.git
cd zarrnii
uv sync --dev
Key Features
- 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.
Advanced Topics
Orientation Metadata Backwards Compatibility
Starting from version 0.2.0, ZarrNii implements improved orientation metadata handling with backwards compatibility for existing OME-Zarr files:
New Format (v0.2.0+)
- 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
Legacy Format (pre-v0.2.0)
- Metadata Key:
orientation - Axis Order: ZYX axes order (reversed from XYZ)
- Example:
"SAR"in ZYX order is equivalent to"RAS"in XYZ order
Automatic Conversion
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
Migration Guide
- Existing files: Continue to work without modification
- New files: Use the improved
xyz_orientationformat automatically - API: The
orientationproperty maintains backwards compatibility
Segmentation Plugin System
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")
Custom Plugins
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"
Quick Start
from zarrnii import ZarrNii
# Load an OME-Zarr dataset
znimg = ZarrNii.from_ome_zarr("path/to/zarr_dataset.ome.zarr")
# 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")
# 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")
Development
For development, this project uses:
- uv for fast dependency management
- pytest for testing
- black for code formatting
- mkdocs for documentation
Available commands (using uv run):
# Run tests
uv run pytest
# Format code
uv run black .
# Build documentation
uv run mkdocs build
# Serve docs locally
uv run mkdocs serve
Using the justfile:
If you have just installed:
# See all available tasks
just help
# Run tests
just test
# Format and lint
just quality_fix
Learn More
Explore the documentation to get started.
Contributing
Contributions are welcome! Please read our contributing guidelines and ensure all tests pass before submitting pull requests.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zarrnii-0.15.3a1.tar.gz.
File metadata
- Download URL: zarrnii-0.15.3a1.tar.gz
- Upload date:
- Size: 1.7 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf49753cc68a2d6bfc28b1f0de35cb4692f3640f17dee2a67eb078997888e974
|
|
| MD5 |
b47dba1a9b5c92bc2caa05afc4a3d38f
|
|
| BLAKE2b-256 |
53e702d8d15d53e3028469932d4c59e1031376ffe428deb113d0cde7918a408a
|
Provenance
The following attestation bundles were made for zarrnii-0.15.3a1.tar.gz:
Publisher:
release.yml on khanlab/zarrnii
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zarrnii-0.15.3a1.tar.gz -
Subject digest:
cf49753cc68a2d6bfc28b1f0de35cb4692f3640f17dee2a67eb078997888e974 - Sigstore transparency entry: 953632713
- Sigstore integration time:
-
Permalink:
khanlab/zarrnii@067edcc65d3c97ed24515c430a6f4a39778d6816 -
Branch / Tag:
refs/tags/v0.15.3-alpha1 - Owner: https://github.com/khanlab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@067edcc65d3c97ed24515c430a6f4a39778d6816 -
Trigger Event:
release
-
Statement type:
File details
Details for the file zarrnii-0.15.3a1-py3-none-any.whl.
File metadata
- Download URL: zarrnii-0.15.3a1-py3-none-any.whl
- Upload date:
- Size: 119.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49c48e0eadb94d58f635398db7d389d8865a4bf072dc5d938872263e99fc0fac
|
|
| MD5 |
d6d0aacce1ffa9472f7dede7ba1b2971
|
|
| BLAKE2b-256 |
7f0494d5f7ae380974a39a34436b21f3d94f8c3a6e9c31bf46c8acacde38d860
|
Provenance
The following attestation bundles were made for zarrnii-0.15.3a1-py3-none-any.whl:
Publisher:
release.yml on khanlab/zarrnii
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zarrnii-0.15.3a1-py3-none-any.whl -
Subject digest:
49c48e0eadb94d58f635398db7d389d8865a4bf072dc5d938872263e99fc0fac - Sigstore transparency entry: 953632715
- Sigstore integration time:
-
Permalink:
khanlab/zarrnii@067edcc65d3c97ed24515c430a6f4a39778d6816 -
Branch / Tag:
refs/tags/v0.15.3-alpha1 - Owner: https://github.com/khanlab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@067edcc65d3c97ed24515c430a6f4a39778d6816 -
Trigger Event:
release
-
Statement type: