Skip to content

Commit

Permalink
Fix format.matches for v0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
will-moore committed Jun 6, 2024
1 parent 8a9d640 commit 767b642
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
21 changes: 20 additions & 1 deletion ome_zarr/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

LOGGER = logging.getLogger("ome_zarr.format")

NGFF_URL_0_5 = "https://ngff.openmicroscopy.org/0.5"


def format_from_version(version: str) -> "Format":
for fmt in format_implementations():
Expand All @@ -24,6 +26,7 @@ def format_implementations() -> Iterator["Format"]:
"""
Return an instance of each format implementation, newest to oldest.
"""
yield FormatV05()
yield FormatV04()
yield FormatV03()
yield FormatV02()
Expand Down Expand Up @@ -353,7 +356,7 @@ def version(self) -> str:

@property
def version_key(self) -> str:
return "https://ngff.openmicroscopy.org/0.5"
return NGFF_URL_0_5

def init_store(self, path: str, mode: str = "r") -> FSStore:
"""
Expand All @@ -376,5 +379,21 @@ def init_store(self, path: str, mode: str = "r") -> FSStore:
print("Created v0.5 store %s(%s, %s, %s)" % (cls, path, mode, kwargs))
return store

def matches(self, metadata: dict) -> bool:
"""Version 0.5+ defined by version_key (URL)"""
version = self._get_metadata_version(metadata)
LOGGER.debug("%s matches %s?", self.version, version)
return version == self.version_key

def _get_metadata_version(self, metadata: dict) -> Optional[str]:
"""
For version 0.5+ we use the NGFF_URL_0_5 key
Returns the version of the first object found in the metadata,
checking for 'multiscales', 'plate', 'well' etc
"""
if self.version_key in metadata.get("attributes", {}):
return self.version_key


CurrentFormat = FormatV05
8 changes: 3 additions & 5 deletions ome_zarr/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(
# loader = CurrentFormat()

self.__init_metadata()
print("init self.__metadata", self.__metadata)
print("ZarrLocation init self.__metadata", self.__metadata)
detected = detect_format(self.__metadata, loader)
LOGGER.debug("ZarrLocation.__init__ %s detected: %s", self.__path, detected)
if detected != self.__fmt:
Expand Down Expand Up @@ -110,20 +110,18 @@ def __init_metadata(self) -> None:
# NB: zarr_format not supported in Group.open() or Array.open() yet
# We want to use zarr_format=None to handle v2 or v3
zarr_group = Group.open(self.__store) #, zarr_format=None)
self.zgroup: JSONDict = zarr_group.metadata.to_dict()
self.zgroup = zarr_group.metadata.to_dict()
self.__metadata = self.zgroup
except FileNotFoundError:
# group doesn't exist yet, try array
try:
zarr_array = Array.open(self.__store) #, zarr_format=None)
self.zarray: JSONDict = zarr_array.metadata.to_dict()
self.zarray = zarr_array.metadata.to_dict()
self.__metadata = self.zarray
except (ValueError, KeyError, FileNotFoundError):
# exceptions raised may change here?
print("__init_metadata DOESN'T EXIST", self.__store)
self.__exists = False

print("__init_metadat EXISTS?", self.__exists, self.__metadata)
# self.zarray: JSONDict = await self.get_json(".zarray")
# self.zgroup: JSONDict = await self.get_json(".zgroup")
# v3_json = await self.get_json("zarr.json")
Expand Down
10 changes: 9 additions & 1 deletion ome_zarr/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np

from .axes import Axes
from .format import format_from_version
from .format import format_from_version, NGFF_URL_0_5
from .io import ZarrLocation
from .types import JSONDict

Expand Down Expand Up @@ -176,6 +176,11 @@ def __init__(self, node: Node) -> None:
LOGGER.debug(v)

def lookup(self, key: str, default: Any) -> Any:

# Handle zarr V3 where everything is under "attributes"
if NGFF_URL_0_5 in self.zarr.root_attrs.get("attributes", {}):
return self.zarr.root_attrs.get("attributes", {}).get(NGFF_URL_0_5, {}).get(key, default)

return self.zarr.root_attrs.get(key, default)


Expand Down Expand Up @@ -270,9 +275,12 @@ class Multiscales(Spec):
@staticmethod
def matches(zarr: ZarrLocation) -> bool:
"""is multiscales metadata present?"""

if zarr.zgroup:
if "multiscales" in zarr.root_attrs:
return True
if "multiscales" in (zarr.root_attrs.get("attributes", {}).get(NGFF_URL_0_5, {})):
return True
return False

def __init__(self, node: Node) -> None:
Expand Down

0 comments on commit 767b642

Please sign in to comment.