Skip to content

Commit fb86e3c

Browse files
Add ValueArray.unique (#70)
Add a performant unique method.
2 parents 95e698b + 8edbc89 commit fb86e3c

3 files changed

Lines changed: 11 additions & 0 deletions

File tree

test/test_value_array.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,10 @@ def test_ufunc() -> None:
286286
_ = np.subtract(x, y)
287287
with pytest.raises(UnitMismatchError):
288288
_ = np.subtract(y, x)
289+
290+
291+
def test_unique() -> None:
292+
xs = np.array([3.7, 0.9, 0.9, 4.3, 3.7])
293+
unit = tu.MHz
294+
v_arr = xs * unit
295+
assert np.array_equal(v_arr.unique(), np.unique(xs) * unit)

tunits/core/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ class ValueArray(Generic[ValueType2], WithUnit):
334334
def to_proto(self, msg: tunits_pb2.ValueArray | None = None) -> tunits_pb2.ValueArray: ...
335335
def __init__(self, data: Any, unit: Any = None) -> None: ...
336336
def allclose(self, other: ValueArray | Value, *args: Any, **kwargs: float) -> bool: ...
337+
def unique(self: ArrayType) -> ArrayType: ...
337338
def __array__(self, dtype: DTypeLike = None) -> NDArray[Any]: ...
338339
def __array_wrap__(self, out_arr: NDArray[Any], context: Any = None) -> NDArray[Any]: ...
339340
def __copy__(self: ArrayType) -> ArrayType: ...

tunits/core/cython/with_unit_value_array.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ class ValueArray(WithUnit):
150150
def allclose(WithUnit self, other, *args, **kw) -> bool:
151151
return np.allclose(self.value, other[self.unit], *args, **kw)
152152

153+
def unique(WithUnit self):
154+
return self.__with_value(np.unique(self.value))
155+
153156
@classmethod
154157
def from_proto(cls: type[T], msg: 'tunits_pb2.ValueArray') -> T:
155158
return cls(_ndarray_from_proto(msg), _proto_to_units(msg.units))

0 commit comments

Comments
 (0)