pytrip.res.interpolate module

This package provides an easy way to perform interpolation of 1-D and 2-D datasets.

1-D dataset is represented by two columns of numbers: X and Y, of the same length. X column should contain increasing sequence of numbers. It is not required that X data should form regular (evenly spaced) grid. X and Y columns of length 1 also form 1-D data (single data point).

2-D dataset is represented by two columns of numbers: X and Y. and 2-dimensional matrix Z. Number of columns in Z matrix should be equal to length of X array, while number of rows to length of Y array. Lengths of X and Y data may differ. One or both of X and Y columns may contain single element - this is also valid 2-D dataset. X and Y column should contain increasing sequence of numbers. It is not required that X and Y data should form regular (evenly spaced) grid.

Two types of interpolation are supported: linear and spline (3rd degree polynomials).

Linear interpolation on 1-D dataset and on reduced 2-D datasets (where X or Y is single element column) is performed using interp method from numpy package.

Spline interpolation on all datasets is performed using InterpolatedUnivariateSpline (for 1-D data) and RectBivariateSpline (for 2-D data) from scipy package.

Cubic spline interpolation requires at least 4 data points. In case number of data points is lower, interpolator implemented in this package will fall back to quadratic spline (for 3 data points) or to linear interpolation (2 or less data points). This is different behaviour from what is implemented in scipy package where exception is thrown is such situation.

Note: scipy package is not imported her as default as its installation is problematic for some group of users (i.e. Windows users working without Anaconda python distribution). Most of the functionality of this package (i.e. 1-D and some cases of 2-D linear interpolation) will work without scipy package installed. If user without scipy package calls cubic spline interpolation method then an exception will be thrown and an error message printed, requesting user to install scipy.

class pytrip.res.interpolate.RegularInterpolator(x, y, z=None, kind='spline')[source]

Bases: object

RegularInterpolator is a helper class to easy interpolation of single- and double-variable function. To use it usually two steps are needed:

1. construction of the object, providing training data (data points) and interpolation type (linear or spline). During this step coefficient of interpolating function will be calculated and stored in the object. RegularInterpolator objects are so-called callables and can be called in same way as plain functions on interpolated values. Example:

>>> interp_func_1d = RegularInterpolator(x=exp_data_x, y=exp_data_y, kind='linear')
>>> interp_func_2d = RegularInterpolator(x=exp_data_x, y=exp_data_y, z=exp_data_z, kind='spline')
  1. Calling interpolation function to get intermediate values (single number or array) on interpolated ones.

    >>> interpolated_y = interp_func_1d(x=intermediate_x)
    >>> interpolated_z = interp_func_2d(x=intermediate_x, y=intermediate_y)
    

Interpolation in single step is also possible (although less efficient than two-step method):

>>> interpolated_y = RegularInterpolator.eval(x=intermediate_x, xp=exp_data_x, yp=exp_data_y, kind='linear')
>>> interpolated_z = RegularInterpolator(x=intermediate_x, y=intermediate_y,         xp=exp_data_x, yp=exp_data_y, zp=exp_data_z, kind='spline')
classmethod eval(x, y=None, xp=None, yp=None, zp=None, kind='linear')[source]
Perform interpolation in a single step. Find interpolation function,
based on data points (xp, yp and zp) and then execute it on interpolated values (x and y).
Parameters:x – array_like or single value

Input x-coordinate(s).

Parameters:y – array_like or single value

Input y-coordinate(s) (in case of 2-D dataset).

Parameters:xp – array_like

The 1-d array of data-points x-coordinates, must be in strictly ascending order.

Parameters:yp – array_like

The 1-d array of data-points y-coordinates. If y is from 1-D data points (z is None) then it should be of the same length (shape) as x. Otherwise (2-D data points, z is not None) then it must be in strictly ascending order.

Parameters:zp – array_like

None in case of 1-D dataset. Otherwise the 2-d array of data-points z-coordinates, of shape (x.size, y.size).

Parameters:kind – interpolation algorithm: ‘linear’ or ‘spline’ (default).
Returns:array_like or single value

Interpolated value