[SciPy-user] Interpolate 1D

Angus McMorland amcmorl at gmail.com
Thu Sep 7 07:09:42 EDT 2006


Hi Maik,

On 07/09/06, Maik Trömel <maik.troemel at maitro.net> wrote:
>
> Hello,


<snip>

I also tried out scipy.interpolate.interpolate.interp2d. But I get an
> error. Probably somebody knows what I'm doing wrong. Here's the script:
>
> import scipy.interpolate
>
> #just some data
> a = numpy.zeros((4,4), dtype=''Float32')
> oldx= numpy.arange(4)
> oldy = numpy.arange(4)
> newx = numpy.zeros((4), dtype=''Float32')
> newy =numpy.zeros((4), dtype=''Float32')
> for s in range(8):
>     newx[s] =  s * 0.5
>     newy[s] =  s * 0.5
>
> #interpolation
> intinst = scipy.interpolate.interpolate.interp2d(oldx, oldy, a, kind =
> 'cubic')
> interpolated = intinst(newx, newy)
>
>
> And here's the error:
> /usr/lib/python2.3/site-packages/scipy/interpolate/interpolate.py in
> __call__(self, x, y, dx, dy)
>      62         x = atleast_1d(x)
>      63         y = atleast_1d(y)
> ---> 64         z,ier=fitpack._fitpack._bispev(*(self.tck+[x,y,dx,dy]))
>      65         if ier==10: raise ValueError,"Invalid input data"
>      66         if ier: raise TypeError,"An error occurred"
>
> AttributeError: interp2d instance has no attribute 'tck'
>
> Thanks for your help.
>
> Greetings Maik


I've tried your script, and get several different errors, so are you sure
that your error messages are coming from this script?

The biggest conceptual jump needed with your script, as it runs on my
system, (scipy 0.5.0.2177, numpy 1.0b4.dev3055) is that oldx and oldy need
to be the same length as the flattened 'a' - that is an (x,y) pair for each
value of a, which itself could also be flat. Here is, I think, a working
version:

import scipy.interpolate
import numpy

#just some data
a = numpy.random.random_sample((4,4)).astype('Float32')
oldinds = numpy.indices((4,4))
oldx = oldinds[1].flatten() # because indices[1] is the x values
oldy = oldinds[0].flatten()
newx = numpy.linspace(0,3,8).astype('Float32')
newy = numpy.linspace(0,3,8).astype('Float32')

#interpolation
intinst = scipy.interpolate.interp2d(oldx, oldy, a, kind = 'cubic')
interpolated = intinst(newx, newy)

#display the result - can ignore if you don't have matplotlib installed
import pylab

pylab.subplot(121)
pylab.imshow(a,interpolation='nearest')
pylab.subplot(122)
pylab.imshow(interpolated, interpolation='nearest')

-- 
AJC McMorland, PhD Student
Physiology, University of Auckland
Armourer, Auckland University Fencing
Secretary, Fencing North Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20060907/f8b067b3/attachment.html>


More information about the SciPy-User mailing list