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

Function slice_with_int_dask_array

dask/array/_array_expr/_slicing.py:29–79  ·  view source on GitHub ↗

Slice x with at most one 1D dask arrays of ints. This is a helper function of :meth:`Array.__getitem__`. Parameters ---------- x: Array index: tuple with as many elements as x.ndim, among which there are one or more Array's with dtype=int Returns -------

(x, index)

Source from the content-addressed store, hash-verified

27
28
29def slice_with_int_dask_array(x, index):
30 """Slice x with at most one 1D dask arrays of ints.
31
32 This is a helper function of :meth:`Array.__getitem__`.
33
34 Parameters
35 ----------
36 x: Array
37 index: tuple with as many elements as x.ndim, among which there are
38 one or more Array's with dtype=int
39
40 Returns
41 -------
42 tuple of (sliced x, new index)
43
44 where the new index is the same as the input, but with slice(None)
45 replaced to the original slicer where a 1D filter has been applied and
46 one less element where a zero-dimensional filter has been applied.
47 """
48 from dask.array._array_expr._collection import Array
49
50 assert len(index) == x.ndim
51 fancy_indexes = [
52 isinstance(idx, (tuple, list))
53 or (isinstance(idx, (np.ndarray, Array)) and idx.ndim > 0)
54 for idx in index
55 ]
56 if sum(fancy_indexes) > 1:
57 raise NotImplementedError("Don't yet support nd fancy indexing")
58
59 out_index = []
60 dropped_axis_cnt = 0
61 for in_axis, idx in enumerate(index):
62 out_axis = in_axis - dropped_axis_cnt
63 if isinstance(idx, Array) and idx.dtype.kind in "iu":
64 if idx.ndim == 0:
65 idx = idx[np.newaxis]
66 x = slice_with_int_dask_array_on_axis(x, idx, out_axis, in_axis)
67 x = x[tuple(0 if i == out_axis else slice(None) for i in range(x.ndim))]
68 dropped_axis_cnt += 1
69 elif idx.ndim == 1:
70 x = slice_with_int_dask_array_on_axis(x, idx, out_axis, in_axis)
71 out_index.append(slice(None))
72 else:
73 raise NotImplementedError(
74 "Slicing with dask.array of ints only permitted when "
75 "the indexer has zero or one dimensions"
76 )
77 else:
78 out_index.append(idx)
79 return x, tuple(out_index)
80
81
82def normalize_index(idx, shape):

Callers 1

__getitem__Method · 0.90

Calls 2

sumFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…