# 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)
```bash
pip install zarrnii
```
### Optional Dependencies
For additional format support:
```bash
# For Imaris (.ims) file support
pip install zarrnii[imaris]
```
### Development installation  
For contributing or development, clone the repository and install with [uv](https://docs.astral.sh/uv/):
```bash
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:
```python
# 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_orientation` format automatically  
- **API**: The `orientation` property maintains backwards compatibility
---
## Segmentation Plugin System
ZarrNii includes a plugin architecture for image segmentation algorithms, starting with Otsu thresholding:
```python
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:
```python
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
```python
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")
```
---
## Development
For development, this project uses:
- **[uv](https://docs.astral.sh/uv/)** for fast dependency management
- **[pytest](https://pytest.org/)** for testing
- **[black](https://black.readthedocs.io/)** for code formatting  
- **[flake8](https://flake8.pycqa.org/)** for linting
- **[mkdocs](https://www.mkdocs.org/)** for documentation
### Available commands (using `uv run`):
```bash
# Run tests
uv run pytest
# Format code
uv run black .
# Check linting  
uv run flake8 .
# Build documentation
uv run mkdocs build
# Serve docs locally
uv run mkdocs serve
```
### Using the justfile:
If you have [just](https://just.systems/) installed:
```bash
# See all available tasks
just help
# Run tests
just test
# Format and lint
just format
just lint
```
---
## Learn More
Explore the [documentation](https://www.khanlab.ca/zarrnii) to get started.
## Contributing
Contributions are welcome! Please read our contributing guidelines and ensure all tests pass before submitting pull requests.
            
         
        Raw data
        
            {
    "_id": null,
    "home_page": null,
    "name": "zarrnii",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "biomedical, imaging, microscopy, mri, neuroimaging, nifti, ome-zarr",
    "author": null,
    "author_email": "Ali Khan <alik@robarts.ca>",
    "download_url": "https://files.pythonhosted.org/packages/8e/17/a6b2618a4bad2d11e55626db838598f0226222f7d0c1cf5622c0c38f3812/zarrnii-0.8.0a1.tar.gz",
    "platform": null,
    "description": "# zarrnii\n\n **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.\n\nZarrNii allows you to:\n\n - Read and write OME-Zarr, NIfTI, and Imaris datasets\n - Perform transformations like cropping, downsampling, and interpolation.\n - Preserve and manipulate metadata from OME-Zarr (e.g., axes, coordinate transformations, OME annotations).\n\n---\n\n## Installation\n\n### Using pip (recommended)\n```bash\npip install zarrnii\n```\n\n### Optional Dependencies\nFor additional format support:\n```bash\n# For Imaris (.ims) file support\npip install zarrnii[imaris]\n```\n\n### Development installation  \nFor contributing or development, clone the repository and install with [uv](https://docs.astral.sh/uv/):\n\n```bash\ngit clone https://github.com/khanlab/zarrnii.git\ncd zarrnii\nuv sync --dev\n```\n\n---\n\n## Key Features\n\n - **Seamless Format Conversion**: Easily convert between OME-Zarr, NIfTI, and Imaris while preserving spatial metadata.\n - **ZipStore Support**: Read and write OME-Zarr files in compressed ZIP format (.ome.zarr.zip) for efficient storage and sharing.\n - **Transformations**: Apply common operations like affine transformations, downsampling, and upsampling.\n - **Multiscale Support**: Work with multiscale OME-Zarr pyramids.\n - **Metadata Handling**: Access and modify OME-Zarr metadata like axes and transformations.\n - **Lazy Loading**: Leverage Dask arrays for efficient processing of large datasets.\n - **Segmentation Plugins**: Extensible plugin architecture for image segmentation algorithms.\n\n---\n\n## Advanced Topics\n\n### Orientation Metadata Backwards Compatibility\n\nStarting from version 0.2.0, ZarrNii implements improved orientation metadata handling with backwards compatibility for existing OME-Zarr files:\n\n#### New Format (v0.2.0+)\n- **Metadata Key**: `xyz_orientation`\n- **Axis Order**: Always in XYZ axes order for consistency\n- **Example**: `\"RAS\"` means Right-to-left, Anterior-to-posterior, Superior-to-inferior in XYZ space\n\n#### Legacy Format (pre-v0.2.0)\n- **Metadata Key**: `orientation` \n- **Axis Order**: ZYX axes order (reversed from XYZ)\n- **Example**: `\"SAR\"` in ZYX order is equivalent to `\"RAS\"` in XYZ order\n\n#### Automatic Conversion\nZarrNii automatically handles both formats when loading OME-Zarr files:\n\n```python\n# Loading prioritizes xyz_orientation, falls back to orientation (with reversal)\nznimg = ZarrNii.from_ome_zarr(\"legacy_file.zarr\")  # Works with both formats\n\n# New files always use xyz_orientation format\nznimg.to_ome_zarr(\"new_file.zarr\")  # Saves with xyz_orientation\n```\n\n#### Migration Guide\n- **Existing files**: Continue to work without modification\n- **New files**: Use the improved `xyz_orientation` format automatically  \n- **API**: The `orientation` property maintains backwards compatibility\n\n---\n\n## Segmentation Plugin System\n\nZarrNii includes a plugin architecture for image segmentation algorithms, starting with Otsu thresholding:\n\n```python\nfrom zarrnii import ZarrNii, OtsuSegmentation\n\n# Load your image\nznimg = ZarrNii.from_ome_zarr(\"image.ome.zarr\")\n\n# Apply Otsu thresholding segmentation\nsegmented = znimg.segment_otsu(nbins=256)\n\n# Or use the generic plugin interface\nplugin = OtsuSegmentation(nbins=128)\nsegmented = znimg.segment(plugin)\n\n# Save segmented results\nsegmented.to_ome_zarr(\"segmented_image.ome.zarr\")\n```\n\n### Custom Plugins\n\nCreate your own segmentation algorithms by extending the `SegmentationPlugin` base class:\n\n```python\nfrom zarrnii.plugins.segmentation import SegmentationPlugin\n\nclass CustomSegmentation(SegmentationPlugin):\n    def segment(self, image, metadata=None):\n        # Your segmentation logic here\n        return binary_mask.astype(np.uint8)\n    \n    @property\n    def name(self):\n        return \"Custom Algorithm\"\n    \n    @property \n    def description(self):\n        return \"Description of your algorithm\"\n```\n\n---\n\n## Quick Start\n\n```python\nfrom zarrnii import ZarrNii\n\n# Load an OME-Zarr dataset\nznimg = ZarrNii.from_ome_zarr(\"path/to/zarr_dataset.ome.zarr\")\n\n<<<<<<< HEAD\n# Or load from Imaris (requires zarrnii[imaris])\n# znimg = ZarrNii.from_imaris(\"path/to/microscopy_data.ims\")\n=======\n# Load from compressed ZIP format\nznimg_zip = ZarrNii.from_ome_zarr(\"path/to/dataset.ome.zarr.zip\")\n>>>>>>> main\n\n# Perform a transformation (e.g., downsample)\ndownsampled_znimg = znimg.downsample(level=2)\n\n# Save as NIfTI\ndownsampled_znimg.to_nifti(\"output_dataset.nii\")\n\n# Save as compressed OME-Zarr ZIP file\ndownsampled_znimg.to_ome_zarr(\"compressed_output.ome.zarr.zip\")\n```\n\n---\n\n## Development\n\nFor development, this project uses:\n\n- **[uv](https://docs.astral.sh/uv/)** for fast dependency management\n- **[pytest](https://pytest.org/)** for testing\n- **[black](https://black.readthedocs.io/)** for code formatting  \n- **[flake8](https://flake8.pycqa.org/)** for linting\n- **[mkdocs](https://www.mkdocs.org/)** for documentation\n\n### Available commands (using `uv run`):\n```bash\n# Run tests\nuv run pytest\n\n# Format code\nuv run black .\n\n# Check linting  \nuv run flake8 .\n\n# Build documentation\nuv run mkdocs build\n\n# Serve docs locally\nuv run mkdocs serve\n```\n\n### Using the justfile:\nIf you have [just](https://just.systems/) installed:\n```bash\n# See all available tasks\njust help\n\n# Run tests\njust test\n\n# Format and lint\njust format\njust lint\n```\n\n---\n\n## Learn More\n\nExplore the [documentation](https://www.khanlab.ca/zarrnii) to get started.\n\n## Contributing\n\nContributions are welcome! Please read our contributing guidelines and ensure all tests pass before submitting pull requests.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Package for working with OME-Zarr and NIFTI images in a unified manner, with a focus on spatial transformations",
    "version": "0.8.0a1",
    "project_urls": {
        "Documentation": "https://www.khanlab.ca/zarrnii",
        "Homepage": "https://github.com/khanlab/zarrnii",
        "Issues": "https://github.com/khanlab/zarrnii/issues",
        "Repository": "https://github.com/khanlab/zarrnii"
    },
    "split_keywords": [
        "biomedical",
        " imaging",
        " microscopy",
        " mri",
        " neuroimaging",
        " nifti",
        " ome-zarr"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "9cc4bb255ae50603117d084c68ec4270999a81a70abe8921fd5249ec23632ad0",
                "md5": "e26b67734e8e56dc5906083994a2a012",
                "sha256": "f9b5fc77873ac5086096829d7e124cc17c3acbc14e147e082ee08e697fe023df"
            },
            "downloads": -1,
            "filename": "zarrnii-0.8.0a1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e26b67734e8e56dc5906083994a2a012",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 74188,
            "upload_time": "2025-10-27T04:30:12",
            "upload_time_iso_8601": "2025-10-27T04:30:12.961892Z",
            "url": "https://files.pythonhosted.org/packages/9c/c4/bb255ae50603117d084c68ec4270999a81a70abe8921fd5249ec23632ad0/zarrnii-0.8.0a1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "8e17a6b2618a4bad2d11e55626db838598f0226222f7d0c1cf5622c0c38f3812",
                "md5": "c1fdca4716bc4696db6c96cbc171697f",
                "sha256": "7ff9cc4d636e3d8a363b782eaa4bcaab7b2eaec995553f8fbdba46b9ab50eb68"
            },
            "downloads": -1,
            "filename": "zarrnii-0.8.0a1.tar.gz",
            "has_sig": false,
            "md5_digest": "c1fdca4716bc4696db6c96cbc171697f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 1674077,
            "upload_time": "2025-10-27T04:30:14",
            "upload_time_iso_8601": "2025-10-27T04:30:14.547047Z",
            "url": "https://files.pythonhosted.org/packages/8e/17/a6b2618a4bad2d11e55626db838598f0226222f7d0c1cf5622c0c38f3812/zarrnii-0.8.0a1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-27 04:30:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "khanlab",
    "github_project": "zarrnii",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "zarrnii"
}