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

Labels properties are a dict of lists #12

Merged
merged 2 commits into from
Sep 22, 2021
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
40 changes: 39 additions & 1 deletion napari_ome_zarr/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def napari_hook_implementation(
LOGGER = logging.getLogger("napari_ome_zarr.reader")

METADATA_KEYS = ("name", "visible", "contrast_limits", "colormap",
"metadata", "properties")
"metadata")

@napari_hook_implementation
def napari_get_reader(path: PathLike) -> Optional[ReaderFunction]:
Expand All @@ -48,6 +48,40 @@ def napari_get_reader(path: PathLike) -> Optional[ReaderFunction]:
return None


def transform_properties(props=None):
"""
Transform properties

Transform a dict of {label_id : {key: value, key2: value2}}
with a key for every LABEL
into a dict of a key for every VALUE, with a list of values for each
{
"index": [1381342, 1381343...]
"omero:roiId": [1381342, 1381343...],
"omero:shapeId": [1682567, 1682567...]
}
"""
if props is None:
return None

properties: Dict[str, List] = {}

# First, create lists for all existing keys...
for label_id, props_dict in props.items():
for key in props_dict.keys():
properties[key] = []

keys = list(properties.keys())

properties["index"] = []
for label_id, props_dict in props.items():
properties["index"].append(label_id)
# ...in case some objects don't have all the keys
for key in keys:
properties[key].append(props_dict.get(key, None))
return properties


def transform(nodes: Iterator[Node]) -> Optional[ReaderFunction]:
def f(*args: Any, **kwargs: Any) -> List[LayerData]:
results: List[LayerData] = list()
Expand Down Expand Up @@ -98,6 +132,10 @@ def f(*args: Any, **kwargs: Any) -> List[LayerData]:
if not isinstance(cm, Colormap):
cms[idx] = Colormap(cm)

properties = transform_properties(node.metadata.get("properties"))
if properties is not None:
metadata["properties"] = properties

rv: LayerData = (data, metadata, layer_type)
LOGGER.debug(f"Transformed: {rv}")
results.append(rv)
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ deps =
# you can remove these if you don't use them
napari
magicgui
pooch
Copy link
Member

Choose a reason for hiding this comment

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

Where does pooch get used, @will-moore ?

Copy link
Member Author

Choose a reason for hiding this comment

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

See previous run failure: https://github.com/ome/napari-ome-zarr/runs/3651193426

.tox/py37-linux/lib/python3.7/site-packages/napari/conftest.py:5

>>> import pooch
ModuleNotFoundError

pooch is listed as a test dependency of napari.

Copy link
Member

Choose a reason for hiding this comment

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

As pooch is a testing dependency of napari (https://github.com/napari/napari/blob/95a5d06fe36f10fb26f5798f1327a3ea082d581f/setup.cfg#L105), updating https://github.com/ome/napari-ome-zarr/blob/main/tox.ini#L33 with

-    napari
+   napari[testing]

would be another option. Does it bring too many extra dependencies?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just trying this locally into my existing napari & ome-zarr-py env:

$ pip install -U napari[testing]

This download 8 packages, mostly very small (few MB or less) except torch (218 MB). 🤷

pytest-qt
qtpy
pyqt5
Expand Down