Skip to content

Commit 0f37222

Browse files
Opening 0D scalars (#102)
* regression test * test creating scalars from kerchunk refs * fix bug by removing faulty check * refcator to remove duplicated code path * use temporary directory * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a3dab6c commit 0f37222

File tree

5 files changed

+29
-47
lines changed

5 files changed

+29
-47
lines changed

virtualizarr/kerchunk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def parse_array_refs(
151151
arr_refs: KerchunkArrRefs,
152152
) -> Tuple[dict, ZArray, ZAttrs]:
153153
zarray = ZArray.from_kerchunk_refs(arr_refs.pop(".zarray"))
154-
zattrs = arr_refs.pop(".zattrs")
154+
zattrs = arr_refs.pop(".zattrs", {})
155155
chunk_dict = arr_refs
156156

157157
return chunk_dict, zarray, zattrs

virtualizarr/manifests/array.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import re
21
import warnings
32
from typing import Any, Tuple, Union
43

@@ -7,7 +6,7 @@
76
from ..kerchunk import KerchunkArrRefs
87
from ..zarr import ZArray
98
from .array_api import MANIFESTARRAY_HANDLED_ARRAY_FUNCTIONS
10-
from .manifest import _CHUNK_KEY, ChunkManifest
9+
from .manifest import ChunkManifest
1110

1211

1312
class ManifestArray:
@@ -54,30 +53,20 @@ def __init__(
5453
f"chunkmanifest arg must be of type ChunkManifest, but got type {type(chunkmanifest)}"
5554
)
5655

57-
# Check that the chunk grid implied by zarray info is consistent with shape implied by chunk keys in manifest
58-
if _zarray.shape_chunk_grid != _chunkmanifest.shape_chunk_grid:
59-
raise ValueError(
60-
f"Inconsistent chunk grid shape between zarray info and manifest: {_zarray.shape_chunk_grid} vs {_chunkmanifest.shape_chunk_grid}"
61-
)
62-
6356
self._zarray = _zarray
6457
self._manifest = _chunkmanifest
6558

6659
@classmethod
6760
def _from_kerchunk_refs(cls, arr_refs: KerchunkArrRefs) -> "ManifestArray":
68-
from virtualizarr.kerchunk import fully_decode_arr_refs
61+
from virtualizarr.kerchunk import fully_decode_arr_refs, parse_array_refs
6962

7063
decoded_arr_refs = fully_decode_arr_refs(arr_refs)
7164

72-
zarray = ZArray.from_kerchunk_refs(decoded_arr_refs[".zarray"])
73-
74-
kerchunk_chunk_dict = {
75-
k: v for k, v in decoded_arr_refs.items() if re.match(_CHUNK_KEY, k)
76-
}
77-
chunkmanifest = ChunkManifest._from_kerchunk_chunk_dict(kerchunk_chunk_dict)
65+
chunk_dict, zarray, _zattrs = parse_array_refs(decoded_arr_refs)
66+
manifest = ChunkManifest._from_kerchunk_chunk_dict(chunk_dict)
7867

7968
obj = object.__new__(cls)
80-
obj._manifest = chunkmanifest
69+
obj._manifest = manifest
8170
obj._zarray = zarray
8271

8372
return obj
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import xarray as xr
2+
3+
from virtualizarr import open_virtual_dataset
4+
5+
6+
def test_open_scalar_variable(tmpdir):
7+
# regression test for GH issue #100
8+
9+
ds = xr.Dataset(data_vars={"a": 0})
10+
ds.to_netcdf(f"{tmpdir}/scalar.nc")
11+
12+
vds = open_virtual_dataset(f"{tmpdir}/scalar.nc")
13+
assert vds["a"].shape == ()

virtualizarr/tests/test_manifests/test_array.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,6 @@ def test_create_manifestarray(self):
3434
assert marr.size == 5 * 2 * 20
3535
assert marr.ndim == 3
3636

37-
def test_create_invalid_manifestarray(self):
38-
chunks_dict = {
39-
"0.0.0": {"path": "foo.nc", "offset": 100, "length": 100},
40-
}
41-
manifest = ChunkManifest(entries=chunks_dict)
42-
chunks = (5, 1, 10)
43-
shape = (5, 2, 20)
44-
zarray = ZArray(
45-
chunks=chunks,
46-
compressor="zlib",
47-
dtype=np.dtype("int32"),
48-
fill_value=0.0,
49-
filters=None,
50-
order="C",
51-
shape=shape,
52-
zarr_format=2,
53-
)
54-
55-
with pytest.raises(ValueError, match="Inconsistent chunk grid shape"):
56-
ManifestArray(zarray=zarray, chunkmanifest=manifest)
57-
5837
def test_create_manifestarray_from_kerchunk_refs(self):
5938
arr_refs = {
6039
".zarray": '{"chunks":[2,3],"compressor":null,"dtype":"<i8","fill_value":null,"filters":null,"order":"C","shape":[2,3],"zarr_format":2}',
@@ -70,6 +49,16 @@ def test_create_manifestarray_from_kerchunk_refs(self):
7049
assert marr.zarray.filters is None
7150
assert marr.zarray.order == "C"
7251

52+
def test_create_scalar_manifestarray_from_kerchunk_refs(self):
53+
arr_refs = {
54+
".zarray": '{"chunks":[],"compressor":null,"dtype":"<i8","fill_value":null,"filters":null,"order":"C","shape":[],"zarr_format":2}',
55+
"0": ["test1.nc", 6144, 48],
56+
}
57+
marr = ManifestArray._from_kerchunk_refs(arr_refs)
58+
59+
assert marr.shape == ()
60+
assert marr.chunks == ()
61+
7362

7463
class TestEquals:
7564
def test_equals(self):

virtualizarr/zarr.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,6 @@ def codec(self) -> Codec:
7373
"""For comparison against other arrays."""
7474
return Codec(compressor=self.compressor, filters=self.filters)
7575

76-
@property
77-
def shape_chunk_grid(self) -> Tuple[int, ...]:
78-
"""Shape of the chunk grid implied by the array shape and chunk shape."""
79-
chunk_grid_shape = tuple(
80-
ceildiv(array_side_length, chunk_length)
81-
for array_side_length, chunk_length in zip(self.shape, self.chunks)
82-
)
83-
return chunk_grid_shape
84-
8576
def __repr__(self) -> str:
8677
return f"ZArray(shape={self.shape}, chunks={self.chunks}, dtype={self.dtype}, compressor={self.compressor}, filters={self.filters}, fill_value={self.fill_value})"
8778

0 commit comments

Comments
 (0)