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 all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/posix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: actions/checkout@v2

- name: Setup miniconda
uses: conda-incubator/setup-miniconda@v1
uses: conda-incubator/setup-miniconda@v2
with:
auto-update-conda: true
channels: conda-forge,ome
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: actions/checkout@v2

- name: Setup miniconda
uses: conda-incubator/setup-miniconda@v1
uses: conda-incubator/setup-miniconda@v2
with:
auto-update-conda: true
channels: conda-forge,ome
Expand Down
3 changes: 3 additions & 0 deletions ome_zarr/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,14 @@ def create_zarr(
write_multiscale(labels, label_grp)

colors = []
properties = []
for x in range(1, 9):
rgba = [randrange(0, 256) for i in range(4)]
colors.append({"label-value": x, "rgba": rgba})
properties.append({"label-value": x, "class": f"class {x}"})
label_grp.attrs["image-label"] = {
"version": "0.1",
"colors": colors,
"properties": properties,
"source": {"image": "../../"},
}
21 changes: 20 additions & 1 deletion ome_zarr/napari.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import logging
import warnings
from typing import Any, Callable, Dict, Iterator, List, Optional
from typing import Any, Callable, Dict, Iterator, List, Optional, Set

from .data import CHANNEL_DIMENSION
from .io import parse_url
Expand Down Expand Up @@ -62,6 +62,25 @@ 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 all possible properties
all_keys: Set[str]
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
for i in label_indices
]
metadata["properties"] = reader_props

elif shape[CHANNEL_DIMENSION] > 1:
metadata["channel_axis"] = CHANNEL_DIMENSION
Expand Down
10 changes: 10 additions & 0 deletions ome_zarr/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ 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"]
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(
Expand All @@ -226,6 +234,8 @@ def __init__(self, node: Node) -> None:
"metadata": {"image": self.lookup("image", {}), "path": name},
}
)
if properties:
node.metadata.update({"properties": properties})


class Multiscales(Spec):
Expand Down
10 changes: 8 additions & 2 deletions tests/test_napari.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initdir(self, tmpdir):
self.path = tmpdir.mkdir("data")
create_zarr(str(self.path), astronaut, "astronaut")

def assert_layers(self, layers, visible_1, visible_2):
def assert_layers(self, layers, visible_1, visible_2, label_props=None):
# TODO: check name

assert len(layers) == 2
Expand All @@ -27,6 +27,8 @@ def assert_layers(self, layers, visible_1, visible_2):

data, metadata, layer_type = self.assert_layer(label)
assert visible_2 == metadata["visible"]
if label_props:
assert label_props == metadata["properties"]

def assert_layer(self, layer_data):
data, metadata, layer_type = layer_data
Expand All @@ -47,7 +49,11 @@ def test_labels(self):
def test_label(self):
filename = str(self.path.join("labels", "astronaut"))
layers = napari_get_reader(filename)()
self.assert_layers(layers, False, True)
properties = {
"index": [i for i in range(1, 9)],
"class": [f"class {i}" for i in range(1, 9)],
}
self.assert_layers(layers, False, True, properties)

@pytest.mark.skipif(
not sys.platform.startswith("darwin") or sys.version_info < (3, 7),
Expand Down