forked from zarr-developers/zarr-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_buffer.py
More file actions
130 lines (103 loc) · 3.87 KB
/
test_buffer.py
File metadata and controls
130 lines (103 loc) · 3.87 KB
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
from __future__ import annotations
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, Literal
import numpy as np
import numpy.typing as npt
import pytest
from zarr.array import AsyncArray
from zarr.buffer import ArrayLike, Buffer, BufferPrototype, NDArrayLike, NDBuffer
from zarr.codecs.blosc import BloscCodec
from zarr.codecs.bytes import BytesCodec
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.store.core import StorePath
from zarr.store.memory import MemoryStore
if TYPE_CHECKING:
from typing_extensions import Self
class MyNDArrayLike(np.ndarray):
"""An example of a ndarray-like class"""
class MyBuffer(Buffer):
"""Example of a custom Buffer that handles ArrayLike"""
class MyNDBuffer(NDBuffer):
"""Example of a custom NDBuffer that handles MyNDArrayLike"""
@classmethod
def create(
cls,
*,
shape: Iterable[int],
dtype: npt.DTypeLike,
order: Literal["C", "F"] = "C",
fill_value: Any | None = None,
) -> Self:
"""Overwrite `NDBuffer.create` to create an MyNDArrayLike instance"""
ret = cls(MyNDArrayLike(shape=shape, dtype=dtype, order=order))
if fill_value is not None:
ret.fill(fill_value)
return ret
class MyStore(MemoryStore):
"""Example of a custom Store that expect MyBuffer for all its non-metadata
We assume that keys containing "json" is metadata
"""
async def set(self, key: str, value: Buffer, byte_range: tuple[int, int] | None = None) -> None:
if "json" not in key:
assert isinstance(value, MyBuffer)
await super().set(key, value, byte_range)
async def get(
self,
key: str,
prototype: BufferPrototype,
byte_range: tuple[int, int | None] | None = None,
) -> Buffer | None:
if "json" not in key:
assert prototype.buffer is MyBuffer
return await super().get(key, byte_range)
def test_nd_array_like(xp):
ary = xp.arange(10)
assert isinstance(ary, ArrayLike)
assert isinstance(ary, NDArrayLike)
@pytest.mark.asyncio
async def test_async_array_prototype():
"""Test the use of a custom buffer prototype"""
expect = np.zeros((9, 9), dtype="uint16", order="F")
a = await AsyncArray.create(
StorePath(MyStore(mode="w")) / "test_async_array_prototype",
shape=expect.shape,
chunk_shape=(5, 5),
dtype=expect.dtype,
fill_value=0,
)
expect[1:4, 3:6] = np.ones((3, 3))
my_prototype = BufferPrototype(buffer=MyBuffer, nd_buffer=MyNDBuffer)
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)
assert isinstance(got, MyNDArrayLike)
assert np.array_equal(expect, got)
@pytest.mark.asyncio
async def test_codecs_use_of_prototype():
expect = np.zeros((10, 10), dtype="uint16", order="F")
a = await AsyncArray.create(
StorePath(MyStore(mode="w")) / "test_codecs_use_of_prototype",
shape=expect.shape,
chunk_shape=(5, 5),
dtype=expect.dtype,
fill_value=0,
filters=[TransposeCodec(order=(1, 0))],
pre_compressor=BytesCodec(),
compressors=(BloscCodec(), Crc32cCodec(), GzipCodec(), ZstdCodec()),
)
expect[:] = np.arange(100).reshape(10, 10)
my_prototype = BufferPrototype(buffer=MyBuffer, nd_buffer=MyNDBuffer)
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)
assert isinstance(got, MyNDArrayLike)
assert np.array_equal(expect, got)