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)
| 5592 | |
| 5593 | |
| 5594 | def _vindex(x, *indexes): |
| 5595 | """Point wise indexing with broadcasting. |
| 5596 | |
| 5597 | >>> x = np.arange(56).reshape((7, 8)) |
| 5598 | >>> x |
| 5599 | array([[ 0, 1, 2, 3, 4, 5, 6, 7], |
| 5600 | [ 8, 9, 10, 11, 12, 13, 14, 15], |
| 5601 | [16, 17, 18, 19, 20, 21, 22, 23], |
| 5602 | [24, 25, 26, 27, 28, 29, 30, 31], |
| 5603 | [32, 33, 34, 35, 36, 37, 38, 39], |
| 5604 | [40, 41, 42, 43, 44, 45, 46, 47], |
| 5605 | [48, 49, 50, 51, 52, 53, 54, 55]]) |
| 5606 | |
| 5607 | >>> d = from_array(x, chunks=(3, 4)) |
| 5608 | >>> result = _vindex(d, [0, 1, 6, 0], [0, 1, 0, 7]) |
| 5609 | >>> result.compute() |
| 5610 | array([ 0, 9, 48, 7]) |
| 5611 | """ |
| 5612 | indexes = replace_ellipsis(x.ndim, indexes) |
| 5613 | |
| 5614 | nonfancy_indexes = [] |
| 5615 | reduced_indexes = [] |
| 5616 | for ind in indexes: |
| 5617 | if isinstance(ind, Number): |
| 5618 | nonfancy_indexes.append(ind) |
| 5619 | elif isinstance(ind, slice): |
| 5620 | nonfancy_indexes.append(ind) |
| 5621 | reduced_indexes.append(slice(None)) |
| 5622 | else: |
| 5623 | nonfancy_indexes.append(slice(None)) |
| 5624 | reduced_indexes.append(ind) |
| 5625 | |
| 5626 | nonfancy_indexes = tuple(nonfancy_indexes) |
| 5627 | reduced_indexes = tuple(reduced_indexes) |
| 5628 | |
| 5629 | x = x[nonfancy_indexes] |
| 5630 | |
| 5631 | array_indexes = {} |
| 5632 | for i, (ind, size) in enumerate(zip(reduced_indexes, x.shape)): |
| 5633 | if not isinstance(ind, slice): |
| 5634 | ind = np.array(ind, copy=True) |
| 5635 | if ind.dtype.kind == "b": |
| 5636 | raise IndexError("vindex does not support indexing with boolean arrays") |
| 5637 | if ((ind >= size) | (ind < -size)).any(): |
| 5638 | raise IndexError( |
| 5639 | "vindex key has entries out of bounds for " |
| 5640 | "indexing along axis %s of size %s: %r" % (i, size, ind) |
| 5641 | ) |
| 5642 | ind %= size |
| 5643 | array_indexes[i] = ind |
| 5644 | |
| 5645 | if array_indexes: |
| 5646 | x = _vindex_array(x, array_indexes) |
| 5647 | |
| 5648 | return x |
| 5649 | |
| 5650 | |
| 5651 | def _vindex_array(x, dict_indexes): |
no test coverage detected