@@ -571,12 +571,13 @@ def _set_meta(
571571 )
572572 example_image : ImageArray = self [
573573 self .metadata .multiscales [0 ].datasets [0 ].path
574- ]. channels
574+ ]
575575 self ._channel_names = list (range (example_image .channels ))
576576
577577 def _parse_meta (self ):
578- multiscales = self .zattrs .get ("multiscales" )
579- omero = self .zattrs .get ("omero" )
578+ attrs = self .zattrs .get ("ome" ) or self .zattrs
579+ multiscales = attrs .get ("multiscales" )
580+ omero = attrs .get ("omero" )
580581 if multiscales :
581582 try :
582583 self ._set_meta (multiscales = multiscales , omero = omero )
@@ -1925,6 +1926,49 @@ def rename_well(self, old: str, new: str):
19251926 self .dump_meta ()
19261927
19271928
1929+ def _check_file_mode (
1930+ store_path : Path ,
1931+ mode : Literal ["r" , "r+" , "a" , "w" , "w-" ],
1932+ disable_path_checking : bool ,
1933+ ) -> bool :
1934+ if mode == "a" :
1935+ mode = ("w-" , "r+" )[int (store_path .exists ())]
1936+ parse_meta = False
1937+ if mode in ("r" , "r+" ):
1938+ parse_meta = True
1939+ elif mode == "w-" :
1940+ if store_path .exists ():
1941+ raise FileExistsError (store_path )
1942+ elif mode == "w" :
1943+ if store_path .exists ():
1944+ if (
1945+ ".zarr" not in str (store_path .resolve ())
1946+ and not disable_path_checking
1947+ ):
1948+ raise ValueError (
1949+ "Cannot overwrite a path that does not contain '.zarr', "
1950+ "use `disable_path_checking=True` if you are sure that "
1951+ f"{ store_path } should be overwritten."
1952+ )
1953+ _logger .warning (f"Overwriting data at { store_path } " )
1954+ else :
1955+ raise ValueError (f"Invalid persistence mode '{ mode } '." )
1956+ return parse_meta
1957+
1958+
1959+ def _detect_layout (meta_keys : list [str ]) -> Literal ["fov" , "hcs" ]:
1960+ if "plate" in meta_keys :
1961+ return "hcs"
1962+ elif "multiscales" in meta_keys :
1963+ return "fov"
1964+ else :
1965+ raise KeyError (
1966+ "Dataset metadata keys ('plate'/'multiscales') not in "
1967+ f"the found store metadata keys: { meta_keys } . "
1968+ "Is this a valid OME-Zarr dataset?"
1969+ )
1970+
1971+
19281972def open_ome_zarr (
19291973 store_path : StrOrBytesPath | Path ,
19301974 layout : Literal ["auto" , "fov" , "hcs" , "tiled" ] = "auto" ,
@@ -1998,42 +2042,16 @@ def open_ome_zarr(
19982042 or :py:class:`iohub.ngff.TiledPosition`)
19992043 """
20002044 store_path = Path (store_path )
2001- if mode == "a" :
2002- mode = ("w-" , "r+" )[int (store_path .exists ())]
2003- parse_meta = False
2004- if mode in ("r" , "r+" ):
2005- parse_meta = True
2006- elif mode == "w-" :
2007- if store_path .exists ():
2008- raise FileExistsError (store_path )
2009- elif mode == "w" :
2010- if store_path .exists ():
2011- if (
2012- ".zarr" not in str (store_path .resolve ())
2013- and not disable_path_checking
2014- ):
2015- raise ValueError (
2016- "Cannot overwrite a path that does not contain '.zarr', "
2017- "use `disable_path_checking=True` if you are sure that "
2018- f"{ store_path } should be overwritten."
2019- )
2020- _logger .warning (f"Overwriting data at { store_path } " )
2021- else :
2022- raise ValueError (f"Invalid persistence mode '{ mode } '." )
2045+ parse_meta = _check_file_mode (
2046+ store_path , mode , disable_path_checking = disable_path_checking
2047+ )
20232048 root = _open_store (store_path , mode , version )
20242049 meta_keys = root .attrs .keys () if parse_meta else []
2050+ if "ome" in meta_keys :
2051+ meta_keys = root .attrs ["ome" ].keys ()
20252052 if layout == "auto" :
20262053 if parse_meta :
2027- if "plate" in meta_keys :
2028- layout = "hcs"
2029- elif "multiscales" in meta_keys :
2030- layout = "fov"
2031- else :
2032- raise KeyError (
2033- "Dataset metadata keys ('plate'/'multiscales') not in "
2034- f"the found store metadata keys: { meta_keys } . "
2035- "Is this a valid OME-Zarr dataset?"
2036- )
2054+ layout = _detect_layout (meta_keys )
20372055 else :
20382056 raise ValueError (
20392057 "Store layout must be specified when creating a new dataset."
@@ -2054,5 +2072,6 @@ def open_ome_zarr(
20542072 parse_meta = parse_meta ,
20552073 channel_names = channel_names ,
20562074 axes = axes ,
2075+ version = version ,
20572076 ** kwargs ,
20582077 )
0 commit comments