Skip to content

Commit

Permalink
adding inverse and counts functions written via numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
ohrechykha committed Jul 15, 2024
1 parent f7753cb commit 203124b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/unique_counts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def unique_counts(arr):
if not isinstance(arr, ragged.array):
print("Input is not a ragged array")

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

arr_flat=ak.ravel(arr)
arr_np = ak.to_numpy(arr_flat)
unique_elements, counts = np.unique(arr_np, return_counts=True)
unique_arr_counts = list(zip(unique_elements, counts))

return unique_arr_counts
19 changes: 19 additions & 0 deletions tests/unique_inverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def unique_inverse(arr):
if not isinstance(arr, ragged.array):
print("Input is not a ragged array")

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

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

sorted_indices = np.argsort(first_indices)
unique_elements = unique_elements[sorted_indices]
first_indices = first_indices[sorted_indices]

unique_arr = ak.from_numpy(unique_elements)
unique_indices_arr = ak.from_numpy(first_indices)

return unique_arr, unique_indices_arr

0 comments on commit 203124b

Please sign in to comment.