-
Notifications
You must be signed in to change notification settings - Fork 89
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: Content.form_with_key_path() #3311
Conversation
The type hint is wrong. One moment... |
This is great 🎉, and would help me with dask-contrib/dask-awkward#551 significantly! Btw, I think array[ast.literal_eval("('muons', 0, 'pt')")]
# >> <Array [1.1, 2.2] type='2 * float64'> which is something that I'd like to do in dask-contrib/dask-awkward#551 for translating touched form_keys of a type tracer report into slices that I can use for explicit touching at a later stage (i.e. in a second function pass), something like: def render_buffer_key(form: ak.forms.Form, form_key: str, attribute: str) -> str:
return f"{form_key}"
tt, report = ak.typetracer.typetracer_with_report(
array.layout.form_with_key_path(),
highlevel=True,
buffer_key=render_buffer_key,
)
def fun(x):
return x["muons"][0]["pt"]
_ = fun(tt)
...
# use the report later to touch the columns explicitly again,
# but don't run the full `fun` pass again
# ~something like:
if ak.backend(array) == "typetracer":
for form_key in set(report.data_touched):
array[ast.literal_eval(form_key)].layout._touch_data(recursive=True)
for form_key in set(report.shape_touched):
array[ast.literal_eval(form_key)].layout._touch_shape(recursive=True)
else:
fun(array) (PS: this PR reminds me of https://jax.readthedocs.io/en/latest/working-with-pytrees.html#explicit-key-paths. ) |
Careful: that only works if the lists are not empty. Also, what about the use of It won't be possible to use these as slices in general. But for >>> ast.literal_eval("('string', None, True, False, ['nested', {'key': 'value'}])")
('string', None, True, False, ['nested', {'key': 'value'}]) But Any list could take |
Hi @jpivarski, |
As it stands, this PR lacks tests, but the new feature is:
These form keys can be turned back into tuples with ast.literal_eval to get a full path from root to each node.
The new function takes
root
as an argument to put a prefix in the tuple, for uniqueness beyond the tree.Maybe those
None
values should optionally be0
? They're needed for nodes to be unique, so that the form keys of this list are not all identical:But maybe we don't care about those extra 3 characters per nesting level. How much nesting is there going to be, anyway? Yeah, I think this is fine.