使用SciPy插值3d数据时如何提高性能 [英] How to improve performance when interpolating on 3d data with SciPy
问题描述
我有3d数据代表大气.现在,我想将此数据插值到一个公共的Z坐标(我的意思是应该从函数的配角中清楚看出这一点).以下代码可以正常工作,但是我想知道是否有一种方法可以改善性能...
I have 3d-data representing the atmosphere. Now I want to interpolate this data to a common Z coordinate (what I mean by that should be clear from the function's doctring). The following code works fine, but I was wondering if there were a way to improve the performance ...
def interpLevel(grid,value,data,interp='linear'):
"""
Interpolate 3d data to a common z coordinate.
Can be used to calculate the wind/pv/whatsoever values for a common
potential temperature / pressure level.
grid : numpy.ndarray
The grid. For example the potential temperature values for the whole 3d
grid.
value : float
The common value in the grid, to which the data shall be interpolated.
For example, 350.0
data : numpy.ndarray
The data which shall be interpolated. For example, the PV values for
the whole 3d grid.
kind : str
This indicates which kind of interpolation will be done. It is directly
passed on to scipy.interpolate.interp1d().
returs : numpy.ndarray
A 2d array containing the *data* values at *value*.
"""
ret = np.zeros_like(data[0,:,:])
# we need to copy the grid to a new one, because otherwise the flipping
# done below will be messed up
gr = np.zeros_like(grid)
da = np.zeros_like(data)
for latIdx in xrange(grid.shape[1]):
for lonIdx in xrange(grid.shape[2]):
# check if we need to flip the column
if grid[0,latIdx,lonIdx] > grid[-1,latIdx,lonIdx]:
gr[:,latIdx,lonIdx] = grid[::-1,latIdx,lonIdx]
da[:,latIdx,lonIdx] = data[::-1,latIdx,lonIdx]
else:
gr[:,latIdx,lonIdx] = grid[:,latIdx,lonIdx]
da[:,latIdx,lonIdx] = data[:,latIdx,lonIdx]
f = interpolate.interp1d(gr[:,latIdx,lonIdx], \
da[:,latIdx,lonIdx], \
kind=interp)
ret[latIdx,lonIdx] = f(value)
return ret
推荐答案
好吧,这可能只是因为它使用较少的内存而使速度稍有提高.
Well, this might give a small speed-up just because it uses less memory.
ret = np.zeros_like(data[0,:,:])
for latIdx in xrange(grid.shape[1]):
for lonIdx in xrange(grid.shape[2]):
# check if we need to flip the column
if grid[0,latIdx,lonIdx] > grid[-1,latIdx,lonIdx]:
ind = -1
else:
ind = 1
f = interpolate.interp1d(grid[::ind,latIdx,lonIdx], \
data[::ind,latIdx,lonIdx], \
kind=interp)
ret[latIdx,lonIdx] = f(value)
return ret
我所做的只是真正摆脱了gr和da.
All I've done is get rid of gr and da really.
除此之外,您是否使用大量不同的值(即值不同但其他参数相同)来调用此函数?如果是这样,您可能希望使该函数能够处理多个值(添加另一个维以退回,换句话说就是与值的长度一样长).然后,您可以更好地利用已创建的插值函数.
Other than that, are you calling this function with a whole lot of different values(i.e. value being different but other parameters the same)? If so, you might want to make the function be able to handle multiple values (add another dimension to ret in other words that is as long as the length of values). Then you are making better use of the interpolation function that you've created.
最后的建议是尝试分析器.它将使您看到花费最多的时间.
The last suggestion is to try a profiler. It will allow you to see what is taking the most time.
这篇关于使用SciPy插值3d数据时如何提高性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!