Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带孔的迭代二维栅格插值(缺少值)_Python_Scipy_Interpolation - Fatal编程技术网

Python 带孔的迭代二维栅格插值(缺少值)

Python 带孔的迭代二维栅格插值(缺少值),python,scipy,interpolation,Python,Scipy,Interpolation,我有以下问题: 我有两个网格,s0B和s1B,都是20x20,还有一个函数s_0(s0,s1)在这些网格点上进行计算 但是,对于某些角点,su0(s0,s1)无法计算,并返回np.nan 我希望有一个计算网格点的插值函数,然后我可以在根解问题中使用它(固定点在s_0和模拟s_1)。下面是代码的大致草图: # mesh 1d grids: ss0 = np.array([ 0.38445455, 0.40388198, 0.42110923, 0.43626859, 0.44949237,

我有以下问题:

我有两个网格,
s0B
s1B
,都是
20x20
,还有一个函数
s_0(s0,s1)
在这些网格点上进行计算

但是,对于某些角点,
su0(s0,s1)
无法计算,并返回
np.nan

我希望有一个计算网格点的插值函数,然后我可以在根解问题中使用它(固定点在
s_0
和模拟
s_1
)。下面是代码的大致草图:

# mesh 1d grids:
ss0 = np.array([ 0.38445455,  0.40388198,  0.42110923,  0.43626859,  0.44949237,
    0.46091287,  0.47066239,  0.47887323,  0.48567771,  0.49120811,
    0.49559675,  0.49897593,  0.50147794,  0.50323509,  0.50437969,
    0.50504404,  0.50536044,  0.50546118,  0.50547859,  0.50554495])
ss1 = np.array([ 0.43490909,  0.50764658,  0.5719651 ,  0.62838234,  0.67741596,
    0.71958365,  0.75540309,  0.78539195,  0.81006791,  0.82994865,
    0.84555185,  0.85739519,  0.86599635,  0.87187299,  0.87554281,
    0.87752348,  0.87833267,  0.87848808,  0.87850736,  0.87890821])

s0B, s1B = np.meshgrid(ss0, ss1, indexing='ij')
# generate interpolated functions
idx = ~np.isnan(s0Max)
if idx.sum() == 0:
    # check if there are any points at all
    raise notImplementedError
s0MaxInterp = interpolate.interp2d(
    s0B[idx], s1B[idx], s0Max[idx],
    fill_value=np.nan, kind='linear')
idx = ~np.isnan(s1Max)
s1MaxInterp = interpolate.interp2d(
    s0B[idx], s1B[idx], s1Max[idx],
    fill_value=np.nan, kind='linear')

def errorFixedPoint(x, s0MaxInterp, s1MaxInterp, grids):
    s0, s1 = x
    # I skip some checks whether s0, s1 are inside the interpolation range
    return np.array([s0 - s0MaxInterp(s0, s1)[0], s1 - s1MaxInterp(s0, s1)[0]])

result = optimize.root(errorFixedPoint, np.array([0.5, 0.9]),
                           args=(s0MaxInterp, s1MaxInterp, (ss0, ss1)), method='lm')
但是,在尝试此操作时,我得到了一个
运行时警告

RuntimeWarning: No more knots can be added because the number of B-spline
coefficients already exceeds the number of data points m.
Probable causes: either s or m too small. (fp>s)
    kx,ky=1,1 nx,ny=20,20 m=314 fp=0.000001 s=0.000000
  warnings.warn(RuntimeWarning(_iermess2[ierm][0] + _mess))
插值有时显得非常离谱:

然而,这可能是因为函数在两个维度上都是非常非线性的:

我在其他地方读到建议使用
scipy.interpolate.griddata
,而不是
interpolate2d
。但是,我必须迭代插值(对于根查找过程),而网格数据不支持这一点

我如何解决这个问题

为了再现性,我将
s0Max
s1Max
的值粘贴到此处:

