[Scipy-tickets] [SciPy] #643: scipy.ndimage.zoom fails with pre-allocated output arrays

SciPy scipy-tickets@scipy....
Wed Apr 16 10:28:01 CDT 2008


#643: scipy.ndimage.zoom fails with pre-allocated output arrays
---------------------------+------------------------------------------------
 Reporter:  zachrahan      |       Owner:  somebody
     Type:  defect         |      Status:  new     
 Priority:  normal         |   Milestone:  0.7     
Component:  scipy.ndimage  |     Version:  devel   
 Severity:  normal         |    Keywords:          
---------------------------+------------------------------------------------
 scipy.ndimage functions have an 'output' parameter which can accept either
 a dtype for the output array, or a pre-allocated output array. This latter
 functionality does not work because of a bug in the code that compares the
 output array's shape to the required shape.

 {{{
 In: scipy.__version__
 Out: '0.7.0.dev4004'

 In: a = numpy.zeros((2,2), dtype=float)

 In: scipy.ndimage.zoom(a, 2)
 Out:
 array([[ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.]])

 In: scipy.ndimage.zoom(a, 2, output=int)
 Out:
 array([[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]])

 In: b = numpy.zeros((4,4))

 In: scipy.ndimage.zoom(a, 2, output=b)
 ---------------------------------------------------------------------------
 RuntimeError                              Traceback (most recent call
 last)

 /Users/zpincus/<ipython console> in <module>()

 /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-
 packages/scipy/ndimage/interpolation.pyc in zoom(input, zoom, output_type,
 output, order, mode, cval, prefilter)
     327     zoom =
 (numpy.array(input.shape)-1)/(numpy.array(output_shape,float)-1)
     328     output, return_value = _ni_support._get_output(output, input,
 --> 329                                         output_type, shape =
 output_shape)
     330     zoom = numpy.asarray(zoom, dtype = numpy.float64)
     331     zoom = numpy.ascontiguousarray(zoom)

 /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-
 packages/scipy/ndimage/_ni_support.pyc in _get_output(output, input,
 output_type, shape)
      89     else:
      90         if output.shape != shape:
 ---> 91             raise RuntimeError, "output shape not correct"
      92         return_value = None
      93     return output, return_value

 RuntimeError: output shape not correct
 }}}

 The problem is that 'output.shape' is a tuple, and 'shape' is a list
 constructed by ndimage.zoom.
 The offending line is 326 of interpolation.py:
 {{{
 output_shape = [int(ii * jj) for ii, jj in zip(input.shape, zoom)]
 }}}

 The fix should be:
 {{{
 output_shape = tuple([int(ii * jj) for ii, jj in zip(input.shape, zoom)])
 }}}

 Alternately or in addition, _get_output from _ni_support should be more
 generous about the types of the output.shape and shape parameters.

 For a test case, see my above code. It might be good to add output
 parameter tests for all of ndimage, but at least the above tests this
 specific problem.

-- 
Ticket URL: <http://scipy.org/scipy/scipy/ticket/643>
SciPy <http://www.scipy.org/>
SciPy is open-source software for mathematics, science, and engineering.


More information about the Scipy-tickets mailing list