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

Filter out branches that can't be interpreted by Uproot/Awkward #118

Merged
merged 3 commits into from
Apr 8, 2024
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
5 changes: 4 additions & 1 deletion func_adl_uproot/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

input_filenames_argument_name = 'input_filenames'
tree_name_argument_name = 'tree_name'
branch_filter_name = '_remove_not_interpretable'

unary_op_dict = {ast.UAdd: '+', ast.USub: '-', ast.Invert: '~'}

Expand Down Expand Up @@ -437,7 +438,9 @@ def visit_Call(self, node):
+ "(logging.getLogger(__name__).info('Using treename='"
+ ' + repr(tree_name_to_use)),'
+ ' uproot.dask({input_file: tree_name_to_use'
+ ' for input_file in input_files}))[1])'
+ ' for input_file in input_files}, filter_branch='
+ branch_filter_name
+ '))[1])'
+ '('
+ source_rep
+ ', '
Expand Down
37 changes: 35 additions & 2 deletions func_adl_uproot/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,39 @@
import qastle

from .transformer import PythonSourceGeneratorTransformer
from .transformer import input_filenames_argument_name, tree_name_argument_name
from .transformer import branch_filter_name, input_filenames_argument_name, tree_name_argument_name

# Adapted from https://github.com/CoffeaTeam/coffea/blob/v2024.4.0/src/coffea/util.py#L217-L248
remove_not_interpretable_source = (
' def '
+ branch_filter_name
+ '''(branch):
if isinstance(branch.interpretation, uproot.interpretation.identify.uproot.AsGrouped):
for name, interpretation in branch.interpretation.subbranches.items():
if isinstance(
interpretation, uproot.interpretation.identify.UnknownInterpretation
):
logging.getLogger(__name__).warning(
f"Skipping {branch.name} as it is not interpretable by Uproot"
)
return False
if isinstance(branch.interpretation, uproot.interpretation.identify.UnknownInterpretation):
logging.getLogger(__name__).warning(
f"Skipping {branch.name} as it is not interpretable by Uproot"
)
return False
try:
_ = branch.interpretation.awkward_form(None)
except uproot.interpretation.objects.CannotBeAwkward:
logging.getLogger(__name__).warning(
f"Skipping {branch.name} as it cannot be represented as an Awkward array"
)
return False
else:
return True

'''
)


def python_ast_to_python_source(python_ast):
Expand All @@ -26,7 +58,8 @@ def generate_python_source(ast, function_name='run_query'):
+ '=None):\n'
)
source += ' import functools, logging, numpy as np, dask_awkward as dak, uproot, vector\n'
source += ' vector.register_awkward()\n'
source += ' vector.register_awkward()\n\n'
source += remove_not_interpretable_source
source += ' return ' + python_ast_to_python_source(python_ast) + '.compute()\n'
return source

Expand Down
Loading