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)
| 798 | |
| 799 | |
| 800 | def 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: |
no test coverage detected
searching dependent graphs…