unique#

named_arrays.unique(a, axis_new, return_inverse=False, return_counts=False, equal_nan=True)#

Thin wrapper around numpy.unique().

Gather the distinct values of an array, in increasing order, along a new axis.

Every axis of a is consumed, since which values repeat is a question about the array as a whole, and the answer has no length known in advance. The new axis is named by axis_new, which is why this function takes that name instead of the axis every other function here takes.

The return_index argument of numpy.unique() has no counterpart, since it indexes the flattened array, and a flat index means nothing to an array whose axes are named.

Only scalars support this function, where named_arrays.searchsorted() and named_arrays.digitize() also accept uncertain scalars and vectors. How many distinct values there are is a property of the values themselves, so the samples of an uncertain array, or the components of a vector, would each give a different number of them, and the result would have no single length to put on axis_new.

Parameters:
  • a (AbstractArray) – The array to find the distinct values of.

  • axis_new (str) – The name of the new axis along which to gather them.

  • return_inverse (bool) – Whether to also return, for each element of a, the index along axis_new of the distinct value it is equal to. That array carries the axes of a.

  • return_counts (bool) – Whether to also return how many times each distinct value appears.

  • equal_nan (bool) – Whether to collapse the NaNs of a into a single NaN.

Returns:

  • The distinct values, or a tuple of them and whichever of the inverse

  • and the counts were asked for, in that order.

Return type:

AbstractArray | tuple[AbstractArray, …]

See also

numpy.unique()

The function this one wraps.

Examples

Find the distinct wavelengths of a grid which repeats itself.

import numpy as np
import astropy.units as u
import named_arrays as na

a = na.ScalarArray(
    ndarray=np.array([[100, 200, 200], [300, 100, 200]]) * u.AA,
    axes=("x", "y"),
)

na.unique(a, axis_new="wavelength")
ScalarArray(
    ndarray=[100., 200., 300.] Angstrom,
    shape={'wavelength': 3},
)

Count how often each of them appears.

na.unique(a, axis_new="wavelength", return_counts=True)
(ScalarArray(
     ndarray=[100., 200., 300.] Angstrom,
     shape={'wavelength': 3},
 ),
 ScalarArray(
     ndarray=[2, 3, 1],
     shape={'wavelength': 3},
 ))