[Scipy-svn] r6642 - in trunk/scipy/interpolate: . tests
scipy-svn at scipy.org
scipy-svn at scipy.org
Wed Jul 28 09:57:26 EDT 2010
Author: rgommers
Date: 2010-07-28 08:57:25 -0500 (Wed, 28 Jul 2010)
New Revision: 6642
Modified:
trunk/scipy/interpolate/fitpack2.py
trunk/scipy/interpolate/tests/test_fitpack.py
Log:
ENH: For empty input, return array([]) from splines created by UnivariateSpline
and BivariateSpline.
This is the normal behavior for functions. Closes #1014.
Modified: trunk/scipy/interpolate/fitpack2.py
===================================================================
--- trunk/scipy/interpolate/fitpack2.py 2010-07-26 15:13:58 UTC (rev 6641)
+++ trunk/scipy/interpolate/fitpack2.py 2010-07-28 13:57:25 UTC (rev 6642)
@@ -18,7 +18,8 @@
'RectBivariateSpline']
import warnings
-from numpy import zeros, concatenate, alltrue, ravel, all, diff
+from numpy import zeros, concatenate, alltrue, ravel, all, diff, array
+import numpy as np
import fitpack
import dfitpack
@@ -212,6 +213,10 @@
if x is (partially) ordered.
"""
+ x = np.asarray(x)
+ # empty input yields empty output
+ if x.size == 0:
+ return array([])
# if nu is None:
# return dfitpack.splev(*(self._eval_args+(x,)))
# return dfitpack.splder(nu=nu,*(self._eval_args+(x,)))
@@ -485,6 +490,7 @@
approximation: sum ((w[i]*(z[i]-s(x[i],y[i])))**2,axis=0)
"""
return self.fp
+
def get_knots(self):
""" Return a tuple (tx,ty) where tx,ty contain knots positions
of the spline with respect to x-, y-variable, respectively.
@@ -492,11 +498,19 @@
t[k+1:-k-1] and t[:k+1]=b, t[-k-1:]=e, respectively.
"""
return self.tck[:2]
+
def get_coeffs(self):
""" Return spline coefficients."""
return self.tck[2]
- def __call__(self,x,y,mth='array'):
+
+ def __call__(self, x, y, mth='array'):
""" Evaluate spline at positions x,y."""
+ x = np.asarray(x)
+ y = np.asarray(y)
+ # empty input yields empty output
+ if (x.size == 0) and (y.size == 0):
+ return array([])
+
if mth=='array':
tx,ty,c = self.tck[:3]
kx,ky = self.degrees
Modified: trunk/scipy/interpolate/tests/test_fitpack.py
===================================================================
--- trunk/scipy/interpolate/tests/test_fitpack.py 2010-07-26 15:13:58 UTC (rev 6641)
+++ trunk/scipy/interpolate/tests/test_fitpack.py 2010-07-28 13:57:25 UTC (rev 6642)
@@ -57,6 +57,14 @@
sp = ZeroSpline([1,2,3,4,5], [3,2,3,2,3], k=2)
assert_array_equal(sp([1.5, 2.5]), [0., 0.])
+ def test_empty_input(self):
+ """Test whether empty input returns an empty output. Ticket 1014"""
+ x = [1,3,5,7,9]
+ y = [0,4,9,12,21]
+ spl = UnivariateSpline(x, y, k=3)
+ assert_array_equal(spl([]), array([]))
+
+
class TestLSQBivariateSpline(TestCase):
def test_linear_constant(self):
x = [1,1,1,2,2,2,3,3,3]
@@ -109,6 +117,18 @@
assert_almost_equal(lut.integral(tx[0], tx[-1], ty[0], ty[-1]), trpz)
+ def test_empty_input(self):
+ """Test whether empty inputs returns an empty output. Ticket 1014"""
+ x = [1,1,1,2,2,2,3,3,3]
+ y = [1,2,3,1,2,3,1,2,3]
+ z = [3,3,3,3,3,3,3,3,3]
+ s = 0.1
+ tx = [1+s,3-s]
+ ty = [1+s,3-s]
+ lut = LSQBivariateSpline(x,y,z,tx,ty,kx=1,ky=1)
+
+ assert_array_equal(lut([], []), array([]))
+
class TestSmoothBivariateSpline(TestCase):
def test_linear_constant(self):
x = [1,1,1,2,2,2,3,3,3]
More information about the Scipy-svn
mailing list