-
Notifications
You must be signed in to change notification settings - Fork 21
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
investigate dropping ome-zarr dependency #123
Draft
will-moore
wants to merge
6
commits into
ome:main
Choose a base branch
from
will-moore:investigate_ome_zarr_py_alternative
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
68b26bd
investigate dropping ome-zarr dependency
will-moore 29fb252
Handle more channel metadata
will-moore 076cc7c
Handle no channel_axis
will-moore 4fc57d3
Remove ome_zarr imports and unused code from _reader.py
will-moore e4cda75
Read OME/METADATA.xml for bioformats2raw.layout and open all series i…
will-moore 920d9cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[settings] | ||
known_third_party = numpy,ome_zarr,pytest,setuptools,vispy | ||
known_third_party = dask,numpy,ome_zarr,pytest,setuptools,vispy,zarr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
# zarr v3 | ||
|
||
from typing import Any, Dict, List, Tuple, Union | ||
from xml.etree import ElementTree as ET | ||
|
||
import dask.array as da | ||
import zarr | ||
from vispy.color import Colormap | ||
from zarr import Group | ||
from zarr.core.buffer import default_buffer_prototype | ||
from zarr.core.sync import SyncMixin | ||
|
||
LayerData = Union[Tuple[Any], Tuple[Any, Dict], Tuple[Any, Dict, str]] | ||
|
||
|
||
class Spec: | ||
def __init__(self, group: Group): | ||
self.group = group | ||
|
||
@staticmethod | ||
def matches(group: Group) -> bool: | ||
return False | ||
|
||
def data(self) -> List[da.core.Array] | None: | ||
return None | ||
|
||
def metadata(self) -> Dict[str, Any] | None: | ||
# napari layer metadata | ||
return {} | ||
|
||
def children(self): | ||
return [] | ||
|
||
def iter_nodes(self): | ||
yield self | ||
for child in self.children(): | ||
yield from child.iter_nodes() | ||
|
||
def iter_data(self): | ||
for node in self.iter_nodes(): | ||
data = node.data() | ||
if data: | ||
yield data | ||
|
||
@staticmethod | ||
def get_attrs(group: Group): | ||
if "ome" in group.attrs: | ||
return group.attrs["ome"] | ||
return group.attrs | ||
|
||
|
||
class Multiscales(Spec): | ||
@staticmethod | ||
def matches(group: Group) -> bool: | ||
return "multiscales" in Spec.get_attrs(group) | ||
|
||
def children(self): | ||
ch = [] | ||
# test for child "labels" | ||
try: | ||
grp = self.group["labels"] | ||
attrs = Spec.get_attrs(grp) | ||
if "labels" in attrs: | ||
for name in attrs["labels"]: | ||
g = grp[name] | ||
if Label.matches(g): | ||
ch.append(Label(g)) | ||
except KeyError: | ||
pass | ||
return ch | ||
|
||
def data(self): | ||
attrs = Spec.get_attrs(self.group) | ||
paths = [ds["path"] for ds in attrs["multiscales"][0]["datasets"]] | ||
return [da.from_zarr(self.group[path]) for path in paths] | ||
|
||
def metadata(self): | ||
rsp = {} | ||
attrs = Spec.get_attrs(self.group) | ||
axes = attrs["multiscales"][0]["axes"] | ||
atypes = [axis["type"] for axis in axes] | ||
if "channel" in atypes: | ||
channel_axis = atypes.index("channel") | ||
rsp["channel_axis"] = channel_axis | ||
if "omero" in attrs: | ||
colormaps = [] | ||
ch_names = [] | ||
visibles = [] | ||
contrast_limits = [] | ||
|
||
for index, ch in enumerate(attrs["omero"]["channels"]): | ||
color = ch.get("color", None) | ||
if color is not None: | ||
rgb = [(int(color[i : i + 2], 16) / 255) for i in range(0, 6, 2)] | ||
# colormap is range: black -> rgb color | ||
colormaps.append(Colormap([[0, 0, 0], rgb])) | ||
ch_names.append(ch.get("label", str(index))) | ||
visibles.append(ch.get("active", True)) | ||
|
||
window = ch.get("window", None) | ||
if window is not None: | ||
start = window.get("start", None) | ||
end = window.get("end", None) | ||
if start is None or end is None: | ||
# Disable contrast limits settings if one is missing | ||
contrast_limits = None | ||
elif contrast_limits is not None: | ||
contrast_limits.append([start, end]) | ||
|
||
if rsp.get("channel_axis") is not None: | ||
rsp["colormap"] = colormaps | ||
rsp["name"] = ch_names | ||
rsp["contrast_limits"] = contrast_limits | ||
rsp["visible"] = visibles | ||
else: | ||
rsp["colormap"] = colormaps[0] | ||
rsp["name"] = ch_names[0] | ||
rsp["contrast_limits"] = contrast_limits[0] | ||
rsp["visible"] = visibles[0] | ||
|
||
return rsp | ||
|
||
|
||
class Bioformats2raw(Spec): | ||
@staticmethod | ||
def matches(group: Group) -> bool: | ||
attrs = Spec.get_attrs(group) | ||
# Don't consider "plate" as a Bioformats2raw layout | ||
return "bioformats2raw.layout" in attrs and "plate" not in attrs | ||
|
||
def children(self): | ||
# lookup children from series of OME/METADATA.xml | ||
xml_data = SyncMixin()._sync( | ||
self.group.store.get( | ||
"OME/METADATA.ome.xml", prototype=default_buffer_prototype() | ||
) | ||
) | ||
# print("xml_data", xml_data.to_bytes()) | ||
root = ET.fromstring(xml_data.to_bytes()) | ||
rv = [] | ||
for child in root: | ||
# {http://www.openmicroscopy.org/Schemas/OME/2016-06}Image | ||
print(child.tag) | ||
node_id = child.attrib.get("ID", "") | ||
if child.tag.endswith("Image") and node_id.startswith("Image:"): | ||
print("Image ID", node_id) | ||
image_path = node_id.replace("Image:", "") | ||
g = self.group[image_path] | ||
if Multiscales.matches(g): | ||
rv.append(Multiscales(g)) | ||
return rv | ||
|
||
# override to NOT yield self since node has no data | ||
def iter_nodes(self): | ||
for child in self.children(): | ||
yield from child.iter_nodes() | ||
|
||
|
||
class Plate(Spec): | ||
@staticmethod | ||
def matches(group: Group) -> bool: | ||
return "plate" in Spec.get_attrs(group) | ||
|
||
|
||
class Label(Multiscales): | ||
@staticmethod | ||
def matches(group: Group) -> bool: | ||
# label must also be Multiscales | ||
if not Multiscales.matches(group): | ||
return False | ||
return "image-label" in Spec.get_attrs(group) | ||
|
||
def metadata(self) -> Dict[str, Any] | None: | ||
# override Multiscales metadata | ||
return {} | ||
|
||
|
||
def read_ome_zarr(url): | ||
def f(*args: Any, **kwargs: Any) -> List[LayerData]: | ||
results: List[LayerData] = list() | ||
|
||
# TODO: handle missing file | ||
root_group = zarr.open(url) | ||
|
||
print("Root group", root_group.attrs.asdict()) | ||
|
||
if Bioformats2raw.matches(root_group): | ||
spec = Bioformats2raw(root_group) | ||
elif Multiscales.matches(root_group): | ||
spec = Multiscales(root_group) | ||
elif Plate.matches(root_group): | ||
spec = Plate(root_group) | ||
|
||
if spec: | ||
print("spec", spec) | ||
nodes = list(spec.iter_nodes()) | ||
print("Nodes", nodes) | ||
for node in nodes: | ||
node_data = node.data() | ||
metadata = node.metadata() | ||
# print(Spec.get_attrs(node.group)) | ||
if Label.matches(node.group): | ||
rv: LayerData = (node_data, metadata, "labels") | ||
else: | ||
rv: LayerData = (node_data, metadata) | ||
results.append(rv) | ||
|
||
return results | ||
|
||
return f |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dstansby By "graph traversal" logic, I mean, if I start with multiscales group e.g.
group = zarr.open("https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.4/idr0062A/6001240.zarr")
I then want to get the labels (if they exist). Here this is implemented in thechildren()
method, where we know to look in a child "labels" group and check attrs for"labels": ["labels1.zarr", "labels2.zarr"]
then return objects for those child labels so that the arrays (and metadata) can be added to the layers that are passed to napari.I don't see that ome-zarr-models-py includes that kind of logic for traversing the graph between these objects?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can get the list of labels paths from
Image.attributes.labels
. But the labels part of the spec just says these point to "labels objects", which I don't think are more specifically defined anywhere else?If the OME-Zarr spec was more prescriptive about what these "labels objects" were (are they meant to be groups with image-label metadata ??) then we could certainly do more, but I don't think the spec allows us to make those assumptions unfortunately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am only now reading version 0.5 of OME-zarr, and see that the labels section is much improved over 0.4 😄 . It's definitely within scope of
ome-zarr-models-py
to provide logic for getting from an Image dataset to the labels dataset if it's in the metadata. Tracking issue at ome-zarr-models/ome-zarr-models-py#92There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently in the tutorial at https://github.com/BioImageTools/ome-zarr-models-py/blob/main/docs/tutorial.py
If I add:
I get
None
(even though that image does have labels).I don't see any population of the labels in https://github.com/BioImageTools/ome-zarr-models-py/blob/7659a114a2428fe9d8acbd06aa7bc1c9d32624bb/src/ome_zarr_models/v04/image.py#L85 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a labels group, but looking at that dataset in the validator the top level group is missing the labels metadata, which is why .labels is giving None.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be precise, if you look at the image group in the validator, it should have a "labels" key at the same level as the "multiscales" and "omero" keys. If that was there, the paths under the "labels" key would be in the .labels attribute in ome-zarr-models-py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh hold on, am I just reading the spec wrong? Does:
Really mean:
?
If so then we should definitely implement that in ome-zarr-models-py!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
image.zarr/labels/
group.This is shown a bit more clearly in the layout at https://ngff.openmicroscopy.org/0.4/index.html#image-layout
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, I always thought the "labels" group was an arbitrary name and the example was just an example 🤦 - thanks for explaining, and I'll let you know once I've implemented this in ome-zarr-models-py 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, ome-zarr-models/ome-zarr-models-py#96 looks good