Replies: 1 comment 6 replies
-
Hi @jrueb, thanks for opening this discussion! We have a function def gamma_pdf(x, *args, loc=0, scale=1):
first, *remainder = ak.broadcast_arrays(x, *args, loc, scale)
def apply(inputs, **kwargs):
if all(layout.is_numpy for layout in inputs):
arrays = [x.to_backend_array() for x in inputs]
return ak.to_layout(
scipy.stats.gamma.pdf(*arrays)
)
return ak.transform(apply, first, *remainder, return_value="original") Although The above implementation does not tell you if the transformer did nothing — maybe we should add that as a feature. You can quickly define an outer-scope variable to ensure this (or define a complex def gamma_pdf(x, *args, loc=0, scale=1):
first, *remainder = ak.broadcast_arrays(x, *args, loc, scale)
transformer_did_return = False
def apply(inputs, **kwargs):
nonlocal transformer_did_return
if all(layout.is_numpy for layout in inputs):
arrays = [x.to_backend_array() for x in inputs]
transformer_did_return = True
return ak.to_layout(
scipy.stats.gamma.pdf(*arrays)
)
result = ak.transform(apply, first, *remainder, return_value="original")
if not transformer_did_return:
raise TypeError
return result Hopefully this helps to answer your question? |
Beta Was this translation helpful? Give feedback.
-
I often encounter the problem of wanting to evaluate a function that is numpy-compatible on awkward arrays. Let's take as an example
scipy.stats.gamma.pdf
. I can doBut if
a
is an awkward array (for examplea2 = ak.Array([[[1, 2], []], [None], [[None]]])
), I have to find another way.I found this old comment about unwrapping the array manually with
array.layout.contents
: #489 (comment)However, this is not trivial and handling everything correctly can be error-prone if the user is not so experienced with awkward layouts.
What I would like to have is a functionality to easily obtain the awkward array from a function like
scipy.stats.gamma.pdf
. Using above example, this functionality would easily generate an array equal toak.Array([[p, []], [None], [[None]]])
.Maybe one can make a function called
ak.map_content
(or something like this), that does the unwrapping. For example:Or maybe this functionality already exists? In this case, I am happy to learn more.
Beta Was this translation helpful? Give feedback.
All reactions