From aa0e1ba9ce4d7dcb0d69afdb04e688a89f0227e6 Mon Sep 17 00:00:00 2001 From: ioanaif Date: Fri, 8 Jul 2022 11:33:07 +0200 Subject: [PATCH 1/6] Added an axis value check in --- src/awkward/_v2/operations/ak_is_none.py | 11 ++++++++++- .../v2/test_1999-isnone-axis-check-bug1417.py | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/v2/test_1999-isnone-axis-check-bug1417.py diff --git a/src/awkward/_v2/operations/ak_is_none.py b/src/awkward/_v2/operations/ak_is_none.py index 665edc4420..183830c405 100644 --- a/src/awkward/_v2/operations/ak_is_none.py +++ b/src/awkward/_v2/operations/ak_is_none.py @@ -29,8 +29,18 @@ def is_none(array, axis=0, highlevel=True, behavior=None): def _impl(array, axis, highlevel, behavior): + layout = ak._v2.operations.to_layout(array) + if axis > layout.length: + raise ak._v2._util.error( + ValueError( + "cannot use axis={} on an array with {} axes".format( + axis, layout.length + ) + ) + ) # Determine the (potentially nested) bytemask def getfunction_inner(layout, depth, **kwargs): + if not isinstance(layout, ak._v2.contents.Content): return @@ -69,7 +79,6 @@ def getfunction_outer(layout, depth, depth_context, **kwargs): if depth_context["posaxis"] == depth - 1: return layout.recursively_apply(getfunction_inner) - layout = ak._v2.operations.to_layout(array) behavior = ak._v2._util.behavior_of(array, behavior=behavior) depth_context = {"posaxis": axis} out = layout.recursively_apply(getfunction_outer, depth_context=depth_context) diff --git a/tests/v2/test_1999-isnone-axis-check-bug1417.py b/tests/v2/test_1999-isnone-axis-check-bug1417.py new file mode 100644 index 0000000000..8dc0107991 --- /dev/null +++ b/tests/v2/test_1999-isnone-axis-check-bug1417.py @@ -0,0 +1,19 @@ +# 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 +import numpy as np + +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' From 1444eaeb21e09e87762babab060df7d029b5ec10 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 8 Jul 2022 09:36:06 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/v2/test_1999-isnone-axis-check-bug1417.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/v2/test_1999-isnone-axis-check-bug1417.py b/tests/v2/test_1999-isnone-axis-check-bug1417.py index 8dc0107991..4ad88f02b0 100644 --- a/tests/v2/test_1999-isnone-axis-check-bug1417.py +++ b/tests/v2/test_1999-isnone-axis-check-bug1417.py @@ -9,11 +9,15 @@ 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] + 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) + 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' + assert str(ak._v2.type(ak._v2.is_none(array, axis=0)[0])) == "bool" From 798fc9db5592683607c662a9a90cd747097eda5c Mon Sep 17 00:00:00 2001 From: ioanaif Date: Fri, 8 Jul 2022 12:44:07 +0200 Subject: [PATCH 3/6] Renamed tests with PR nr and fixes pre-commit issues --- src/awkward/_v2/operations/ak_is_none.py | 2 ++ ...st_1999-bug1406.py => test_1502-getitem-jagged-issue1406.py} | 0 ...heck-bug1417.py => test_1539-isnone-axis-check-issue1417.py} | 1 - 3 files changed, 2 insertions(+), 1 deletion(-) rename tests/v2/{test_1999-bug1406.py => test_1502-getitem-jagged-issue1406.py} (100%) rename tests/v2/{test_1999-isnone-axis-check-bug1417.py => test_1539-isnone-axis-check-issue1417.py} (97%) diff --git a/src/awkward/_v2/operations/ak_is_none.py b/src/awkward/_v2/operations/ak_is_none.py index 183830c405..6b4ebdab79 100644 --- a/src/awkward/_v2/operations/ak_is_none.py +++ b/src/awkward/_v2/operations/ak_is_none.py @@ -38,7 +38,9 @@ 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): diff --git a/tests/v2/test_1999-bug1406.py b/tests/v2/test_1502-getitem-jagged-issue1406.py similarity index 100% rename from tests/v2/test_1999-bug1406.py rename to tests/v2/test_1502-getitem-jagged-issue1406.py diff --git a/tests/v2/test_1999-isnone-axis-check-bug1417.py b/tests/v2/test_1539-isnone-axis-check-issue1417.py similarity index 97% rename from tests/v2/test_1999-isnone-axis-check-bug1417.py rename to tests/v2/test_1539-isnone-axis-check-issue1417.py index 4ad88f02b0..2c2e98b2c3 100644 --- a/tests/v2/test_1999-isnone-axis-check-bug1417.py +++ b/tests/v2/test_1539-isnone-axis-check-issue1417.py @@ -2,7 +2,6 @@ import pytest # noqa: F401 import awkward as ak # noqa: F401 -import numpy as np to_list = ak._v2.operations.to_list From d7569c075850cc2ee98ccea7069486996309b163 Mon Sep 17 00:00:00 2001 From: ioanaif Date: Fri, 8 Jul 2022 13:21:38 +0200 Subject: [PATCH 4/6] Fixed ak.is_none --- src/awkward/_v2/operations/ak_is_none.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/awkward/_v2/operations/ak_is_none.py b/src/awkward/_v2/operations/ak_is_none.py index 6b4ebdab79..c0da625758 100644 --- a/src/awkward/_v2/operations/ak_is_none.py +++ b/src/awkward/_v2/operations/ak_is_none.py @@ -29,18 +29,12 @@ def is_none(array, axis=0, highlevel=True, behavior=None): def _impl(array, axis, highlevel, behavior): - layout = ak._v2.operations.to_layout(array) - if axis > layout.length: + if axis > len(array): raise ak._v2._util.error( - ValueError( - "cannot use axis={} on an array with {} axes".format( - axis, layout.length - ) - ) + ValueError(f"cannot use axis={axis} on an array of length {len(array)}") ) # Determine the (potentially nested) bytemask - def getfunction_inner(layout, depth, **kwargs): if not isinstance(layout, ak._v2.contents.Content): @@ -81,6 +75,7 @@ def getfunction_outer(layout, depth, depth_context, **kwargs): if depth_context["posaxis"] == depth - 1: return layout.recursively_apply(getfunction_inner) + layout = ak._v2.operations.to_layout(array) behavior = ak._v2._util.behavior_of(array, behavior=behavior) depth_context = {"posaxis": axis} out = layout.recursively_apply(getfunction_outer, depth_context=depth_context) From 8c73c5ae5042fa55d26116c03da132c6cb89804b Mon Sep 17 00:00:00 2001 From: ioanaif Date: Fri, 8 Jul 2022 16:56:13 +0200 Subject: [PATCH 5/6] Fixed axis value and added more tests --- src/awkward/_v2/operations/ak_is_none.py | 9 +-- tests/v2/test_0193-is_none-axis-parameter.py | 65 +++++++++++++++++++ tests/v2/test_0863-is-none-numpy-array.py | 20 ++++++ .../test_1539-isnone-axis-check-issue1417.py | 4 ++ 4 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 tests/v2/test_0193-is_none-axis-parameter.py create mode 100644 tests/v2/test_0863-is-none-numpy-array.py diff --git a/src/awkward/_v2/operations/ak_is_none.py b/src/awkward/_v2/operations/ak_is_none.py index c0da625758..5f15a92ea7 100644 --- a/src/awkward/_v2/operations/ak_is_none.py +++ b/src/awkward/_v2/operations/ak_is_none.py @@ -29,10 +29,6 @@ def is_none(array, axis=0, highlevel=True, behavior=None): def _impl(array, axis, highlevel, behavior): - if axis > len(array): - raise ak._v2._util.error( - ValueError(f"cannot use axis={axis} on an array of length {len(array)}") - ) # Determine the (potentially nested) bytemask def getfunction_inner(layout, depth, **kwargs): @@ -76,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) diff --git a/tests/v2/test_0193-is_none-axis-parameter.py b/tests/v2/test_0193-is_none-axis-parameter.py new file mode 100644 index 0000000000..8375e47256 --- /dev/null +++ b/tests/v2/test_0193-is_none-axis-parameter.py @@ -0,0 +1,65 @@ +# 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, + ] + diff --git a/tests/v2/test_0863-is-none-numpy-array.py b/tests/v2/test_0863-is-none-numpy-array.py new file mode 100644 index 0000000000..fdb85e69cf --- /dev/null +++ b/tests/v2/test_0863-is-none-numpy-array.py @@ -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 diff --git a/tests/v2/test_1539-isnone-axis-check-issue1417.py b/tests/v2/test_1539-isnone-axis-check-issue1417.py index 2c2e98b2c3..6e3ffddf5b 100644 --- a/tests/v2/test_1539-isnone-axis-check-issue1417.py +++ b/tests/v2/test_1539-isnone-axis-check-issue1417.py @@ -20,3 +20,7 @@ def test_1417issue_is_none_check_axis(): 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]], []] From ea5ef3815159a5e29ef029367f16f0d4a7a1b61d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 8 Jul 2022 14:58:43 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/v2/test_0193-is_none-axis-parameter.py | 25 ++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/tests/v2/test_0193-is_none-axis-parameter.py b/tests/v2/test_0193-is_none-axis-parameter.py index 8375e47256..96ccde55f4 100644 --- a/tests/v2/test_0193-is_none-axis-parameter.py +++ b/tests/v2/test_0193-is_none-axis-parameter.py @@ -13,27 +13,37 @@ def test(): True, False, ] - assert ak._v2.operations.is_none(ak._v2.Array([[1, 2, 3], [], [None, 5]])).tolist() == [ + 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() == [ + 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() == [ + 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() == [ + 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() == [ + assert ak._v2.operations.is_none( + ak._v2.Array([[1, None, 2, 3], [], [None, 5]]), axis=-2 + ).tolist() == [ False, False, False, @@ -42,7 +52,9 @@ def test(): 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) + 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) @@ -62,4 +74,3 @@ def test(): True, False, ] -