5
votes

I have a set o data (stored in 2D numpy arrays) representing simulations for a same problem. However, each simulation comes from a different model, which results in different resolutions for each of them. For example, these are some of the simulations' sizes:

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

What I'd like to do is to convert them all to the same resolution, for instance, 144 x 157. I believe I have to perform an interpolation, however, I'm not sure which method to use in Python.

I've been reading about these:

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

The (3) and (4) seems to fit best the problem, however, I'm unsure about how to make them return a new gridded (2D) data, with an specified resolution.

1
Are all of your data cover the same range of coordinates? In other words, are all of the measurements between (for instance) 0 to 3 in x, and 0 to 10 in y?James
Yes, they all cover the same region, but in different resolutions.pceccon
These being simulations are thee data noise-free?Paul Panzer
They are smoothed during the last step of simulation.pceccon
I was just asking because if noise is isgnificant you may want consider using not interpolation but some kind of averaging on the oversampled bits of the data.Paul Panzer

1 Answers

6
votes

It turns out that I could solve it using 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))

Input:

enter image description here

Output:

enter image description here