Skip to main content

xarray and ome-ngff

Project description

xarray-ome-ngff

Integration between xarray and the ome-ngff data model.

At present (February, 2023) this is a partial implementation of the OME-NGFF spec. Specifcally, only the multiscales and specs required by multiscales are implemented. Complete support for the spec would be welcome.

How it works

This library depends on pydantic-ome-ngff which implements OME-NGFF metadata as pydantic models. Axes metadata is inferred from a DataArray by iterating over the dimensions of the array and checking for units and type properties in the attributes of the coords assigned to each dimension. Dimensions without coordinates will raise an exception. Scale and translation CoordinateTransforms are inferred by inspecting the values of the coordinates for each dimension. Be advised that no attempt is made to verify that arrays are sampled on a regular grid.

Usage

Generate multiscales metadata from a multiscale collection of DataArrays.

from xarray import DataArray
import numpy as np
from xarray_ome_ngff import create_multiscale_metadata
import json
coords = {'z' : DataArray(np.arange(100), attrs={'units': 'nm', 'type': 'space'}, dims=('z',)),
         'y' : DataArray(np.arange(300) * 2.2, attrs={'units': 'nm', 'type': 'space'}, dims=('y')),
         'x' : DataArray((np.arange(300) * .5) + 1, attrs={'units': 'nm', 'type': 'space'}, dims=('x',))}

s0 = DataArray(data=0, coords=coords, dims=('z','y','x'), name='s0')
s1 = s0.coarsen({dim: 2 for dim in s0.dims}).mean()
s1.name = 's1'
# create a small multiscale pyramid
multiscale = [s0, s1]
metadata = create_multiscale_metadata(name='test', type='yes', arrays=multiscale)
print(metadata.json(indent=2))
{
  "version": "0.5-dev",
  "name": "test",
  "type": "yes",
  "metadata": null,
  "datasets": [
    {
      "path": "s0",
      "coordinateTransformations": [
        {
          "type": "scale",
          "scale": [
            1.0,
            2.2,
            0.5
          ]
        },
        {
          "type": "translation",
          "translation": [
            0.0,
            0.0,
            1.0
          ]
        }
      ]
    },
    {
      "path": "s1",
      "coordinateTransformations": [
        {
          "type": "scale",
          "scale": [
            2.0,
            4.4,
            1.0
          ]
        },
        {
          "type": "translation",
          "translation": [
            0.5,
            1.1,
            1.25
          ]
        }
      ]
    }
  ],
  "axes": [
    {
      "name": "z",
      "type": "space",
      "units": null
    },
    {
      "name": "y",
      "type": "space",
      "units": null
    },
    {
      "name": "x",
      "type": "space",
      "units": null
    }
  ],
  "coordinateTransformations": [
    {
      "type": "scale",
      "scale": [
        1.0,
        1.0,
        1.0
      ]
    }
  ]
}

It is not possible to create a DataArray from OME-NGFF metadata, but together the OME-NGFF Axes and CoordinateTransformations metadata are sufficient to create coordinates for a DataArray, provided you know the shape of the data. The function create_coords performs this operation:

from xarray_ome_ngff import create_coords
from pydantic_ome_ngff.v05.coordinateTransformations import VectorScaleTransform, VectorTranslationTransform
from pydantic_ome_ngff.v05.axes import Axis


shape = (3, 3)
axes = [Axis(name='a', units="meter", type="space"), Axis(name='b', units="meter", type="space")]

transforms = [VectorScaleTransform(scale=[1, .5]), VectorTranslationTransform(translation=[1, 2])]

coords = create_coords(axes, transforms, shape)
print(coords)
'''
{'a': <xarray.DataArray (a: 3)>
array([1., 2., 3.])
Dimensions without coordinates: a
Attributes:
    units:    meter, 'b': <xarray.DataArray (b: 3)>
array([2. , 2.5, 3. ])
Dimensions without coordinates: b
Attributes:
    units:    meter}
'''

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xarray_ome_ngff-0.1.0.tar.gz (5.7 kB view hashes)

Uploaded Source

Built Distribution

xarray_ome_ngff-0.1.0-py3-none-any.whl (5.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page