Skip to content

Instantly share code, notes, and snippets.

@agoose77
Last active December 27, 2021 18:38
Show Gist options
  • Save agoose77/a1bbdf6ac20fce483c1ce6e0550e929d to your computer and use it in GitHub Desktop.
Save agoose77/a1bbdf6ac20fce483c1ce6e0550e929d to your computer and use it in GitHub Desktop.
import numpy as np
import awkward as ak
def simplify_option(array: ak.Array, highlevel=True, behavior=None) -> ak.Array:
"""Return the result of removing superfluous option-types from the given array
:param array: `None`-containing Array
"""
# Pack array layout to ensure no nested options
layout = ak.packed(array, highlevel=False)
nplike = ak.nplike.of(layout)
def transform(layout, depth, user=None):
if isinstance(layout, ak._util.optiontypes):
mask = nplike.asarray(layout.bytemask()).view(np.bool_)
if not np.any(mask):
layout = layout.project()
# We are above the child layout with None's
return ak._util.transform_child_layouts(transform, layout, depth)
out = transform(layout, 1)
return ak._util.maybe_wrap_like(out, array, behavior, highlevel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment