Blockwise-reshape into a new shape. The regular reshape operation in Dask preserves C-ordering in the array which requires a rechunking for most reshaping operations, making the computation relatively expensive. Blockwise-reshape reshapes every block into the new shape and concaten
(
x: Array,
shape: int | tuple[int, ...],
chunks: tuple[tuple[int, ...], ...] | None = None,
)
| 387 | |
| 388 | |
| 389 | def reshape_blockwise( |
| 390 | x: Array, |
| 391 | shape: int | tuple[int, ...], |
| 392 | chunks: tuple[tuple[int, ...], ...] | None = None, |
| 393 | ) -> Array: |
| 394 | """Blockwise-reshape into a new shape. |
| 395 | |
| 396 | The regular reshape operation in Dask preserves C-ordering in the array |
| 397 | which requires a rechunking for most reshaping operations, making the |
| 398 | computation relatively expensive. |
| 399 | |
| 400 | Blockwise-reshape reshapes every block into the new shape and concatenates |
| 401 | the results. This is a trivial blockwise computation but will return the |
| 402 | result in a different order than NumPy. This is a good solution for |
| 403 | subsequent operations that don't rely on the order. |
| 404 | |
| 405 | Parameters |
| 406 | ---------- |
| 407 | x: Array |
| 408 | The input array to reshape. |
| 409 | shape : int or tuple of ints |
| 410 | The new shape should be compatible with the original shape. If |
| 411 | an integer, then the result will be a 1-D array of that length. |
| 412 | One shape dimension can be -1. In this case, the value is |
| 413 | inferred from the length of the array and remaining dimensions. |
| 414 | chunks: tuple of ints, default None |
| 415 | The chunk sizes for every chunk in the output array. Dask will expand |
| 416 | the chunks per dimension into the cross product of chunks for every |
| 417 | chunk in the array. |
| 418 | |
| 419 | An error is raised if chunks is given and the number of dimensions |
| 420 | decreases. |
| 421 | |
| 422 | .. note:: |
| 423 | This information is required if the number of dimensions is increased. |
| 424 | Dask cannot infer the output chunks in this case. The keyword is ignored |
| 425 | if the number of dimensions is reduced. |
| 426 | |
| 427 | Notes |
| 428 | ----- |
| 429 | This is a parallelized version of the ``np.reshape`` function with the |
| 430 | following limitations: |
| 431 | |
| 432 | 1. It does not return elements in the same order as NumPy would |
| 433 | 2. It only allows for reshapings that collapse like |
| 434 | ``(1, 2, 3, 4) -> (1, 6, 4)`` |
| 435 | |
| 436 | Examples |
| 437 | -------- |
| 438 | >>> import dask.array as da |
| 439 | >>> import numpy as np |
| 440 | >>> x = da.from_array(np.arange(0, 27).reshape(3, 3, 3), chunks=(3, 2, (2, 1))) |
| 441 | >>> result = reshape_blockwise(x, (3, 9)) |
| 442 | >>> result.chunks |
| 443 | ((3,), (4, 2, 2, 1)) |
| 444 | |
| 445 | The resulting chunks are calculated automatically to match the new shape. |
| 446 |
searching dependent graphs…