20
20
from zarr .codecs .bytes import Endian
21
21
from zarr .common import BytesLike
22
22
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
24
26
NDArrayLike : TypeAlias = np .ndarray
25
27
26
28
@@ -89,10 +91,10 @@ class Buffer:
89
91
90
92
We use Buffer throughout Zarr to represent a contiguous block of memory.
91
93
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
93
95
the memory. The memory type is unspecified; can be regular host memory,
94
96
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
96
98
(host memory).
97
99
98
100
Note
@@ -101,16 +103,16 @@ class Buffer:
101
103
102
104
Parameters
103
105
----------
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.
106
108
"""
107
109
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
114
116
115
117
@classmethod
116
118
def create_zero_length (cls ) -> Self :
@@ -123,19 +125,19 @@ def create_zero_length(cls) -> Self:
123
125
return cls (np .array ([], dtype = "b" ))
124
126
125
127
@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
128
130
129
131
Parameters
130
132
----------
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.
133
135
134
136
Return
135
137
------
136
- New buffer representing `ndarray_like `
138
+ New buffer representing `array_like `
137
139
"""
138
- return cls (ndarray_like )
140
+ return cls (array_like )
139
141
140
142
@classmethod
141
143
def from_bytes (cls , bytes_like : BytesLike ) -> Self :
@@ -150,9 +152,9 @@ def from_bytes(cls, bytes_like: BytesLike) -> Self:
150
152
------
151
153
New buffer representing `bytes_like`
152
154
"""
153
- return cls .from_ndarray_like (np .frombuffer (bytes_like , dtype = "b" ))
155
+ return cls .from_array_like (np .frombuffer (bytes_like , dtype = "b" ))
154
156
155
- def as_ndarray_like (self ) -> NDArrayLike :
157
+ def as_array_like (self ) -> NDArrayLike :
156
158
"""Return the underlying array (host or device memory) of this buffer
157
159
158
160
This will never copy data.
@@ -175,7 +177,7 @@ def as_nd_buffer(self, *, dtype: np.DTypeLike) -> NDBuffer:
175
177
176
178
Return
177
179
------
178
- New NDbuffer representing `self.as_ndarray_like ()`
180
+ New NDbuffer representing `self.as_array_like ()`
179
181
"""
180
182
return NDBuffer .from_ndarray_like (self ._data .view (dtype = dtype ))
181
183
@@ -184,7 +186,7 @@ def as_numpy_array(self) -> np.ndarray:
184
186
185
187
Warning
186
188
-------
187
- Might have to copy data, consider using `.as_ndarray_like ()` instead.
189
+ Might have to copy data, consider using `.as_array_like ()` instead.
188
190
189
191
Return
190
192
------
@@ -198,7 +200,7 @@ def to_bytes(self) -> bytes:
198
200
Warning
199
201
-------
200
202
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.
202
204
203
205
Return
204
206
------
@@ -220,7 +222,7 @@ def __len__(self) -> int:
220
222
def __add__ (self , other : Buffer ) -> Self :
221
223
"""Concatenate two buffers"""
222
224
223
- other_array = other .as_ndarray_like ()
225
+ other_array = other .as_array_like ()
224
226
assert other_array .dtype == np .dtype ("b" )
225
227
return self .__class__ (np .concatenate ((self ._data , other_array )))
226
228
@@ -230,7 +232,7 @@ def __eq__(self, other: Any) -> bool:
230
232
# convert the bytes to a Buffer and try again
231
233
return self == self .from_bytes (other )
232
234
if isinstance (other , Buffer ):
233
- return (self ._data == other .as_ndarray_like ()).all ()
235
+ return (self ._data == other .as_array_like ()).all ()
234
236
raise ValueError (
235
237
f"equal operator not supported between { self .__class__ } and { other .__class__ } "
236
238
)
0 commit comments