_set_functions.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from __future__ import annotations
  2. from ._array_object import Array
  3. from typing import NamedTuple
  4. import numpy as np
  5. # Note: np.unique() is split into four functions in the array API:
  6. # unique_all, unique_counts, unique_inverse, and unique_values (this is done
  7. # to remove polymorphic return types).
  8. # Note: The various unique() functions are supposed to return multiple NaNs.
  9. # This does not match the NumPy behavior, however, this is currently left as a
  10. # TODO in this implementation as this behavior may be reverted in np.unique().
  11. # See https://github.com/numpy/numpy/issues/20326.
  12. # Note: The functions here return a namedtuple (np.unique() returns a normal
  13. # tuple).
  14. class UniqueAllResult(NamedTuple):
  15. values: Array
  16. indices: Array
  17. inverse_indices: Array
  18. counts: Array
  19. class UniqueCountsResult(NamedTuple):
  20. values: Array
  21. counts: Array
  22. class UniqueInverseResult(NamedTuple):
  23. values: Array
  24. inverse_indices: Array
  25. def unique_all(x: Array, /) -> UniqueAllResult:
  26. """
  27. Array API compatible wrapper for :py:func:`np.unique <numpy.unique>`.
  28. See its docstring for more information.
  29. """
  30. values, indices, inverse_indices, counts = np.unique(
  31. x._array,
  32. return_counts=True,
  33. return_index=True,
  34. return_inverse=True,
  35. equal_nan=False,
  36. )
  37. # np.unique() flattens inverse indices, but they need to share x's shape
  38. # See https://github.com/numpy/numpy/issues/20638
  39. inverse_indices = inverse_indices.reshape(x.shape)
  40. return UniqueAllResult(
  41. Array._new(values),
  42. Array._new(indices),
  43. Array._new(inverse_indices),
  44. Array._new(counts),
  45. )
  46. def unique_counts(x: Array, /) -> UniqueCountsResult:
  47. res = np.unique(
  48. x._array,
  49. return_counts=True,
  50. return_index=False,
  51. return_inverse=False,
  52. equal_nan=False,
  53. )
  54. return UniqueCountsResult(*[Array._new(i) for i in res])
  55. def unique_inverse(x: Array, /) -> UniqueInverseResult:
  56. """
  57. Array API compatible wrapper for :py:func:`np.unique <numpy.unique>`.
  58. See its docstring for more information.
  59. """
  60. values, inverse_indices = np.unique(
  61. x._array,
  62. return_counts=False,
  63. return_index=False,
  64. return_inverse=True,
  65. equal_nan=False,
  66. )
  67. # np.unique() flattens inverse indices, but they need to share x's shape
  68. # See https://github.com/numpy/numpy/issues/20638
  69. inverse_indices = inverse_indices.reshape(x.shape)
  70. return UniqueInverseResult(Array._new(values), Array._new(inverse_indices))
  71. def unique_values(x: Array, /) -> Array:
  72. """
  73. Array API compatible wrapper for :py:func:`np.unique <numpy.unique>`.
  74. See its docstring for more information.
  75. """
  76. res = np.unique(
  77. x._array,
  78. return_counts=False,
  79. return_index=False,
  80. return_inverse=False,
  81. equal_nan=False,
  82. )
  83. return Array._new(res)