Python SciPy RectSphereVariableSphere球体上的样条插值返回值错误
我有一个非常粗糙的球体上的三维测量数据,我想插值。 我发现scipy.interpolate中的RectSphereBivariateSpline应该是最合适的。 我使用RectSphereBivariateSpline文档中的示例作为起点,现在有以下代码:Python SciPy RectSphereVariableSphere球体上的样条插值返回值错误,python,numpy,scipy,interpolation,Python,Numpy,Scipy,Interpolation,我有一个非常粗糙的球体上的三维测量数据,我想插值。 我发现scipy.interpolate中的RectSphereBivariateSpline应该是最合适的。 我使用RectSphereBivariateSpline文档中的示例作为起点,现在有以下代码: """ read csv input file, post process and plot 3D data """ import csv import numpy as np from mayavi import mlab from sc
""" read csv input file, post process and plot 3D data """
import csv
import numpy as np
from mayavi import mlab
from scipy.interpolate import RectSphereBivariateSpline
# user input
nElevationPoints = 17 # needs to correspond with csv file
nAzimuthPoints = 40 # needs to correspond with csv file
threshold = - 40 # needs to correspond with how measurement data was captured
turnTableStepSize = 72 # needs to correspond with measurement settings
resolution = 0.125 # needs to correspond with measurement settings
# read data from file
patternData = np.empty([nElevationPoints, nAzimuthPoints]) # empty buffer
ifile = open('ttest.csv') # need the 'b' suffix to prevent blank rows being inserted
reader = csv.reader(ifile,delimiter=',')
reader.next() # skip first line in csv file as this is only text
for nElevation in range (0,nElevationPoints):
# azimuth
for nAzimuth in range(0,nAzimuthPoints):
patternData[nElevation,nAzimuth] = reader.next()[2]
ifile.close()
# post process
def r(thetaIndex,phiIndex):
"""r(thetaIndex,phiIndex): function in 3D plotting to return positive vector length from patternData[theta,phi]"""
radius = -threshold + patternData[thetaIndex,phiIndex]
return radius
#phi,theta = np.mgrid[0:nAzimuthPoints,0:nElevationPoints]
theta = np.arange(0,nElevationPoints)
phi = np.arange(0,nAzimuthPoints)
thetaMesh, phiMesh = np.meshgrid(theta,phi)
stepSizeRad = turnTableStepSize * resolution * np.pi / 180
theta = theta * stepSizeRad
phi = phi * stepSizeRad
# create new grid to interpolate on
phiIndex = np.linspace(1,360,360)
phiNew = phiIndex*np.pi/180
thetaIndex = np.linspace(1,180,180)
thetaNew = thetaIndex*np.pi/180
thetaNew,phiNew = np.meshgrid(thetaNew,phiNew)
# create interpolator object and interpolate
data = r(thetaMesh,phiMesh)
lut = RectSphereBivariateSpline(theta,phi,data.T)
data_interp = lut.ev(thetaNew.ravel(),phiNew.ravel()).reshape((360,180)).T
x = (data_interp(thetaIndex,phiIndex)*np.cos(phiNew)*np.sin(thetaNew))
y = (-data_interp(thetaIndex,phiIndex)*np.sin(phiNew)*np.sin(thetaNew))
z = (data_interp(thetaIndex,phiIndex)*np.cos(thetaNew))
# plot 3D data
obj = mlab.mesh(x, y, z, colormap='jet')
obj.enable_contours = True
obj.contour.filled_contours = True
obj.contour.number_of_contours = 20
mlab.show()
文档中的示例是有效的,但当我尝试使用以下测试数据运行上述代码时:在声明RectSphereBivariateSpline插值器对象的代码位置,我得到一个ValueError:
值错误:
错误:输入时,输入数据受有效性控制
必须满足以下限制条件。
-1如果在调用
RectSphereBivariateSpline
之前稍微更改它,则看起来theta[0]
不能为0:
theta[0] += 1e-6
完美的这解决了插值器对象调用!我现在看到我还有一些其他问题需要解决,但希望我能自己解决这些问题。你能告诉我你是怎么知道的吗(这样我才能变得更加自给自足)?@user3116919,我将你的数据与示例数据进行了比较,唯一不同的是你的lats数据包含零。当lat为0时,lons将没有办法,所以我认为这就是问题所在。在实现了您的解决方案并解决了之后出现的一些其他问题之后,我现在有了运行代码。请参见上面的编辑。虽然它运行,但插值数据的值与原始数据(以及结果图)相差很大。有什么想法吗?@user3116919因为stackoverflow只关注一个问题+答案,所以最好发布一个新的、孤立的、具体的问题,而不是在这个问题中展开。祝你好运看起来很有趣。谢谢,@kobejohn,我刚刚按照你的建议,把下面的问题作为一个单独的主题发布了。
theta[0] += 1e-6