Search code examples
python-3.xscipygridinterpolationmesh

Python scipy interpolation meshgrid data


Dear all I want to interpolate an experimental data in order to make it look with higher resolution but apparently it does not work. I followed the example in this link for mgrid data the csv data can be found goes as follow.

Csv data

My code

import pandas as pd
import numpy as np
import scipy    

x=np.linspace(0,2.8,15)
y=np.array([2.1,2,1.9,1.8,1.7,1.6,1.5,1.4,1.3,1.2,1.1,0.9,0.7,0.5,0.3,0.13])
[X, Y]=np.meshgrid(x,y)

Vx_df=pd.read_csv("Vx.csv", header=None)
Vx=Vx_df.to_numpy()

tck=scipy.interpolate.bisplrep(X,Y,Vx)

plt.pcolor(X,Y,Vx, shading='nearest');
plt.show()

xi=np.linspace(0.1, 2.5, 30)
yi=np.linspace(0.15, 2.0, 50)

[X1, Y1]=np.meshgrid(xi,yi)

VxNew = scipy.interpolate.bisplev(X1[:,0], Y1[0,:], tck, dx=1, dy=1)

plt.pcolor(X1,Y1,VxNew, shading='nearest')
plt.show()

CSV DATA:

0.73,,,-0.08,-0.19,-0.06,0.02,0.27,0.35,0.47,0.64,0.77,0.86,0.90,0.93
0.84,,,0.13,0.03,0.12,0.23,0.32,0.52,0.61,0.72,0.83,0.91,0.96,0.95
1.01,1.47,,0.46,0.46,0.48,0.51,0.65,0.74,0.80,0.89,0.99,0.99,1.07,1.06
1.17,1.39,1.51,1.19,1.02,0.96,0.95,1.01,1.01,1.05,1.06,1.05,1.11,1.13,1.19
1.22,1.36,1.42,1.44,1.36,1.23,1.24,1.17,1.18,1.14,1.14,1.09,1.08,1.14,1.19
1.21,1.30,1.35,1.37,1.43,1.36,1.33,1.23,1.14,1.11,1.05,0.98,1.01,1.09,1.15
1.14,1.17,1.22,1.25,1.23,1.16,1.23,1.00,1.00,0.93,0.93,0.80,0.82,1.05,1.09
,0.89,0.95,0.98,1.03,0.97,0.94,0.84,0.77,0.68,0.66,0.61,0.48,,
,0.06,0.25,0.42,0.55,0.55,0.61,0.49,0.46,0.56,0.51,0.40,0.28,,
,0.01,0.05,0.13,0.23,0.32,0.33,0.37,0.29,0.30,0.32,0.27,0.25,,
,-0.02,0.01,0.07,0.15,0.21,0.23,0.22,0.20,0.19,0.17,0.20,0.21,0.13,
,-0.07,-0.05,-0.02,0.06,0.07,0.07,0.16,0.11,0.08,0.12,0.08,0.13,0.16,
,-0.13,-0.14,-0.09,-0.07,0.01,-0.03,0.06,0.02,-0.01,0.00,0.01,0.02,0.04,
,-0.16,-0.23,-0.21,-0.16,-0.10,-0.08,-0.05,-0.11,-0.14,-0.17,-0.16,-0.11,-0.05,
,-0.14,-0.25,-0.29,-0.32,-0.31,-0.33,-0.31,-0.34,-0.36,-0.35,-0.31,-0.26,-0.14,
,-0.02,-0.07,-0.24,-0.36,-0.39,-0.45,-0.45,-0.52,-0.48,-0.41,-0.43,-0.37,-0.22,

The image of the low resolution (without iterpolation) is Low resolution and the image I get after interpolation is High resolution

Can you please give me some advice? why it does not interpolate properly?


Solution

  • Ok so to interpolate we need to set up an input and output grid an possibly need to remove values from the grid that are missing. We do that like so

    array = pd.read_csv(StringIO(csv_string), header=None).to_numpy()
    
    def interp(array, scale=1, method='cubic'):
        x = np.arange(array.shape[1]*scale)[::scale]
        y = np.arange(array.shape[0]*scale)[::scale]
        x_in_grid, y_in_grid = np.meshgrid(x,y)
        x_out, y_out = np.meshgrid(np.arange(max(x)+1),np.arange(max(y)+1))
        array = np.ma.masked_invalid(array)
        x_in = x_in_grid[~array.mask]
        y_in = y_in_grid[~array.mask]
        return interpolate.griddata((x_in, y_in), array[~array.mask].reshape(-1),(x_out, y_out), method=method)
    

    Now we need to call this function 3 times. First we fill the missing values in the middle with spline interpolation. Then we fill the boundary values with nearest neighbor interpolation. And finally we size it up by interpreting the pixels as being a few pixels apart and filling in gaps with spline interpolation.

    array = interp(array)
    array = interp(array, method='nearest')
    array = interp(array, 50)
    plt.imshow(array)
    

    And we get the following result final picture