使用scipy.interpolateBSpline的困难:"TypeError: 'list' object cannot be interpreted as an integer"



从这里开始:

  • splrep能够根据路径和平滑因子计算B样条线结、系数和阶数
  • splev使用生成的B样条线启用插值
  • BSpline能够直接从节点、系数和次数构建样条曲线

然后,我应该被允许执行:

import numpy as np
from scipy.interpolate import splev, splprep, BSpline
path =  [(2077.0, 712.0, 1136.6176470588234), (2077.0004154771536, 974.630482962754, 1313.735294117647), (2077.1630960823995, 1302.460574562254, 1490.8529411764707), (2078.1944091179635, 1674.693193015173, 1667.9705882352941), (2080.5096120056783, 2086.976611915444, 1845.0882352941176), (2085.1051468332066, 2711.054258877495, 2022.2058823529412), (1477.0846185328733, 2803.6223679691457, 2199.323529411765), (948.4693105162195, 2802.0390667447105, 2376.4411764705883), (383.8615403256207, 2804.843424134807, 2553.5588235294117), (-41.6669725172834, 2497.067373170676, 2730.676470588235), (-37.94311919744064, 1970.5155845437525, 2907.794117647059), (-35.97395938535092, 1576.713103381243, 3084.9117647058824), (-35.125016151504795, 1214.2319876178394, 3262.029411764706), (-35.000550767864524, 893.3910350913443, 3439.1470588235297), (-35.0, 631.2108462417168, 3616.264705882353), (-35.0, 365.60545190581837, 3793.3823529411766), (-35.0, 100.00005756991993, 3970.5)]
p = [[x for x,y,z in path], [y for x,y,z in path], [z for x,y,z in path]]
tck, u = splprep(p, k=3)
t, c0, k = tck
sp = BSpline(t, k, c0)

目标是能够调整B样条线。但BSpline对我的论点并不满意:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/lalebarde/anaconda3/lib/python3.7/site-packages/scipy/interpolate/_bsplines.py", line 184, in __init__
self.k = operator.index(k)
TypeError: 'list' object cannot be interpreted as an integer

如果我检查变量的形状和类型:

type(t)
<class 'numpy.ndarray'>
type(c0)
<class 'list'>
type(k)
<class 'int'>
t.shape
(21,)
np.array(c0).shape
(3, 17)

我对BSpline的使用失败,来自文档:

类scipy.interpole.BSpline(t,c,k,extractive=True,axis=0(

t: ndarray,形状(n+k+1,(->节

c: ndarray,形状(>=n,…(-->样条曲线系数-k次样条曲线至少需要k+1个系数,因此n>=k+1。附加系数c[j],其中j>n、 被忽略。

k: int-->B样条阶

除了系数CCD_ 5之外,它应该是与我的路径CCD_。

例如,sp = BSpline(t, c0[0], k)执行时没有错误,就像c0[1]c0[2]一样,但当然,我希望使用splprep计算的所有系数。

从这里来看,scipy插值手册似乎令人困惑:

tck[1]:重新定位的控制点的x和y坐标

手册上写着:

(t,c,k(一个元组,包含节点向量、B样条系数和样条的阶

最终,我错误地解释了BSpline的样条曲线系数参数,从而对其进行了错误使用。

那么,我如何用BSpline或另一个函数从splprep返回的结和系数构建BSpline呢?

BSpline(t, k, c0)应为BSpline(t, c0, k)

编辑。事实上,还有一个问题:splprep返回数组列表,它与BSpline不一致。

注意splrep和splprep:之间的差异

基本上,splrep/prev是一致的,splrep/BSpline是一致的。但是splprep/BSplne不是。这是一个已知的疣,不能以向后兼容的方式修复。

如果要将它们一起使用,则需要转置c数组。基于您的OP示例:

In [1]: import numpy as np
...: from scipy.interpolate import splev, splprep, BSpline
...: path =  [(2077.0, 712.0, 1136.6176470588234), (2077.0004154771536, 974.6
...: 30482962754, 1313.735294117647), (2077.1630960823995, 1302.460574562254,
...:  1490.8529411764707), (2078.1944091179635, 1674.693193015173, 1667.97058
...: 82352941), (2080.5096120056783, 2086.976611915444, 1845.0882352941176), 
...: (2085.1051468332066, 2711.054258877495, 2022.2058823529412), (1477.08461
...: 85328733, 2803.6223679691457, 2199.323529411765), (948.4693105162195, 28
...: 02.0390667447105, 2376.4411764705883), (383.8615403256207, 2804.84342413
...: 4807, 2553.5588235294117), (-41.6669725172834, 2497.067373170676, 2730.6
...: 76470588235), (-37.94311919744064, 1970.5155845437525, 2907.794117647059
...: ), (-35.97395938535092, 1576.713103381243, 3084.9117647058824), (-35.125
...: 016151504795, 1214.2319876178394, 3262.029411764706), (-35.0005507678645
...: 24, 893.3910350913443, 3439.1470588235297), (-35.0, 631.2108462417168, 3
...: 616.264705882353), (-35.0, 365.60545190581837, 3793.3823529411766), (-35
...: .0, 100.00005756991993, 3970.5)]
...: p = [[x for x,y,z in path], [y for x,y,z in path], [z for x,y,z in path]
...: ]
...: tck, u = splprep(p, k=3, s=0)      # ADDED s=0 for clarity
...: 
In [2]: t, c, k = tck
In [3]: c1 = np.asarray(c)
In [4]: spl = BSpline(t, c1.T, k)         # Note the transpose
In [5]: spl(u) - path                     # these should match, and they do
Out[5]: 
array([[ -4.54747351e-13,  -1.13686838e-13,  -4.54747351e-13],
[  0.00000000e+00,  -1.13686838e-13,   0.00000000e+00],
[ -4.54747351e-13,   0.00000000e+00,   0.00000000e+00],
[  0.00000000e+00,  -2.27373675e-13,  -2.27373675e-13],
[ -4.54747351e-13,   0.00000000e+00,   4.54747351e-13],
[ -4.54747351e-13,   0.00000000e+00,  -6.82121026e-13],
[  2.27373675e-13,   0.00000000e+00,   0.00000000e+00],
[ -1.13686838e-13,  -4.54747351e-13,  -4.54747351e-13],
[  0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
[  4.26325641e-14,  -9.09494702e-13,   0.00000000e+00],
[  1.42108547e-14,  -4.54747351e-13,   0.00000000e+00],
[  0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
[  7.10542736e-15,   0.00000000e+00,  -4.54747351e-13],
[  0.00000000e+00,  -3.41060513e-13,   0.00000000e+00],
[ -7.10542736e-15,  -1.13686838e-13,   0.00000000e+00],
[  0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
[  0.00000000e+00,   0.00000000e+00,   0.00000000e+00]])

这个答案是基于https://github.com/scipy/scipy/issues/10389.那里的一般建议适用:如果你想要插值,更喜欢make_interp_spline而不是splrepsplprep。如果你想要平滑,目前只有FITPACK,splrep(与BSpline兼容(或splprep(需要手动转置(。

相关内容

  • 没有找到相关文章

最新更新