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

Multiscale labeled images #45

Merged
merged 39 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
c45f450
Open images for a mask via the array link
joshmoore Aug 18, 2020
47d8563
Move python code to a module
joshmoore Aug 19, 2020
aafd5ed
Fix mypy errors
joshmoore Aug 19, 2020
185157f
Refactor to re-use multiscale loading
joshmoore Aug 19, 2020
266b357
Have mypy check all API methods
joshmoore Aug 19, 2020
fc8df8f
Enfore mypy annotations on all definitions
joshmoore Aug 19, 2020
db43af5
Fix 'bool(false)' in color test
joshmoore Aug 19, 2020
967b36c
move create_test_data to ome_zarr.data
joshmoore Aug 21, 2020
c146101
Add isort as the first pre-commit step
joshmoore Aug 21, 2020
1c50a26
refactor create_zarr method for use from CLI
joshmoore Aug 21, 2020
3a9f360
Major API refactoring
joshmoore Aug 24, 2020
7c8cff1
More tests and bug fixes
joshmoore Aug 25, 2020
b57d439
Deal with empty labels from astronauts
joshmoore Aug 26, 2020
9665c4c
ome_zarr.scale: migrate scale.py to ome_zarr
joshmoore Aug 26, 2020
6b18581
Enable make_test_viewer from napari
joshmoore Aug 26, 2020
4fb4213
Fix recursive reading, info, and downloading
joshmoore Aug 26, 2020
c4ef738
Fix number of channels in coins()
joshmoore Aug 26, 2020
77e631a
Fix CLI tools incl. prefix stripping
joshmoore Aug 27, 2020
7a4f865
Update style and fill out well-formed docs
joshmoore Aug 28, 2020
56f7ef5
Use GH Actions & Conda for tests
joshmoore Aug 31, 2020
12b9493
Fix "loaded as" doc sentence
joshmoore Aug 31, 2020
b90b68d
Activate doctests
joshmoore Aug 31, 2020
151c52e
Correct channel array for grayscale images
joshmoore Aug 31, 2020
930c303
Use latest setup-conda for posix and windows
joshmoore Aug 31, 2020
b7e8196
Try pyside2 for all platforms
joshmoore Aug 31, 2020
69288fc
Fix label colors key
joshmoore Aug 31, 2020
ab0cdc1
Fix 'colormap' key thanks to Will
joshmoore Sep 1, 2020
723fe0c
Remove unwrapping of pyramids thanks to Will
joshmoore Sep 1, 2020
8125dd4
Change 'Layer' to 'Node'
joshmoore Sep 2, 2020
31082bc
Remove is_zarr() in favor of exists()
joshmoore Sep 3, 2020
0a95ff2
Fix double negative
joshmoore Sep 3, 2020
f2cff53
Remove extra 'of'
joshmoore Sep 3, 2020
c0806ec
Rename test_layer to test_node
joshmoore Sep 3, 2020
6cc1c25
Re-instate visiblility via a property
joshmoore Sep 3, 2020
171e122
Use 'greyscale'
joshmoore Sep 4, 2020
ee73e06
Restore downloading without --output
joshmoore Sep 4, 2020
d2f9f1c
Rework Node.add documentation
joshmoore Sep 4, 2020
fbfa932
Disable napari viewer test on non-OSX platforms
joshmoore Sep 4, 2020
4ed3078
Use bash script with activated conda environment on Windows
joshmoore Sep 4, 2020
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 ome_zarr/napari.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def f(*args: Any, **kwargs: Any) -> List[LayerData]:
if "colormap" in metadata:
del metadata["colormap"]

if shape[CHANNEL_DIMENSION] > 1:
elif shape[CHANNEL_DIMENSION] > 1:
metadata["channel_axis"] = CHANNEL_DIMENSION
else:
for x in ("name", "visible", "contrast_limits", "colormap"):
Expand Down
71 changes: 57 additions & 14 deletions ome_zarr/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class Node:
hierarchy."""

def __init__(
self, zarr: BaseZarrLocation, root: Union["Node", "Reader", List[str]]
self,
zarr: BaseZarrLocation,
root: Union["Node", "Reader", List[str]],
visibility: bool = True,
):
self.zarr = zarr
self.root = root
Expand All @@ -28,7 +31,7 @@ def __init__(
self.seen = root.seen
else:
self.seen = cast(List[str], root)
self.visible = True
self.__visible = visibility

# Likely to be updated by specs
self.metadata: JSONDict = dict()
Expand All @@ -47,22 +50,64 @@ def __init__(
if OMERO.matches(zarr):
self.specs.append(OMERO(self))

@property
def visible(self) -> bool:
"""True if this node should be displayed by default.

An invisible node may have been requested by the instrument, by the
user, or by the ome_zarr library after determining that this node
is lower priority, e.g. to prevent too many nodes from being shown
at once.
"""
return self.__visible

@visible.setter
def visible(self, visibility: bool) -> bool:
"""
Set the visibility for this node, returning the previous value.

A change of the visibility will propagate to all subnodes.
"""
old = self.__visible
if old != visibility:
self.__visible = visibility
for node in self.pre_nodes + self.post_nodes:
node.visible = visibility
return old

def load(self, spec_type: Type["Spec"]) -> Optional["Spec"]:
for spec in self.specs:
if isinstance(spec, spec_type):
return spec
return None

def add(self, zarr: BaseZarrLocation, prepend: bool = False,) -> "Optional[Node]":
"""Create a child node if this location has not yet been seen; otherwise return
None."""
def add(
self,
zarr: BaseZarrLocation,
prepend: bool = False,
visibility: Optional[bool] = None,
) -> "Optional[Node]":
"""Create a child node if this location has not yet been seen.