nan = np.nan
s0Max = np.array([[        nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
         0.51494141,  0.51347656,  0.51210937,  0.51210937,  0.51210937,
         0.51210937,  0.51210937,  0.51210937,  0.51210937,  0.51210937],
       [        nan,         nan,         nan,         nan,         nan,
         0.51337891,  0.5109375 ,  0.51083984,  0.50253906,  0.50175781,
         0.50429687,  0.50957031,  0.50859375,  0.50849609,  0.50839844,
         0.50839844,  0.50839844,  0.50839844,  0.50839844,  0.50839844],
       [        nan,         nan,         nan,  0.51347656,  0.50527344,
         0.50009766,  0.49824219,  0.49746094,  0.49746094,  0.496875  ,
         0.49746094,  0.49941406,  0.50742187,  0.50732422,  0.50732422,
         0.50732422,  0.50732422,  0.50732422,  0.50732422,  0.50732422],
       [        nan,         nan,  0.51347656,  0.5015625 ,  0.49863281,
         0.49746094,  0.49628906,  0.49619141,  0.49570313,  0.49570313,
         0.49619141,  0.49746094,  0.50722656,  0.50634766,  0.50625   ,
         0.50625   ,  0.50625   ,  0.50625   ,  0.50625   ,  0.50625   ],
       [        nan,         nan,  0.50253906,  0.49824219,  0.496875  ,
         0.49619141,  0.49560547,  0.49511719,  0.49511719,  0.49511719,
         0.49550781,  0.49677734,  0.50625   ,  0.50625   ,  0.50625   ,
         0.50625   ,  0.50625   ,  0.50625   ,  0.50625   ,  0.50625   ],
       [        nan,  0.51210937,  0.49941406,  0.49746094,  0.49619141,
         0.49560547,  0.49511719,  0.49453125,  0.49453125,  0.49453125,
         0.49511719,  0.49628906,  0.50625   ,  0.50527344,  0.50527344,
         0.50527344,  0.50527344,  0.50527344,  0.50527344,  0.50527344],
       [        nan,  0.50341797,  0.49804688,  0.49628906,  0.49570313,
         0.49511719,  0.49453125,  0.49414062,  0.49404297,  0.49404297,
         0.49453125,  0.49570313,  0.50527344,  0.50527344,  0.50527344,
         0.50527344,  0.50527344,  0.50527344,  0.50527344,  0.50527344],
       [        nan,  0.50078125,  0.49746094,  0.49619141,  0.49511719,
         0.49453125,  0.49414062,  0.49404297,  0.49394531,  0.49394531,
         0.49453125,  0.49570313,  0.50527344,  0.50527344,  0.50527344,
         0.50527344,  0.50527344,  0.50527344,  0.50527344,  0.50527344],
       [        nan,  0.49941406,  0.496875  ,  0.49570313,  0.49511719,
         0.49453125,  0.49404297,  0.49394531,  0.49355469,  0.49394531,
         0.49414062,  0.49570313,  0.50527344,  0.50527344,  0.50517578,
         0.50517578,  0.50507812,  0.50507812,  0.50507812,  0.50449219],
       [        nan,  0.49873047,  0.49667969,  0.49560547,  0.49462891,
         0.49414062,  0.49394531,  0.49355469,  0.49345703,  0.49355469,
         0.49453125,  0.49628906,  0.50527344,  0.50517578,  0.50429687,
         0.50429687,  0.50429687,  0.50429687,  0.50429687,  0.50429687],
       [ 0.51347656,  0.49863281,  0.49628906,  0.49511719,  0.49453125,
         0.49404297,  0.49394531,  0.49355469,  0.49355469,  0.49404297,
         0.49570313,  0.50527344,  0.50429687,  0.50429687,  0.50429687,
         0.50429687,  0.50429687,  0.50429687,  0.50429687,  0.50429687],
       [ 0.51347656,  0.49814453,  0.49628906,  0.49511719,  0.49453125,
         0.49404297,  0.49394531,  0.49355469,  0.49404297,  0.49570313,
         0.50527344,  0.50429687,  0.50429687,  0.50429687,  0.50351563,
         0.50351563,  0.50341797,  0.50341797,  0.50341797,  0.50341797],
       [ 0.51347656,  0.49814453,  0.49619141,  0.49511719,  0.49453125,
         0.49404297,  0.49394531,  0.49404297,  0.49511719,  0.50527344,
         0.50429687,  0.50429687,  0.50351563,  0.50341797,  0.50341797,
         0.50332031,         nan,         nan,         nan,         nan],
       [ 0.51347656,  0.49814453,  0.49619141,  0.49511719,  0.49453125,
         0.49404297,  0.49404297,  0.49453125,  0.49873047,  0.50507812,
         0.50429687,  0.50351563,  0.50341797,         nan,         nan,
                nan,         nan,         nan,         nan,         nan],
       [ 0.51347656,  0.49814453,  0.49619141,  0.49511719,  0.49453125,
         0.49404297,  0.49404297,  0.49511719,  0.50527344,  0.50429687,
         0.50429687,  0.50341797,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan],
       [ 0.51347656,  0.49804688,  0.49619141,  0.49511719,  0.49453125,
         0.49404297,  0.49453125,  0.49570313,  0.50527344,  0.50429687,
         0.50429687,  0.50341797,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan],
       [ 0.51347656,  0.49804688,  0.49619141,  0.49511719,  0.49453125,
         0.49414062,  0.49453125,  0.49619141,  0.50527344,  0.50429687,
         0.50429687,  0.50341797,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan],
       [ 0.51347656,  0.49804688,  0.49619141,  0.49511719,  0.49453125,
         0.49414062,  0.49453125,  0.49628906,  0.50527344,  0.50429687,
         0.50351563,  0.50341797,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan],
       [ 0.51347656,  0.49804688,  0.49619141,  0.49511719,  0.49453125,
         0.49414062,  0.49453125,  0.49628906,  0.50527344,  0.50429687,
         0.50351563,  0.50341797,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan],
       [ 0.51347656,  0.49804688,  0.49619141,  0.49511719,  0.49453125,
         0.49414062,  0.49453125,  0.49628906,  0.50527344,  0.50429687,
         0.50351563,  0.50332031,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan]])


