|
2 | 2 | These are functions that are just aliases of existing functions in NumPy.
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from typing import TYPE_CHECKING |
| 8 | +if TYPE_CHECKING: |
| 9 | + from typing import Tuple |
| 10 | + from numpy import ndarray, dtype |
| 11 | + |
| 12 | +from typing import NamedTuple |
| 13 | + |
5 | 14 | from numpy import (arccos, arccosh, arcsin, arcsinh, arctan, arctan2, arctanh,
|
6 |
| - left_shift, invert, right_shift, bool_, concatenate, power) |
| 15 | + left_shift, invert, right_shift, bool_, concatenate, power, |
| 16 | + transpose, unique) |
7 | 17 |
|
| 18 | +# Basic renames |
8 | 19 | acos = arccos
|
9 | 20 | acosh = arccosh
|
10 | 21 | asin = arcsin
|
|
18 | 29 | bool = bool_
|
19 | 30 | concat = concatenate
|
20 | 31 | pow = power
|
| 32 | + |
| 33 | +# These functions are modified from the NumPy versions. |
| 34 | + |
| 35 | +# Unlike transpose(), the axes argument to permute_dims() is required. |
| 36 | +def permute_dims(x: ndarray, /, axes: Tuple[int, ...]) -> ndarray: |
| 37 | + return transpose(x, axes) |
| 38 | + |
| 39 | +# np.unique() is split into four functions in the array API: |
| 40 | +# unique_all, unique_counts, unique_inverse, and unique_values (this is done |
| 41 | +# to remove polymorphic return types). |
| 42 | + |
| 43 | +# The functions here return namedtuples (np.unique() returns a normal |
| 44 | +# tuple). |
| 45 | +class UniqueAllResult(NamedTuple): |
| 46 | + values: ndarray |
| 47 | + indices: ndarray |
| 48 | + inverse_indices: ndarray |
| 49 | + counts: ndarray |
| 50 | + |
| 51 | + |
| 52 | +class UniqueCountsResult(NamedTuple): |
| 53 | + values: ndarray |
| 54 | + counts: ndarray |
| 55 | + |
| 56 | + |
| 57 | +class UniqueInverseResult(NamedTuple): |
| 58 | + values: ndarray |
| 59 | + inverse_indices: ndarray |
| 60 | + |
| 61 | + |
| 62 | +def unique_all(x: ndarray, /) -> UniqueAllResult: |
| 63 | + values, indices, inverse_indices, counts = unique( |
| 64 | + x, |
| 65 | + return_counts=True, |
| 66 | + return_index=True, |
| 67 | + return_inverse=True, |
| 68 | + equal_nan=False, |
| 69 | + ) |
| 70 | + # np.unique() flattens inverse indices, but they need to share x's shape |
| 71 | + # See https://github.com/numpy/numpy/issues/20638 |
| 72 | + inverse_indices = inverse_indices.reshape(x.shape) |
| 73 | + return UniqueAllResult( |
| 74 | + values, |
| 75 | + indices, |
| 76 | + inverse_indices, |
| 77 | + counts, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +def unique_counts(x: ndarray, /) -> UniqueCountsResult: |
| 82 | + res = unique( |
| 83 | + x, |
| 84 | + return_counts=True, |
| 85 | + return_index=False, |
| 86 | + return_inverse=False, |
| 87 | + equal_nan=False, |
| 88 | + ) |
| 89 | + |
| 90 | + return UniqueCountsResult(*res) |
| 91 | + |
| 92 | + |
| 93 | +def unique_inverse(x: ndarray, /) -> UniqueInverseResult: |
| 94 | + values, inverse_indices = unique( |
| 95 | + x, |
| 96 | + return_counts=False, |
| 97 | + return_index=False, |
| 98 | + return_inverse=True, |
| 99 | + equal_nan=False, |
| 100 | + ) |
| 101 | + # np.unique() flattens inverse indices, but they need to share x's shape |
| 102 | + # See https://github.com/numpy/numpy/issues/20638 |
| 103 | + inverse_indices = inverse_indices.reshape(x.shape) |
| 104 | + return UniqueInverseResult(values, inverse_indices) |
| 105 | + |
| 106 | + |
| 107 | +def unique_values(x: ndarray, /) -> ndarray: |
| 108 | + return unique( |
| 109 | + x, |
| 110 | + return_counts=False, |
| 111 | + return_index=False, |
| 112 | + return_inverse=False, |
| 113 | + equal_nan=False, |
| 114 | + ) |
| 115 | + |
| 116 | +def astype(x: ndarray, dtype: dtype, /, *, copy: bool = True) -> ndarray: |
| 117 | + if not copy and dtype == x.dtype: |
| 118 | + return x |
| 119 | + return x.astype(dtype=dtype, copy=copy) |
0 commit comments