python - 使用 SciPy 对 3d 数据进行插值时如何提高性能

标签 python numpy scipy interpolation

我有代表大气层的 3d 数据。现在我想将此数据插入到一个公共(public) Z 坐标(我的意思应该从函数的文档中清楚)。下面的代码工作正常,但我想知道是否有提高性能的方法......

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

最佳答案

好吧,这可能会稍微提速,因为它使用的内存更少。

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。

除此之外,您是否使用大量不同的值调用此函数(即值不同但其他参数相同)?如果是这样,您可能希望使该函数能够处理多个值(添加另一个维度 ret 换句话说,它与值的长度一样长)。这样您就可以更好地利用您创建的插值函数。

最后的建议是尝试a profiler .它可以让您看到什么花费的时间最多。

关于python - 使用 SciPy 对 3d 数据进行插值时如何提高性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2312665/

相关文章:

python - 如何在 Python 2.7 中实现 GMRES 的 ILU 预处理器?

python - 为什么 Sonic Visualizer 和我的 Python 脚本之间的频谱分析存在 dB 差异?

python - 在 python 多处理池中共享 numpy 数组

python - 处理复数和numpy时如何在python中正确指定dtype?

mongodb - 从 mongodb 文档创建矩阵

apache-spark - svd 性能 pyspark 与 scipy

javascript - 带有 React 前端的 Django 后端

python - 如何打开并迭代 CSV 文件列表 - Python

python - 限制使用元素总数的背包

python - 记录脚本适用于 python2 和 python3 以及特定的 matplotlib