8000 bug in apply_along_axis with empty arrays · Issue #6927 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

bug in apply_along_axis with empty arrays #6927

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

Closed
rgommers opened this issue Jan 3, 2016 · 7 comments
Closed

bug in apply_along_axis with empty arrays #6927

rgommers opened this issue Jan 3, 2016 · 7 comments

Comments

@rgommers
Copy link
Member
rgommers commented Jan 3, 2016

Still present in numpy 1.10.2:

In [47]: a = np.ones((0, 2))

In [48]: np.sum(a, axis=0)
Out[48]: array([ 0.,  0.])

In [49]: np.sum(a, axis=1)
Out[49]: array([], dtype=float64)

In [50]: np.apply_along_axis(np.sum, axis=0, arr=a)
Out[50]: array([ 0.,  0.])

In [51]: np.apply_along_axis(np.sum, axis=1, arr=a)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-51-c8275650df94> in <module>()
----> 1 np.apply_along_axis(np.sum, axis=1, arr=a)

/home/rgommers/.local/lib/python2.7/site-packages/numpy/lib/shape_base.pyc in apply_along_axis(func1d, axis, arr, *args, **kwargs)
     89     outshape = asarray(arr.shape).take(indlist)
     90     i.put(indlist, ind)
---> 91     res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
     92     #  if res is a number, then we have a smaller output array
     93     if isscalar(res):

IndexError: index 0 is out of bounds for ax
@seberg
Copy link
Member
seberg commented Jan 3, 2016

This seems tricky. How should apply_along_axis figure out what the result shape is along a single axes (e.g. is it a reduction or element-wise)? It would have to invent some dummy array like zeros just for that purpose.

@seberg
Copy link
Member
seberg commented Jan 3, 2016

Maybe a solution could be to provide a kwarg that informs about the output size along that dimension. For reductions it is always 1 after all. This is a problem even for internal fuctions like the nanfuncs I guess?

@charris
Copy link
Member
charris commented Jan 5, 2016

Is this a regression?

@rgommers
Copy link
Member Author
rgommers commented Jan 8, 2016

No, it's not a regression (just checked with v1.7.2).

@rgommers
Copy link
Member Author
rgommers commented Jan 8, 2016

Hmm, a keyword seems like an ugly solution to me. I'd rather just try figuring it out with a dummy array. Or simply catch IndexErrors and then try to recover (to not hurt performance for normal cases).

@njsmith
Copy link
Member
njsmith commented Jan 9, 2016

I don't understand how a dummy array solves the problem - what we want to know is what shape the function's return value would have been if there had been an array there to call it on. In general this could depend on anything, though, including the values that would have been in that array if it existed... When in doubt refuse to guess and all that.

A better error message and a kwarg both seem like good improvements to me though.

@eric-wieser
Copy link
Member
eric-wieser commented Feb 13, 2017

@njsmith :

In general this could depend on anything, though, including the values that would have been in that array if it existed

Right, but this is already assumed not to be the case in apply_along_axis anyway - we already assume that the first shape and dtype are representative of all the rest. So I'd be in favor of allocating a dummy array and passing it to the function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
0