An array-like interface to the blocks of an array. ``BlockView`` provides an array-like interface to the blocks of a dask array. Numpy-style indexing of a ``BlockView`` returns a selection of blocks as a new dask array. You can index ``BlockView`` like a numpy array of shape
| 5967 | |
| 5968 | |
| 5969 | class BlockView: |
| 5970 | """An array-like interface to the blocks of an array. |
| 5971 | |
| 5972 | ``BlockView`` provides an array-like interface |
| 5973 | to the blocks of a dask array. Numpy-style indexing of a |
| 5974 | ``BlockView`` returns a selection of blocks as a new dask array. |
| 5975 | |
| 5976 | You can index ``BlockView`` like a numpy array of shape |
| 5977 | equal to the number of blocks in each dimension, (available as |
| 5978 | array.blocks.size). The dimensionality of the output array matches |
| 5979 | the dimension of this array, even if integer indices are passed. |
| 5980 | Slicing with ``np.newaxis`` or multiple lists is not supported. |
| 5981 | |
| 5982 | Examples |
| 5983 | -------- |
| 5984 | >>> import dask.array as da |
| 5985 | >>> from dask.array.core import BlockView |
| 5986 | >>> x = da.arange(8, chunks=2) |
| 5987 | >>> bv = BlockView(x) |
| 5988 | >>> bv.shape # aliases x.numblocks |
| 5989 | (4,) |
| 5990 | >>> bv.size |
| 5991 | 4 |
| 5992 | >>> bv[0].compute() |
| 5993 | array([0, 1]) |
| 5994 | >>> bv[:3].compute() |
| 5995 | array([0, 1, 2, 3, 4, 5]) |
| 5996 | >>> bv[::2].compute() |
| 5997 | array([0, 1, 4, 5]) |
| 5998 | >>> bv[[-1, 0]].compute() |
| 5999 | array([6, 7, 0, 1]) |
| 6000 | >>> bv.ravel() # doctest: +NORMALIZE_WHITESPACE |
| 6001 | [dask.array<blocks, shape=(2,), dtype=int64, chunksize=(2,), chunktype=numpy.ndarray>, |
| 6002 | dask.array<blocks, shape=(2,), dtype=int64, chunksize=(2,), chunktype=numpy.ndarray>, |
| 6003 | dask.array<blocks, shape=(2,), dtype=int64, chunksize=(2,), chunktype=numpy.ndarray>, |
| 6004 | dask.array<blocks, shape=(2,), dtype=int64, chunksize=(2,), chunktype=numpy.ndarray>] |
| 6005 | |
| 6006 | Returns |
| 6007 | ------- |
| 6008 | An instance of ``da.array.Blockview`` |
| 6009 | """ |
| 6010 | |
| 6011 | def __init__(self, array: Array): |
| 6012 | self._array = array |
| 6013 | |
| 6014 | def __getitem__(self, index: Any) -> Array: |
| 6015 | from dask.array.slicing import normalize_index |
| 6016 | |
| 6017 | if not isinstance(index, tuple): |
| 6018 | index = (index,) |
| 6019 | if sum(isinstance(ind, (np.ndarray, list)) for ind in index) > 1: |
| 6020 | raise ValueError("Can only slice with a single list") |
| 6021 | if any(ind is None for ind in index): |
| 6022 | raise ValueError("Slicing with np.newaxis or None is not supported") |
| 6023 | index = normalize_index(index, self._array.numblocks) |
| 6024 | index = tuple( |
| 6025 | slice(k, k + 1) if isinstance(k, Number) else k # type: ignore |
| 6026 | for k in index |
no outgoing calls