nearest_interpolator

odl.discr.discr_utils.nearest_interpolator(f, coord_vecs)[source]

Return the nearest neighbor interpolator for discrete values.

Given points x[1] < x[2] < ... < x[N], and function values f[1], ..., f[N], nearest neighbor interpolation at x is defined as

I(x) = f[j]

with j such that |x - x[j]| is minimal. The ambiguity at the midpoints is resolved by preferring the right neighbor. In higher dimensions, this principle is applied per axis. The returned interpolator is the piecewise constant function x -> I(x).

Parameters:
fnumpy.ndarray

Function values that should be interpolated.

coord_vecssequence of numpy.ndarray

Coordinate vectors of the rectangular grid on which interpolation should be based. They must be sorted in ascending order. Usually they are obtained as grid.coord_vectors from a RectGrid.

Returns:
interpolatorfunction

Python function that will interpolate the given values when called with a point or multiple points (vectorized).

See also

linear_interpolator

(bi-/tri-/...)linear interpolation

per_axis_interpolator

potentially different interpolation in each axis

Notes

  • Important: if called on a point array, the points are assumed to be sorted in ascending order in each dimension for efficiency reasons.

  • Nearest neighbor interpolation is the only scheme which works with data of non-numeric data type since it does not involve any arithmetic operations on the values, in contrast to other interpolation methods.

Examples

We interpolate a 1d function. If called with a single point, the interpolator returns a single value, and with multiple points at once, an array of values is returned:

>>> part = odl.uniform_partition(0, 2, 5)
>>> part.coord_vectors  # grid points
(array([ 0.2,  0.6,  1. ,  1.4,  1.8]),)
>>> f = [1, 2, 3, 4, 5]
>>> interpolator = nearest_interpolator(f, part.coord_vectors)
>>> interpolator(0.3)  # closest to 0.2 -> value 1
1
>>> interpolator([0.6, 1.3, 1.9])  # closest to [0.6, 1.4, 1.8]
array([2, 4, 5])

In 2 dimensions, we can either use a (transposed) list of points or a meshgrid:

>>> part = odl.uniform_partition([0, 0], [1, 5], shape=(2, 4))
>>> part.coord_vectors  # grid points
(array([ 0.25,  0.75]), array([ 0.625,  1.875,  3.125,  4.375]))
>>> f = np.array([[1, 2, 3, 4],
...               [5, 6, 7, 8]],
...              dtype=float)
>>> interpolator = nearest_interpolator(f, part.coord_vectors)
>>> interpolator([1, 1])  # single point
5.0
>>> x = np.array([[0.5, 2.0],
...               [0.0, 4.5],
...               [0.0, 3.0]]).T  # 3 points at once
>>> interpolator(x)
array([ 6.,  4.,  3.])
>>> from odl.discr.grid import sparse_meshgrid
>>> mesh = sparse_meshgrid([0.0, 0.4, 1.0], [1.5, 3.5])
>>> interpolator(mesh)  # 3x2 grid of points
array([[ 2.,  3.],
       [ 2.,  3.],
       [ 6.,  7.]])

With nearest neighbor interpolation, we can also use non-scalar data types like strings:

>>> part = odl.uniform_partition(0, 3, 6)
>>> part.coord_vectors  # grid points
(array([ 0.25,  0.75,  1.25,  1.75,  2.25,  2.75]),)
>>> f = ['s', 't', 'r', 'i', 'n', 'g']
>>> interpolator = nearest_interpolator(f, part.coord_vectors)
>>> print(interpolator(0.9))
t