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

Feat: add after_option argument to ak.zip #1308

Merged
merged 5 commits into from
Feb 23, 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
23 changes: 23 additions & 0 deletions src/awkward/_v2/operations/structure/ak_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def zip(
highlevel=True,
behavior=None,
right_broadcast=False,
optiontype_outside_record=False,
):

"""
Expand All @@ -35,6 +36,8 @@ def zip(
high-level.
right_broadcast (bool): If True, follow rules for implicit
right-broadcasting, as described in #ak.broadcast_arrays.
optiontype_outside_record (bool): If True, continue broadcasting past
any option types before creating the new #ak.layout.RecordArray node.

Combines `arrays` into a single structure as the fields of a collection
of records or the slots of a collection of tuples. If the `arrays` have
Expand Down Expand Up @@ -104,6 +107,22 @@ def zip(
As an extreme, `depth_limit=1` is a handy way to make a record structure
at the outermost level, regardless of whether the fields have matching
structure or not.

When zipping together arrays with optional values, it can be useful to create
the #ak.layout.RecordArray node after the option types. By default, #ak.zip
does not do this:

>>> one = ak.Array([1, 2, None])
>>> two = ak.Array([None, 5, 6])
>>> ak.zip([one, two])
<Array [(1, None), (2, 5), (None, 6)] type='3 * (?int64, ?int64)'>

If the `optiontype_outside_record` option is set to `True`, Awkward will continue to
broadcast the arrays together at the depth_limit until it reaches non-option
types. This effectively takes the union of the option mask:

>>> ak.zip([one, two], optiontype_outside_record=True)
<Array [None, (2, 5), None] type='3 * ?(int64, int64)'>
"""
if depth_limit is not None and depth_limit <= 0:
raise ValueError("depth_limit must be None or at least 1")
Expand Down Expand Up @@ -165,6 +184,10 @@ def action(inputs, depth, **ignore):
for x in inputs
)
):
# If we want to zip after option types at this depth
if optiontype_outside_record and any(x.is_OptionType for x in inputs):
return None

