core.spline

Spline utility module, building on top of the scipy.interpolate modules.

ximpol.core.spline.interpolate(xa, ya, xb, yb, x)[source]

Simple two-point linear interpolation/extrapolation.

ximpol.core.spline.optimize_grid_linear(x, y, tolerance=0.0001)[source]

Optimize a pair of (x, y) arrays for the corresponding spline definition.

This loops over the input arrays and removes unnecessary data points to minimize the length of the arrays necessary to the spline definition.

Parameters:
  • x (array) – The input x-array.
  • y (array) – The input y-array.
  • tolerance (float) – The maximum relative difference between the generic yi value and the estrapolation of the two previous optimized data points for the point i to be removed.
class ximpol.core.spline.xBivariateSplineBase(x, y, z, xname=None, xunits=None, yname=None, yunits=None, zname=None, zunits=None)[source]

Base class for all the bivariate spline classes.

This is somewhat similar in spirit to the corresponding univariate base class, except that the additional functionalities are, for the moment, limited to bookkeeping and plotting facilities.

Parameters:
  • x (array) – Input x values (assumed to be sorted).
  • y (array) – Input y values (assumed to be sorted).
  • z (array) – Input z values, with shape (x.size,y.size).
  • xname (str, optional) – The name of the quantity on the x-axis.
  • xunits (str, optional) – The units for the x-axis.
  • yname (str, optional) – The name of the quantity on the y-axis.
  • yunits (str, optional) – The units for the y-axis.
  • zname (str, optional) – The name of the quantity on the z-axis.
  • zunits (str, optional) – The units for the z-axis.

Note

This is a do-nothing class to be subclassed and not instantiated directly.

scale(scale_factor)[source]
xlabel()[source]

Return the x-label for a plot.

xmax()[source]

Return the maximum of the underlying x-array.

xmin()[source]

Return the minimum of the underlying x-array.

ylabel()[source]

Return the y-label for a plot.

ymax()[source]

Return the maximum of the underlying y-array.

ymin()[source]

Return the minimum of the underlying y-array.

zlabel()[source]

Return the z-label for a plot.

class ximpol.core.spline.xInterpolatedBivariateSpline(x, y, z, kx=1, ky=1, xname=None, xunits=None, yname=None, yunits=None, zname=None, zunits=None)[source]

Bivariate interpolated spline on a rectangular grid.

build_vppf()[source]

Create the vertical percent point function (or inverse of cdf).

Warning

This really, really need to be fixed. Instead of grabbing a vertical slice at xmean, we should pass an argument to the function so that the subclasses can implement whatever is right for them.

hslice(y)[source]

Return an horizontal slice at a given y of the bivariate spline.

Parameters:y (float) – The y value at which the horizontal slice should be calculated.
plot(num_pointsx=100, num_pointsy=100, num_contours=75, logz=False, show=True)[source]

Plot the spline.

Parameters:
  • num_pointsx (int) – The number of x sampling points to be used to draw the spline.
  • num_pointsy (int) – The number of y sampling points to be used to draw the spline.
  • num_contours (int) – The number of contours for the color plot.
  • show (bool, optional) – If True, plt.show() is called at the end, interrupting the flow.
vslice(x)[source]

Return a vertical slice at a given x of the bivariate spline.

Parameters:x (float) – The x value at which the vertical slice should be calculated.
class ximpol.core.spline.xInterpolatedBivariateSplineLinear(x, y, z, xname=None, xunits=None, yname=None, yunits=None, zname=None, zunits=None)[source]

Bivariate linear interpolated spline on a rectangular grid.

class ximpol.core.spline.xInterpolatedUnivariateLogSpline(x, y, w=None, bbox=[None, None], k=3, xname=None, xunits=None, yname=None, yunits=None)[source]

Poor man’s attempt at a spline in logarithmic space.

The basic interpolation is trivial, as we simply take the logarithms of the x and y values in the constructor and overload the __call__ method to raise 10 to the power of the interpolated value.

The integral is more tricky, as obviously the implementation in the base class operates in logarirthmic space and the result is just nonsense. To this end we proceed by brute force and create a second spline, this time in linear space, with a relatively large number of points to guarantee the accuracy of the integral.

Warning

This is not supporting the calculation of the derivatives or roots in any sensible way, so just refrain from calling any other methods than the overloaded ones, i.e., __call__() and integral(). In the future it might make sense to overload all the other methods and simply abort the executions whenever any of them is called.

integral(x1, x2)[source]

Overloaded integral method.

The integral spline is calculated and cached the first time this method is called.

