Skip to content

Commit

Permalink
unique_values1d through numpy and awkward functions + 1 test
Browse files Browse the repository at this point in the history
  • Loading branch information
ohrechykha committed Jul 12, 2024
1 parent c0693c2 commit a509214
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
5 changes: 3 additions & 2 deletions tests/test_unique_values1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]))
25 changes: 12 additions & 13 deletions tests/unique_values1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a509214

Please sign in to comment.