![]() ![]() :return volume_needed: desired value of the volume, i.e. :param z_needed: desired z coordinate of volume :param y_needed: desired y coordinate of volume :param x_needed: desired x coordinate of volume :param x_volume: z points of the volume grid :param y_volume: y points of the volume grid :param x_volume: x points of the volume grid scipy.interpolate's RegularGridInterpolator.ĭef trilinear_interpolation(x_volume, y_volume, z_volume, volume, x_needed, y_needed, z_needed): The question is old, but I think it needs some clarification, as nobody pointed out that the operation requested ( trilinear interpolation) can be easily implemented from scratch with consistent saving of computational time (around 10 times faster) w.r.t. Map_coordinates(v, coords, order=1, output=output, **kwargs) Return np.interp(interp_locs, corner_locs, index)Ĭoords =, )] Additional kwargsĪre passed on to ``_coordinates``."""ĭef index_coords(corner_locs, interp_locs):Ĭorner_locs, index = corner_locs, index Points in "xi", "yi", "zi" using linear interpolation. """Sample a 3D array "v" with pixel corner locations at "x","y","z" at the Print interp3(x, y, z, data, xi, yi, -25 * np.ones_like(xi))ĭef interp3(x, y, z, v, xi, yi, zi, **kwargs): # Interpolate a region of the x-y plane at z=-25 Howver, it should more or less replicate the behavior of interp3 as I remember it (ignoring the "zooming" functionality of interp3(data, zoom_factor), which handles.) import numpy as npįrom scipy.ndimage import map_coordinates ![]() I'll try to add more explanation later tonight (this is rather dense code).Īll in all, the interp3 function I have is more complex than it may need to be for your exact purposes. For the code snippet below, I'll assume that you always want floating point output. If you interpolate an integer array, you'll get integer output, which may or may not be what you want. There's also the additional wrinkle that map_coordinates always preserves the dtype of the input array in the output. If you want to specify the interpolated coordinates similar to matlab's interp3, then you'll need to convert your intput coordinates into "index" coordinates. The interface to it seems a bit clunky at first, but it does give you a lot of flexibility. Other than that, the two are similar and equally easy to use.īasically, ndimage.map_coordinates works in "index" coordinates (a.k.a. So you see some slight differences: Scipy uses x,y,z index order while MATLAB uses y,x,z (strangely) In Scipy you define a function in a separate step and when you call it, the coordinates are grouped like (x1,y1,z1),(x2,y2,z2). SCIPY CODE: from scipy.interpolate import RegularGridInterpolatorĪgain it's. The result is Vi= which is indeed the value at those two points (2,6,8) and (3,5,7). Here is a full example demonstrating both it will help you understand the exact differences. Vi = my_interpolating_function(array().T) My_interpolating_function = rgi((x,y,z), V) The MATLAB command Vi = interp3(x,y,z,V,xi,yi,zi) would translate to something like: from numpy import arrayįrom scipy.interpolate import RegularGridInterpolator as rgi At the left is your original dataset and the interpolated values are on the right.In scipy 0.14 or later, there is a new function which closely resembles interp3. One caveat is that you are going to obtain the interpolation on the convex hull of your data (unless you use the Nearest neighbor method).įollowing is an example. One option available in scipy.interpolate is griddata, where you pass your points and values and interpolate in another set of points that you pass. The data comes to me as a table: x y z f(x,y,z)Īnd I have to organize the f(x,y,z) function in an array of fxyz = ,]].ĭo you guys recommend another method to create this interpolation function assuming that the data comes in this form?Īs I mentioned in my comment, your data is not defined over a regular grid and that's why you should not be using that function. The problem I am having is related to the format the data is given to me and how the function receives it. It gets three one dimensional arrays (x,y,z) and the function answer (f(x,y,z)) to return the interpolating function. Interp_func = RegularGridInterpolator((x,y,z), fxyz) Using the RegularGridInterpolator, I created this function: def interp_3d(x,y,z,fxyz,x_desired,y_desired,z_desired): The problem is more related with organizing the data than how to do the interpolations. I am trying to finish a series of interpolation functions. ![]()
0 Comments
Leave a Reply. |