Re: [SciPy-user] Interpolate 1D
Hello, scipy.interpolate.fitpack2.InterpolatedUnivariateSpline seems to be a 1d interpolation method. I used the Cookbook/Rebinning-Example to Interpolate. I don't know if it works with InterpolatedUnivariateSpline. 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 Maik Trömel wrote:
Date: Tue, 5 Sep 2006 11:29:49 -0400
From: "A. M. Archibald" <peridot.faceted@gmail.com> Subject: Re: [SciPy-user] Interpolate 1D To: "SciPy Users List" <scipy-user@scipy.org> Message-ID: <ce557a360609050829n797be48bv81709ad18bed7e0f@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 05/09/06, Maik Tr?mel <maik.troemel@maitro.net> wrote:
Hello list,
when I try to use scipy.interpolate.interpolate.interp1d( olddims[-1], a, kind='cubic' ) I get an ERROR:
File "/usr/lib/python2.3/site-packages/scipy/interpolate/interpolate.py", line 118, in __init__ raise NotImplementedError, "Only linear supported for now. Use "\ NotImplementedError: Only linear supported for now. Use fitpack routines for other types.
But under
http://www.scipy.org/doc/api_docs/scipy.interpolate.interpolate.interp1d.htm...
kind = 'cubic' is listed. Whats wrong with the command? Or is cubic not implemented yet?
Thanks for your help!
It seems not to be implemented, but you can use scipy.interpolate.fitpack2.InterpolatedUnivariateSpline to do the same thing for splines (of various orders). I think it also provides all the handy extras like derivatives and root-finding. It doesn't seem to be able to raise exceptions or insert NaNs for out-of-bound values, it just extrapolates. But otherwise it seems to be the right tool for the job.
A. M. Archibald
------------------------------
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
End of SciPy-user Digest, Vol 37, Issue 3 *****************************************
Hi Maik, On 07/09/06, Maik Trömel <maik.troemel@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.
participants (2)
-
Angus McMorland -
Maik Trömel