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 4 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"),
)
keewis marked this conversation as resolved.
Show resolved Hide resolved

@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
3 changes: 3 additions & 0 deletions virtualizarr/readers/kerchunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ def variable_from_kerchunk_refs(
if chunk_dict:
manifest = ChunkManifest._from_kerchunk_chunk_dict(chunk_dict)
varr = virtual_array_class(zarray=zarray, chunkmanifest=manifest)
elif len(zarray.shape) != 0:
keewis marked this conversation as resolved.
Show resolved Hide resolved
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
21 changes: 21 additions & 0 deletions virtualizarr/tests/test_readers/test_kerchunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,24 @@ def test_dataset_from_df_refs_with_filters():
ds = dataset_from_kerchunk_refs(ds_refs)
da = ds["a"]
assert da.data.zarray.filters == filters


def test_empty_netcdf4():
zarray = {
"chunks": [50, 100],
"compressor": None,
"dtype": "<i8",
"fill_value": 100,
"filters": None,
"order": "C",
"shape": [100, 200],
"zarr_format": 2,
}
refs = gen_ds_refs(zarray=ujson.dumps(zarray))
del refs["refs"]["a/0.0"]

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