diff --git a/tests/unique_counts.py b/tests/unique_counts.py new file mode 100644 index 0000000..89690e2 --- /dev/null +++ b/tests/unique_counts.py @@ -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 \ No newline at end of file diff --git a/tests/unique_inverse.py b/tests/unique_inverse.py new file mode 100644 index 0000000..9f951b9 --- /dev/null +++ b/tests/unique_inverse.py @@ -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 \ No newline at end of file