return (
ak._v2.contents.RecordArray(
inputs, recordlookup, parameters=parameters
Expand Down
26 changes: 26 additions & 0 deletions src/awkward/operations/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ def zip(
highlevel=True,
behavior=None,
right_broadcast=False,
optiontype_outside_record=False,
):
"""
Args:
Expand All @@ -496,6 +497,8 @@ def zip(
high-level.
right_broadcast (bool): If True, follow rules for implicit
right-broadcasting, as described in #ak.broadcast_arrays.
optiontype_outside_record (bool): If True, continue broadcasting past
any option types before creating the new #ak.layout.RecordArray node.

Combines `arrays` into a single structure as the fields of a collection
of records or the slots of a collection of tuples. If the `arrays` have
Expand Down Expand Up @@ -565,6 +568,23 @@ def zip(
As an extreme, `depth_limit=1` is a handy way to make a record structure
at the outermost level, regardless of whether the fields have matching
structure or not.

When zipping together arrays with optional values, it can be useful to create
the #ak.layout.RecordArray node after the option types. By default, #ak.zip
does not do this:

>>> one = ak.Array([1, 2, None])
>>> two = ak.Array([None, 5, 6])
>>> ak.zip([one, two])
<Array [(1, None), (2, 5), (None, 6)] type='3 * (?int64, ?int64)'>

If the `optiontype_outside_record` option is set to `True`, Awkward will continue to
broadcast the arrays together at the depth_limit until it reaches non-option
types. This effectively takes the union of the option mask:

>>> ak.zip([one, two], optiontype_outside_record=True)
<Array [None, (2, 5), None] type='3 * ?(int64, int64)'>

"""
if depth_limit is not None and depth_limit <= 0:
raise ValueError(
Expand Down Expand Up @@ -629,6 +649,12 @@ def getfunction(inputs, depth):
for x in inputs
)
):
# If we want to zip after option types at this depth
if optiontype_outside_record and any(
isinstance(x, ak._util.optiontypes) for x in inputs
):
return None

return lambda: (
ak.layout.RecordArray(inputs, recordlookup, parameters=parameters),
)
Expand Down
Empty file added tests/.tmpHaXt1g
Empty file.
44 changes: 44 additions & 0 deletions tests/test_1308-zip-after-option.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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


def test_all_options():
one = ak.Array([1, 2, None])
two = ak.Array([None, 5, None])
result = ak.zip([one, two], optiontype_outside_record=True)
assert str(result.type) == "3 * ?(int64, int64)"
assert result.tolist() == [None, (2, 5), None]


def test_mixed_options():
one = ak.Array([1, 2, None])
two = ak.Array([4, 5, 6])
result = ak.zip([one, two], optiontype_outside_record=True)
assert str(result.type) == "3 * ?(int64, int64)"
assert result.tolist() == [(1, 4), (2, 5), None]


def test_no_options():
one = ak.Array([1, 2, 3])
two = ak.Array([4, 5, 6])
result = ak.zip([one, two], optiontype_outside_record=True)
assert str(result.type) == "3 * (int64, int64)"
assert result.tolist() == [(1, 4), (2, 5), (3, 6)]


def test_complex_inner():
one = ak.Array([1, 2, 3])
two = ak.Array([[7, 5], [1, 2], [4, None]])
result = ak.zip([one, two], optiontype_outside_record=True)
assert str(result.type) == "3 * var * ?(int64, int64)"
assert result.tolist() == [[(1, 7), (1, 5)], [(2, 1), (2, 2)], [(3, 4), None]]


def test_complex_outer():
one = ak.Array([1, None, 3])
two = ak.Array([[7, 5], [1, 2], [4, None]])
result = ak.zip([one, two], optiontype_outside_record=True)
assert str(result.type) == "3 * option[var * ?(int64, int64)]"
assert result.tolist() == [[(1, 7), (1, 5)], None, [(3, 4), None]]
44 changes: 44 additions & 0 deletions tests/v2/test_1308-zip-after-option.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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


def test_all_options():
one = ak._v2.highlevel.Array([1, 2, None])
two = ak._v2.highlevel.Array([None, 5, None])
result = ak._v2.operations.structure.zip([one, two], optiontype_outside_record=True)
assert str(result.type) == "3 * ?(int64, int64)"
assert result.tolist() == [None, (2, 5), None]


def test_mixed_options():
one = ak._v2.highlevel.Array([1, 2, None])
two = ak._v2.highlevel.Array([4, 5, 6])
result = ak._v2.operations.structure.zip([one, two], optiontype_outside_record=True)
assert str(result.type) == "3 * ?(int64, int64)"
assert result.tolist() == [(1, 4), (2, 5), None]


def test_no_options():
one = ak._v2.highlevel.Array([1, 2, 3])
two = ak._v2.highlevel.Array([4, 5, 6])
result = ak._v2.operations.structure.zip([one, two], optiontype_outside_record=True)
assert str(result.type) == "3 * (int64, int64)"
assert result.tolist() == [(1, 4), (2, 5), (3, 6)]


def test_complex_inner():
one = ak._v2.highlevel.Array([1, 2, 3])
two = ak._v2.highlevel.Array([[7, 5], [1, 2], [4, None]])
result = ak._v2.operations.structure.zip([one, two], optiontype_outside_record=True)
assert str(result.type) == "3 * var * ?(int64, int64)"
assert result.tolist() == [[(1, 7), (1, 5)], [(2, 1), (2, 2)], [(3, 4), None]]


def test_complex_outer():
one = ak._v2.highlevel.Array([1, None, 3])
two = ak._v2.highlevel.Array([[7, 5], [1, 2], [4, None]])
result = ak._v2.operations.structure.zip([one, two], optiontype_outside_record=True)
assert str(result.type) == "3 * option[var * ?(int64, int64)]"
assert result.tolist() == [[(1, 7), (1, 5)], None, [(3, 4), None]]