使用scipy.interpolate.LSQBivariateSplines将二维样条曲线拟合到有间隙的噪声数据



我有一个在网格上有矩形数据的numpy数组,我想在其中拟合一个二维样条曲线,以再现大规模变化,同时消除所有/大部分噪声。数据中还有一些区域被标记为无效,其值为NaN。

我试着使用scipy.interpolate.RectBivariateSpline函数,但这些差距把结果搞砸了。因此,我尝试使用同一包中的LSQBivativeSpline函数,希望当我将所有NaN像素的权重设置为0时,它会忽略它们。然而,就在那时,我遇到了以下错误,我不知道如何避免:

我的代码是:

# some preparation, loading data and stuff
# all my data is stored in 'data'
# Create the knots (10 knots in each direction, making 100 total
xcoord = numpy.linspace(5, data.shape[0]-5, 10)
ycoord = numpy.linspace(5, data.shape[1]-5, 10)
# Create all weights, and set them to 0 when the data is NaN
weights = numpy.ones(data.shape)
weights[numpy.isnan(data)] = 1e-15  # weights must be >0
# LSQBivariateSpline needs x and y coordinates as 1-D arrays
x, y = numpy.indices(data.shape)
spline_fit = scipy.interpolate.LSQBivariateSpline(x.ravel(), y.ravel(), data.ravel(), 
                                               xcoord, ycoord, 
                                               w=weights.ravel(),
                                               bbox=[None, None, None, None], 
                                               kx=2, ky=2)

代码的输出是以下错误消息:

The coefficients of the spline returned have been computed as the
minimal norm least-squares solution of a (numerically) rank deficient
system (deficiency=25). If deficiency is large, the results may be
inaccurate. Deficiency may strongly depend on the value of eps.
done!
Traceback (most recent call last):
  File "./fitpg.py", line 513, in <module>
    fit_pupilghost(prebinned, (center_y, center_x), (r_inner, r_outer), dr)
  File "./fitpg.py", line 298, in fit_pupilghost
    fitout = pupil2d(radius[:,y], angle[:,y])
  File "/usr/local/lib64/python2.7/site-packages/scipy/interpolate/fitpack2.py", line 545, in __call__
    raise ValueError("Error code returned by bispev: %s" % ier)
ValueError: Error code returned by bispev: 10

我输入的输入矩阵("数据")大约是1000 x 1000 px,这应该足以将样条曲线约束在100节处。将每个方向的节数增加到100 amek,代码运行速度会慢一些,但除了不足的数量外,没有任何变化。我还试图增加和减少eps值,其值在1-e30到0.9之间(默认值为1e-16

我也试着在谷歌上搜索错误代码,但没有找到一个好的结果,所以这也没有帮助。

知道这里可能出了什么问题吗?或者有没有解决这个问题的变通方法/更好的方法?

任何帮助都将不胜感激。

感谢

样条曲线拟合代码不以任何特殊方式处理NaN。由于任何与NaN接触的数字也会变成NaN,这意味着它们的存在会破坏整个计算,所以你不会得到任何结果。

除了设置零权重(例如)之外,您还可以用一些(任意)有限值替换NaN值

weights = numpy.ones(data.shape)
mask = numpy.isnan(data)
weights[mask] = 0
data[mask] = 0   # arbitrary

由于权重很小,因此所选的值应该无关紧要。如果出于某种原因不喜欢零权重,您也可以尝试将相应的权重设置为较小的值,例如1e-15

最新更新