【问题标题】:Interpolate large irregular grid onto another irregular grid in Python在 Python 中将大的不规则网格插入到另一个不规则网格上
【发布时间】:2011-12-03 20:08:40
【问题描述】:

我正在尝试使用 Python 将复杂值从一个不规则网格插入到另一个不规则网格。网格是二维的,有 103,113 个数据点。我正在使用 Python 2.6.6、Scipy 0.7.2、Numpy 1.3.0、Matplotlib 0.99.3

在 Matlab 中使用 griddata 大约需要 5 秒。

BnGRID2  = griddata(R_GRID1,Z_GRID1,BnGRID1,R_GRID2,Z_GRID2) (MATLAB)

(注意所有数组都是 201 x 513)

但是,如果我尝试使用 matplotlib.mlab.griddata,即使我尝试仅使用实部,也会出现 memoryError:

mlab.griddata(R_GRID1.flatten(),Z_GRID1.flatten(),num.real(BnGRID1.flatten()),R_GRID2.flatten(),Z_GRID2.flatten())

如果我尝试使用 interp2d,我会遇到分段错误并且 Python 退出:

a = interp.interp2d(R_GRID1,Z_GRID1,num.real(BnGRID1))

我已经尝试使用 KDTree,这似乎可以正常工作,但是,与 Matlab 的几秒钟相比,它需要几分钟,但我还没有过多地探索这个选项。

想知道是否有人知道如何像 Matlab 一样快速完成这项工作?我注意到新版本的 Scipy 也有 griddata,有谁知道这是否可以处理大型不规则网格?

【问题讨论】:

    标签: python numpy matplotlib scipy interpolation


    【解决方案1】:

    Scipy 的 griddata 似乎能够毫无问题地处理这种大小的数据集:

    将 numpy 导入为 np 导入 scipy.interpolate # 旧网格 x, y = np.mgrid[0:1:201j, 0:1:513j] z = np.sin(x*20) * (1j + np.cos(y*3))**2 # 一些数据 # 新网格 x2, y2 = np.mgrid[0.1:0.9:201j, 0.1:0.9:513j] # 插入到新网格上 z2 = scipy.interpolate.griddata((x.ravel(), y.ravel()), z.ravel(), (x2, y2), method='cubic')

    在旧的 AMD Athlon 上,griddata 步骤大约需要 5 秒。

    如果你的数据在一个网格上(即z[i,j]值对应的坐标是(x[i], y[j])),使用scipy.interpolate.RectBivariateSpline可以得到更快的速度

    z3 = (scipy.interpolate.RectBivariateSpline(x[:,0], y[0,:], z.real)(x2[:,0], y2[0,:]) + 1j*scipy.interpolate.RectBivariateSpline(x[:,0], y[0,:], z.imag)(x2[:,0], y2[0,:]))

    这需要 0.05 秒。它要快得多,因为即使您的网格间距不规则,只要网格是矩形的,也可以使用更高效的算法。

    【讨论】:

      猜你喜欢
      • 2014-03-08
      • 1970-01-01
      • 2013-03-13
      • 2011-10-20
      • 1970-01-01
      • 2018-08-14
      • 2021-12-01
      • 2013-08-26
      • 1970-01-01
      相关资源
      最近更新 更多