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