scipp.interpolate.interp1d(da, dim, *, kind='linear', fill_value=nan, **kwargs)#

Interpolate a 1-D function.

A data array is used to approximate some function f: y = f(x), where y is given by the array values and x is is given by the coordinate for the given dimension. This class returns a function whose call method uses interpolation to find the value of new points.

The function is a wrapper for scipy.interpolate.interp1d. The differences are:

  • Instead of x and y, a data array defining these is used as input.

  • Instead of an axis, a dimension label defines the interpolation dimension.

  • The returned function does not just return the values of f(x) but a new data array with values defined as f(x) and x as a coordinate for the interpolation dimension.

  • The returned function accepts an extra argument midpoints. When setting midpoints=True the interpolation uses the midpoints of the new points instead of the points itself. The returned data array is then a histogram, i.e., the new coordinate is a bin-edge coordinate.

If the input data array contains masks that depend on the interpolation dimension the masked points are treated as missing, i.e., they are ignored for the definition of the interpolation function. If such a mask also depends on additional dimensions scipp.DimensionError is raised since interpolation requires points to be 1-D.

For structured input data dtypes such as vectors, rotations, or linear transformations interpolation is structure-element-wise. While this is appropriate for vectors, such a naive interpolation for, e.g., rotations does typically not yield a rotation so this should be used with care, unless the ‘kind’ parameter is set to, e.g., ‘previous’, ‘next’, or ‘nearest’.

Parameters not described above are forwarded to scipy.interpolate.interp1d. The most relevant ones are (see scipy.interpolate.interp1d for details):

  • da (DataArray) – Input data. Defines both dependent and independent variables for interpolation.

  • dim (str) – Dimension of the interpolation.

  • kind (Union[str, int], default: 'linear') – Specifies the kind of interpolation as a string or as an integer specifying the order of the spline interpolator to use. The string has to be one of ‘linear’, ‘nearest’, ‘nearest-up’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, or ‘next’. ‘zero’, ‘slinear’, ‘quadratic’ and ‘cubic’ refer to a spline interpolation of zeroth, first, second or third order; ‘previous’ and ‘next’ simply return the previous or next value of the point; ‘nearest-up’ and ‘nearest’ differ when interpolating half-integers (e.g. 0.5, 1.5) in that ‘nearest-up’ rounds up and ‘nearest’ rounds down. Default is ‘linear’.

  • fill_value (Any, default: nan) – Set to ‘extrapolate’ to allow for extrapolation of points outside the range.


>>> x = sc.geomspace(dim='x', start=0.1, stop=0.4, num=4, unit='rad')
>>> da = sc.DataArray(sc.sin(x), coords={'x': x})
>>> from scipp.interpolate import interp1d
>>> f = interp1d(da, 'x')
>>> xnew = sc.linspace(dim='x', start=0.1, stop=0.4, num=5, unit='rad')
>>> f(xnew)  # use interpolation function returned by `interp1d`
Dimensions: Sizes[x:5, ]
  x                         float64            [rad]  (x)  [0.1, 0.175, ..., 0.325, 0.4]
                            float64  [dimensionless]  (x)  [0.0998334, 0.173987, ..., 0.318433, 0.389418]
>>> f(xnew, midpoints=True)
Dimensions: Sizes[x:4, ]
  x                         float64            [rad]  (x [bin-edge])  [0.1, 0.175, ..., 0.325, 0.4]
                            float64  [dimensionless]  (x)  [0.137015, 0.210685, 0.282941, 0.353926]
Return type