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

Support init of ome_zarr_py.io.ZarrLocation with zarr.storage.FSStore #349

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[settings]
known_third_party = dask,numcodecs,numpy,pytest,scipy,setuptools,skimage,zarr
known_third_party = dask,fsspec,numcodecs,numpy,pytest,scipy,setuptools,skimage,zarr
multi_line_output = 3
include_trailing_comma = True
force_grid_wrap = 0
Expand Down
11 changes: 9 additions & 2 deletions ome_zarr/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class ZarrLocation:
"""

def __init__(
self, path: Union[Path, str], mode: str = "r", fmt: Format = CurrentFormat()
self,
path: Union[Path, str, FSStore],
mode: str = "r",
fmt: Format = CurrentFormat(),
) -> None:
LOGGER.debug("ZarrLocation.__init__ path: %s, fmt: %s", path, fmt.version)
self.__fmt = fmt
Expand All @@ -37,13 +40,17 @@ def __init__(
self.__path = str(path.resolve())
elif isinstance(path, str):
self.__path = path
elif isinstance(path, FSStore):
self.__path = path.path
else:
raise TypeError(f"not expecting: {type(path)}")

loader = fmt
if loader is None:
loader = CurrentFormat()
self.__store = loader.init_store(self.__path, mode)
self.__store: FSStore = (
path if isinstance(path, FSStore) else loader.init_store(self.__path, mode)
)

self.__init_metadata()
detected = detect_format(self.__metadata, loader)
Expand Down
38 changes: 38 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from pathlib import Path

import fsspec
import pytest
import zarr

from ome_zarr.data import create_zarr
from ome_zarr.io import ZarrLocation, parse_url


class TestIO:
@pytest.fixture(autouse=True)
def initdir(self, tmpdir):
self.path = tmpdir.mkdir("data")
create_zarr(str(self.path))
self.store = parse_url(str(self.path), mode="w").store
self.root = zarr.group(store=self.store)

def test_parse_url(self):
assert parse_url(str(self.path))

def test_parse_nonexistent_url(self):
assert parse_url(self.path + "/does-not-exist") is None

def test_loc_str(self):
assert ZarrLocation(str(self.path))

def test_loc_path(self):
assert ZarrLocation(Path(self.path))

def test_loc_store(self):
assert ZarrLocation(self.store)

def test_loc_fs(self):
fs = fsspec.filesystem("memory")
fsstore = zarr.storage.FSStore(url="/", fs=fs)
loc = ZarrLocation(fsstore)
assert loc