Skip to content

Commit

Permalink
tiffslide._zarr: path zarr.storage.KVStore if it's missing __contains__
Browse files Browse the repository at this point in the history
Closes #72
  • Loading branch information
ap-- committed Jul 10, 2023
1 parent a7ac150 commit b8b7393
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ install_requires =
fsspec!=2022.11.0,!=2023.1.0
pillow
tifffile>=2021.6.14
zarr
zarr>=2.11.0
typing_extensions>=4.0


Expand Down
37 changes: 24 additions & 13 deletions tiffslide/_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
from fsspec.implementations.reference import ReferenceFileSystem
from tifffile import TiffFile
from tifffile import ZarrTiffStore
from zarr.storage import BaseStore
from zarr.storage import FSStore
from zarr.storage import KVStore as _KVStore

from tiffslide._compat import NotTiffFile
from tiffslide._types import Point3D
Expand All @@ -26,11 +28,6 @@
from numpy.typing import DTypeLike
from numpy.typing import NDArray

try:
from zarr.storage import KVStore
except ImportError:
KVStore = lambda x: x # noqa


__all__ = [
"get_zarr_store",
Expand All @@ -41,6 +38,16 @@

# --- zarr storage classes --------------------------------------------

if "__contains__" not in _KVStore.__dict__:
# fix missing contains caused double decode:
# https://github.com/bayer-science-for-a-better-life/tiffslide/issues/72#issuecomment-1627918238
class KVStore(_KVStore):
def __contains__(self, item: str) -> bool:
return item in self._mutable_mapping

else:
KVStore = _KVStore # type: ignore


class _CompositedStore(Mapping[str, Any]):
"""prefix zarr stores to allow mounting them in groups"""
Expand Down Expand Up @@ -118,7 +125,7 @@ def get_zarr_store(
tf: TiffFile | ReferenceFileSystem | None,
*,
num_decode_threads: int | None = None,
) -> Mapping[str, Any]:
) -> BaseStore:
"""return a zarr store
Parameters
Expand All @@ -142,7 +149,7 @@ def get_zarr_store(
composition: SeriesCompositionInfo | None = properties.get(
"tiffslide.series-composition"
)
store: Mapping[str, Any]
store: BaseStore
if composition:
prefixed_stores = {}
for series_idx in composition["located_series"].keys():
Expand All @@ -154,17 +161,21 @@ def get_zarr_store(
_store = _CompositedStore({"0": _store})
prefixed_stores[str(series_idx)] = _store

store = _CompositedStore(prefixed_stores, zattrs=composition)
store = KVStore(store)
_store = _CompositedStore(prefixed_stores, zattrs=composition)
store = KVStore(_store)

else:
series_idx = properties.get("tiffslide.series-index", 0)
store = _get_series_zarr(tf, series_idx, num_decode_threads=num_decode_threads)
_store = _get_series_zarr(tf, series_idx, num_decode_threads=num_decode_threads)

# encapsulate store as group if tifffile returns a zarr array
if ".zarray" in store:
store = _CompositedStore({"0": store})
store = KVStore(store)
if ".zarray" in _store:
_store = _CompositedStore({"0": _store})
store = KVStore(_store)
elif isinstance(_store, BaseStore):
store = _store
else:
store = KVStore(_store)

return store

Expand Down

0 comments on commit b8b7393

Please sign in to comment.