class ximpol.core.spline.xInterpolatedUnivariateLogSplineLinear(x, y, xname=None, xunits=None, yname=None, yunits=None)[source]

Subclass of xInterpolatedUnivariateLogSpline with k=1.

class ximpol.core.spline.xInterpolatedUnivariateSpline(x, y, w=None, bbox=[None, None], k=3, xname=None, xunits=None, yname=None, yunits=None)[source]

Light-weight wrapper over the scipy InterpolatedUnivariateSpline class (see the corresponding documentation for the meaning of the parameters passed to the constructor).

Note

Note that the interface to the base class has changed from numpy 0.14. An ext argument can be passed to the constructor starting with scipy 0.15 to control the extrapolation behavior and a check_finite argument is available in 0.16 to avoid nans in the input data. We currently do not use either one.

class ximpol.core.spline.xInterpolatedUnivariateSplineLinear(x, y, xname=None, xunits=None, yname=None, yunits=None, optimize=False, tolerance=0.0001)[source]

xInterpolatedUnivariateSplineLinear subclass implementing the simplest possible linear interpolator.

This particular class is offering the facility to optimize the input arrays via the optimize argument.

Parameters:
  • x (array) – Input x values (assumed to be sorted).
  • y (array) – Input y values.
  • xname (str, optional) – The name of the quantity on the x-axis.
  • xunits (str, optional) – The units for the x-axis.
  • yname (str, optional) – The name of the quantity on the y-axis.
  • yunits (str, optional) – The units for the y-axis.
  • optimize (bool) – If True, the input arrays are optimized via the optimize_grid_linear() function.
  • tolerance (float) – The tolerance for the input array optimization. (If optimize is False, this has no effect.)

Example

>>> from ximpol.core.spline import xInterpolatedUnivariateSplineLinear
>>>
>>> x = numpy.linspace(0, 2*numpy.pi, 100)
>>> y = numpy.sin(x)
>>> s = xInterpolatedUnivariateSplineLinear(x, y, xname='x', xunits='au')
>>> s.plot()
class ximpol.core.spline.xUnivariateSpline(x, y, w=None, bbox=[None, None], k=3, s=None, xname=None, xunits=None, yname=None, yunits=None)[source]

Light-weight wrapper over the scipy UnivariateSpline class (see the corresponding documentation for the meaning of the parameters passed to the constructor).

Note

Note that the interface to the base class has changed from numpy 0.14. An ext argument can be passed to the constructor starting with scipy 0.15 to control the extrapolation behavior and a check_finite argument is available in 0.16 to avoid nans in the input data. We currently do not use either one.

class ximpol.core.spline.xUnivariateSplineBase(x, y, xname=None, xunits=None, yname=None, yunits=None)[source]

Base class for all the univariate spline classes.

The basic idea is to keep track of the original arrays passed to the interpolator and to support arithmetic operations. We also allow the user to supply optional arguments to control the ranges and specify names and units for the quantities involved.

Parameters:
  • x (array) – Input x values.
  • y (array) – Input y values.
  • xname (str, optional) – The name of the quantity on the x-axis.
  • xunits (str, optional) – The units for the x-axis.
  • yname (str, optional) – The name of the quantity on the y-axis.
  • yunits (str, optional) – The units for the y-axis.

Note

This is a do-nothing class to be subclassed and not instantiated directly.

build_cdf()[source]

Create the cumulative distribution function.

Note that the cdf is built using a linear interpolated spline, no matter what the class of the original spline is.

build_ppf()[source]

Create the percent point function (or inverse of the cdf).

Note that the cdf is built using a linear interpolated spline, no matter what the class of the original spline is.

dist(x, y)[source]
classmethod label(name, units=None)[source]

Compose an axis label given a name and some units.

norm()[source]

Return the integral over the entire spline domain.

plot(num_points=1000, overlay=False, logx=False, logy=False, scale=1.0, offset=0.0, show=True, **kwargs)[source]

Plot the spline.

Parameters:
  • num_points (int, optional) – The number of sampling points to be used to draw the spline.
  • overlay (bool, optional) – If True, the original arrays passed to the spline are overlaid.
  • show (bool, optional) – If True, plt.show() is called at the end, interrupting the flow.
scale(scale, yname=None, yunits=None)[source]

Scale the spline y values.

Warning

Need to set the names and units properly—the main issue is ths for derived classes the names of the corresponding members can be different.

xlabel()[source]

Return the x-label for a plot.

xmax()[source]

Return the maximum of the underlying x-array.

xmin()[source]

Return the minimum of the underlying x-array.

ylabel()[source]

Return the y-label for a plot.