Skip to content

Commit 96197ac

Browse files
committed
Improve handling of omero metadata in import-ome-zarr task (ref #578)
1 parent be5b822 commit 96197ac

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

fractal_tasks_core/tasks/import_ome_zarr.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def _process_single_image(
4646
This task:
4747
4848
1. Validates OME-NGFF image metadata, via `NgffImageMeta`;
49-
2. Optionally generates and writes two ROI tables.
49+
2. Optionally generates and writes two ROI tables;
50+
3. Optionally update OME-NGFF omero metadata.
5051
5152
Args:
5253
image_path: Absolute path to the image Zarr group.
@@ -107,12 +108,34 @@ def _process_single_image(
107108

108109
# Update Omero-channels metadata
109110
if update_omero_metadata:
110-
if image_meta.omero is None or image_meta.omero.channels == []:
111-
# TODO: create omero-channels list from scratch
112-
raise NotImplementedError
113-
old_channels = [c.dict() for c in image_meta.omero.channels]
111+
112+
# Extract number of channels from zarr array
113+
try:
114+
channel_axis_index = image_meta.axes_names.index("c")
115+
except ValueError:
116+
raise NotImplementedError(
117+
f"Existing axes ({image_meta.axes_names}) do not "
118+
'include "c".'
119+
)
120+
num_channels_zarr = array.shape[channel_axis_index]
121+
print(num_channels_zarr)
122+
123+
old_omero = image_group.attrs.get("omero", {})
124+
old_channels = old_omero.get("channels", [])
125+
if old_channels != []:
126+
# Case 1: omero/channels exist
127+
if len(old_channels) != num_channels_zarr:
128+
error_msg = (
129+
"Channels-number mismatch: Number of channels in the "
130+
f"zarr array ({num_channels_zarr}) differ from number "
131+
"of channels listed in NGFF omero metadata "
132+
f"({len(old_channels)})."
133+
)
134+
raise ValueError(error_msg)
135+
else:
136+
# Case 2: omero or omero/channels do not exist
137+
old_channels = [{} for ind in range(num_channels_zarr)]
114138
new_channels = update_omero_channels(old_channels)
115-
old_omero = image_group.attrs["omero"]
116139
new_omero = old_omero.copy()
117140
new_omero["channels"] = new_channels
118141
image_group.attrs.update(omero=new_omero)

0 commit comments

Comments
 (0)