From 2e88f6817584939a0226877d729e01353b6288ad Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Mon, 14 Aug 2023 15:16:41 -0400 Subject: [PATCH] Feature/ome_metadata-for-nd2 (#521) * feat: add ome_meta from nd2 * add test * black * remove import --- aicsimageio/readers/nd2_reader.py | 22 +++++++++++++++++++ .../readers/extra_readers/test_nd2_reader.py | 20 +++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/aicsimageio/readers/nd2_reader.py b/aicsimageio/readers/nd2_reader.py index 50cd930da..b89ad4be2 100644 --- a/aicsimageio/readers/nd2_reader.py +++ b/aicsimageio/readers/nd2_reader.py @@ -11,6 +11,7 @@ if TYPE_CHECKING: import xarray as xr from fsspec.spec import AbstractFileSystem + from ome_types import OME try: @@ -102,3 +103,24 @@ def physical_pixel_sizes(self) -> types.PhysicalPixelSizes: """ with nd2.ND2File(self._path) as rdr: return types.PhysicalPixelSizes(*rdr.voxel_size()[::-1]) + + @property + def ome_metadata(self) -> OME: + """Return OME metadata. + + Returns + ------- + metadata: OME + The original metadata transformed into the OME specfication. + This likely isn't a complete transformation but is guarenteed to + be a valid transformation. + + Raises + ------ + NotImplementedError + No metadata transformer available. + """ + if hasattr(nd2.ND2File, "ome_metadata"): + with nd2.ND2File(self._path) as rdr: + return rdr.ome_metadata() + raise NotImplementedError() diff --git a/aicsimageio/tests/readers/extra_readers/test_nd2_reader.py b/aicsimageio/tests/readers/extra_readers/test_nd2_reader.py index 88c17e10c..1b12d49bb 100644 --- a/aicsimageio/tests/readers/extra_readers/test_nd2_reader.py +++ b/aicsimageio/tests/readers/extra_readers/test_nd2_reader.py @@ -5,6 +5,7 @@ import numpy as np import pytest +from ome_types import OME from aicsimageio import AICSImage, dimensions, exceptions from aicsimageio.readers.nd2_reader import ND2Reader @@ -229,6 +230,25 @@ def test_aicsimage( ) +@pytest.mark.parametrize( + "filename", + [ + "ND2_jonas_header_test2.nd2", + "ND2_maxime_BF007.nd2", + "ND2_dims_p4z5t3c2y32x32.nd2", + ], +) +def test_ome_metadata(filename: str) -> None: + # Get full filepath + uri = get_resource_full_path(filename, LOCAL) + + # Init image + img = AICSImage(uri) + + # Test the transform + assert isinstance(img.ome_metadata, OME) + + def test_frame_metadata() -> None: filename = "ND2_dims_rgb_t3p2c2z3x64y64.nd2" uri = get_resource_full_path(filename, LOCAL)