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

Fix: ak._v2.is_none check for axis value #1539

Merged
merged 6 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/awkward/_v2/operations/ak_is_none.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ def is_none(array, axis=0, highlevel=True, behavior=None):


def _impl(array, axis, highlevel, behavior):

# Determine the (potentially nested) bytemask
def getfunction_inner(layout, depth, **kwargs):

if not isinstance(layout, ak._v2.contents.Content):
return

Expand Down Expand Up @@ -70,6 +72,11 @@ def getfunction_outer(layout, depth, depth_context, **kwargs):
return layout.recursively_apply(getfunction_inner)

layout = ak._v2.operations.to_layout(array)
max_axis = layout.branch_depth[1] - 1
if axis > max_axis:
raise ak._v2._util.error(
np.AxisError(f"axis={axis} exceeds the depth ({max_axis}) of this array")
)
behavior = ak._v2._util.behavior_of(array, behavior=behavior)
depth_context = {"posaxis": axis}
out = layout.recursively_apply(getfunction_outer, depth_context=depth_context)
Expand Down
76 changes: 76 additions & 0 deletions tests/v2/test_0193-is_none-axis-parameter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE

import pytest # noqa: F401
import numpy as np # noqa: F401
import awkward as ak # noqa: F401


def test():
assert ak._v2.operations.is_none(ak._v2.Array([1, 2, 3, None, 5])).tolist() == [
False,
False,
False,
True,
False,
]
assert ak._v2.operations.is_none(
ak._v2.Array([[1, 2, 3], [], [None, 5]])
).tolist() == [
False,
False,
False,
]
assert ak._v2.operations.is_none(
ak._v2.Array([[1, 2, 3], [], [None, 5]]), axis=1
).tolist() == [
[False, False, False],
[],
[True, False],
]
assert ak._v2.operations.is_none(
ak._v2.Array([[1, None, 2, 3], [], [None, 5]]), axis=1
).tolist() == [
[False, True, False, False],
[],
[True, False],
]
assert ak._v2.operations.is_none(
ak._v2.Array([[1, None, 2, 3], [], [None, 5]]), axis=-1
).tolist() == [
[False, True, False, False],
[],
[True, False],
]
assert ak._v2.operations.is_none(
ak._v2.Array([[1, None, 2, 3], [], [None, 5]]), axis=-2
).tolist() == [
False,
False,
False,
]
assert ak._v2.operations.is_none(
ak._v2.Array([[1, None, 2, 3], None, [None, 5]]), axis=-2
).tolist() == [False, True, False]
with pytest.raises(ValueError):
ak._v2.operations.is_none(
ak._v2.Array([[1, None, 2, 3], [], [None, 5]]), axis=-3
)

one = ak._v2.operations.from_iter([1, None, 3], highlevel=False)
two = ak._v2.operations.from_iter([[], [1], None, [3, 3, 3]], highlevel=False)
tags = ak._v2.index.Index8(np.array([0, 1, 1, 0, 0, 1, 1], dtype=np.int8))
index = ak._v2.index.Index64(np.array([0, 0, 1, 1, 2, 2, 3], dtype=np.int64))
array = ak._v2.Array(
ak._v2.contents.UnionArray(tags, index, [one, two]), check_valid=True
)
assert ak._v2.to_list(array) == [1, [], [1], None, 3, None, [3, 3, 3]]

assert ak._v2.to_list(ak._v2.operations.is_none(array)) == [
False,
False,
False,
True,
False,
True,
False,
]
20 changes: 20 additions & 0 deletions tests/v2/test_0863-is-none-numpy-array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE

import pytest # noqa: F401
import numpy as np # noqa: F401
import awkward as ak # noqa: F401


def test_numpy_array():
x = np.arange(12)
y = ak._v2.operations.is_none(x)

assert y.tolist() == [False] * 12


def test_awkward_from_numpy_array():
x = np.arange(12)
y = ak._v2.operations.from_numpy(x)
z = ak._v2.operations.is_none(y)

assert z.tolist() == [False] * 12
26 changes: 26 additions & 0 deletions tests/v2/test_1539-isnone-axis-check-issue1417.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE

import pytest # noqa: F401
import awkward as ak # noqa: F401

to_list = ak._v2.operations.to_list


def test_1417issue_is_none_check_axis():
array = ak._v2.Array([[[1, 2, None], None], None])

assert to_list(ak._v2.is_none(array, axis=0)) == [False, True]
assert to_list(ak._v2.is_none(array, axis=1)) == [[False, True], None]
assert to_list(ak._v2.is_none(array, axis=2)) == [
[[False, False, True], None],
None,
]

with pytest.raises(ValueError):
ak._v2.is_none(array, axis=3)

assert str(ak._v2.type(ak._v2.is_none(array, axis=0)[0])) == "bool"

array = ak._v2.Array([[[[[1], [3]], [1]]], []])

assert to_list(ak._v2.is_none(array, axis=2)) == [[[False, False]], []]