Skip to content

Commit

Permalink
Fix: ak._v2.is_none check for axis value (#1539)
Browse files Browse the repository at this point in the history
* Added an axis value check in

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Renamed tests with PR nr and fixes pre-commit issues

* Fixed ak.is_none

* Fixed axis value and added more tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ioanaif and pre-commit-ci[bot] authored Jul 8, 2022
1 parent 818b373 commit 8c070be
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
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
File renamed without changes.
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]], []]

0 comments on commit 8c070be

Please sign in to comment.