[NumPy-Tickets] [NumPy] #1989: apply_along_axis: error when applied function returns something other than scalar or array

NumPy Trac numpy-tickets@scipy....
Sat Nov 26 10:45:30 CST 2011


#1989: apply_along_axis: error when applied function returns something other than
scalar or array
------------------------------+---------------------------------------------
 Reporter:  pjohn             |       Owner:  somebody   
     Type:  defect            |      Status:  new        
 Priority:  normal            |   Milestone:  Unscheduled
Component:  numpy.lib         |     Version:  1.6.0      
 Keywords:  apply_along_axis  |  
------------------------------+---------------------------------------------
 When using a function that returns something that is not a scalar or an
 array (e.g. scipy.interpolate.UnivariateSpline returns an object),
 numpy.apply_along_axis gives an error.

 {{{
 TypeError: "object of type 'LSQUnivariateSpline' has no len()"

 function apply_along_axis in shape_base.py at line 104
 outshape[axis] = len(res)
 }}}

 However, when using the masked version (numpy.ma.apply_along_axis), this
 error does not occur. The masked version (in ma/extras.py) includes this
 bit of code:

 {{{
     res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
     #  if res is a number, then we have a smaller output array
     asscalar = np.isscalar(res)
     if not asscalar:
         try:
             len(res)
         except TypeError:
             asscalar = True
     # Note: we shouldn't set the dtype of the output from the first
 result...
     #...so we force the type to object, and build a list of dtypes
     #...we'll just take the largest, to avoid some downcasting
     dtypes = []
     if asscalar:
         ...
     else:
         ...
         outshape[axis] = res.shape
 }}}

 whereas numpy.apply_along_axis (in lib/shape_base.py) does:

 {{{
     if isscalar(res):
         ...
     else:
         ...
         outshape[axis] = len(res)
 }}}

 This portion of ma.apply_along_axis should be backported to
 numpy.apply_along_axis.

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1989>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list