Skip to content

Commit 53aa0dc

Browse files
authored
Merge pull request #32 from braingram/to_internal_tests
To internal tests
2 parents 20741ff + de61df1 commit 53aa0dc

File tree

4 files changed

+56
-26
lines changed

4 files changed

+56
-26
lines changed

asdf_zarr/converter.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ def to_yaml_tree(self, obj, tag, ctx):
3232
chunk_key_block_index_map = {}
3333
for chunk_key in storage._iter_chunk_keys(obj, only_initialized=True):
3434
data_callback = storage._generate_chunk_data_callback(obj, chunk_key)
35-
asdf_key = chunk_store._chunk_asdf_keys.get(chunk_key, asdf.util.BlockKey())
36-
block_index = ctx.find_block_index(asdf_key, data_callback)
35+
asdf_key = chunk_store._chunk_asdf_keys.get(chunk_key, ctx.generate_block_key())
36+
block_index = ctx.find_available_block_index(data_callback, asdf_key)
3737
chunk_key_block_index_map[chunk_key] = block_index
3838
asdf_key = chunk_store._chunk_block_map_asdf_key
3939
if asdf_key is None:
40-
asdf_key = asdf.util.BlockKey()
41-
obj_dict["chunk_block_map"] = ctx.find_block_index(
42-
asdf_key, storage._generate_chunk_map_callback(obj, chunk_key_block_index_map)
40+
asdf_key = ctx.generate_block_key()
41+
obj_dict["chunk_block_map"] = ctx.find_available_block_index(
42+
storage._generate_chunk_map_callback(obj, chunk_key_block_index_map), asdf_key
4343
)
4444
return obj_dict
4545

@@ -75,7 +75,7 @@ def from_yaml_tree(self, node, tag, ctx):
7575
return obj
7676

7777
chunk_store = util.decode_storage(node["store"])
78-
if "meta" in node:
78+
if "meta_store" in node:
7979
# separate meta and chunk stores
8080
store = util.decode_storage(node["meta_store"])
8181
else:

asdf_zarr/storage.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import json
33
import math
44

5-
import asdf
65
import numpy
76
import zarr
87

@@ -61,7 +60,7 @@ def to_internal(zarray):
6160
return zarray
6261
# make a new internal store based off an existing store
6362
internal_store = ConvertedInternalStore(zarray.chunk_store or zarray.store)
64-
return zarr.open(internal_store)
63+
return zarr.open(zarray.store, chunk_store=internal_store)
6564

6665

6766
class InternalStore(zarr.storage.Store):
@@ -139,11 +138,10 @@ def __init__(self, ctx, chunk_block_map_index, zarray_meta):
139138
# split into 4 chunks) the chunk_block_map will be
140139
# 4 x 5
141140
cdata_shape = tuple(math.ceil(s / c) for s, c in zip(zarray_meta["shape"], zarray_meta["chunks"]))
141+
self._chunk_block_map_asdf_key = ctx.generate_block_key()
142142
self._chunk_block_map = numpy.frombuffer(
143-
ctx.get_block_data_callback(chunk_block_map_index)(), dtype="int32"
143+
ctx.get_block_data_callback(chunk_block_map_index, self._chunk_block_map_asdf_key)(), dtype="int32"
144144
).reshape(cdata_shape)
145-
self._chunk_block_map_asdf_key = asdf.util.BlockKey()
146-
ctx.assign_block_key(chunk_block_map_index, self._chunk_block_map_asdf_key)
147145

148146
self._chunk_block_map_asdf_key = None
149147

@@ -154,10 +152,9 @@ def __init__(self, ctx, chunk_block_map_index, zarray_meta):
154152
coord = tuple(coord)
155153
block_index = int(self._chunk_block_map[coord])
156154
chunk_key = self._sep.join((str(c) for c in tuple(coord)))
157-
asdf_key = asdf.util.BlockKey()
155+
asdf_key = ctx.generate_block_key()
158156
self._chunk_asdf_keys[chunk_key] = asdf_key
159-
ctx.assign_block_key(block_index, asdf_key)
160-
self._chunk_callbacks[chunk_key] = ctx.get_block_data_callback(block_index)
157+
self._chunk_callbacks[chunk_key] = ctx.get_block_data_callback(block_index, asdf_key)
161158

162159
def _sep_key(self, key):
163160
if self._sep is None:

asdf_zarr/tests/test_zarr.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
import itertools
33

44
import asdf
5+
import asdf_zarr
6+
import asdf_zarr.storage
57
import numpy
68
import pytest
79
import zarr
8-
from zarr.storage import DirectoryStore, NestedDirectoryStore
10+
from zarr.storage import DirectoryStore, KVStore, MemoryStore, NestedDirectoryStore, TempStore
911

1012

11-
def create_zarray(shape=None, chunks=None, dtype="f8", store=None):
13+
def create_zarray(shape=None, chunks=None, dtype="f8", store=None, chunk_store=None):
1214
if shape is None:
1315
shape = (6, 9)
1416
if chunks is None:
1517
chunks = [max(1, d // 3) for d in shape]
16-
arr = zarr.creation.create((6, 9), store=store, chunks=chunks, dtype=dtype, compressor=None)
18+
arr = zarr.creation.create(
19+
(6, 9), store=store, chunk_store=chunk_store, chunks=chunks, dtype=dtype, compressor=None
20+
)
1721
for chunk_index in itertools.product(*[range(c) for c in arr.cdata_shape]):
1822
inds = []
1923
for i, c in zip(chunk_index, arr.chunks):
@@ -25,14 +29,37 @@ def create_zarray(shape=None, chunks=None, dtype="f8", store=None):
2529
@pytest.mark.parametrize("copy_arrays", [True, False])
2630
@pytest.mark.parametrize("lazy_load", [True, False])
2731
@pytest.mark.parametrize("compression", ["input", "zlib"])
28-
@pytest.mark.parametrize("store_type", [DirectoryStore, NestedDirectoryStore])
29-
def test_write_to(tmp_path, copy_arrays, lazy_load, compression, store_type):
30-
store1 = store_type(tmp_path / "zarr_array_1")
31-
store2 = store_type(tmp_path / "zarr_array_2")
32+
@pytest.mark.parametrize("store_type", [DirectoryStore, KVStore, MemoryStore, NestedDirectoryStore, TempStore])
33+
@pytest.mark.parametrize("to_internal", [True, False])
34+
@pytest.mark.parametrize("meta_store", [True, False])
35+
def test_write_to(tmp_path, copy_arrays, lazy_load, compression, store_type, to_internal, meta_store):
36+
if store_type in (DirectoryStore, NestedDirectoryStore):
37+
store1 = store_type(tmp_path / "zarr_array_1")
38+
store2 = store_type(tmp_path / "zarr_array_2")
39+
elif store_type is KVStore:
40+
store1 = store_type({})
41+
store2 = store_type({})
42+
else:
43+
store1 = store_type()
44+
store2 = store_type()
45+
46+
# should meta be in a different store?
47+
if meta_store:
48+
chunk_store1 = store1
49+
store1 = KVStore({})
50+
chunk_store2 = store2
51+
store2 = KVStore({})
52+
else:
53+
chunk_store1 = None
54+
chunk_store2 = None
55+
56+
arr1 = create_zarray(store=store1, chunk_store=chunk_store1)
57+
arr2 = create_zarray(store=store2, chunk_store=chunk_store2)
3258

33-
arr1 = create_zarray(store=store1)
34-
arr2 = create_zarray(store=store2)
3559
arr2[:] = arr2[:] * -2
60+
if to_internal:
61+
arr1 = asdf_zarr.storage.to_internal(arr1)
62+
arr2 = asdf_zarr.storage.to_internal(arr2)
3663
tree = {"arr1": arr1, "arr2": arr2}
3764

3865
fn = tmp_path / "test.asdf"
@@ -42,8 +69,10 @@ def test_write_to(tmp_path, copy_arrays, lazy_load, compression, store_type):
4269
with asdf.open(fn, mode="r", copy_arrays=copy_arrays, lazy_load=lazy_load) as af:
4370
for n, a in (("arr1", arr1), ("arr2", arr2)):
4471
assert isinstance(af[n], zarr.core.Array)
45-
# for these tests, data should not be converted to a different storage format
46-
assert isinstance(af[n].chunk_store, store_type)
72+
if to_internal or store_type in (KVStore, MemoryStore, TempStore):
73+
assert isinstance(af[n].chunk_store, asdf_zarr.storage.InternalStore)
74+
else:
75+
assert isinstance(af[n].chunk_store, store_type)
4776
assert numpy.allclose(af[n], a)
4877

4978

asdf_zarr/util.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import fsspec
44
import zarr.storage
5-
from zarr.storage import DirectoryStore, FSStore, NestedDirectoryStore, TempStore
5+
from zarr.storage import DirectoryStore, FSStore, KVStore, NestedDirectoryStore, TempStore
66

77

88
def encode_storage(store):
@@ -33,6 +33,8 @@ def encode_storage(store):
3333
obj_dict["mode"] = store.mode
3434
# store.fs.to_json to get full filesystem (see fsspec.AbstractFileSystem.from_json)
3535
obj_dict["fs"] = store.fs.to_json()
36+
elif isinstance(store, KVStore):
37+
obj_dict["map"] = {k: store[k] for k in store}
3638
else:
3739
raise NotImplementedError(f"zarr.storage.Store subclass {store.__class__} not supported")
3840
return obj_dict
@@ -59,4 +61,6 @@ def decode_storage(obj_dict): # TODO needs kwargs for dimension sep?
5961
if "fs" in kwargs and type_string == "FSStore":
6062
kwargs["fs"] = fsspec.AbstractFileSystem.from_json(kwargs["fs"])
6163
args.append(kwargs.pop("path"))
64+
elif "map" in kwargs and type_string == "KVStore":
65+
args.append(kwargs.pop("map"))
6266
return getattr(zarr.storage, type_string)(*args, **kwargs)

0 commit comments

Comments
 (0)