在 Python 中更改数据分辨率

pce*_*con 4 python interpolation numpy scipy

我有一组 o 数据(存储在 2D numpy 数组中)表示同一问题的模拟。但是,每个模拟都来自不同的模型,这会导致每个模拟的分辨率不同。例如,这些是一些模拟的大小:

  1. 1159 x 1367
  2. 144 × 157
  3. 72 × 82
  4. 446 × 500
  5. 135 × 151

我想做的是将它们全部转换为相同的分辨率,例如 144 x 157。我相信我必须执行插值,但是,我不确定在 Python 中使用哪种方法。

我一直在阅读这些:

  1. scipy.interpolate.griddata
  2. scipy.ndimage.interpolation.zoom.html
  3. scipy.interpolate.RegularGridInterpolator.html
  4. scipy.ndimage.interpolation.map_coordinates.html

(3) 和 (4) 似乎最适合解决问题,但是,我不确定如何使它们返回具有指定分辨率的新网格 (2D) 数据。

pce*_*con 5

事实证明,我可以使用scipy.interpolate.RegularGridInterpolator.html解决它:

import numpy as np
import pylab as plt

from scipy.interpolate import RegularGridInterpolator

def regrid(data, out_x, out_y):
    m = max(data.shape[0], data.shape[1])
    y = np.linspace(0, 1.0/m, data.shape[0])
    x = np.linspace(0, 1.0/m, data.shape[1])
    interpolating_function = RegularGridInterpolator((y, x), data)

    yv, xv = np.meshgrid(np.linspace(0, 1.0/m, out_y), np.linspace(0, 1.0/m, out_x))

    return interpolating_function((xv, yv))
Run Code Online (Sandbox Code Playgroud)

输入:

在此处输入图片说明

输出:

在此处输入图片说明