Skip to content

Commit

Permalink
ENH: Add ext parameter to UnivariateSpline
Browse files Browse the repository at this point in the history
Pass parameter to fitpack.splev to control how to handle out of range
evaluations. Parameter ext can be passed via __init__ or __call__. Added
test to scipy/interpolate/tests/test_fitpack2.py. Tests if UnivariateSpline
call equals x ** 3 for out of range x and extrapolation modes 0 and 1.
Modify _from_tck, derivative, and antiderivative methods to use ext
parameter. __init__ of LSQUnivariateSpline and InterpolatedUnivariateSpline
now accept ext.

See ticket 3557.

closes scipygh-3557
  • Loading branch information
jacobcvt12 committed Jul 10, 2014
1 parent 04e049d commit db535b2
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions scipy/interpolate/fitpack2.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,15 @@ def __init__(self, x, y, w=None, bbox=[None]*2, k=3, s=None, ext=0):
self._reset_class()

@classmethod
def _from_tck(cls, tck):
def _from_tck(cls, tck, ext):
"""Construct a spline object from given tck"""
self = cls.__new__(cls)
t, c, k = tck
self._eval_args = tck
#_data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier
self._data = (None,None,None,None,None,k,None,len(t),t,
c,None,None,None,None)
self.ext = ext
return self

def _reset_class(self):
Expand Down Expand Up @@ -368,7 +369,7 @@ def derivative(self, n=1):
"""
tck = fitpack.splder(self._eval_args, n)
return UnivariateSpline._from_tck(tck)
return UnivariateSpline._from_tck(tck, self.ext)

def antiderivative(self, n=1):
"""
Expand Down Expand Up @@ -419,7 +420,7 @@ def antiderivative(self, n=1):
"""
tck = fitpack.splantider(self._eval_args, n)
return UnivariateSpline._from_tck(tck)
return UnivariateSpline._from_tck(tck, self.ext)


class InterpolatedUnivariateSpline(UnivariateSpline):
Expand All @@ -444,6 +445,15 @@ class InterpolatedUnivariateSpline(UnivariateSpline):
None (default), bbox=[x[0],x[-1]].
k : int, optional
Degree of the smoothing spline. Must be 1 <= `k` <= 5.
ext : int, optional
Controls the extrapolation mode for elements
not in the interval defined by the knot sequence
* if ext=0, return the extrapolated value.
* if ext=1, return 0
* if ext=2, raise a ValueError
The default value is 0.
See Also
--------
Expand Down Expand Up @@ -477,7 +487,7 @@ class InterpolatedUnivariateSpline(UnivariateSpline):
"""

def __init__(self, x, y, w=None, bbox=[None]*2, k=3):
def __init__(self, x, y, w=None, bbox=[None]*2, k=3, ext=0):
"""
Input:
x,y - 1-d sequences of data points (x must be
Expand All @@ -489,11 +499,22 @@ def __init__(self, x, y, w=None, bbox=[None]*2, k=3):
the approximation interval.
By default, bbox=[x[0],x[-1]]
k=3 - degree of the univariate spline.
ext - Controls the extrapolation mode for elements
not in the interval defined by the knot sequence
* if ext=0, return the extrapolated value.
* if ext=1, return 0
* if ext=2, raise a ValueError
The default value is 0.
"""
# _data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier
self._data = dfitpack.fpcurf0(x,y,k,w=w,
xb=bbox[0],xe=bbox[1],s=0)
self._reset_class()
self.ext = ext
if ext not in (0, 1, 2):
raise ValueError("unknown extrapolation mode")


class LSQUnivariateSpline(UnivariateSpline):
Expand All @@ -520,6 +541,15 @@ class LSQUnivariateSpline(UnivariateSpline):
None (default), bbox=[x[0],x[-1]].
k : int, optional
Degree of the smoothing spline. Must be 1 <= `k` <= 5.
ext : int, optional
Controls the extrapolation mode for elements
not in the interval defined by the knot sequence
* if ext=0, return the extrapolated value.
* if ext=1, return 0
* if ext=2, raise a ValueError
The default value is 0.
Raises
------
Expand Down Expand Up @@ -560,7 +590,7 @@ class LSQUnivariateSpline(UnivariateSpline):
"""

def __init__(self, x, y, t, w=None, bbox=[None]*2, k=3):
def __init__(self, x, y, t, w=None, bbox=[None]*2, k=3, ext=0):
"""
Input:
x,y - 1-d sequences of data points (x must be
Expand All @@ -575,6 +605,14 @@ def __init__(self, x, y, t, w=None, bbox=[None]*2, k=3):
the approximation interval.
By default, bbox=[x[0],x[-1]]
k=3 - degree of the univariate spline.
ext - Controls the extrapolation mode for elements
not in the interval defined by the knot sequence
* if ext=0, return the extrapolated value.
* if ext=1, return 0
* if ext=2, raise a ValueError
The default value is 0.
"""
# _data == x,y,w,xb,xe,k,s,n,t,c,fp,fpint,nrdata,ier
xb = bbox[0]
Expand All @@ -591,6 +629,9 @@ def __init__(self, x, y, t, w=None, bbox=[None]*2, k=3):
data = dfitpack.fpcurfm1(x,y,k,t,w=w,xb=xb,xe=xe)
self._data = data[:-3] + (None,None,data[-1])
self._reset_class()
self.ext = ext
if ext not in (0, 1, 2):
raise ValueError("unknown extrapolation mode")


################ Bivariate spline ####################
Expand Down

0 comments on commit db535b2

Please sign in to comment.