[SciPy-User] 1-d Interpolation of unevenly spaced data

Jonathan Stickel jjstickel at vcn.com
Wed Feb 2 10:19:13 EST 2011


On 2/2/11 03:33 , scipy-user-request at scipy.org wrote:
> Date: Wed, 2 Feb 2011 11:33:52 +0100
 > From: "Dybbroe Adam"
 > Subject: Re: [SciPy-User] 1-d Interpolation of unevenly spaced data
 >
> Pauli Virtanen wrote:
>> >  On Wed, 02 Feb 2011 00:23:08 +0100, Dybbroe Adam wrote:
>> >
>>> >>  Ok, I have stored my data in an npz-file (attached). You should be able
>>> >>  to run the following code (with the npz file in the same directory from
>>> >>  where you run):
>>> >>
>> >  [clip]
>> >
>> >  The spline interpolation routines assume that (i) the x-coordinates are
>> >  sorted, and (ii) there are no duplicate x-entries. Your data seems not to
>> >  satisfy these conditions: you'll need to sort and de-dupe the data before
>> >  interpolation.
>> >
>> >  Like so:
>> >
>> >  import numpy as np
>> >  from scipy.interpolate import InterpolatedUnivariateSpline
>> >
>> >  def sort_data(x, y):
>> >        # Sort data
>> >        j = np.argsort(x)
>> >        x = x[j]
>> >        y = y[j]
>> >
>> >        # De-duplicate data
>> >        mask = np.r_[True, (np.diff(x)>   0)]
>> >        if not mask.all():
>> >            print "Dropping data on the floor..."
>> >            # could do something smarter here
>> >        x = x[mask]
>> >        y = y[mask]
>> >        return x, y
>> >
>> >  x, y = sort_data(x, y)
>> >  ip = InterpolatedUnivariateSpline(x2, y2)
>> >
>> >
> Thank you so much, this was very useful. It works, yes.
> Had overlooked the possibility that my data was not sorted. That of
> course explains why some response curves was okay and others were not.
> I shall improve on my quality-checking (data filtering).
>
> The "np.r_" de-duplicate thing was new to me. Nice!
>

You might also like to try the datasmooth scikit:

http://pypi.python.org/pypi/scikits.datasmooth/0.5

Although you may not need smoothing of your data, it can be useful for 
interpolation.  It can be used with scattered data; unsorted and 
duplicates are OK.  Here is the code:


import numpy as np
from matplotlib.pyplot import *
import scikits.datasmooth.regularsmooth as ds

# Load data and try do the spline interpolation:
npzfile = np.load('ipol_test_arrays.npz')
wvl = npzfile['wvl']
resp = npzfile['resp']

xspl = np.linspace(wvl[0], wvl[-1], 300)
resp_ipol = ds.smooth_data(wvl,resp,d=2,lmbd=1e-10,xhat=xspl)

clf()
plot(wvl,resp,'o',xspl,resp_ipol,lw=2)
draw()
show()


Regards,
Jonathan



More information about the SciPy-User mailing list