diff options
author | Anton Khirnov <anton@khirnov.net> | 2013-12-17 10:28:12 +0100 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2013-12-17 10:28:12 +0100 |
commit | 7daae202d9a1ec7928603a65f5c06b4518c3596e (patch) | |
tree | 20a6102527f34eedc494f146484d86ed9da1b330 | |
parent | e960c7f6a8bbc8fadd57a83369aa010aff41d1bf (diff) |
Add a convenience wrapper around scipy ndimage interpolation.
-rw-r--r-- | cactus_utils.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/cactus_utils.py b/cactus_utils.py index 100f31f..af5959e 100644 --- a/cactus_utils.py +++ b/cactus_utils.py @@ -1,6 +1,7 @@ import h5py import numpy as np import pylab +import scipy.ndimage.interpolation import scipy.interpolate import scipy.integrate import scipy.optimize @@ -16,6 +17,38 @@ axis_data = { 'x' : 2, 'y' : 1, 'z' : 0 } EPSILON = 1e-3 +def ndinterpolate(in_coords, values, out_coords): + """ + A wrapper around scipy.image.interpolation.map_coordinates() providing more convenient API. + + Given the values of a function on an N-dimensional regular grid, interpolate + its value on given coordinates. + + @param in_coords An N+1-dimensional array of the coordinates of the input values. + The first axis goes over the dimensions (i.e. in_coords[0] is + an N-dimensional array that gives the x-coordinate for each value). + @param values An N-dimensional array of the values (at in_coords) of the function to + be interpolated + @param out_coords A 2-dimensional array of the coordinates at which the output is + desired. The first axis goes over the dimensions (i.e. out_coords[0] + is a 1-dimensional array of length N of the x-coordinates for every + desired interpolation point). + """ + N = len(values.shape) + + if len(in_coords.shape) != N + 1 or in_coords.shape[0] != N: + raise ValueError('Invalid coords/values shapes: ' + str(in_coords.shape) + str(values.shape)) + + out_coords = np.copy(out_coords) + for i in xrange(N): + max = np.max(in_coords[i]) + min = np.min(in_coords[i]) + out_coords[i] -= min + out_coords[i] *= values.shape[i] / (max - min) + + return scipy.ndimage.interpolation.map_coordinates(values, out_coords) + + def get_default_gridfunc(datafile): """ Get the name of the default grid function from a given data file |