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)
| 937 | |
| 938 | |
| 939 | def slice_with_int_dask_array(x, index): |
| 940 | """Slice x with at most one 1D dask arrays of ints. |
| 941 | |
| 942 | This is a helper function of :meth:`Array.__getitem__`. |
| 943 | |
| 944 | Parameters |
| 945 | ---------- |
| 946 | x: Array |
| 947 | index: tuple with as many elements as x.ndim, among which there are |
| 948 | one or more Array's with dtype=int |
| 949 | |
| 950 | Returns |
| 951 | ------- |
| 952 | tuple of (sliced x, new index) |
| 953 | |
| 954 | where the new index is the same as the input, but with slice(None) |
| 955 | replaced to the original slicer where a 1D filter has been applied and |
| 956 | one less element where a zero-dimensional filter has been applied. |
| 957 | """ |
| 958 | from dask.array.core import Array |
| 959 | |
| 960 | assert len(index) == x.ndim |
| 961 | fancy_indexes = [ |
| 962 | isinstance(idx, (tuple, list)) |
| 963 | or (isinstance(idx, (np.ndarray, Array)) and idx.ndim > 0) |
| 964 | for idx in index |
| 965 | ] |
| 966 | if sum(fancy_indexes) > 1: |
| 967 | raise NotImplementedError("Don't yet support nd fancy indexing") |
| 968 | |
| 969 | out_index = [] |
| 970 | dropped_axis_cnt = 0 |
| 971 | for in_axis, idx in enumerate(index): |
| 972 | out_axis = in_axis - dropped_axis_cnt |
| 973 | if isinstance(idx, Array) and idx.dtype.kind in "iu": |
| 974 | if idx.ndim == 0: |
| 975 | idx = idx[np.newaxis] |
| 976 | x = slice_with_int_dask_array_on_axis(x, idx, out_axis) |
| 977 | x = x[tuple(0 if i == out_axis else slice(None) for i in range(x.ndim))] |
| 978 | dropped_axis_cnt += 1 |
| 979 | elif idx.ndim == 1: |
| 980 | x = slice_with_int_dask_array_on_axis(x, idx, out_axis) |
| 981 | out_index.append(slice(None)) |
| 982 | else: |
| 983 | raise NotImplementedError( |
| 984 | "Slicing with dask.array of ints only permitted when " |
| 985 | "the indexer has zero or one dimensions" |
| 986 | ) |
| 987 | else: |
| 988 | out_index.append(idx) |
| 989 | return x, tuple(out_index) |
| 990 | |
| 991 | |
| 992 | def slice_with_int_dask_array_on_axis(x, idx, axis): |
no test coverage detected
searching dependent graphs…