Skip to content

Commit

Permalink
Fix loading of PlateLabels
Browse files Browse the repository at this point in the history
  • Loading branch information
will-moore committed Jun 29, 2022
1 parent dbbd940 commit 8ab3ad6
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions ome_zarr/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,12 @@ def __init__(self, node: Node) -> None:
LOGGER.debug(f"Plate created with ZarrLocation fmt:{ self.zarr.fmt}")

self.first_field = "0"
self.plate_data = self.get_plate_zarr().root_attrs.get("plate", {})
# For Plate, plate_zarr is same as self.zarr, but for PlateLabels
# (node at /plate.zarr/labels) this is the parent at /plate.zarr node.
self.plate_zarr = self.get_plate_zarr()
self.plate_data = self.plate_zarr.root_attrs.get("plate", {})

LOGGER.info("plate_data: %s", self.plate_data)
print("Plate --- self.plate_data --> ", self.plate_data)
self.rows = self.plate_data.get("rows")
self.columns = self.plate_data.get("columns")
self.row_names = [row["name"] for row in self.rows]
Expand All @@ -483,11 +485,10 @@ def __init__(self, node: Node) -> None:
self.column_count = len(self.columns)

img_path = self.get_image_path(self.well_paths[0])
print("Plate.__init__ - img_path ------> ", img_path)
if not img_path:
# E.g. PlateLabels subclass has no Labels
return
image_zarr = self.get_plate_zarr().create(img_path)
image_zarr = self.plate_zarr.create(img_path)
# Create a Node for image, with no 'root'
self.first_well_image = Node(image_zarr, [])

Expand All @@ -496,7 +497,7 @@ def __init__(self, node: Node) -> None:
# Load possible node data
child_zarr = self.zarr.create("labels")
# This is a 'virtual' path to plate.zarr/labels
node.add(child_zarr, visibility=False)
node.add(child_zarr)

def get_plate_zarr(self) -> ZarrLocation:
return self.zarr
Expand Down Expand Up @@ -546,7 +547,7 @@ def get_tile(tile_name: str) -> np.ndarray:
LOGGER.debug(f"LOADING tile... {path} with shape: {tile_shape}")

try:
data = self.zarr.load(path)
data = self.plate_zarr.load(path)
except ValueError as e:
LOGGER.error(f"Failed to load {path}")
LOGGER.debug(f"{e}")
Expand All @@ -572,15 +573,13 @@ def get_tile(tile_name: str) -> np.ndarray:
class PlateLabels(Plate):
@staticmethod
def matches(zarr: ZarrLocation) -> bool:
print("PlateLabels matches", zarr.path)
# If the path ends in plate/labels...
if not zarr.path.endswith("labels"):
return False

# and the parent is a plate
path = zarr.path
parent_path = path[: path.rfind("/")]
print("parent_path", parent_path)
parent = zarr.create(parent_path)
return "plate" in parent.root_attrs

Expand Down Expand Up @@ -614,27 +613,22 @@ def get_plate_zarr(self) -> ZarrLocation:
path = self.zarr.path
# remove the /labels
parent_path = path[: path.rfind("/")]
print("get_plate_zarr parent_path", parent_path)
return self.zarr.create(parent_path)

def get_image_path(self, well_path: str) -> Optional[str]:
"""Returns path to .zattr for Well labels, e.g. /A/1/0/labels/my_cells/"""
labels_attrs = self.well_labels_zattrs.get(well_path)
print("PlateLabels get_image_path well_path", well_path)
if labels_attrs is None:
# if not cached, load...
path = f"{well_path}/{self.first_field}/labels/"
LOGGER.info("loading labels/.zattrs: %s.zattrs", path)
# print("labels_path", path)
plate_zarr = self.get_plate_zarr()
# print("check plate_zarr", plate_zarr.root_attrs.get("plate", {}))
first_field_labels = plate_zarr.create(path)
# print("first_field_labels zarr", first_field_labels)
# loads labels/.zattrs when new ZarrLocation is created
labels_attrs = first_field_labels.root_attrs
# print("labels_attrs", labels_attrs)
self.well_labels_zattrs[well_path] = labels_attrs
label_paths = labels_attrs.get("labels", [])
LOGGER.debug("label_paths: %s", label_paths)
if len(label_paths) > 0:
return f"{well_path}/{self.first_field}/labels/{label_paths[0]}/"
return None
Expand Down

0 comments on commit 8ab3ad6

Please sign in to comment.