根据这个极好的答案,使用
interp2d
将无法处理更复杂的数据。建议使用

这似乎是可行的,并提供了更好的插值结果

import scipy.optimize as optimize
from scipy.interpolate import Rbf

s0MaxInterp = Rbf(s0B[idx], s1B[idx], s0Max[idx], function='cubic')
s1MaxInterp = Rbf(s0B[idx], s1B[idx], s1Max[idx], function='cubic')

def errorFixedPoint(x, s0MaxInterp, s1MaxInterp, grids):
    s0, s1 = x
    # I skip some checks whether s0, s1 are inside the interpolation range
    return np.array([s0 - s0MaxInterp(s0, s1), s1 - s1MaxInterp(s0, s1)])

result = optimize.root(errorFixedPoint, np.array([0.5, 0.9]),
                       args=(s0MaxInterp, s1MaxInterp, (ss0, ss1)), method='lm')
请注意,我删除了
errorFixedPoint
函数中的
[0]

import scipy.optimize as optimize
from scipy.interpolate import Rbf

s0MaxInterp = Rbf(s0B[idx], s1B[idx], s0Max[idx], function='cubic')
s1MaxInterp = Rbf(s0B[idx], s1B[idx], s1Max[idx], function='cubic')

def errorFixedPoint(x, s0MaxInterp, s1MaxInterp, grids):
    s0, s1 = x
    # I skip some checks whether s0, s1 are inside the interpolation range
    return np.array([s0 - s0MaxInterp(s0, s1), s1 - s1MaxInterp(s0, s1)])

result = optimize.root(errorFixedPoint, np.array([0.5, 0.9]),
                       args=(s0MaxInterp, s1MaxInterp, (ss0, ss1)), method='lm')