MCPcopy Create free account
hub / github.com/dask/dask / BlockView

Class BlockView

dask/array/core.py:6169–6268  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

6167
6168
6169class BlockView:
6170 """An array-like interface to the blocks of an array.
6171
6172 ``BlockView`` provides an array-like interface
6173 to the blocks of a dask array. Numpy-style indexing of a
6174 ``BlockView`` returns a selection of blocks as a new dask array.
6175
6176 You can index ``BlockView`` like a numpy array of shape
6177 equal to the number of blocks in each dimension, (available as
6178 array.blocks.size). The dimensionality of the output array matches
6179 the dimension of this array, even if integer indices are passed.
6180 Slicing with ``np.newaxis`` or multiple lists is not supported.
6181
6182 Examples
6183 --------
6184 >>> import dask.array as da
6185 >>> from dask.array.core import BlockView
6186 >>> x = da.arange(8, chunks=2)
6187 >>> bv = BlockView(x)
6188 >>> bv.shape # aliases x.numblocks
6189 (4,)
6190 >>> bv.size
6191 4
6192 >>> bv[0].compute()
6193 array([0, 1])
6194 >>> bv[:3].compute()
6195 array([0, 1, 2, 3, 4, 5])
6196 >>> bv[::2].compute()
6197 array([0, 1, 4, 5])
6198 >>> bv[[-1, 0]].compute()
6199 array([6, 7, 0, 1])
6200 >>> bv.ravel() # doctest: +NORMALIZE_WHITESPACE
6201 [dask.array<blocks, shape=(2,), dtype=int64, chunksize=(2,), chunktype=numpy.ndarray>,
6202 dask.array<blocks, shape=(2,), dtype=int64, chunksize=(2,), chunktype=numpy.ndarray>,
6203 dask.array<blocks, shape=(2,), dtype=int64, chunksize=(2,), chunktype=numpy.ndarray>,
6204 dask.array<blocks, shape=(2,), dtype=int64, chunksize=(2,), chunktype=numpy.ndarray>]
6205
6206 Returns
6207 -------
6208 An instance of ``da.array.Blockview``
6209 """
6210
6211 def __init__(self, array: Array):
6212 self._array = array
6213
6214 def __getitem__(self, index: Any) -> Array:
6215 from dask.array.slicing import normalize_index
6216
6217 if not isinstance(index, tuple):
6218 index = (index,)
6219 if sum(isinstance(ind, (np.ndarray, list)) for ind in index) > 1:
6220 raise ValueError("Can only slice with a single list")
6221 if any(ind is None for ind in index):
6222 raise ValueError("Slicing with np.newaxis or None is not supported")
6223 index = normalize_index(index, self._array.numblocks)
6224 index = tuple(
6225 slice(k, k + 1) if isinstance(k, Number) else k # type: ignore[operator]
6226 for k in index

Callers 2

test_blockviewFunction · 0.90
blocksMethod · 0.85

Calls

no outgoing calls

Tested by 1

test_blockviewFunction · 0.72