Skip to content

Commit 4e0454e

Browse files
fix some testing issues
1 parent 8d00be5 commit 4e0454e

File tree

6 files changed

+39
-27
lines changed

6 files changed

+39
-27
lines changed

src/ngio/common/_pyramid.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,10 @@ def init_empty_pyramid(
262262
**array_static_kwargs,
263263
)
264264

265-
_shape = [
265+
ref_shape = [
266266
math.floor(s / sc) for s, sc in zip(ref_shape, scaling_factors, strict=True)
267267
]
268-
ref_shape = _shape
269-
270-
if chunks == "auto":
271-
# We keep the same chunks as the first level
272-
chunks = new_arr.chunks
273-
else:
274-
# Adjust chunks to not be larger than the new shape
275-
chunks = tuple(min(c, s) for c, s in zip(chunks, ref_shape, strict=True))
268+
chunks = tuple(
269+
min(c, s) for c, s in zip(new_arr.chunks, ref_shape, strict=True)
270+
)
276271
return None

src/ngio/images/_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ def derive_image_container(
794794
dimension_separator = find_dimension_separator(ref_image.zarr_array)
795795

796796
if compressors is None:
797-
compressors = ref_image.zaxxr_array.compressors # type: ignore
797+
compressors = ref_image.zarr_array.compressors # type: ignore
798798

799799
handler = create_empty_image_container(
800800
store=store,

src/ngio/utils/_zarr_utils.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from filelock import BaseFileLock, FileLock
99
from zarr.abc.store import Store
1010
from zarr.core.array import CompressorLike
11-
from zarr.errors import ContainsGroupError, GroupNotFoundError
11+
from zarr.errors import ContainsGroupError
1212
from zarr.storage import FsspecStore, LocalStore, MemoryStore
1313

1414
from ngio.utils import NgioFileExistsError, NgioFileNotFoundError, NgioValueError
@@ -38,13 +38,12 @@ def _check_store(store) -> NgioSupportedStore:
3838

3939
def _check_group(group: zarr.Group, mode: AccessModeLiteral) -> zarr.Group:
4040
"""Check the group and return a valid group."""
41-
is_read_only = getattr(group, "_read_only", False)
42-
if is_read_only and mode in ["w", "w-"]:
41+
if group.read_only and mode in ["w", "w-"]:
4342
raise NgioValueError(
4443
"The group is read only. Cannot open in write mode ['w', 'w-']"
4544
)
4645

47-
if mode == "r" and not is_read_only:
46+
if mode == "r" and not group.read_only:
4847
# let's make sure we don't accidentally write to the group
4948
group = zarr.open_group(store=group.store, path=group.path, mode="r")
5049

@@ -75,14 +74,19 @@ def open_group_wrapper(
7574
_check_store(store)
7675
group = zarr.open_group(store=store, mode=mode, zarr_format=zarr_format)
7776

78-
except ContainsGroupError as e:
77+
except FileExistsError as e:
7978
raise NgioFileExistsError(
8079
f"A Zarr group already exists at {store}, consider setting overwrite=True."
8180
) from e
8281

83-
except GroupNotFoundError as e:
82+
except FileNotFoundError as e:
8483
raise NgioFileNotFoundError(f"No Zarr group found at {store}") from e
8584

85+
except ContainsGroupError as e:
86+
raise NgioFileExistsError(
87+
f"A Zarr group already exists at {store}, consider setting overwrite=True."
88+
) from e
89+
8690
return group
8791

8892

@@ -215,6 +219,19 @@ def remove_lock(self) -> None:
215219
@property
216220
def group(self) -> zarr.Group:
217221
"""Return the group."""
222+
if self._parallel_safe:
223+
# If we are parallel safe, we need to reopen the group
224+
# to make sure that the attributes are up to date
225+
if self.mode == "r":
226+
mode = "r"
227+
else:
228+
mode = "r+"
229+
return zarr.open_group(
230+
store=self._group.store,
231+
path=self._group.path,
232+
mode=mode,
233+
zarr_format=self._group.metadata.zarr_format,
234+
)
218235
return self._group
219236

220237
def add_to_cache(self, key: str, value: object) -> None:
@@ -246,8 +263,7 @@ def load_attrs(self) -> dict:
246263

247264
def _write_attrs(self, attrs: dict, overwrite: bool = False) -> None:
248265
"""Write the metadata to the store."""
249-
is_read_only = getattr(self._group, "_read_only", False)
250-
if is_read_only:
266+
if self.group.read_only:
251267
raise NgioValueError("The group is read only. Cannot write metadata.")
252268

253269
# we need to invalidate the current attrs cache

tests/unit/common/test_pyramid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ def test_on_disk_zooms(
2222
tmp_path: Path, order: InterpolationOrder, mode: Literal["dask", "numpy", "coarsen"]
2323
):
2424
source = tmp_path / "source.zarr"
25-
source_array = zarr.open_array(source, shape=(16, 128, 128), dtype="uint8")
25+
source_array = zarr.create_array(source, shape=(16, 128, 128), dtype="uint8")
2626

2727
target = tmp_path / "target.zarr"
28-
target_array = zarr.open_array(target, shape=(16, 64, 64), dtype="uint8")
28+
target_array = zarr.create_array(target, shape=(16, 64, 64), dtype="uint8")
2929

3030
on_disk_zoom(source_array, target_array, order=order, mode=mode)

tests/unit/tables/test_backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def test_parquet_backend(tmp_path: Path):
155155

156156
def test_anndata_backend(tmp_path: Path):
157157
store = tmp_path / "test_anndata_backend.zarr"
158-
handler = ZarrGroupHandler(store=store, cache=True, mode="a")
158+
handler = ZarrGroupHandler(store=store, cache=True, mode="a", zarr_format=2)
159159
backend = AnnDataBackend()
160160
backend.set_group_handler(handler, index_type="int")
161161

tests/unit/utils/test_zarr_utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import pytest
99
import zarr
10+
from zarr.storage import LocalStore
1011

1112
from ngio.utils import (
1213
NgioFileExistsError,
@@ -23,8 +24,8 @@ def test_group_handler_creation(tmp_path: Path, cache: bool):
2324
handler = ZarrGroupHandler(store=store, cache=cache, mode="a")
2425

2526
_store = handler.group.store
26-
assert isinstance(_store, zarr.DirectoryStore)
27-
assert Path(_store.path) == store
27+
assert isinstance(_store, LocalStore)
28+
assert Path(_store.root.as_posix()) == store
2829
assert handler.use_cache == cache
2930

3031
attrs = handler.load_attrs()
@@ -65,12 +66,12 @@ def test_group_handler_from_group(tmp_path: Path):
6566
def test_group_handler_read(tmp_path: Path):
6667
store = tmp_path / "test_group_handler_read.zarr"
6768

68-
group = zarr.group(store=store, overwrite=True)
69+
group = zarr.create_group(store=store, overwrite=True)
6970
input_attrs = {"a": 1, "b": 2, "c": 3}
7071
group.attrs.update(input_attrs)
7172

7273
group.create_group("group1")
73-
group.create_dataset("array1", shape=(10, 10), dtype="int32")
74+
group.create_array("array1", shape=(10, 10), dtype="int32")
7475

7576
handler = ZarrGroupHandler(store=store, cache=True, mode="r")
7677

@@ -97,10 +98,10 @@ def test_group_handler_read(tmp_path: Path):
9798

9899
def test_open_fail(tmp_path: Path):
99100
store = tmp_path / "test_open_fail.zarr"
100-
group = zarr.group(store=store, overwrite=True)
101+
group = zarr.create_group(store=store, overwrite=True)
101102

102103
read_only_group = open_group_wrapper(store=group, mode="r")
103-
assert read_only_group._read_only
104+
assert read_only_group.read_only
104105

105106
with pytest.raises(NgioFileExistsError):
106107
open_group_wrapper(store=store, mode="w-")

0 commit comments

Comments
 (0)