| 1 |
import pytest |
| 2 |
|
| 3 |
import fsspec |
| 4 |
import numpy as np |
| 5 |
import zarr |
| 6 |
|
| 7 |
import kerchunk.combine |
| 8 |
import kerchunk.zarr |
| 9 |
import kerchunk.df |
| 10 |
from kerchunk.utils import fs_as_store, refs_as_store |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.parametrize( |
| 14 |
"arrays,chunks,axis", |
| 15 |
[ |
| 16 |
( |
| 17 |
[np.arange(10), np.arange(10, 20)], |
| 18 |
(2,), |
| 19 |
0, |
| 20 |
), |
| 21 |
( |
| 22 |
[np.arange(12), np.arange(12, 36), np.arange(36, 42)], |
| 23 |
(6,), |
| 24 |
0, |
| 25 |
), |
| 26 |
( |
| 27 |
# Terminal chunk does not need to be filled |
| 28 |
[np.arange(5), np.arange(5, 10), np.arange(10, 17)], |
| 29 |
(5,), |
| 30 |
0, |
| 31 |
), |
| 32 |
( |
| 33 |
[ |
| 34 |
np.broadcast_to(np.arange(6), (10, 6)), |
| 35 |
np.broadcast_to(np.arange(7, 10), (10, 3)), |
| 36 |
], |
| 37 |
(10, 3), |
| 38 |
1, |
| 39 |
), |
| 40 |
( |
| 41 |
[ |
| 42 |
np.broadcast_to(np.arange(6), (10, 6)).T, |
| 43 |
np.broadcast_to(np.arange(7, 10), (10, 3)).T, |
| 44 |
], |
| 45 |
(3, 10), |
| 46 |
0, |
| 47 |
), |
| 48 |
], |
| 49 |
) |
| 50 |
def test_success(tmpdir, arrays, chunks, axis, m): |
| 51 |
fns = [] |
| 52 |
refs = [] |
| 53 |
for i, x in enumerate(arrays): |
| 54 |
fn = f"{tmpdir}/out{i}.zarr" |
| 55 |
g = zarr.open(fn, zarr_format=2) |
| 56 |
arr = g.create_array("x", shape=x.shape, dtype=x.dtype, chunks=chunks) |
| 57 |
arr[:] = x |
| 58 |
fns.append(fn) |
| 59 |
ref = kerchunk.zarr.single_zarr(fn, inline=0) |
| 60 |
refs.append(ref) |
| 61 |
|
| 62 |
out = kerchunk.combine.concatenate_arrays( |
| 63 |
refs, axis=axis, path="x", check_arrays=True |
| 64 |
) |
| 65 |
|
| 66 |
store = refs_as_store(out) |
| 67 |
g = zarr.open(store, zarr_format=2) |
| 68 |
assert (g["x"][:] == np.concatenate(arrays, axis=axis)).all() |
| 69 |
|
| 70 |
try: |
| 71 |
import fastparquet |
| 72 |
except ImportError: |
| 73 |
return |
| 74 |
kerchunk.df.refs_to_dataframe(out, "memory://out.parq") |
| 75 |
storage_options = dict( |
| 76 |
fo="memory://out.parq", |
| 77 |
remote_protocol="file", |
| 78 |
skip_instance_cache=True, |
| 79 |
) |
| 80 |
g = zarr.open("reference://", zarr_format=2, storage_options=storage_options) |
| 81 |
assert (g["x"][:] == np.concatenate(arrays, axis=axis)).all() |
| 82 |
|
| 83 |
kerchunk.df.refs_to_dataframe(out, "memory://out.parq", record_size=1) |
| 84 |
storage_options = dict( |
| 85 |
fo="memory://out.parq", |
| 86 |
remote_protocol="file", |
| 87 |
skip_instance_cache=True, |
| 88 |
) |
| 89 |
g = zarr.open("reference://", zarr_format=2, storage_options=storage_options) |
| 90 |
assert (g["x"][:] == np.concatenate(arrays, axis=axis)).all() |
| 91 |
|
| 92 |
|
| 93 |
def test_fail_chunks(tmpdir): |
| 94 |
fn1 = f"{tmpdir}/out1.zarr" |
| 95 |
fn2 = f"{tmpdir}/out2.zarr" |
| 96 |
x1 = np.arange(10) |
| 97 |
x2 = np.arange(10, 20) |
| 98 |
g = zarr.open(fn1, zarr_format=2) |
| 99 |
arr = g.create_array("x", shape=x1.shape, dtype=x1.dtype, chunks=(2,)) |
| 100 |
arr[:] = x1 |
| 101 |
g = zarr.open(fn2, zarr_format=2) |
| 102 |
arr = g.create_array("x", shape=x2.shape, dtype=x2.dtype, chunks=(3,)) |
| 103 |
arr[:] = x2 |
| 104 |
|
| 105 |
ref1 = kerchunk.zarr.single_zarr(fn1, inline=0) |
| 106 |
ref2 = kerchunk.zarr.single_zarr(fn2, inline=0) |
| 107 |
|
| 108 |
with pytest.raises(ValueError, match=r"Incompatible array chunks at index 1.*"): |
| 109 |
kerchunk.combine.concatenate_arrays([ref1, ref2], path="x", check_arrays=True) |
| 110 |
|
| 111 |
|
| 112 |
def test_fail_shape(tmpdir): |
| 113 |
fn1 = f"{tmpdir}/out1.zarr" |
| 114 |
fn2 = f"{tmpdir}/out2.zarr" |
| 115 |
x1 = np.arange(12).reshape(6, 2) |
| 116 |
x2 = np.arange(12, 24) |
| 117 |
g = zarr.open(fn1, zarr_format=2) |
| 118 |
arr = g.create_array("x", shape=x1.shape, dtype=x1.dtype) |
| 119 |
arr[:] = x1 |
| 120 |
g = zarr.open(fn2, zarr_format=2) |
| 121 |
arr = g.create_array("x", shape=x2.shape, dtype=x2.dtype) |
| 122 |
arr[:] = x2 |
| 123 |
|
| 124 |
ref1 = kerchunk.zarr.single_zarr(fn1, inline=0) |
| 125 |
ref2 = kerchunk.zarr.single_zarr(fn2, inline=0) |
| 126 |
|
| 127 |
with pytest.raises(ValueError, match=r"Incompatible array shape at index 1.*"): |
| 128 |
kerchunk.combine.concatenate_arrays([ref1, ref2], path="x", check_arrays=True) |
| 129 |
|
| 130 |
|
| 131 |
def test_fail_irregular_chunk_boundaries(tmpdir): |
| 132 |
fn1 = f"{tmpdir}/out1.zarr" |
| 133 |
fn2 = f"{tmpdir}/out2.zarr" |
| 134 |
x1 = np.arange(10) |
| 135 |
x2 = np.arange(10, 24) |
| 136 |
g = zarr.open(fn1, zarr_format=2) |
| 137 |
arr = g.create_array("x", shape=x1.shape, dtype=x1.dtype, chunks=(4,)) |
| 138 |
arr[:] = x1 |
| 139 |
g = zarr.open(fn2, zarr_format=2) |
| 140 |
arr = g.create_array("x", shape=x2.shape, dtype=x2.dtype, chunks=(4,)) |
| 141 |
arr[:] = x2 |
| 142 |
|
| 143 |
ref1 = kerchunk.zarr.single_zarr(fn1, inline=0) |
| 144 |
ref2 = kerchunk.zarr.single_zarr(fn2, inline=0) |
| 145 |
|
| 146 |
with pytest.raises(ValueError, match=r"Array at index 0 has irregular chunking.*"): |
| 147 |
kerchunk.combine.concatenate_arrays([ref1, ref2], path="x", check_arrays=True) |