Skip to content
This repository was archived by the owner on Dec 1, 2025. It is now read-only.

Commit 942cc53

Browse files
authored
Feature/nd2reader-xarray-ome-metadata (#533)
* include parsed ome metadata as attrs of returned xarray DataArray * update tests to reflect change in type of metadata returned by reader
1 parent e12198a commit 942cc53

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

aicsimageio/readers/nd2_reader.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ def _xarr_reformat(self, delayed: bool) -> xr.DataArray:
8585
xarr.attrs[constants.METADATA_UNPROCESSED][
8686
"frame"
8787
] = rdr.frame_metadata(self.current_scene_index)
88+
89+
# include OME metadata as attrs of returned xarray.DataArray if possible
90+
# (not possible with `nd2` version < 0.7.0; see PR #521)
91+
try:
92+
xarr.attrs[constants.METADATA_PROCESSED] = self.ome_metadata
93+
except NotImplementedError:
94+
pass
95+
8896
return xarr.isel({nd2.AXIS.POSITION: 0}, missing_dims="ignore")
8997

9098
@property

aicsimageio/tests/readers/extra_readers/test_nd2_reader.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"expected_dtype, "
3232
"expected_dims_order, "
3333
"expected_channel_names, "
34-
"expected_physical_pixel_sizes",
34+
"expected_physical_pixel_sizes, "
35+
"expected_metadata_type",
3536
[
3637
pytest.param(
3738
"example.txt",
@@ -42,6 +43,7 @@
4243
None,
4344
None,
4445
None,
46+
None,
4547
marks=pytest.mark.xfail(raises=exceptions.UnsupportedFileFormatError),
4648
),
4749
pytest.param(
@@ -53,6 +55,7 @@
5355
"TCYX",
5456
["20phase", "20xDiO"],
5557
(1, 50, 50),
58+
dict,
5659
),
5760
(
5861
"ND2_jonas_header_test2.nd2",
@@ -63,6 +66,7 @@
6366
"CTZYX",
6467
["Jonas_DIC"],
6568
(0.5, 0.12863494437945, 0.12863494437945),
69+
OME,
6670
),
6771
(
6872
"ND2_maxime_BF007.nd2",
@@ -73,6 +77,7 @@
7377
"CYX",
7478
["405/488/561/633nm"],
7579
(1.0, 0.158389678930686, 0.158389678930686),
80+
OME,
7681
),
7782
(
7883
"ND2_dims_p4z5t3c2y32x32.nd2",
@@ -83,6 +88,7 @@
8388
"TZCYX",
8489
["Widefield Green", "Widefield Red"],
8590
(1.0, 0.652452890023035, 0.652452890023035),
91+
OME,
8692
),
8793
(
8894
"ND2_dims_c2y32x32.nd2",
@@ -93,6 +99,7 @@
9399
"CYX",
94100
["Widefield Green", "Widefield Red"],
95101
(1.0, 0.652452890023035, 0.652452890023035),
102+
OME,
96103
),
97104
(
98105
"ND2_dims_p1z5t3c2y32x32.nd2",
@@ -103,6 +110,7 @@
103110
"TZCYX",
104111
["Widefield Green", "Widefield Red"],
105112
(1.0, 0.652452890023035, 0.652452890023035),
113+
OME,
106114
),
107115
(
108116
"ND2_dims_p2z5t3-2c4y32x32.nd2",
@@ -113,6 +121,7 @@
113121
"TZCYX",
114122
["Widefield Green", "Widefield Red", "Widefield Far-Red", "Brightfield"],
115123
(1.0, 0.652452890023035, 0.652452890023035),
124+
OME,
116125
),
117126
(
118127
"ND2_dims_t3c2y32x32.nd2",
@@ -123,6 +132,7 @@
123132
"TCYX",
124133
["Widefield Green", "Widefield Red"],
125134
(1.0, 0.652452890023035, 0.652452890023035),
135+
OME,
126136
),
127137
(
128138
"ND2_dims_rgb_t3p2c2z3x64y64.nd2",
@@ -133,6 +143,7 @@
133143
"TZCYXS",
134144
["Brightfield", "Brightfield"],
135145
(0.01, 0.34285714285714286, 0.34285714285714286),
146+
OME,
136147
),
137148
(
138149
"ND2_dims_rgb.nd2",
@@ -143,6 +154,7 @@
143154
"CYXS",
144155
["Brightfield"],
145156
(1.0, 0.34285714285714286, 0.34285714285714286),
157+
OME,
146158
),
147159
],
148160
)
@@ -156,6 +168,7 @@ def test_nd2_reader(
156168
expected_dims_order: str,
157169
expected_channel_names: List[str],
158170
expected_physical_pixel_sizes: Tuple[float, float, float],
171+
expected_metadata_type: Union[type, Tuple[Union[type, Tuple[Any, ...]], ...]],
159172
) -> None:
160173
# Construct full filepath
161174
uri = get_resource_full_path(filename, host)
@@ -172,7 +185,7 @@ def test_nd2_reader(
172185
expected_dims_order=expected_dims_order,
173186
expected_channel_names=expected_channel_names,
174187
expected_physical_pixel_sizes=expected_physical_pixel_sizes,
175-
expected_metadata_type=dict,
188+
expected_metadata_type=expected_metadata_type,
176189
)
177190

178191

@@ -196,7 +209,7 @@ def test_nd2_reader(
196209
dimensions.DEFAULT_DIMENSION_ORDER,
197210
["Jonas_DIC"],
198211
(0.5, 0.12863494437945, 0.12863494437945),
199-
dict,
212+
OME,
200213
),
201214
],
202215
)

0 commit comments

Comments
 (0)