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

Add support for label properties in zattrs and plugin #61

Merged
merged 8 commits into from
Nov 30, 2020
Merged
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
18 changes: 18 additions & 0 deletions ome_zarr/napari.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ def f(*args: Any, **kwargs: Any) -> List[LayerData]:
layer_type = "labels"
if "colormap" in metadata:
del metadata["colormap"]
if "properties" in metadata:
will-moore marked this conversation as resolved.
Show resolved Hide resolved
props = metadata["properties"]
reader_props = {}
label_indices = list(props.keys())
reader_props["index"] = label_indices

# properties may be ragged, so we need to know all possible properties
all_keys = set()
for index in label_indices:
all_keys = all_keys.union(set(props[index].keys()))

# napari expects lists of equal length so we must fill with None
for prop_key in all_keys:
reader_props[prop_key] = [
props[i][prop_key] if prop_key in props[i] else "None"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably be an actual None value here instead of a string "None" (unless it has to be a string for some reason). Since if some code is testing for the value being None, then the string won't be recognised as None.

for i in label_indices
]
metadata['properties'] = reader_props

elif shape[CHANNEL_DIMENSION] > 1:
metadata["channel_axis"] = CHANNEL_DIMENSION
Expand Down
9 changes: 9 additions & 0 deletions ome_zarr/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,22 @@ def __init__(self, node: Node) -> None:
except Exception as e:
LOGGER.error(f"invalid color - {color}: {e}")

properties: Dict[int, Dict[str, str]] = {}
props_list = image_label.get('properties', [])
if props_list:
for props in props_list:
label_val = props['label-value']
del props['label-value']
properties[label_val] = props
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain what's happening here? We'll need to check whether the underlying dictionary is a deep-copy of the original, or whether it returns a reference which means this may update the properties seen by other code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if I do image_label.get('properties', []) after this for loop, I get the properties without the label-values so you probably should create a copy of the props before editing.
This seems to work:

                properties[label_val] = dict(props)
                del properties[label_val]['label-value']


# TODO: a metadata transform should be provided by specific impls.
name = self.zarr.basename()
node.metadata.update(
{
"visible": node.visible,
"name": name,
"color": colors,
"properties": properties,
"metadata": {"image": self.lookup("image", {}), "path": name},
}
)
Expand Down