aeisenbarth / ngff-writer

A writer and reader for NGFF OME-Zarr datasets
14 stars 3 forks source link

NGFF Writer

This is a higher-level wrapper around the Zarr library for creating and reading NGFF OME-Zarr datasets for TCZYX image data.

Installation

Create a new python environment for installing

conda create -n ngff-writer python=3.8
conda activate ngff-writer
pip install git+https://github.com/aeisenbarth/ngff-writer

Usage

Creating an NGFF dataset

import dask.array as da
import numpy as np
from dask_image.imread import imread

from ngff_writer.array_utils import affine_matrix_to_tczyx, to_tczyx
from ngff_writer.writer import open_ngff_zarr

with open_ngff_zarr(
    store="output.zarr",
    dimension_separator="/",
    overwrite=True,
) as f:
    channel_paths = ["img_t1_z1_c0.tif", "img_t1_z1_c1.tif", "img_t1_z1_c2.tif"]
    affine2d = np.array([[1.29, 0.12, 335.25], [-0.12, 1.29, 120.92], [0.0, 0.0, 1.0]])
    transformation = {
        "type": "affine",
        "parameters": affine_matrix_to_tczyx(affine2d, axes_names=("y", "x")).tolist(),
    }

    collection = f.add_collection(name="well1")

    image = collection.add_image(
        image_name="microscopy1",
        array=to_tczyx(
            da.concatenate(imread(p) for p in channel_paths), axes_names=("c", "y", "x")
        ),
        transformation=transformation,
        channel_names=["brightfield", "GFP", "DAPI"],
    )

    image.add_label(
        name="cells",
        array=to_tczyx(imread("cells.tif"), axes_names=("y", "x")),
        transformation=transformation,
    )

Reading an NGFF dataset

Install additional packages:

pip install napari[all]==0.4.10
pip install git+https://github.com/aeisenbarth/ome-zarr-py@transformations-and-collections
pip install git+https://github.com/aeisenbarth/napari-ome-zarr@transformations-and-collections

Start Napari, which will load the plugins…

napari ./tests/resources/spacem_mini_dataset4x5.zarr

…or drag&drop an NGFF dataset.

Screenshot of Napari with multiple transformed layers

Comments for discussion