buffer
zarr.abc.buffer ¶
ArrayLike ¶
Bases: Protocol
Protocol for the array-like type that underlie Buffer
Source code in zarr/core/buffer/core.py
Buffer ¶
Bases: ABC
A flat contiguous memory block
We use Buffer throughout Zarr to represent a contiguous block of memory.
A Buffer is backed by a underlying array-like instance that represents the memory. The memory type is unspecified; can be regular host memory, CUDA device memory, or something else. The only requirement is that the array-like instance can be copied/converted to a regular Numpy array (host memory).
Notes
This buffer is untyped, so all indexing and sizes are in bytes.
Parameters:
-
array_like
(ArrayLike
) –array-like object that must be 1-dim, contiguous, and byte dtype.
Source code in zarr/core/buffer/core.py
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
__add__
abstractmethod
¶
as_array_like ¶
as_array_like() -> ArrayLike
Returns the underlying array (host or device memory) of this buffer
This will never copy data.
Returns:
-
The underlying 1d array such as a NumPy or CuPy array.
–
Source code in zarr/core/buffer/core.py
as_buffer_like ¶
Returns the buffer as an object that implements the Python buffer protocol.
Notes
Might have to copy data, since the implementation uses .as_numpy_array()
.
Returns:
-
An object that implements the Python buffer protocol
–
Source code in zarr/core/buffer/core.py
as_numpy_array
abstractmethod
¶
Returns the buffer as a NumPy array (host memory).
Notes
Might have to copy data, consider using .as_array_like()
instead.
Returns:
-
NumPy array of this buffer (might be a data copy)
–
Source code in zarr/core/buffer/core.py
create_zero_length
abstractmethod
classmethod
¶
create_zero_length() -> Self
Create an empty buffer with length zero
Returns:
-
New empty 0-length buffer
–
Source code in zarr/core/buffer/core.py
from_array_like
classmethod
¶
Create a new buffer of an array-like object
Parameters:
-
array_like
(ArrayLike
) –array-like object that must be 1-dim, contiguous, and byte dtype.
Returns:
-
New buffer representing `array_like`
–
Source code in zarr/core/buffer/core.py
from_buffer
abstractmethod
classmethod
¶
Create a new buffer of an existing Buffer
This is useful if you want to ensure that an existing buffer is of the correct subclass of Buffer. E.g., MemoryStore uses this to return a buffer instance of the subclass specified by its BufferPrototype argument.
Typically, this only copies data if the data has to be moved between memory types, such as from host to device memory.
Parameters:
-
buffer
(Buffer
) –buffer object.
Returns:
-
A new buffer representing the content of the input buffer
–
Notes
Subclasses of Buffer
must override this method to implement
more optimal conversions that avoid copies where possible
Source code in zarr/core/buffer/core.py
from_bytes
abstractmethod
classmethod
¶
from_bytes(bytes_like: BytesLike) -> Self
Create a new buffer of a bytes-like object (host memory)
Parameters:
-
bytes_like
(BytesLike
) –bytes-like object
Returns:
-
New buffer representing `bytes_like`
–
Source code in zarr/core/buffer/core.py
to_bytes ¶
to_bytes() -> bytes
Returns the buffer as bytes
(host memory).
Warnings
Will always copy data, only use this method for small buffers such as metadata
buffers. If possible, use .as_numpy_array()
or .as_array_like()
instead.
Returns:
-
`bytes` of this buffer (data copy)
–
Source code in zarr/core/buffer/core.py
BufferPrototype ¶
Bases: NamedTuple
Prototype of the Buffer and NDBuffer class
The protocol must be pickable.
Attributes:
-
buffer
(type[Buffer]
) –The Buffer class to use when Zarr needs to create new Buffer.
-
nd_buffer
(type[NDBuffer]
) –The NDBuffer class to use when Zarr needs to create new NDBuffer.
Source code in zarr/core/buffer/core.py
NDArrayLike ¶
Bases: Protocol
Protocol for the nd-array-like type that underlie NDBuffer
Source code in zarr/core/buffer/core.py
__eq__ ¶
Element-wise equal
Notes
Type checkers such as mypy complains because the return type isn't a bool like its supertype "object", which violates the Liskov substitution principle. This is true, but since NumPy's ndarray is defined as an element-wise equal, our hands are tied.
Source code in zarr/core/buffer/core.py
NDBuffer ¶
An n-dimensional memory block
We use NDBuffer throughout Zarr to represent a n-dimensional memory block.
A NDBuffer is backed by a underlying ndarray-like instance that represents the memory. The memory type is unspecified; can be regular host memory, CUDA device memory, or something else. The only requirement is that the ndarray-like instance can be copied/converted to a regular Numpy array (host memory).
Notes
The two buffer classes Buffer and NDBuffer are very similar. In fact, Buffer is a special case of NDBuffer where dim=1, stride=1, and dtype="B". However, in order to use Python's type system to differentiate between the contiguous Buffer and the n-dim (non-contiguous) NDBuffer, we keep the definition of the two classes separate.
Parameters:
-
array
(ndarray_like
) –ndarray-like object that is convertible to a regular Numpy array.
Source code in zarr/core/buffer/core.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
|
all_equal ¶
Compare to other
using np.array_equal.
Source code in zarr/core/buffer/core.py
as_ndarray_like ¶
as_ndarray_like() -> NDArrayLike
Returns the underlying array (host or device memory) of this buffer
This will never copy data.
Returns:
-
The underlying array such as a NumPy or CuPy array.
–
Source code in zarr/core/buffer/core.py
as_numpy_array
abstractmethod
¶
Returns the buffer as a NumPy array (host memory).
Warnings
Might have to copy data, consider using .as_ndarray_like()
instead.
Returns:
-
NumPy array of this buffer (might be a data copy)
–
Source code in zarr/core/buffer/core.py
as_scalar ¶
Returns the buffer as a scalar value
create
abstractmethod
classmethod
¶
create(
*,
shape: Iterable[int],
dtype: DTypeLike,
order: Literal["C", "F"] = "C",
fill_value: Any | None = None,
) -> Self
Create a new buffer and its underlying ndarray-like object
Parameters:
-
shape
(Iterable[int]
) –The shape of the buffer and its underlying ndarray-like object
-
dtype
(DTypeLike
) –The datatype of the buffer and its underlying ndarray-like object
-
order
(Literal['C', 'F']
, default:'C'
) –Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
-
fill_value
(Any | None
, default:None
) –If not None, fill the new buffer with a scalar value.
Returns:
-
New buffer representing a new ndarray_like object
–
Notes
A subclass can overwrite this method to create a ndarray-like object other then the default Numpy array.
Source code in zarr/core/buffer/core.py
empty
classmethod
¶
Create an empty buffer with the given shape, dtype, and order.
This method can be faster than NDBuffer.create
because it doesn't
have to initialize the memory used by the underlying ndarray-like
object.
Parameters:
-
shape
(tuple[int, ...]
) –The shape of the buffer and its underlying ndarray-like object
-
dtype
(DTypeLike
) –The datatype of the buffer and its underlying ndarray-like object
-
order
(Literal['C', 'F']
, default:'C'
) –Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
Returns:
-
buffer
–New buffer representing a new ndarray_like object with empty data.
See Also
NDBuffer.create Create a new buffer with some initial fill value.
Source code in zarr/core/buffer/core.py
from_ndarray_like
classmethod
¶
from_ndarray_like(ndarray_like: NDArrayLike) -> Self
Create a new buffer of a ndarray-like object
Parameters:
-
ndarray_like
(NDArrayLike
) –ndarray-like object
Returns:
-
New buffer representing `ndarray_like`
–
Source code in zarr/core/buffer/core.py
from_numpy_array
abstractmethod
classmethod
¶
Create a new buffer of Numpy array-like object
Parameters:
-
array_like
(ArrayLike
) –Object that can be coerced into a Numpy array
Returns:
-
New buffer representing `array_like`
–