spyrit.misc.sampling.reindex
- spyrit.misc.sampling.reindex(values: ndarray, indices: ndarray, axis: str = 'rows', inverse_permutation: bool = False) ndarray[source]
Sorts a tensor along a specified axis using the indices tensor.
The indices tensor contains the new indices of the elements in the values tensor. values[0] will be placed at the index indices[0], values[1] at indices[1], and so on.
- Args:
values (np.ndarray): Array to sort. Can be 1D, 2D, or any multi-dimensional batch of 2D arrays.
indices (np.ndarray): Array containing the new indices of the elements contained in values.
axis (str, optional): The axis to sort along. Must be either ‘rows’, ‘cols’ or None. If None, values is flattened before sorting, and then reshaped to its original shape. If values is 1D, axis is not used. Default is ‘rows’.
inverse_permutation (bool, optional): Whether to apply the permutation inverse. Default is False.
- Raises:
ValueError: If axis is not ‘rows’ or ‘cols’.
- Returns:
np.ndarray: Array ordered by the given indices along the specified axis. The type is the same as the input array values.
- Example:
>>> values = np.array([[10, 20, 30], [100, 200, 300]]) >>> indices = np.array(2, 0, 1]) >>> reindex(values, indices, axis="cols") array([[ 20, 30, 10], [200, 300, 100]])