-
-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathtest_buffer.py
153 lines (126 loc) · 4.95 KB
/
test_buffer.py
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
148
149
150
151
152
153
from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
import pytest
import zarr
from zarr.codecs.blosc import BloscCodec
from zarr.codecs.crc32c_ import Crc32cCodec
from zarr.codecs.gzip import GzipCodec
from zarr.codecs.transpose import TransposeCodec
from zarr.codecs.zstd import ZstdCodec
from zarr.core.buffer import ArrayLike, BufferPrototype, NDArrayLike, cpu, gpu
from zarr.storage import MemoryStore, StorePath
from zarr.testing.buffer import (
NDBufferUsingTestNDArrayLike,
StoreExpectingTestBuffer,
TestBuffer,
TestNDArrayLike,
)
from zarr.testing.utils import gpu_test
if TYPE_CHECKING:
import types
try:
import cupy as cp
except ImportError:
cp = None
if TYPE_CHECKING:
import types
def test_nd_array_like(xp: types.ModuleType) -> None:
ary = xp.arange(10)
assert isinstance(ary, ArrayLike)
assert isinstance(ary, NDArrayLike)
@pytest.mark.asyncio
async def test_async_array_prototype() -> None:
"""Test the use of a custom buffer prototype"""
expect = np.zeros((9, 9), dtype="uint16", order="F")
a = await zarr.api.asynchronous.create_array(
StorePath(StoreExpectingTestBuffer()) / "test_async_array_prototype",
shape=expect.shape,
chunks=(5, 5),
dtype=expect.dtype,
fill_value=0,
)
expect[1:4, 3:6] = np.ones((3, 3))
my_prototype = BufferPrototype(buffer=TestBuffer, nd_buffer=NDBufferUsingTestNDArrayLike)
await a.setitem(
selection=(slice(1, 4), slice(3, 6)),
value=np.ones((3, 3)),
prototype=my_prototype,
)
got = await a.getitem(selection=(slice(0, 9), slice(0, 9)), prototype=my_prototype)
# ignoring a mypy error here that TestNDArrayLike doesn't meet the NDArrayLike protocol
# The test passes, so it clearly does.
assert isinstance(got, TestNDArrayLike) # type: ignore[unreachable]
assert np.array_equal(expect, got) # type: ignore[unreachable]
@gpu_test
@pytest.mark.asyncio
async def test_async_array_gpu_prototype() -> None:
"""Test the use of the GPU buffer prototype"""
expect = cp.zeros((9, 9), dtype="uint16", order="F")
a = await zarr.api.asynchronous.create_array(
StorePath(MemoryStore()) / "test_async_array_gpu_prototype",
shape=expect.shape,
chunks=(5, 5),
dtype=expect.dtype,
fill_value=0,
)
expect[1:4, 3:6] = cp.ones((3, 3))
await a.setitem(
selection=(slice(1, 4), slice(3, 6)),
value=cp.ones((3, 3)),
prototype=gpu.buffer_prototype,
)
got = await a.getitem(selection=(slice(0, 9), slice(0, 9)), prototype=gpu.buffer_prototype)
assert isinstance(got, cp.ndarray)
assert cp.array_equal(expect, got)
@pytest.mark.asyncio
async def test_codecs_use_of_prototype() -> None:
expect = np.zeros((10, 10), dtype="uint16", order="F")
a = await zarr.api.asynchronous.create_array(
StorePath(StoreExpectingTestBuffer()) / "test_codecs_use_of_prototype",
shape=expect.shape,
chunks=(5, 5),
dtype=expect.dtype,
fill_value=0,
compressors=[BloscCodec(), Crc32cCodec(), GzipCodec(), ZstdCodec()],
filters=[TransposeCodec(order=(1, 0))],
)
expect[:] = np.arange(100).reshape(10, 10)
my_prototype = BufferPrototype(buffer=TestBuffer, nd_buffer=NDBufferUsingTestNDArrayLike)
await a.setitem(
selection=(slice(0, 10), slice(0, 10)),
value=expect[:],
prototype=my_prototype,
)
got = await a.getitem(selection=(slice(0, 10), slice(0, 10)), prototype=my_prototype)
# ignoring a mypy error here that TestNDArrayLike doesn't meet the NDArrayLike protocol
# The test passes, so it clearly does.
assert isinstance(got, TestNDArrayLike) # type: ignore[unreachable]
assert np.array_equal(expect, got) # type: ignore[unreachable]
@gpu_test
@pytest.mark.asyncio
async def test_codecs_use_of_gpu_prototype() -> None:
expect = cp.zeros((10, 10), dtype="uint16", order="F")
a = await zarr.api.asynchronous.create_array(
StorePath(MemoryStore()) / "test_codecs_use_of_gpu_prototype",
shape=expect.shape,
chunks=(5, 5),
dtype=expect.dtype,
fill_value=0,
compressors=[BloscCodec(), Crc32cCodec(), GzipCodec(), ZstdCodec()],
filters=[TransposeCodec(order=(1, 0))],
)
expect[:] = cp.arange(100).reshape(10, 10)
await a.setitem(
selection=(slice(0, 10), slice(0, 10)),
value=expect[:],
prototype=gpu.buffer_prototype,
)
got = await a.getitem(selection=(slice(0, 10), slice(0, 10)), prototype=gpu.buffer_prototype)
assert isinstance(got, cp.ndarray)
assert cp.array_equal(expect, got)
def test_numpy_buffer_prototype() -> None:
buffer = cpu.buffer_prototype.buffer.create_zero_length()
ndbuffer = cpu.buffer_prototype.nd_buffer.create(shape=(1, 2), dtype=np.dtype("int64"))
assert isinstance(buffer.as_array_like(), np.ndarray)
assert isinstance(ndbuffer.as_ndarray_like(), np.ndarray)