Skip to content

Commit

Permalink
_array_object changes, empty array handling + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ohrechykha committed Sep 10, 2024
1 parent 7fb48f1 commit 6e8b6ee
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/ragged/_spec_array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import numpy as np
from awkward.contents import (
Content,
EmptyArray,
ListArray,
ListOffsetArray,
NumpyArray,
Expand Down Expand Up @@ -44,7 +45,10 @@ def _shape_dtype(layout: Content) -> tuple[Shape, Dtype]:
else:
shape = (*shape, None)
node = node.content

if isinstance(node, EmptyArray):
node = node.to_NumpyArray(dtype=np.float64)
shape = shape + node.data.shape[1:]
return shape, node.data.dtype
if isinstance(node, NumpyArray):
shape = shape + node.data.shape[1:]
return shape, node.data.dtype
Expand Down
22 changes: 22 additions & 0 deletions src/ragged/_spec_set_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ def unique_all(x: array, /) -> tuple[array, array, array, array]:
)
else:
x_flat = ak.ravel(x._impl)
if isinstance(x_flat.layout, ak.contents.EmptyArray):
return unique_all_result(
values=ragged.array([]),
indices=ragged.array([]),
inverse_indices=ragged.array([]),
counts=ragged.array([]),
)
values, indices, inverse_indices, counts = np.unique(
x_flat.layout.data,
return_index=True,
Expand Down Expand Up @@ -113,6 +120,10 @@ def unique_counts(x: array, /) -> tuple[array, array]:
)
else:
x_flat = ak.ravel(x._impl)
if isinstance(x_flat.layout, ak.contents.EmptyArray):
return unique_counts_result(
values=ragged.array([]), counts=ragged.array([])
)
values, counts = np.unique(x_flat.layout.data, return_counts=True)
return unique_counts_result(
values=ragged.array(values), counts=ragged.array(counts)
Expand Down Expand Up @@ -155,6 +166,10 @@ def unique_inverse(x: array, /) -> tuple[array, array]:
)
else:
x_flat = ak.ravel(x._impl)
if isinstance(x_flat.layout, ak.contents.EmptyArray):
return unique_inverse_result(
values=ragged.array([]), inverse_indices=ragged.array([])
)
values, inverse_indices = np.unique(x_flat.layout.data, return_inverse=True)

return unique_inverse_result(
Expand Down Expand Up @@ -187,6 +202,13 @@ def unique_values(x: array, /) -> array:

else:
x_flat = ak.ravel(x._impl)
if isinstance(x_flat.layout, ak.contents.EmptyArray):
return ragged.array([])
# print("x._impl type is", type(x._impl))
# print("x_flat type is", type(x_flat))
# print("x_flat laoyut is", x_flat.layout)
# print("x_flat layout type is", type(x_flat.layout))
# print("x_flat layout data type is", type(x_flat.layout.data))
return ragged.array(np.unique(x_flat.layout.data))
else:
err = f"Expected ragged type but got {type(x)}" # type: ignore[unreachable]
Expand Down
46 changes: 39 additions & 7 deletions tests/test_spec_set_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ def test_can_take_list():


def test_can_take_empty_arr():
# with pytest.raises(TypeError):
arr = ragged.array([])
expected_unique_values = ragged.array([0])
expected_unique_values = ragged.array([])
unique_values = ragged.unique_values(arr)
assert ak.to_list(expected_unique_values) == ak.to_list(unique_values)

Expand Down Expand Up @@ -71,6 +70,15 @@ def test_can_count_list():
assert ak.to_list(unique_counts) == ak.to_list(expected_unique_counts)


def test_can_count_empty_arr():
arr = ragged.array([])
expected_unique_values = ragged.array([])
expected_counts = ragged.array([])
unique_values, unique_counts = ragged.unique_counts(arr)
assert ak.to_list(expected_unique_values) == ak.to_list(unique_values)
assert ak.to_list(expected_counts) == ak.to_list(unique_counts)


def test_can_count_simple_array():
arr = ragged.array([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
expected_unique_values = ragged.array([1, 2, 3, 4])
Expand Down Expand Up @@ -110,11 +118,20 @@ def test_can_count_scalar_float():
# unique_inverse tests
def test_can_inverse_list():
arr = ragged.array([1, 2, 4, 3, 4, 5, 6, 20])
expected_values = ragged.array([1, 2, 3, 4, 5, 6, 20])
expected_inverse = ragged.array([0, 1, 3, 2, 3, 4, 5, 6])
values, inverse = ragged.unique_inverse(arr)
assert ak.to_list(expected_values) == ak.to_list(values)
assert ak.to_list(expected_inverse) == ak.to_list(inverse)
expected_unique_values = ragged.array([1, 2, 3, 4, 5, 6, 20])
expected_inverse_indices = ragged.array([0, 1, 3, 2, 3, 4, 5, 6])
unique_values, inverse_indices = ragged.unique_inverse(arr)
assert ak.to_list(unique_values) == ak.to_list(expected_unique_values)
assert ak.to_list(inverse_indices) == ak.to_list(expected_inverse_indices)


def test_can_inverse_empty_arr():
arr = ragged.array([])
expected_unique_values = ragged.array([])
expected_inverse_indices = ragged.array([])
unique_values, inverse_indices = ragged.unique_inverse(arr)
assert ak.to_list(unique_values) == ak.to_list(expected_unique_values)
assert ak.to_list(inverse_indices) == ak.to_list(expected_inverse_indices)


def test_can_inverse_simple_array():
Expand Down Expand Up @@ -169,6 +186,21 @@ def test_can_all_list():
assert ak.to_list(unique_counts) == ak.to_list(expected_unique_counts)


def test_can_all_empty_arr():
arr = ragged.array([])
expected_unique_values = ragged.array([])
expected_unique_indices = ragged.array([])
expected_unique_inverse = ragged.array([])
expected_unique_counts = ragged.array([])
unique_values, unique_indices, unique_inverse, unique_counts = ragged.unique_all(
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)
assert ak.to_list(unique_inverse) == ak.to_list(expected_unique_inverse)
assert ak.to_list(unique_counts) == ak.to_list(expected_unique_counts)


def test_can_all_normal_array():
arr = ragged.array([[2, 2, 2], [3], [3, 5], [4, 4, 4], [4]])
expected_unique_values = ragged.array([2, 3, 4, 5])
Expand Down

0 comments on commit 6e8b6ee

Please sign in to comment.