Point wise indexing with broadcasting. >>> x = np.arange(56).reshape((7, 8)) >>> x array([[ 0, 1, 2, 3, 4, 5, 6, 7], [ 8, 9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29, 30, 31], [32, 33, 34, 35,
(x, *indexes)
| 5795 | |
| 5796 | |
| 5797 | def _vindex(x, *indexes): |
| 5798 | """Point wise indexing with broadcasting. |
| 5799 | |
| 5800 | >>> x = np.arange(56).reshape((7, 8)) |
| 5801 | >>> x |
| 5802 | array([[ 0, 1, 2, 3, 4, 5, 6, 7], |
| 5803 | [ 8, 9, 10, 11, 12, 13, 14, 15], |
| 5804 | [16, 17, 18, 19, 20, 21, 22, 23], |
| 5805 | [24, 25, 26, 27, 28, 29, 30, 31], |
| 5806 | [32, 33, 34, 35, 36, 37, 38, 39], |
| 5807 | [40, 41, 42, 43, 44, 45, 46, 47], |
| 5808 | [48, 49, 50, 51, 52, 53, 54, 55]]) |
| 5809 | |
| 5810 | >>> d = from_array(x, chunks=(3, 4)) |
| 5811 | >>> result = _vindex(d, [0, 1, 6, 0], [0, 1, 0, 7]) |
| 5812 | >>> result.compute() |
| 5813 | array([ 0, 9, 48, 7]) |
| 5814 | """ |
| 5815 | indexes = replace_ellipsis(x.ndim, indexes) |
| 5816 | |
| 5817 | nonfancy_indexes = [] |
| 5818 | reduced_indexes = [] |
| 5819 | for ind in indexes: |
| 5820 | if isinstance(ind, Number): |
| 5821 | nonfancy_indexes.append(ind) |
| 5822 | elif isinstance(ind, slice): |
| 5823 | nonfancy_indexes.append(ind) |
| 5824 | reduced_indexes.append(slice(None)) |
| 5825 | else: |
| 5826 | nonfancy_indexes.append(slice(None)) |
| 5827 | reduced_indexes.append(ind) |
| 5828 | |
| 5829 | nonfancy_indexes = tuple(nonfancy_indexes) |
| 5830 | reduced_indexes = tuple(reduced_indexes) |
| 5831 | |
| 5832 | x = x[nonfancy_indexes] |
| 5833 | |
| 5834 | array_indexes = {} |
| 5835 | for i, (ind, size) in enumerate(zip(reduced_indexes, x.shape)): |
| 5836 | if not isinstance(ind, slice): |
| 5837 | ind = np.array(ind, copy=True) |
| 5838 | if ind.dtype.kind == "b": |
| 5839 | raise IndexError("vindex does not support indexing with boolean arrays") |
| 5840 | if ((ind >= size) | (ind < -size)).any(): |
| 5841 | raise IndexError( |
| 5842 | "vindex key has entries out of bounds for " |
| 5843 | f"indexing along axis {i} of size {size}: {ind!r}" |
| 5844 | ) |
| 5845 | ind %= size |
| 5846 | array_indexes[i] = ind |
| 5847 | |
| 5848 | if array_indexes: |
| 5849 | x = _vindex_array(x, array_indexes) |
| 5850 | |
| 5851 | return x |
| 5852 | |
| 5853 | |
| 5854 | def _vindex_array(x, dict_indexes): |
no test coverage detected
searching dependent graphs…