Skip to content

Commit 750ca02

Browse files
committed
Merge remote-tracking branch 'origin/fix-zarr-check' into fix-zarr-check
2 parents d12bb1c + 4beb04a commit 750ca02

3 files changed

Lines changed: 15 additions & 38 deletions

File tree

tests/test_wsireader.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from tiatoolbox.annotation import SQLiteStore
3535
from tiatoolbox.utils import imread, tiff_to_fsspec
3636
from tiatoolbox.utils.exceptions import FileNotSupportedError
37-
from tiatoolbox.utils.magic import is_sqlite3
3837
from tiatoolbox.utils.transforms import imresize, locsize2bounds
3938
from tiatoolbox.utils.visualization import AnnotationRenderer
4039
from tiatoolbox.wsicore import WSIReader, wsireader
@@ -2219,22 +2218,6 @@ def test_is_ngff_regular_zarr(track_tmp_path: Path) -> None:
22192218
WSIReader.open(zarr_path)
22202219

22212220

2222-
def test_is_ngff_sqlite3(track_tmp_path: Path, remote_sample: Callable) -> None:
2223-
"""Test is_ngff is false for a sqlite3 file.
2224-
2225-
Copies the ngff-1 sample to a sqlite3 file and checks that it is
2226-
identified as an ngff file.
2227-
2228-
"""
2229-
ngff_path = remote_sample("ngff-1")
2230-
source = zarr.storage.LocalStore(ngff_path)
2231-
dest = zarr.SQLiteStore(track_tmp_path / "ngff.sqlite3")
2232-
# Copy the store to a sqlite3 file
2233-
zarr.copy_store(source, dest)
2234-
2235-
assert is_sqlite3(dest.path)
2236-
2237-
22382221
def test_store_reader_no_info(track_tmp_path: Path) -> None:
22392222
"""Test AnnotationStoreReader with no info."""
22402223
SQLiteStore(track_tmp_path / "store.db")
@@ -2319,16 +2302,6 @@ def test_store_reader_info_from_base(
23192302
assert store_reader.info.mpp[0] == wsi_reader.info.mpp[0]
23202303

23212304

2322-
def test_ngff_sqlitestore(track_tmp_path: Path, remote_sample: Callable) -> None:
2323-
"""Test SQLiteStore with an NGFF file."""
2324-
ngff_path = remote_sample("ngff-1")
2325-
source = zarr.storage.LocalStore(ngff_path)
2326-
dest = zarr.SQLiteStore(track_tmp_path / "ngff.sqlite3")
2327-
# Copy the store to a sqlite3 file
2328-
zarr.copy_store(source, dest)
2329-
wsireader.NGFFWSIReader(dest.path)
2330-
2331-
23322305
def test_ngff_zattrs_non_micrometer_scale_mpp(
23332306
track_tmp_path: Path,
23342307
remote_sample: Callable,

tiatoolbox/wsicore/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
__all__ = [
1212
"WSIMeta",
1313
"WSIReader",
14+
"WSIReaderParams",
1415
]
1516

1617

@@ -20,3 +21,4 @@ class WSIReaderParams(TypedDict, total=False):
2021
meta: WSIMeta | None
2122
mpp: tuple[Number, Number] | Number
2223
power: Number
24+
storage_options: dict # For FsspecStore

tiatoolbox/wsicore/wsireader.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from datetime import UTC, datetime
1313
from numbers import Number
1414
from pathlib import Path
15-
from typing import TYPE_CHECKING
15+
from typing import TYPE_CHECKING, Unpack
1616

1717
import cv2
1818
import fsspec
@@ -31,14 +31,13 @@
3131
from PIL import Image
3232
from tifffile import TiffPages
3333
from zarr.experimental.cache_store import CacheStore
34-
from zarr.storage import MemoryStore
34+
from zarr.storage import FsspecStore, MemoryStore
3535

3636
from tiatoolbox import logger, utils
3737
from tiatoolbox.annotation import AnnotationStore, SQLiteStore
3838
from tiatoolbox.utils import postproc_defs
3939
from tiatoolbox.utils.env_detection import pixman_warning
4040
from tiatoolbox.utils.exceptions import FileNotSupportedError
41-
from tiatoolbox.utils.magic import is_sqlite3
4241
from tiatoolbox.utils.visualization import AnnotationRenderer
4342
from tiatoolbox.wsicore.wsimeta import WSIMeta
4443

@@ -55,6 +54,7 @@
5554
Resolution,
5655
Units,
5756
)
57+
from tiatoolbox.wsicore import WSIReaderParams
5858
from tiatoolbox.wsicore.metadata.ngff import Multiscales
5959

6060
pixman_warning()
@@ -146,9 +146,8 @@ def is_ngff( # noqa: PLR0911
146146
147147
"""
148148
path = Path(path)
149-
store = zarr.SQLiteStore(str(path)) if path.is_file() and is_sqlite3(path) else path
150149
try:
151-
zarr_group = zarr.open(store, mode="r")
150+
zarr_group = zarr.open(path, mode="r")
152151
except Exception: # skipcq: PYL-W0703 # noqa: BLE001
153152
return False
154153
if not isinstance(zarr_group, zarr.Group):
@@ -347,7 +346,7 @@ def open(
347346
mpp: tuple[Number, Number] | None = None,
348347
power: Number | None = None,
349348
post_proc: str | callable | None = "auto",
350-
**kwargs: dict,
349+
**kwargs: Unpack[WSIReaderParams],
351350
) -> WSIReader:
352351
"""Return an appropriate :class:`.WSIReader` object.
353352
@@ -482,7 +481,7 @@ def _handle_special_cases(
482481
mpp: tuple[Number, Number] | None = None,
483482
power: Number | None = None,
484483
post_proc: str | callable | None = "auto",
485-
**kwargs: dict,
484+
**kwargs: Unpack[WSIReaderParams],
486485
) -> WSIReader | None:
487486
"""Handle special cases for selecting the appropriate WSIReader.
488487
@@ -5744,15 +5743,18 @@ class NGFFWSIReader(WSIReader):
57445743
57455744
"""
57465745

5747-
def __init__(self: NGFFWSIReader, path: str | Path, **kwargs: dict) -> None:
5746+
def __init__(
5747+
self: NGFFWSIReader, path: str | Path, **kwargs: Unpack[WSIReaderParams]
5748+
) -> None:
57485749
"""Initialize :class:`NGFFWSIReader`."""
57495750
super().__init__(path, **kwargs)
57505751
from imagecodecs import numcodecs # noqa: PLC0415
57515752

57525753
from tiatoolbox.wsicore.metadata import ngff # noqa: PLC0415
57535754

57545755
numcodecs.register_codecs()
5755-
store = zarr.SQLiteStore(path) if is_sqlite3(path) else path
5756+
storage_options = kwargs.get("storage_options", {})
5757+
store = FsspecStore.from_url(path, storage_options=storage_options)
57565758
self._zarr_group: zarr.Group = zarr.open(store, mode="r")
57575759
attrs = self._zarr_group.attrs
57585760
multiscales = attrs["multiscales"][0]
@@ -5815,7 +5817,7 @@ def _info(self: NGFFWSIReader) -> WSIMeta:
58155817
array.shape[:2][::-1]
58165818
for _, array in sorted(self._zarr_group.arrays(), key=lambda x: x[0])
58175819
],
5818-
slide_dimensions=self._zarr_group[0].shape[:2][::-1],
5820+
slide_dimensions=self._zarr_group["0"].shape[:2][::-1],
58195821
vendor=self.zattrs._creator.name, # skipcq: PYL-W0212 # noqa: SLF001
58205822
raw=self._zarr_group.attrs,
58215823
mpp=mpp,
@@ -6319,7 +6321,7 @@ def __init__(
63196321
renderer: AnnotationRenderer | None = None,
63206322
base_wsi: WSIReader | str | None = None,
63216323
alpha: float = 1.0,
6322-
**kwargs: dict,
6324+
**kwargs: Unpack[WSIReaderParams],
63236325
) -> None:
63246326
"""Initialize :class:`AnnotationStoreReader`."""
63256327
super().__init__(store, **kwargs)

0 commit comments

Comments
 (0)