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 2 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
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,
after_option=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.
after_option (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 `after_option` 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], after_option=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 after_option and any(
isinstance(x, ak._util.optiontypes) for x in inputs
):
return None

return lambda: (
ak.layout.RecordArray(inputs, recordlookup, parameters=parameters),
)
Expand Down
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], after_option=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], after_option=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], after_option=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], after_option=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], after_option=True)
assert str(result.type) == "3 * option[var * ?(int64, int64)]"
assert result.tolist() == [[(1, 7), (1, 5)], None, [(3, 4), None]]