Skip to content

Commit

Permalink
Merge pull request #61 from DragaDoncila/add-label-properties
Browse files Browse the repository at this point in the history
Add support for label properties in zattrs and plugin
  • Loading branch information
joshmoore authored Nov 30, 2020
2 parents ea299f3 + 309d1fe commit 8cfe679
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
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:
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 @@ -223,6 +223,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 @@ -233,6 +241,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

0 comments on commit 8cfe679

Please sign in to comment.