[Scipy-tickets] [SciPy] #600: fitpack2.py __call__() does not preserve input arguments shape.

SciPy scipy-tickets@scipy....
Wed Feb 6 05:42:16 CST 2008


#600: fitpack2.py __call__() does not preserve input arguments shape.
---------------------+------------------------------------------------------
 Reporter:  mforbes  |       Owner:  somebody
     Type:  defect   |      Status:  new     
 Priority:  normal   |   Milestone:  0.7     
Component:  Other    |     Version:          
 Severity:  normal   |    Keywords:          
---------------------+------------------------------------------------------
 The __call__() functions in the fitpack2.py wrappers
 ({{{UnivariateSpline}}} etc.) just return the results from the fitpack
 routines.  These do not always preserve the shape of the input arguments.
 For example:

 {{{
 #!python
 >>> from scipy.interpolate import UnivariateSpline
 >>> from numpy import shape
 >>> f = UnivariateSpline([0,1,2,3],[0,1,2,3])
 >>> shape(4)
 ()
 >>> shape(f(4))
 (1,)
 }}}

 This is easily fixed with the following patchs for fitpack2.py and
 test_fitpack.py.  {{{BivariateSpline.__call__()}}} should probably also be
 checked...

 {{{
 Index: fitpack2.py
 ===================================================================
 --- fitpack2.py (revision 3893)
 +++ fitpack2.py (working copy)
 @@ -18,7 +18,7 @@
      'RectBivariateSpline']

  import warnings
 -from numpy import zeros, concatenate, alltrue, ravel, all, diff
 +from numpy import zeros, concatenate, alltrue, ravel, all, diff, shape

  import dfitpack

 @@ -158,8 +158,10 @@

          """
          if nu is None:
 -            return dfitpack.splev(*(self._eval_args+(x,)))
 -        return dfitpack.splder(nu=nu,*(self._eval_args+(x,)))
 +            ans = dfitpack.splev(*(self._eval_args+(x,)))
 +        else:
 +            ans = dfitpack.splder(nu=nu,*(self._eval_args+(x,)))
 +        return ans.reshape(shape(x))

      def get_knots(self):
          """ Return the positions of (boundary and interior)

 Index: test_fitpack.py
 ===================================================================
 --- test_fitpack.py     (revision 3893)
 +++ test_fitpack.py     (working copy)
 @@ -14,7 +14,7 @@

  import sys
  from scipy.testing import *
 -from numpy import array
 +from numpy import array, shape
  from scipy.interpolate.fitpack2 import
 UnivariateSpline,LSQUnivariateSpline,\
       InterpolatedUnivariateSpline
  from scipy.interpolate.fitpack2 import LSQBivariateSpline, \
 @@ -39,6 +39,14 @@
          assert_almost_equal(lut.get_residual(),0.0)
          assert_array_almost_equal(lut([1,1.5,2]),[0,1,2])

 +    def test_preserve_shape(self):
 +        x = [1,2,3]
 +        y = [0,2,4]
 +        lut = UnivariateSpline(x,y,k=1)
 +        assert_equal(shape(4),shape(lut(4)))
 +        assert_equal(shape(4),shape(lut(4,nu=1)))
 +
 +
  class TestLSQBivariateSpline(TestCase):
      def test_linear_constant(self):
          x = [1,1,1,2,2,2,3,3,3]
 }}}

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


More information about the Scipy-tickets mailing list