Returns None if the node has already been processed.

By setting prepend, the addition will be considered as higher priority
node which will likely be turned into a lower layer for display.

By unsetting visible, the node (and in turn the layer) can be
deactivated for initial display. By default, the value of the current
Copy link
Member

Choose a reason for hiding this comment

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

Slightly confused: "by default" suggests a means of override but I am not seeing what that is.

Copy link
Member Author

Choose a reason for hiding this comment

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

Passing a value would override the None. I'm pushing a commit to rework the wording. Let me know if you think it's better.

node will be propagated.
"""

if zarr.zarr_path in self.seen:
LOGGER.debug(f"already seen {zarr}; stopping recursion")
return None

if visibility is None:
visibility = self.visible

self.seen.append(zarr.zarr_path)
node = Node(zarr, self)
node = Node(zarr, self, visibility=visibility)
if prepend:
self.pre_nodes.append(node)
else:
Expand All @@ -80,6 +125,8 @@ def __repr__(self) -> str:
suffix += " [zgroup]"
if self.zarr.zarray:
suffix += " [zarray]"
if not self.visible:
suffix += " (hidden)"
return f"{self.zarr.zarr_path}{suffix}"


Expand Down Expand Up @@ -137,7 +184,6 @@ def matches(zarr: BaseZarrLocation) -> bool:

def __init__(self, node: Node) -> None:
super().__init__(node)
node.visible = True

image = self.lookup("image", {}).get("array", None)
parent_zarr = None
Expand All @@ -146,9 +192,7 @@ def __init__(self, node: Node) -> None:
parent_zarr = self.zarr.create(image)
if parent_zarr.exists():
LOGGER.debug(f"delegating to parent image: {parent_zarr}")
parent_node = node.add(parent_zarr, prepend=True)
if parent_node is not None:
node.visible = False
node.add(parent_zarr, prepend=True, visibility=False)
else:
parent_zarr = None
if parent_zarr is None:
Expand All @@ -174,7 +218,7 @@ def __init__(self, node: Node) -> None:
name = self.zarr.zarr_path.split("/")[-1]
node.metadata.update(
{
"visible": False,
"visible": node.visible,
"name": name,
"color": colors,
"metadata": {"image": self.lookup("image", {}), "path": name},
Expand Down Expand Up @@ -219,7 +263,7 @@ def __init__(self, node: Node) -> None:
# Load possible node data
child_zarr = self.zarr.create("labels")
if child_zarr.exists():
node.add(child_zarr)
node.add(child_zarr, visibility=False)


class OMERO(Spec):
Expand Down Expand Up @@ -269,7 +313,7 @@ def __init__(self, node: Node) -> None:

visible = ch.get("active", None)
if visible is not None:
visibles[idx] = visible
visibles[idx] = visible and node.visible

window = ch.get("window", None)
if window is not None:
Expand Down Expand Up @@ -311,7 +355,6 @@ def __call__(self) -> Iterator[Node]:

# TODO: API thoughts for the Spec type
# - ask for recursion or not
# - ask for visible or invisible (?)
# - ask for "provides data", "overrides data"

elif self.zarr.zarray: # Nothing has matched
Expand Down
42 changes: 18 additions & 24 deletions tests/test_napari.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,41 @@ def initdir(self, tmpdir):
self.path = tmpdir.mkdir("data")
create_zarr(str(self.path), astronaut, "astronaut")

def assert_layer(self, layer_data):
data, metadata, layer_type = layer_data
if not data or not metadata:
assert False, f"unknown layer: {layer_data}"
assert layer_type in ("image", "labels")
return data, metadata, layer_type
def assert_layers(self, layers, visible_1, visible_2):
# TODO: check name

def test_image(self):
layers = napari_get_reader(str(self.path))()
assert len(layers) == 2
image, label = layers

data, metadata, layer_type = self.assert_layer(image)
assert 1 == metadata["channel_axis"]
assert ["Red", "Green", "Blue"] == metadata["name"]
assert [[0, 1], [0, 1], [0, 1]] == metadata["contrast_limits"]
assert [True, True, True] == metadata["visible"]
assert [[0, 1]] * 3 == metadata["contrast_limits"]
assert [visible_1] * 3 == metadata["visible"]

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

def assert_layer(self, layer_data):
data, metadata, layer_type = layer_data
if not data or not metadata:
assert False, f"unknown layer: {layer_data}"
assert layer_type in ("image", "labels")
return data, metadata, layer_type

def test_image(self):
layers = napari_get_reader(str(self.path))()
self.assert_layers(layers, True, False)

def test_labels(self):
filename = str(self.path.join("labels"))
layers = napari_get_reader(filename)()
assert layers
for layer_data in layers:
data, metadata, layer_type = self.assert_layer(layer_data)
self.assert_layers(layers, False, True)

def test_label(self):
filename = str(self.path.join("labels", "astronaut"))
layers = napari_get_reader(filename)()
assert layers
for layer_data in layers:
data, metadata, layer_type = self.assert_layer(layer_data)

def test_layers(self):
filename = str(self.path.join("labels", "astronaut"))
layers = napari_get_reader(filename)()
assert layers
# check order
# check name
# check visibility
self.assert_layers(layers, False, True)

def test_viewer(self, make_test_viewer):
"""example of testing the viewer."""
Expand Down