Skip to content

Commit

Permalink
Feature/nd2reader-xarray-ome-metadata (#533)
Browse files Browse the repository at this point in the history
* include parsed ome metadata as attrs of returned xarray DataArray

* update tests to reflect change in type of metadata returned by reader
  • Loading branch information
yichechang authored Oct 18, 2023
1 parent e12198a commit 942cc53
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 8 additions & 0 deletions aicsimageio/readers/nd2_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ def _xarr_reformat(self, delayed: bool) -> xr.DataArray:
xarr.attrs[constants.METADATA_UNPROCESSED][
"frame"
] = rdr.frame_metadata(self.current_scene_index)

# include OME metadata as attrs of returned xarray.DataArray if possible
# (not possible with `nd2` version < 0.7.0; see PR #521)
try:
xarr.attrs[constants.METADATA_PROCESSED] = self.ome_metadata
except NotImplementedError:
pass

return xarr.isel({nd2.AXIS.POSITION: 0}, missing_dims="ignore")

@property
Expand Down
19 changes: 16 additions & 3 deletions aicsimageio/tests/readers/extra_readers/test_nd2_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"expected_dtype, "
"expected_dims_order, "
"expected_channel_names, "
"expected_physical_pixel_sizes",
"expected_physical_pixel_sizes, "
"expected_metadata_type",
[
pytest.param(
"example.txt",
Expand All @@ -42,6 +43,7 @@
None,
None,
None,
None,
marks=pytest.mark.xfail(raises=exceptions.UnsupportedFileFormatError),
),
pytest.param(
Expand All @@ -53,6 +55,7 @@
"TCYX",
["20phase", "20xDiO"],
(1, 50, 50),
dict,
),
(
"ND2_jonas_header_test2.nd2",
Expand All @@ -63,6 +66,7 @@
"CTZYX",
["Jonas_DIC"],
(0.5, 0.12863494437945, 0.12863494437945),
OME,
),
(
"ND2_maxime_BF007.nd2",
Expand All @@ -73,6 +77,7 @@
"CYX",
["405/488/561/633nm"],
(1.0, 0.158389678930686, 0.158389678930686),
OME,
),
(
"ND2_dims_p4z5t3c2y32x32.nd2",
Expand All @@ -83,6 +88,7 @@
"TZCYX",
["Widefield Green", "Widefield Red"],
(1.0, 0.652452890023035, 0.652452890023035),
OME,
),
(
"ND2_dims_c2y32x32.nd2",
Expand All @@ -93,6 +99,7 @@
"CYX",
["Widefield Green", "Widefield Red"],
(1.0, 0.652452890023035, 0.652452890023035),
OME,
),
(
"ND2_dims_p1z5t3c2y32x32.nd2",
Expand All @@ -103,6 +110,7 @@
"TZCYX",
["Widefield Green", "Widefield Red"],
(1.0, 0.652452890023035, 0.652452890023035),
OME,
),
(
"ND2_dims_p2z5t3-2c4y32x32.nd2",
Expand All @@ -113,6 +121,7 @@
"TZCYX",
["Widefield Green", "Widefield Red", "Widefield Far-Red", "Brightfield"],
(1.0, 0.652452890023035, 0.652452890023035),
OME,
),
(
"ND2_dims_t3c2y32x32.nd2",
Expand All @@ -123,6 +132,7 @@
"TCYX",
["Widefield Green", "Widefield Red"],
(1.0, 0.652452890023035, 0.652452890023035),
OME,
),
(
"ND2_dims_rgb_t3p2c2z3x64y64.nd2",
Expand All @@ -133,6 +143,7 @@
"TZCYXS",
["Brightfield", "Brightfield"],
(0.01, 0.34285714285714286, 0.34285714285714286),
OME,
),
(
"ND2_dims_rgb.nd2",
Expand All @@ -143,6 +154,7 @@
"CYXS",
["Brightfield"],
(1.0, 0.34285714285714286, 0.34285714285714286),
OME,
),
],
)
Expand All @@ -156,6 +168,7 @@ def test_nd2_reader(
expected_dims_order: str,
expected_channel_names: List[str],
expected_physical_pixel_sizes: Tuple[float, float, float],
expected_metadata_type: Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]],
) -> None:
# Construct full filepath
uri = get_resource_full_path(filename, host)
Expand All @@ -172,7 +185,7 @@ def test_nd2_reader(
expected_dims_order=expected_dims_order,
expected_channel_names=expected_channel_names,
expected_physical_pixel_sizes=expected_physical_pixel_sizes,
expected_metadata_type=dict,
expected_metadata_type=expected_metadata_type,
)


Expand All @@ -196,7 +209,7 @@ def test_nd2_reader(
dimensions.DEFAULT_DIMENSION_ORDER,
["Jonas_DIC"],
(0.5, 0.12863494437945, 0.12863494437945),
dict,
OME,
),
],
)
Expand Down

0 comments on commit 942cc53

Please sign in to comment.