using UnivariateSpline

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

using UnivariateSpline

David Warde-Farley-2
I must be crazy, but how does one actually USE UnivariateSpline, etc.  
to do interpolation? How do I evaluate the spline at other data after  
it's fit?

There seems to be no "evaluate" method or equivalent to splev.

Thanks,

David
_______________________________________________
SciPy-user mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: using UnivariateSpline

Robert Kern-2
On Fri, May 22, 2009 at 14:57, David Warde-Farley <[hidden email]> wrote:
> I must be crazy, but how does one actually USE UnivariateSpline, etc.
> to do interpolation? How do I evaluate the spline at other data after
> it's fit?
>
> There seems to be no "evaluate" method or equivalent to splev.

    def __call__(self, x, nu=None):
        """ Evaluate spline (or its nu-th derivative) at positions x.
        Note: x can be unordered but the evaluation is more efficient
        if x is (partially) ordered.

        """

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco
_______________________________________________
SciPy-user mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: using UnivariateSpline

josef.pktd
In reply to this post by David Warde-Farley-2
On Fri, May 22, 2009 at 3:57 PM, David Warde-Farley <[hidden email]> wrote:
> I must be crazy, but how does one actually USE UnivariateSpline, etc.
> to do interpolation?

read the source, look at the tests,
scipy\interpolate\tests\test_fitpack.py, search the mailing lists and
hope for the best (and file a bug report)

below are some of my attempt of understanding what's going on with the
spline classes

> How do I evaluate the spline at other data after
> it's fit?
>
> There seems to be no "evaluate" method or equivalent to splev.


-------------------


"""
try_spline.py
"""

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt

npoints = 51
x = np.linspace(0, 20, npoints)
y = np.sqrt(x) + np.sin(x) + 0.2* np.random.randn(npoints)
tck = interpolate.splrep(x, y)
x2 = np.linspace(0, 20, 200)
y2 = interpolate.splev(x2, tck)
plt.plot(x, y, 'o', x2, y2, '-.')
#plt.show()

#tck2 = interpolate.splrep(x, y, t=tck[1])
#x3 = np.linspace(0, 20, 300)
#y3 = interpolate.splev(x3, tck2)
#plt.plot(x, y, 'o', x3, y3, '-.')
#

us = interpolate.UnivariateSpline(x,y)
t = us.get_knots()
print t
#tck2 = interpolate.splrep(x2, y2, t=t, full_output=True)

tck2 = interpolate.splrep(x, y, s=1, full_output=True)
y2 = interpolate.splev(x2, tck2[0])
plt.plot(x, y, 'o', x2, y2, '-.')

tt=x[1:-1:2]
lsus=interpolate.LSQUnivariateSpline(x,y,tt)
yh = lsus(x2)
plt.figure()
plt.plot(x, y, 'o', x2, yh, '-.')

lsus=interpolate.UnivariateSpline(x,y,s=2)
yh = lsus(x2)
plt.figure()
plt.plot(x, y, 'o', x2, yh, '-.')

#derivatives doesn't take array arguments correctly
print lsus.derivatives(x)
#using fitpacks spalde directly works
deri = np.array(interpolate.spalde(x, lsus._eval_args))
print deri[:10,:]
print np.all(lsus(x) == deri[:,0])
print np.max(np.abs(lsus(x) - deri[:,0]))
deri2 = np.array(map(lsus.derivatives,x))
print np.all(deri2 == deri)
print np.max(np.abs(deri2 - deri))


print lsus.integral(x[0],x[-1])

#plt.show()


example = 0
if example == 3:
    #copied from
    #http://groups.google.ca/group/scipy-user/browse_thread/thread/ded43ebce135c520/eccf1dd343456137?hl=en&lnk=gst&q=splrep#eccf1dd343456137
    import scipy as sp
    x=sp.linspace(0,10,11)
    y=sp.sin(x)
    x2=sp.linspace(0,10,201)
    tck=sp.interpolate.splrep(x,y,k=3)
    y2=sp.interpolate.splev(x2,tck)
    f=sp.interpolate.interp1d(x,y,kind=3)
    y3=f(x2)

    '''
   :members:  __call__, derivatives, get_coeffs, get_knots, get_residual,
              integral, roots, set_smoothing_factor'
    '''
