MCPcopy
hub / github.com/dask/dask / normalize_index

Function normalize_index

dask/array/slicing.py:800–866  ·  view source on GitHub ↗

Normalize slicing indexes 1. Replaces ellipses with many full slices 2. Adds full slices to end of index 3. Checks bounding conditions 4. Replace multidimensional numpy arrays with dask arrays 5. Replaces numpy arrays with lists 6. Posify's integers and lists 7. N

(idx, shape)

Source from the content-addressed store, hash-verified

798
799
800def normalize_index(idx, shape):
801 """Normalize slicing indexes
802
803 1. Replaces ellipses with many full slices
804 2. Adds full slices to end of index
805 3. Checks bounding conditions
806 4. Replace multidimensional numpy arrays with dask arrays
807 5. Replaces numpy arrays with lists
808 6. Posify's integers and lists
809 7. Normalizes slices to canonical form
810
811 Examples
812 --------
813 >>> normalize_index(1, (10,))
814 (1,)
815 >>> normalize_index(-1, (10,))
816 (9,)
817 >>> normalize_index([-1], (10,))
818 (array([9]),)
819 >>> normalize_index(slice(-3, 10, 1), (10,))
820 (slice(7, None, None),)
821 >>> normalize_index((Ellipsis, None), (10,))
822 (slice(None, None, None), None)
823 >>> normalize_index(np.array([[True, False], [False, True], [True, True]]), (3, 2))
824 (dask.array<array, shape=(3, 2), dtype=bool, chunksize=(3, 2), chunktype=numpy.ndarray>,)
825 """
826 from dask.array import Array, from_array
827
828 if not isinstance(idx, tuple):
829 idx = (idx,)
830
831 # if a > 1D numpy.array is provided, cast it to a dask array
832 if len(idx) > 0 and len(shape) > 1:
833 i = idx[0]
834 if is_arraylike(i) and not isinstance(i, Array) and i.shape == shape:
835 idx = (from_array(i), *idx[1:])
836
837 idx = replace_ellipsis(len(shape), idx)
838 n_sliced_dims = 0
839 for i in idx:
840 if hasattr(i, "ndim") and i.ndim >= 1:
841 n_sliced_dims += i.ndim
842 elif i is None:
843 continue
844 else:
845 n_sliced_dims += 1
846
847 idx = idx + (slice(None),) * (len(shape) - n_sliced_dims)
848 if len([i for i in idx if i is not None]) > len(shape):
849 raise IndexError("Too many indices for array")
850
851 none_shape = []
852 i = 0
853 for ind in idx:
854 if ind is not None:
855 none_shape.append(shape[i])
856 i += 1
857 else:

Callers 4

__getitem__Method · 0.90
__getitem__Method · 0.90
parse_assignment_indicesFunction · 0.70

Calls 5

is_arraylikeFunction · 0.90
from_arrayFunction · 0.90
replace_ellipsisFunction · 0.85
check_indexFunction · 0.85
posify_indexFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…