在Python中将较大的不规则网格插值到另一个不规则网格 [英] Interpolate large irregular grid onto another irregular grid in Python
问题描述
我正在尝试使用Python将复杂值从一个不规则网格插值到另一个不规则网格.网格是二维的,有103,113个数据点.我正在使用Python 2.6.6,Scipy 0.7.2,Numpy 1.3.0,Matplotlib 0.99.3
I am trying to interpolate complex values from one irregular grid to another irregular grid using Python. The grids are in 2D and there are 103,113 data points. I am using Python 2.6.6, Scipy 0.7.2, Numpy 1.3.0, Matplotlib 0.99.3
在使用griddata的Matlab中,这大约需要5秒钟.
In Matlab using griddata this is achieved in roughly 5 seconds.
BnGRID2 = griddata(R_GRID1,Z_GRID1,BnGRID1,R_GRID2,Z_GRID2) (MATLAB)
(请注意,所有数组均为201 x 513)
(Note all arrays are 201 x 513)
但是,如果我尝试使用matplotlib.mlab.griddata,即使我尝试仅使用实部,也会出现memoryError:
However, if I try using matplotlib.mlab.griddata I get a memoryError even if I try to work with the real part only:
mlab.griddata(R_GRID1.flatten(),Z_GRID1.flatten(),num.real(BnGRID1.flatten()),R_GRID2.flatten(),Z_GRID2.flatten())
如果我尝试使用interp2d,则会遇到分段错误,并且Python退出:
If I try using interp2d I get a segmentation fault and Python exits:
a = interp.interp2d(R_GRID1,Z_GRID1,num.real(BnGRID1))
我已经尝试过使用KDTree,这似乎可以正常工作,但是,与Matlab的几秒钟相比,它花费了几分钟,但是我对这个选项的探索还不够.
I have tried using KDTree and this seems to work ok, however, it takes a few minutes compared with the few seconds for Matlab, but I haven't explored this option too much yet.
您是否想知道是否有人对如何像Matlab一样快地完成此工作有任何想法?我注意到较新版本的Scipy也具有griddata,有人知道它是否可以处理较大的不规则网格?
Was wondering if anyone has any ideas how I can get this done as quickly as Matlab seems to? I noticed that the newer version of Scipy also has griddata, does anyone know if this can handle large irregular grids?
推荐答案
Scipy的griddata似乎能够处理这种大小的数据集而不会出现问题:
Scipy's griddata seems to be able to deal with data sets of this size without problems:
import numpy as np
import scipy.interpolate
# old grid
x, y = np.mgrid[0:1:201j, 0:1:513j]
z = np.sin(x*20) * (1j + np.cos(y*3))**2 # some data
# new grid
x2, y2 = np.mgrid[0.1:0.9:201j, 0.1:0.9:513j]
# interpolate onto the new grid
z2 = scipy.interpolate.griddata((x.ravel(), y.ravel()), z.ravel(), (x2, y2), method='cubic')
在旧的AMD Athlon上,griddata步骤大约需要5秒钟.
The griddata step takes about 5s on an old AMD Athlon.
如果您的数据在网格上(即,与值z [i,j]对应的坐标为(x [i],y [j])),则可以使用scipy.interpolate.RectBivariateSpline
If your data is on a grid (i.e., the coordinates corresponding to value z[i,j] are (x[i], y[j])), you can get more speed by using 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秒.它的速度要快得多,因为即使您的网格间距不规则,只要网格是矩形,就可以使用更有效的算法.
which takes 0.05s. It's much faster, because even if your grid spacings are irregular, a more efficient algorithm can be used as long as the grid is rectangular.
这篇关于在Python中将较大的不规则网格插值到另一个不规则网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!