Perform a point-wise selection. `key` can be any of the following items: * A boolean array with the same shape as self. Those positions with True values will signal the coordinates to be returned. * A numpy array (or list or tuple) with the point coordinates.
(self, key: list | tuple | np.ndarray)
| 655 | return data |
| 656 | |
| 657 | def _point_selection(self, key: list | tuple | np.ndarray) -> np.ndarray: |
| 658 | """Perform a point-wise selection. |
| 659 | |
| 660 | `key` can be any of the following items: |
| 661 | |
| 662 | * A boolean array with the same shape as self. Those positions |
| 663 | with True values will signal the coordinates to be returned. |
| 664 | |
| 665 | * A numpy array (or list or tuple) with the point coordinates. |
| 666 | This has to be a two-dimensional array of size len(self.shape) |
| 667 | by num_elements containing a list of zero-based values |
| 668 | specifying the coordinates in the dataset of the selected |
| 669 | elements. The order of the element coordinates in the array |
| 670 | specifies the order in which the array elements are iterated |
| 671 | through when I/O is performed. Duplicate coordinate locations |
| 672 | are not checked for. |
| 673 | |
| 674 | Return the coordinates array. If this is not possible, raise a |
| 675 | `TypeError` so that the next selection method can be tried out. |
| 676 | |
| 677 | This is useful for whatever `Leaf` instance implementing a |
| 678 | point-wise selection. |
| 679 | |
| 680 | """ |
| 681 | input_key = key |
| 682 | if type(key) in (list, tuple): |
| 683 | if isinstance(key, tuple) and len(key) > len(self.shape): |
| 684 | raise IndexError(f"Invalid index or slice: {key!r}") |
| 685 | # Try to convert key to a numpy array. If not possible, |
| 686 | # a TypeError will be issued (to be catched later on). |
| 687 | try: |
| 688 | key = toarray(key) |
| 689 | except ValueError: |
| 690 | raise TypeError(f"Invalid index or slice: {key!r}") |
| 691 | elif not isinstance(key, np.ndarray): |
| 692 | raise TypeError(f"Invalid index or slice: {key!r}") |
| 693 | |
| 694 | # Protection against empty keys |
| 695 | if len(key) == 0: |
| 696 | return np.array([], dtype="i8") |
| 697 | |
| 698 | if key.dtype.kind == "b": |
| 699 | if not key.shape == self.shape: |
| 700 | raise IndexError( |
| 701 | "Boolean indexing array has incompatible shape" |
| 702 | ) |
| 703 | # Get the True coordinates (64-bit indices!) |
| 704 | coords = np.asarray(key.nonzero(), dtype="i8") |
| 705 | coords = np.transpose(coords) |
| 706 | elif key.dtype.kind == "i" or key.dtype.kind == "u": |
| 707 | if len(key.shape) > 2: |
| 708 | raise IndexError( |
| 709 | "Coordinate indexing array has incompatible shape" |
| 710 | ) |
| 711 | elif len(key.shape) == 2: |
| 712 | if key.shape[0] != len(self.shape): |
| 713 | raise IndexError( |
| 714 | "Coordinate indexing array has incompatible shape" |
no test coverage detected