_______________________________________________
SciPy-user mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: using UnivariateSpline

David Warde-Farley-2
In reply to this post by Robert Kern-2
On 22-May-09, at 3:57 PM, Robert Kern wrote:

> On Fri, May 22, 2009 at 14:57, David Warde-Farley  
> <[hidden email]> wrote:
>> I must be crazy, but how does one actually USE UnivariateSpline, etc.
>> to do interpolation? How do I evaluate the spline at other data after
>> it's fit?
>>
>> There seems to be no "evaluate" method or equivalent to splev.
>
>    def __call__(self, x, nu=None):
>        """ Evaluate spline (or its nu-th derivative) at positions x.
>        Note: x can be unordered but the evaluation is more efficient
>        if x is (partially) ordered.

I somehow completely missed this. I guess I was skipping over the  
__init__ method because I already understood it. :S

Thanks Robert.

David
_______________________________________________
SciPy-user mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: using UnivariateSpline

Erik Tollerud-2
These classes are indeed rather poorly documented, but once you get
into them, they work very well.

Also, be aware that the three *UnivariateSpline classes are only
different in how they generate the knots:
*UnivarateSpline: determines the number of knots by adding more knots
until the smoothing condition (sum((w[i]*(y[i]-s(x[i])))**2,axis=0) <=
s) is satisfied - s is specified in the constructor or the
set_smoothing_factor method.
*LSQUnivariateSpline: the knots are specified in a sequence provided
to the constructor (t)
*InterpolatedUnivatiateSpline: the spline is forced to pass through
all the points (equivalent to s=0)

But they are all evaluated by being called, as has already been explained.


On Fri, May 22, 2009 at 1:26 PM, David Warde-Farley <[hidden email]> wrote:

> On 22-May-09, at 3:57 PM, Robert Kern wrote:
>
>> On Fri, May 22, 2009 at 14:57, David Warde-Farley
>> <[hidden email]> wrote:
>>> I must be crazy, but how does one actually USE UnivariateSpline, etc.
>>> to do interpolation? How do I evaluate the spline at other data after
>>> it's fit?
>>>
>>> There seems to be no "evaluate" method or equivalent to splev.
>>
>>    def __call__(self, x, nu=None):
>>        """ Evaluate spline (or its nu-th derivative) at positions x.
>>        Note: x can be unordered but the evaluation is more efficient
>>        if x is (partially) ordered.
>
> I somehow completely missed this. I guess I was skipping over the
> __init__ method because I already understood it. :S
>
> Thanks Robert.
>
> David
> _______________________________________________
> SciPy-user mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



--
Erik Tollerud
Graduate Student
Center For Cosmology
Department of Physics and Astronomy
2142 Frederick Reines Hall
University of California, Irvine
Office Phone: (949)824-2587
Cell: (651)307-9409
[hidden email]
http://ps.uci.edu/~etolleru
_______________________________________________
SciPy-user mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: using UnivariateSpline

Stéfan van der Walt
2009/5/23 Erik Tollerud <[hidden email]>:
> These classes are indeed rather poorly documented, but once you get
> into them, they work very well.

It would be great if you guys could improve the documentation as you
figure out how to use these functions.  Even if you only add an
example or two, that would be useful.

The docs are editable in a wiki-like fashion on http://docs.scipy.org

Thanks!
Stéfan
_______________________________________________
SciPy-user mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: using UnivariateSpline

David Warde-Farley-2

On 22-May-09, at 7:47 PM, Stéfan van der Walt wrote:

> It would be great if you guys could improve the documentation as you
> figure out how to use these functions.  Even if you only add an
> example or two, that would be useful.

Was planning on doing just that. :)

David

_______________________________________________
SciPy-user mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user