Problems evaluating interpolated function at new points
I am relatively new to Scipy, at least when it comes to interpolation. I am trying to replicate some of the functionality of Matlab's interp2. I have a distance function map (available at this Google drive location) https://drive.google.com/open?id=0B6acq_amk5e3X0Q5UG1ya1VhSlE&authuser=0 Values are normalized in the range 0-1. Size is 200 rows by 300 columns. I can load it up with this code snippet: import numpy as np dstnc1=np.load('dstnc.npy') Coordinates are defined by the next snippet: xmin = 0. xmax = 9000. ymin = 0. ymax = 6000. r1,c1 = dstnc1.shape x = np.linspace(xmin,xmax,c1) y = np.linspace(ymin, ymax,r1) I have three map points defined by vectors xnew1, ynew1 with this snippet: xnew1=[3700.540199,3845.940199,3983.240199] ynew1=[1782.8611,1769.862,1694.862] I check their location with respect to the distance map with this: import matplotlib.pyplot as plt fig = plt.figure(figsize=(20, 16)) ax = fig.add_subplot(1, 1, 1) plt.imshow(dstnc1, cmap=my_cmap_r,vmin=0,vmax=0.3, extent=[0, 9000, 0, 6000], origin='upper') plt.scatter(xnew1, ynew1, s=50, linewidths=0.15) plt.show() They plot in the correct location. Now I would like to extract the distance value at those three points. I tried first interp2d. from scipy.interpolate import interp2d x1 = np.linspace(xmin,xmax,c1) y1 = np.linspace(ymin,ymax,r1) f = interp2d(x1, y1, dstnc1, kind='cubic') but when I try to evaluate with: test=f(xnew,ynew) I get this error message: --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-299-d0f42e609b23> in <module>() ----> 1 test=f(xnew,ynew) C:\...\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\interpolate\interpolate.pyc in __call__(self, x, y, dx, dy) 270 (self.y_min, self.y_max))) 271 --> 272 z = fitpack.bisplev(x, y, self.tck, dx, dy) 273 z = atleast_2d(z) 274 z = transpose(z) C:\...\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\interpolate\fitpack.pyc in bisplev(x, y, tck, dx, dy) 1027 z,ier = _fitpack._bispev(tx,ty,c,kx,ky,x,y,dx,dy) 1028 if ier == 10: -> 1029 raise ValueError("Invalid input data") 1030 if ier: 1031 raise TypeError("An error occurred") ValueError: Invalid input data If I try RectBivariateSpline: from scipy.interpolate import RectBivariateSpline x2 = np.linspace(xmin,xmax,r1) y2 = np.linspace(ymin,ymax,c1) f = RectBivariateSpline(x2, y2, dstnc1) I get this error: --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-302-d0f42e609b23> in <module>() ----> 1 test=f(xnew,ynew) C:\...\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\interpolate\fitpack2.pyc in __call__(self, x, y, mth, dx, dy, grid) 643 z,ier = dfitpack.bispev(tx,ty,c,kx,ky,x,y) 644 if not ier == 0: --> 645 raise ValueError("Error code returned by bispev: %s" % ier) 646 else: 647 # standard Numpy broadcasting ValueError: Error code returned by bispev: 10 Any suggestion as to whether I am using the wrong functions or the right function with wrong syntax, and how I may fix it is appreciated. Thank you
Hi All: This is not the answer to Matteo's question but probably a report on unclear documentation here: http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate... I am confused by the returns part, it says """ Returns: *values_x* : ndarray, shape xi.shape[:-1] + values.shape[ndim:] Interpolated values at input coordinates. """ But in the examples, the returned value f is used as a callable... """ Now use the obtained interpolation function and plot the result:
xnew = np.arange(-5.01, 5.01, 1e-2)>>> ynew = np.arange(-5.01, 5.01, 1e-2)>>> znew = f(xnew, ynew)>>> plt.plot(x, z[0, :], 'ro-', xnew, znew[0, :], 'b-')>>> plt.show()
""" Probably returns section corresponds to this version of scipy, but the examples are from the later version, which has probably confused Matteo. Cheers 2015-05-14 17:15 GMT-04:00 Matteo Niccoli <matteo@mycarta.ca>:
I am relatively new to Scipy, at least when it comes to interpolation. I am trying to replicate some of the functionality of Matlab's interp2.
I have a distance function map (available at this Google drive location) https://drive.google.com/open?id=0B6acq_amk5e3X0Q5UG1ya1VhSlE&authuser=0
Values are normalized in the range 0-1. Size is 200 rows by 300 columns.
I can load it up with this code snippet: import numpy as np dstnc1=np.load('dstnc.npy')
Coordinates are defined by the next snippet: xmin = 0. xmax = 9000. ymin = 0. ymax = 6000. r1,c1 = dstnc1.shape x = np.linspace(xmin,xmax,c1) y = np.linspace(ymin, ymax,r1)
I have three map points defined by vectors xnew1, ynew1 with this snippet: xnew1=[3700.540199,3845.940199,3983.240199] ynew1=[1782.8611,1769.862,1694.862]
I check their location with respect to the distance map with this: import matplotlib.pyplot as plt fig = plt.figure(figsize=(20, 16)) ax = fig.add_subplot(1, 1, 1) plt.imshow(dstnc1, cmap=my_cmap_r,vmin=0,vmax=0.3, extent=[0, 9000, 0, 6000], origin='upper') plt.scatter(xnew1, ynew1, s=50, linewidths=0.15) plt.show()
They plot in the correct location. Now I would like to extract the distance value at those three points. I tried first interp2d.
from scipy.interpolate import interp2d x1 = np.linspace(xmin,xmax,c1) y1 = np.linspace(ymin,ymax,r1) f = interp2d(x1, y1, dstnc1, kind='cubic')
but when I try to evaluate with: test=f(xnew,ynew)
I get this error message: --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-299-d0f42e609b23> in <module>() ----> 1 test=f(xnew,ynew)
C:\...\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\interpolate\interpolate.pyc in __call__(self, x, y, dx, dy) 270 (self.y_min, self.y_max))) 271 --> 272 z = fitpack.bisplev(x, y, self.tck, dx, dy) 273 z = atleast_2d(z) 274 z = transpose(z)
C:\...\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\interpolate\fitpack.pyc in bisplev(x, y, tck, dx, dy) 1027 z,ier = _fitpack._bispev(tx,ty,c,kx,ky,x,y,dx,dy) 1028 if ier == 10: -> 1029 raise ValueError("Invalid input data") 1030 if ier: 1031 raise TypeError("An error occurred")
ValueError: Invalid input data
If I try RectBivariateSpline: from scipy.interpolate import RectBivariateSpline x2 = np.linspace(xmin,xmax,r1) y2 = np.linspace(ymin,ymax,c1) f = RectBivariateSpline(x2, y2, dstnc1)
I get this error:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-302-d0f42e609b23> in <module>() ----> 1 test=f(xnew,ynew)
C:\...\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\interpolate\fitpack2.pyc in __call__(self, x, y, mth, dx, dy, grid) 643 z,ier = dfitpack.bispev(tx,ty,c,kx,ky,x,y) 644 if not ier == 0: --> 645 raise ValueError("Error code returned by bispev: %s" % ier) 646 else: 647 # standard Numpy broadcasting
ValueError: Error code returned by bispev: 10
Any suggestion as to whether I am using the wrong functions or the right function with wrong syntax, and how I may fix it is appreciated. Thank you
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
-- Sasha
Update. I just checked: I am running Python 2.7.9 and Scipy 0.14.0. As noted by Oleksandr I may have been using the Syntax recommended for a newer version. Can anybody suggest a workaround or the correct syntax for version 0.14.0? Also, there was a typo when I run the notebook last and copied code in my post (xnew, ynew, instead of xnew1, ynew1). I run again my code with xnew1, ynew1 and I still get the errors. Thanks Matteo On Thu, May 14, 2015 6:24 pm, Oleksandr Huziy wrote:
Hi All:
This is not the answer to Matteo's question but probably a report on unclear documentation here:
http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpol ate.interp2d.html
I am confused by the returns part, it says
""" Returns:
*values_x* : ndarray, shape xi.shape[:-1] + values.shape[ndim:]
Interpolated values at input coordinates.
"""
But in the examples, the returned value f is used as a callable...
"""
Now use the obtained interpolation function and plot the result:
xnew = np.arange(-5.01, 5.01, 1e-2)>>> ynew = np.arange(-5.01, 5.01, 1e-2)>>> znew = f(xnew, ynew)>>> plt.plot(x, z[0, :], 'ro-', xnew, znew[0, :], 'b-')>>> plt.show()
"""
Probably returns section corresponds to this version of scipy, but the examples are from the later version, which has probably confused Matteo.
Cheers
2015-05-14 17:15 GMT-04:00 Matteo Niccoli <matteo@mycarta.ca>:
I am relatively new to Scipy, at least when it comes to interpolation. I am trying to replicate some of the functionality of Matlab's interp2.
I have a distance function map (available at this Google drive location) https://drive.google.com/open?id=0B6acq_amk5e3X0Q5UG1ya1VhSlE&authuser= 0
Values are normalized in the range 0-1. Size is 200 rows by 300 columns.
I can load it up with this code snippet: import numpy as np dstnc1=np.load('dstnc.npy')
Coordinates are defined by the next snippet: xmin = 0. xmax = 9000. ymin = 0. ymax = 6000. r1,c1 = dstnc1.shape x = np.linspace(xmin,xmax,c1) y = np.linspace(ymin, ymax,r1)
I have three map points defined by vectors xnew1, ynew1 with this snippet: xnew1=[3700.540199,3845.940199,3983.240199] ynew1=[1782.8611,1769.862,1694.862]
I check their location with respect to the distance map with this: import matplotlib.pyplot as plt fig = plt.figure(figsize=(20, 16)) ax = fig.add_subplot(1, 1, 1) plt.imshow(dstnc1, cmap=my_cmap_r,vmin=0,vmax=0.3, extent=[0, 9000, 0, 6000], origin='upper') plt.scatter(xnew1, ynew1, s=50, linewidths=0.15) plt.show()
They plot in the correct location. Now I would like to extract the distance value at those three points. I tried first interp2d.
from scipy.interpolate import interp2d x1 = np.linspace(xmin,xmax,c1) y1 = np.linspace(ymin,ymax,r1) f = interp2d(x1, y1, dstnc1, kind='cubic')
but when I try to evaluate with: test=f(xnew,ynew)
I get this error message: ------------------------------------------------------------------------ --- ValueError Traceback (most recent call last) <ipython-input-299-d0f42e609b23> in <module>() ----> 1 test=f(xnew,ynew)
C:\...\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\interpo late\interpolate.pyc in __call__(self, x, y, dx, dy) 270 (self.y_min, self.y_max))) 271 --> 272 z = fitpack.bisplev(x, y, self.tck, dx, dy) 273 z = atleast_2d(z) 274 z = transpose(z)
C:\...\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\interpo late\fitpack.pyc in bisplev(x, y, tck, dx, dy) 1027 z,ier = _fitpack._bispev(tx,ty,c,kx,ky,x,y,dx,dy) 1028 if ier == 10: -> 1029 raise ValueError("Invalid input data") 1030 if ier: 1031 raise TypeError("An error occurred")
ValueError: Invalid input data
If I try RectBivariateSpline: from scipy.interpolate import RectBivariateSpline x2 = np.linspace(xmin,xmax,r1) y2 = np.linspace(ymin,ymax,c1) f = RectBivariateSpline(x2, y2, dstnc1)
I get this error:
----------------------------------------------------------------------- ---- ValueError Traceback (most recent call last) <ipython-input-302-d0f42e609b23> in <module>() ----> 1 test=f(xnew,ynew)
C:\...\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\interpo late\fitpack2.pyc in __call__(self, x, y, mth, dx, dy, grid) 643 z,ier = dfitpack.bispev(tx,ty,c,kx,ky,x,y) 644 if not ier == 0: --> 645 raise ValueError("Error code returned by bispev: %s" % ier) 646 else: 647 # standard Numpy broadcasting
ValueError: Error code returned by bispev: 10
Any suggestion as to whether I am using the wrong functions or the right function with wrong syntax, and how I may fix it is appreciated. Thank you
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
-- Sasha _______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
participants (2)
-
Matteo Niccoli
-
Oleksandr Huziy