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

Function _vindex_array

dask/array/core.py:5849–6003  ·  view source on GitHub ↗

Point wise indexing with only NumPy Arrays.

(x, dict_indexes)

Source from the content-addressed store, hash-verified

5847
5848
5849def _vindex_array(x, dict_indexes):
5850 """Point wise indexing with only NumPy Arrays."""
5851
5852 token = tokenize(x, dict_indexes)
5853 try:
5854 broadcast_shape = np.broadcast_shapes(
5855 *(arr.shape for arr in dict_indexes.values())
5856 )
5857 except ValueError as e:
5858 # note: error message exactly matches numpy
5859 shapes_str = " ".join(str(a.shape) for a in dict_indexes.values())
5860 raise IndexError(
5861 "shape mismatch: indexing arrays could not be "
5862 f"broadcast together with shapes {shapes_str}"
5863 ) from e
5864 npoints = math.prod(broadcast_shape)
5865 axes = [i for i in range(x.ndim) if i in dict_indexes]
5866
5867 def _subset_to_indexed_axes(iterable):
5868 for i, elem in enumerate(iterable):
5869 if i in axes:
5870 yield elem
5871
5872 bounds2 = tuple(
5873 np.array(cached_cumsum(c, initial_zero=True))
5874 for c in _subset_to_indexed_axes(x.chunks)
5875 )
5876 axis = _get_axis(tuple(i if i in axes else None for i in range(x.ndim)))
5877 out_name = f"vindex-merge-{token}"
5878
5879 # Now compute indices of each output element within each input block
5880 # The index is relative to the block, not the array.
5881 block_idxs = tuple(
5882 np.searchsorted(b, ind, side="right") - 1
5883 for b, ind in zip(bounds2, dict_indexes.values())
5884 )
5885 starts = (b[i] for i, b in zip(block_idxs, bounds2))
5886 inblock_idxs = []
5887 for idx, start in zip(dict_indexes.values(), starts):
5888 a = idx - start
5889 if len(a) > 0:
5890 dtype = np.min_scalar_type(np.max(a, axis=None))
5891 inblock_idxs.append(a.astype(dtype, copy=False))
5892 else:
5893 inblock_idxs.append(a)
5894
5895 inblock_idxs = np.broadcast_arrays(*inblock_idxs)
5896
5897 chunks = [c for i, c in enumerate(x.chunks) if i not in axes]
5898 # determine number of points in one single output block.
5899 # Use the input chunk size to determine this.
5900 max_chunk_point_dimensions = reduce(
5901 mul, map(cached_max, _subset_to_indexed_axes(x.chunks))
5902 )
5903
5904 n_chunks, remainder = divmod(npoints, max_chunk_point_dimensions)
5905 chunks.insert(
5906 0,

Callers 1

_vindexFunction · 0.85

Calls 15

reshapeMethod · 0.95
cached_cumsumFunction · 0.90
divmodFunction · 0.90
TaskClass · 0.90
TaskRefClass · 0.90
ListClass · 0.90
_subset_to_indexed_axesFunction · 0.85
_get_axisFunction · 0.85
keynameFunction · 0.85
interleave_noneFunction · 0.85
nonzeroMethod · 0.80
from_collectionsMethod · 0.80

Tested by

no test coverage detected