Skip to content

Commit

Permalink
adding unique_inverse tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ohrechykha committed Jul 19, 2024
1 parent 203124b commit 82d4800
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
45 changes: 45 additions & 0 deletions tests/test_unique_inverse_55.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from unique_inverse import unique_inverse
import awkward as ak
import ragged

def test_can_take_none():
assert unique_inverse(None) is None

def test_can_take_list():
assert unique_inverse([1,2,4,3,4,5,6,20]) is None

def test_can_take_empty_arr():
arr = ragged.array([])
expected_unique_values = ragged.array([])
expected_unique_indices = ragged.array([])
unique_values, unique_indices = unique_inverse(arr)
assert ak.to_list(unique_values) == ak.to_list(expected_unique_values)
assert ak.to_list(unique_indices) == ak.to_list(expected_unique_indices)

def test_can_take_simple_array():
arr = ragged.array([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
expected_unique_values = ragged.array([1, 2, 3, 4])
expected_unique_indices = ragged.array([0, 1, 3, 6])
unique_values, unique_indices = unique_inverse(arr)
assert ak.to_list(unique_values) == ak.to_list(expected_unique_values)
assert ak.to_list(unique_indices) == ak.to_list(expected_unique_indices)

def test_can_take_normal_array():
arr = ragged.array([[1, 2, 2], [3], [3, 3], [4, 4, 4], [4]])
expected_unique_values = ragged.array([1, 2, 3, 4])
expected_unique_indices = ragged.array([0, 1, 3, 6])
unique_values, unique_indices = unique_inverse(arr)
assert ak.to_list(unique_values) == ak.to_list(expected_unique_values)
assert ak.to_list(unique_indices) == ak.to_list(expected_unique_indices)

def test_can_take_awkward():
arr = ragged.array([[[1, 2, 2], 3, 3], [3, 4, 4, 4, 4]])
assert unique_inverse(arr) is None

def test_can_take_scalar():
arr = ragged.array([5])
expected_unique_values = ragged.array([5])
expected_unique_indices = ragged.array([0])
unique_values, unique_indices = unique_inverse(arr)
assert ak.to_list(unique_values) == ak.to_list(expected_unique_values)
assert ak.to_list(unique_indices) == ak.to_list(expected_unique_indices)
File renamed without changes.
7 changes: 6 additions & 1 deletion tests/unique_counts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import awkward as ak
import ragged
import numpy as np


def unique_counts(arr):
if not isinstance(arr, ragged.array):
print("Input is not a ragged array")

return None
if len(arr)==1:
return [(arr[0], 1)]

Expand Down
10 changes: 7 additions & 3 deletions tests/unique_inverse.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import awkward as ak
import ragged
import numpy as np

def unique_inverse(arr):
if not isinstance(arr, ragged.array):
print("Input is not a ragged array")

return None
if len(arr) == 1:
return arr, np.array([0])
return (arr, np.array([0]))

arr_list=ak.ravel(arr)
arr_list=ak.ravel(arr._impl)
arr_np = ak.to_numpy(arr_list)
unique_elements, first_indices = np.unique(arr_np, return_index=True)

Expand Down
2 changes: 1 addition & 1 deletion tests/unique_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def unique_values(arr):
if len(arr)==1:
return arr

arr_flat=ak.ravel(arr)
arr_flat=ak.ravel(arr._impl)
arr_np = ak.to_numpy(arr_flat)
unique_np = np.unique(arr_np)
unique_arr = ak.from_numpy(unique_np)
Expand Down

0 comments on commit 82d4800

Please sign in to comment.