Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix masks.[].image.array to match spec #19

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/omero_zarr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
55 changes: 37 additions & 18 deletions src/omero_zarr/masks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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
manics marked this conversation as resolved.
Show resolved Hide resolved
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()
Expand Down