File: test_combine_concat.py

package info (click to toggle)
kerchunk 0.2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 135,172 kB
  • sloc: python: 6,477; makefile: 39
file content (147 lines) | stat: -rw-r--r-- 4,517 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import pytest

import fsspec
import numpy as np
import zarr

import kerchunk.combine
import kerchunk.zarr
import kerchunk.df
from kerchunk.utils import fs_as_store, refs_as_store


@pytest.mark.parametrize(
    "arrays,chunks,axis",
    [
        (
            [np.arange(10), np.arange(10, 20)],
            (2,),
            0,
        ),
        (
            [np.arange(12), np.arange(12, 36), np.arange(36, 42)],
            (6,),
            0,
        ),
        (
            # Terminal chunk does not need to be filled
            [np.arange(5), np.arange(5, 10), np.arange(10, 17)],
            (5,),
            0,
        ),
        (
            [
                np.broadcast_to(np.arange(6), (10, 6)),
                np.broadcast_to(np.arange(7, 10), (10, 3)),
            ],
            (10, 3),
            1,
        ),
        (
            [
                np.broadcast_to(np.arange(6), (10, 6)).T,
                np.broadcast_to(np.arange(7, 10), (10, 3)).T,
            ],
            (3, 10),
            0,
        ),
    ],
)
def test_success(tmpdir, arrays, chunks, axis, m):
    fns = []
    refs = []
    for i, x in enumerate(arrays):
        fn = f"{tmpdir}/out{i}.zarr"
        g = zarr.open(fn, zarr_format=2)
        arr = g.create_array("x", shape=x.shape, dtype=x.dtype, chunks=chunks)
        arr[:] = x
        fns.append(fn)
        ref = kerchunk.zarr.single_zarr(fn, inline=0)
        refs.append(ref)

    out = kerchunk.combine.concatenate_arrays(
        refs, axis=axis, path="x", check_arrays=True
    )

    store = refs_as_store(out)
    g = zarr.open(store, zarr_format=2)
    assert (g["x"][:] == np.concatenate(arrays, axis=axis)).all()

    try:
        import fastparquet
    except ImportError:
        return
    kerchunk.df.refs_to_dataframe(out, "memory://out.parq")
    storage_options = dict(
        fo="memory://out.parq",
        remote_protocol="file",
        skip_instance_cache=True,
    )
    g = zarr.open("reference://", zarr_format=2, storage_options=storage_options)
    assert (g["x"][:] == np.concatenate(arrays, axis=axis)).all()

    kerchunk.df.refs_to_dataframe(out, "memory://out.parq", record_size=1)
    storage_options = dict(
        fo="memory://out.parq",
        remote_protocol="file",
        skip_instance_cache=True,
    )
    g = zarr.open("reference://", zarr_format=2, storage_options=storage_options)
    assert (g["x"][:] == np.concatenate(arrays, axis=axis)).all()


def test_fail_chunks(tmpdir):
    fn1 = f"{tmpdir}/out1.zarr"
    fn2 = f"{tmpdir}/out2.zarr"
    x1 = np.arange(10)
    x2 = np.arange(10, 20)
    g = zarr.open(fn1, zarr_format=2)
    arr = g.create_array("x", shape=x1.shape, dtype=x1.dtype, chunks=(2,))
    arr[:] = x1
    g = zarr.open(fn2, zarr_format=2)
    arr = g.create_array("x", shape=x2.shape, dtype=x2.dtype, chunks=(3,))
    arr[:] = x2

    ref1 = kerchunk.zarr.single_zarr(fn1, inline=0)
    ref2 = kerchunk.zarr.single_zarr(fn2, inline=0)

    with pytest.raises(ValueError, match=r"Incompatible array chunks at index 1.*"):
        kerchunk.combine.concatenate_arrays([ref1, ref2], path="x", check_arrays=True)


def test_fail_shape(tmpdir):
    fn1 = f"{tmpdir}/out1.zarr"
    fn2 = f"{tmpdir}/out2.zarr"
    x1 = np.arange(12).reshape(6, 2)
    x2 = np.arange(12, 24)
    g = zarr.open(fn1, zarr_format=2)
    arr = g.create_array("x", shape=x1.shape, dtype=x1.dtype)
    arr[:] = x1
    g = zarr.open(fn2, zarr_format=2)
    arr = g.create_array("x", shape=x2.shape, dtype=x2.dtype)
    arr[:] = x2

    ref1 = kerchunk.zarr.single_zarr(fn1, inline=0)
    ref2 = kerchunk.zarr.single_zarr(fn2, inline=0)

    with pytest.raises(ValueError, match=r"Incompatible array shape at index 1.*"):
        kerchunk.combine.concatenate_arrays([ref1, ref2], path="x", check_arrays=True)


def test_fail_irregular_chunk_boundaries(tmpdir):
    fn1 = f"{tmpdir}/out1.zarr"
    fn2 = f"{tmpdir}/out2.zarr"
    x1 = np.arange(10)
    x2 = np.arange(10, 24)
    g = zarr.open(fn1, zarr_format=2)
    arr = g.create_array("x", shape=x1.shape, dtype=x1.dtype, chunks=(4,))
    arr[:] = x1
    g = zarr.open(fn2, zarr_format=2)
    arr = g.create_array("x", shape=x2.shape, dtype=x2.dtype, chunks=(4,))
    arr[:] = x2

    ref1 = kerchunk.zarr.single_zarr(fn1, inline=0)
    ref2 = kerchunk.zarr.single_zarr(fn2, inline=0)

    with pytest.raises(ValueError, match=r"Array at index 0 has irregular chunking.*"):
        kerchunk.combine.concatenate_arrays([ref1, ref2], path="x", check_arrays=True)