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

Class BlockView

dask/array/core.py:6177–6276  ·  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

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

Callers 2

test_blockviewFunction · 0.90
blocksMethod · 0.85

Calls

no outgoing calls

Tested by 1

test_blockviewFunction · 0.72