From 6abd57890a778feadc0fa4ae026136ff26222522 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Tue, 21 Jul 2020 11:45:44 +0100 Subject: [PATCH 1/2] Fix masks.[].image.array to match spec Also add docs for masks.py methods --- src/omero_zarr/cli.py | 8 ++++++ src/omero_zarr/masks.py | 55 +++++++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/omero_zarr/cli.py b/src/omero_zarr/cli.py index 561aede..36410c9 100644 --- a/src/omero_zarr/cli.py +++ b/src/omero_zarr/cli.py @@ -144,6 +144,14 @@ def _configure(self, parser): "overlapping masks" ), ) + masks.add_argument( + "--mask-image-array-url", + help=( + "Optional URL to an array inside a Zarr image containing the " + "source image associated with the mask. " + "This array should be loadable by the Image Zarr library." + ), + ) export = parser.add(sub, self.export, EXPORT_HELP) export.add_argument( diff --git a/src/omero_zarr/masks.py b/src/omero_zarr/masks.py index ab2c82c..40b7813 100644 --- a/src/omero_zarr/masks.py +++ b/src/omero_zarr/masks.py @@ -55,7 +55,7 @@ def image_masks_to_zarr(image, args): saver = MaskSaver(image, dtype, args.mask_path, args.style) if args.style == "split": for (roi_id, roi) in masks.items(): - saver.save([roi], str(roi_id)) + saver.save([roi], str(roi_id), args.mask_image_array_url) else: if args.mask_map: @@ -74,15 +74,31 @@ def image_masks_to_zarr(image, args): for name, values in mask_map.items(): print(f"Mask map: {name} (count: {len(values)})") - saver.save(values, name) + saver.save(values, name, args.mask_image_array_url) else: - saver.save(masks.values(), args.mask_name) + saver.save( + masks.values(), args.mask_name, args.mask_image_array_url + ) else: print("No masks found on Image") class MaskSaver: - def __init__(self, image, dtype, path="masks", style="6d"): + def __init__(self, image, dtype, path="masks", style="labelled"): + """ + Saves masks to an image Zarr. + + After creating this object call `save()` to fetch the masks from + OMERO and save them to the image Zarr. + + :param image omero.model.ImageI: OMERO image + :param dtype numpy.dtype: dtype of the mask array + :param path str: Save masks to this group inside the Zarr + :param style str: The form of the masks, "labelled" or "split". + "6d" is supported for historic reasons but is incompatible + with the Zarr spec and will be removed in future + """ + self.image = image self.dtype = dtype self.path = path @@ -100,7 +116,15 @@ def __init__(self, image, dtype, path="masks", style="6d"): self.size_x, ) - def save(self, masks, name): + def save(self, masks, name, image_array_url=None): + """ + Saves masks to an image Zarr. + + :param masks [MaskI]: Iterable container of OMERO masks + :param name str: the name of the mask array the Zarr + :param image_array_url str: Optional URL to a Zarr array in the source + image Zarr + """ # Figure out whether we can flatten some dimensions unique_dims = { @@ -165,20 +189,15 @@ def save(self, masks, name): ) # Setting za.attrs[] doesn't work, so go via parent - if "0" in root: - image_name = "../../0" + image_attrs = {} + if image_array_url: + image_attrs["array"] = image_array_url + elif "0" in root: + image_attrs["array"] = image_array_url else: - image_name = "omero://{}.zarr".format(self.image.id) - out_masks[name].attrs["image"] = { - "array": image_name, - "source": { - # 'ts': [], - # 'cs': [], - # 'zs': [], - # 'ys': [], - # 'xs': [], - }, - } + print("WARNING: Not setting mask image.array") + + out_masks[name].attrs["image"] = image_attrs print(f"Created {filename}/{self.path}/{name}") attrs = out_masks.attrs.asdict() From 43ad6d55598af3b568a6f62a4ad45e504cb8ff5f Mon Sep 17 00:00:00 2001 From: Simon Li Date: Tue, 21 Jul 2020 12:19:30 +0100 Subject: [PATCH 2/2] Fix image.array for root/0 --- src/omero_zarr/masks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/omero_zarr/masks.py b/src/omero_zarr/masks.py index 40b7813..f0a7284 100644 --- a/src/omero_zarr/masks.py +++ b/src/omero_zarr/masks.py @@ -193,7 +193,7 @@ def save(self, masks, name, image_array_url=None): if image_array_url: image_attrs["array"] = image_array_url elif "0" in root: - image_attrs["array"] = image_array_url + image_attrs["array"] = "../../0" else: print("WARNING: Not setting mask image.array")