Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: unique_values function algorithm #55

Closed
wants to merge 11 commits into from
2 changes: 2 additions & 0 deletions tests/test_spec_linear_algebra_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ def test_existence():
assert ragged.matrix_transpose is not None
assert ragged.tensordot is not None
assert ragged.vecdot is not None


7 changes: 7 additions & 0 deletions tests/test_spec_set_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

import ragged

#Specific algorithm for unique_values:
#1 take an input array
#2 flatten input_array unless its 1d
#3 {remember the first element, loop through the rest of the list to see if there are copies
# if yes then discard it and repeat the step
# if not then add it to the output and repeat the step}
#4 once the cycle is over return an array of unique elements in the input array (the output must be of the same type as input array)

def test_existence():
assert ragged.unique_all is not None
Expand Down
23 changes: 23 additions & 0 deletions tests/test_unique_values1d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from unique_values1d import unique_values1d
import awkward as ak
import ragged

def test_can_take_none():
assert unique_values1d(None)==None

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

def test_can_take_0():
assert unique_values1d(ragged.array([0]))==ragged.array([0])

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]]))

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])


20 changes: 20 additions & 0 deletions tests/unique_values1d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
import awkward as ak
import ragged

def unique_values1d(arr):
if not isinstance(arr,ragged.array):
print("input is not a ragged array")
if arr.ndim != 1:
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)
ianna marked this conversation as resolved.
Show resolved Hide resolved
Loading