From a50921402e7a2231f8ed370e87182fa474a8bdb0 Mon Sep 17 00:00:00 2001 From: Oleksii Hrechykha Date: Fri, 12 Jul 2024 08:51:05 +0300 Subject: [PATCH] unique_values1d through numpy and awkward functions + 1 test --- tests/test_unique_values1d.py | 5 +++-- tests/unique_values1d.py | 25 ++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/test_unique_values1d.py b/tests/test_unique_values1d.py index 0e2d0a9..11d3333 100644 --- a/tests/test_unique_values1d.py +++ b/tests/test_unique_values1d.py @@ -15,9 +15,10 @@ def test_can_take_empty_arr(): assert unique_values1d(ragged.array()) def test_can_take_moredimensions(): - assert unique_values1d(ragged.array([1,2,3,4,[5,6]])) + assert unique_values1d(ragged.array([[1,2,3,4],[5,6]])) def test_can_take_1d_array(): assert unique_values1d(ragged.array([5,6,7,8,8,9,1,2,3,4,10,0,15,2]))==ragged.array([0,1,2,3,4,5,6,7,8,9,10,15]) - +def test_can_take_awkward(): + assert unique_values1d(ak.Array([1,2,3,4,[5,6,7]])) diff --git a/tests/unique_values1d.py b/tests/unique_values1d.py index 7894695..d1bfbf6 100644 --- a/tests/unique_values1d.py +++ b/tests/unique_values1d.py @@ -3,18 +3,17 @@ import ragged def unique_values1d(arr): - if not isinstance(arr,ragged.array): - print("input is not a ragged array") + if not isinstance(arr, ragged.array): + print("Input is not a ragged array") + if arr.ndim != 1: - print("input is not a 1D array") + print("Input is not a 1D array") - arr_list=ak.to_list(arr) - arr_list.sort() - - seen=set() - unique=[] - for element in arr_list: - if element not in seen: - unique.append(element) - seen.add(element) - return ragged.array(unique) + if len(arr)==1: + return arr + + arr_np = ak.to_numpy(arr) + unique_np = np.unique(arr_np) + unique_arr = ak.from_numpy(unique_np) + + return unique_arr