Mokus
Mokus

Reputation: 10410

How can I interpolate data in python?

I have a 4D dataset (time, z, y, x) and I would like to interpolate the data to get a higher resolution, this is a simple example code:

import numpy as np
from scipy.interpolate import griddata

x_0 = 10
cut_index = 10

res = 200j
x_index = x_0
y_index = np.linspace(0, 100, 50).astype(int)
z_index = np.linspace(0, 50, 25).astype(int)

#Time, zyx-coordinate
u = np.random.randn(20, 110, 110, 110)

z_index, y_index = np.meshgrid(z_index, y_index)
data = u[cut_index, z_index, y_index, x_index]


res = 200j
y_f = np.mgrid[0:100:res]
z_f = np.mgrid[0:50:res]
z_f, y_f = np.meshgrid(z_f, y_f)

data = griddata((z_index, y_index), data, (z_f, y_f))

I am getting the ValueError: invalid shape for input data points error. What kind of input is expected by the griddata function?

Upvotes: 2

Views: 390

Answers (1)

Ben K.
Ben K.

Reputation: 1150

Your data parameter has to be a 1D array. Try flattening the arrays:

data = griddata((z_index.flatten(), y_index.flatten()), data.flatten(), (z_f, y_f))

Upvotes: 2

Related Questions