Skip to content

Commit 3854bec

Browse files
committed
Buffer is now backed by ArrayLike
1 parent b5f87f1 commit 3854bec

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

src/zarr/buffer.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
from zarr.codecs.bytes import Endian
2121
from zarr.common import BytesLike
2222

23-
# TODO: create a protocol for the attributes we need, for now we just aliasing numpy
23+
# TODO: create a protocol for the attributes we need, for now we alias Numpy's ndarray
24+
# both for the array-like and ndarray-like
25+
ArrayLike: TypeAlias = np.ndarray
2426
NDArrayLike: TypeAlias = np.ndarray
2527

2628

@@ -89,10 +91,10 @@ class Buffer:
8991
9092
We use Buffer throughout Zarr to represent a contiguous block of memory.
9193
92-
A Buffer is backed by a underlying ndarray-like instance that represents
94+
A Buffer is backed by a underlying array-like instance that represents
9395
the memory. The memory type is unspecified; can be regular host memory,
9496
CUDA device memory, or something else. The only requirement is that the
95-
ndarray-like instance can be copied/converted to a regular Numpy array
97+
array-like instance can be copied/converted to a regular Numpy array
9698
(host memory).
9799
98100
Note
@@ -101,16 +103,16 @@ class Buffer:
101103
102104
Parameters
103105
----------
104-
ndarray_like
105-
ndarray-like object that must be 1-dim, contiguous, and byte dtype.
106+
array_like
107+
array-like object that must be 1-dim, contiguous, and byte dtype.
106108
"""
107109

108-
def __init__(self, ndarray_like: NDArrayLike):
109-
if ndarray_like.ndim != 1:
110-
raise ValueError("ndarray_like: only 1-dim allowed")
111-
if ndarray_like.dtype != np.dtype("b"):
112-
raise ValueError("ndarray_like: only byte dtype allowed")
113-
self._data = ndarray_like
110+
def __init__(self, array_like: ArrayLike):
111+
if array_like.ndim != 1:
112+
raise ValueError("array_like: only 1-dim allowed")
113+
if array_like.dtype != np.dtype("b"):
114+
raise ValueError("array_like: only byte dtype allowed")
115+
self._data = array_like
114116

115117
@classmethod
116118
def create_zero_length(cls) -> Self:
@@ -123,19 +125,19 @@ def create_zero_length(cls) -> Self:
123125
return cls(np.array([], dtype="b"))
124126

125127
@classmethod
126-
def from_ndarray_like(cls, ndarray_like: NDArrayLike) -> Self:
127-
"""Create a new buffer of a ndarray-like object
128+
def from_array_like(cls, array_like: NDArrayLike) -> Self:
129+
"""Create a new buffer of a array-like object
128130
129131
Parameters
130132
----------
131-
ndarray_like
132-
ndarray-like object that must be 1-dim, contiguous, and byte dtype.
133+
array_like
134+
array-like object that must be 1-dim, contiguous, and byte dtype.
133135
134136
Return
135137
------
136-
New buffer representing `ndarray_like`
138+
New buffer representing `array_like`
137139
"""
138-
return cls(ndarray_like)
140+
return cls(array_like)
139141

140142
@classmethod
141143
def from_bytes(cls, bytes_like: BytesLike) -> Self:
@@ -150,9 +152,9 @@ def from_bytes(cls, bytes_like: BytesLike) -> Self:
150152
------
151153
New buffer representing `bytes_like`
152154
"""
153-
return cls.from_ndarray_like(np.frombuffer(bytes_like, dtype="b"))
155+
return cls.from_array_like(np.frombuffer(bytes_like, dtype="b"))
154156

155-
def as_ndarray_like(self) -> NDArrayLike:
157+
def as_array_like(self) -> NDArrayLike:
156158
"""Return the underlying array (host or device memory) of this buffer
157159
158160
This will never copy data.
@@ -175,7 +177,7 @@ def as_nd_buffer(self, *, dtype: np.DTypeLike) -> NDBuffer:
175177
176178
Return
177179
------
178-
New NDbuffer representing `self.as_ndarray_like()`
180+
New NDbuffer representing `self.as_array_like()`
179181
"""
180182
return NDBuffer.from_ndarray_like(self._data.view(dtype=dtype))
181183

@@ -184,7 +186,7 @@ def as_numpy_array(self) -> np.ndarray:
184186
185187
Warning
186188
-------
187-
Might have to copy data, consider using `.as_ndarray_like()` instead.
189+
Might have to copy data, consider using `.as_array_like()` instead.
188190
189191
Return
190192
------
@@ -198,7 +200,7 @@ def to_bytes(self) -> bytes:
198200
Warning
199201
-------
200202
Will always copy data, only use this method for small buffers such as metadata
201-
buffers. If possible, use `.as_numpy_array()` or `.as_ndarray_like()` instead.
203+
buffers. If possible, use `.as_numpy_array()` or `.as_array_like()` instead.
202204
203205
Return
204206
------
@@ -220,7 +222,7 @@ def __len__(self) -> int:
220222
def __add__(self, other: Buffer) -> Self:
221223
"""Concatenate two buffers"""
222224

223-
other_array = other.as_ndarray_like()
225+
other_array = other.as_array_like()
224226
assert other_array.dtype == np.dtype("b")
225227
return self.__class__(np.concatenate((self._data, other_array)))
226228

@@ -230,7 +232,7 @@ def __eq__(self, other: Any) -> bool:
230232
# convert the bytes to a Buffer and try again
231233
return self == self.from_bytes(other)
232234
if isinstance(other, Buffer):
233-
return (self._data == other.as_ndarray_like()).all()
235+
return (self._data == other.as_array_like()).all()
234236
raise ValueError(
235237
f"equal operator not supported between {self.__class__} and {other.__class__}"
236238
)

src/zarr/codecs/blosc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ async def encode(
173173
# Since blosc only takes bytes, we convert the input and output of the encoding
174174
# between bytes and Buffer
175175
return await to_thread(
176-
lambda chunk: Buffer.from_bytes(self._blosc_codec.encode(chunk.as_ndarray_like())),
176+
lambda chunk: Buffer.from_bytes(self._blosc_codec.encode(chunk.as_array_like())),
177177
chunk_bytes,
178178
)
179179

src/zarr/codecs/crc32c_.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async def decode(
4646
"Stored and computed checksum do not match. "
4747
+ f"Stored: {stored_checksum!r}. Computed: {computed_checksum!r}."
4848
)
49-
return Buffer.from_ndarray_like(inner_bytes)
49+
return Buffer.from_array_like(inner_bytes)
5050

5151
async def encode(
5252
self,
@@ -57,7 +57,7 @@ async def encode(
5757
# Calculate the checksum and "cast" it to a numpy array
5858
checksum = np.array([crc32c(data)], dtype=np.uint32)
5959
# Append the checksum (as bytes) to the data
60-
return Buffer.from_ndarray_like(np.append(data, checksum.view("b")))
60+
return Buffer.from_array_like(np.append(data, checksum.view("b")))
6161

6262
def compute_encoded_size(self, input_byte_length: int, _chunk_spec: ArraySpec) -> int:
6363
return input_byte_length + 4

0 commit comments

Comments
 (0)