Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing data resolution in Python

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.

like image 747
pceccon Avatar asked Feb 16 '17 13:02

pceccon


1 Answers

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

like image 167
pceccon Avatar answered Sep 20 '22 02:09

pceccon