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

allow creating references for empty archival datasets #260

Merged
merged 22 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
12 changes: 12 additions & 0 deletions virtualizarr/manifests/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ def __init__(self, entries: dict) -> None:
self._offsets = offsets
self._lengths = lengths

@classmethod
def empty(cls) -> "ChunkManifest":
"""Create an empty chunk manifest."""
return cls.from_arrays(
paths=np.array((), dtype=np.dtypes.StringDType),
offsets=np.array((), dtype="uint64"),
lengths=np.array((), dtype="uint64"),
)

@classmethod
def from_arrays(
cls,
Expand Down Expand Up @@ -257,6 +266,9 @@ def dict(self) -> ChunkDict: # type: ignore[override]
Entries whose path is an empty string will be interpreted as missing chunks and omitted from the dictionary.
"""

if len(self) == 0:
return cast(ChunkDict, {})

coord_vectors = np.mgrid[
tuple(slice(None, length) for length in self.shape_chunk_grid)
]
Expand Down
6 changes: 2 additions & 4 deletions virtualizarr/readers/kerchunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,8 @@ def variable_from_kerchunk_refs(
manifest = ChunkManifest._from_kerchunk_chunk_dict(chunk_dict)
varr = virtual_array_class(zarray=zarray, chunkmanifest=manifest)
elif len(zarray.shape) != 0:
raise ValueError(
f"variable {var_name} appears to be broken:"
" it is supposed to have at least one chunk, but none can be found."
)
manifest = ChunkManifest.empty()
varr = virtual_array_class(zarray=zarray, chunkmanifest=manifest)
else:
# This means we encountered a scalar variable of dimension 0,
# very likely that it actually has no numeric value and its only purpose
Expand Down
8 changes: 5 additions & 3 deletions virtualizarr/tests/test_readers/test_kerchunk.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import pytest
import ujson # type: ignore

from virtualizarr.manifests import ManifestArray
Expand Down Expand Up @@ -78,5 +77,8 @@ def test_empty_netcdf4():
refs = gen_ds_refs(zarray=ujson.dumps(zarray))
del refs["refs"]["a/0.0"]

with pytest.raises(ValueError, match="variable a appears to be broken"):
dataset_from_kerchunk_refs(refs)
ds = dataset_from_kerchunk_refs(refs)
assert "a" in ds.variables
assert isinstance(ds["a"].data, ManifestArray)
assert ds["a"].sizes == {"x": 100, "y": 200}
assert ds["a"].chunksizes == {"x": 50, "y": 100}
29 changes: 29 additions & 0 deletions virtualizarr/tests/test_writers/test_kerchunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@ def test_accessor_to_kerchunk_dict(self):
result_ds_refs = ds.virtualize.to_kerchunk(format="dict")
assert result_ds_refs == expected_ds_refs

def test_accessor_to_kerchunk_dict_empty(self):
manifest = ChunkManifest.empty()
arr = ManifestArray(
chunkmanifest=manifest,
zarray=dict(
shape=(2, 3),
dtype=np.dtype("<i8"),
chunks=(2, 3),
compressor=None,
filters=None,
fill_value=np.nan,
order="C",
),
)
ds = Dataset({"a": (["x", "y"], arr)})

expected_ds_refs = {
"version": 1,
"refs": {
".zgroup": '{"zarr_format":2}',
".zattrs": "{}",
"a/.zarray": '{"shape":[2,3],"chunks":[2,3],"dtype":"<i8","fill_value":null,"order":"C","compressor":null,"filters":null,"zarr_format":2}',
"a/.zattrs": '{"_ARRAY_DIMENSIONS":["x","y"]}',
},
}

result_ds_refs = ds.virtualize.to_kerchunk(format="dict")
assert result_ds_refs == expected_ds_refs

def test_accessor_to_kerchunk_json(self, tmp_path):
manifest = ChunkManifest(
entries={"0.0": dict(path="test.nc", offset=6144, length=48)}
Expand Down
Loading