bilby.core.utils.calculus.BoundedRectBivariateSpline
- class bilby.core.utils.calculus.BoundedRectBivariateSpline(x, y, z, bbox=[None, None, None, None], kx=3, ky=3, s=0, fill_value=None)[source]
Bases:
RectBivariateSpline
- __call__(x, y, dx=0, dy=0, grid=False)[source]
Evaluate the spline or its derivatives at given positions.
- Parameters:
- x, yarray_like
Input coordinates.
If grid is False, evaluate the spline at points
(x[i], y[i]), i=0, ..., len(x)-1
. Standard Numpy broadcasting is obeyed.If grid is True: evaluate spline at the grid points defined by the coordinate arrays x, y. The arrays must be sorted to increasing order.
The ordering of axes is consistent with
np.meshgrid(..., indexing="ij")
and inconsistent with the default orderingnp.meshgrid(..., indexing="xy")
.- dxint
Order of x-derivative
Added in version 0.14.0.
- dyint
Order of y-derivative
Added in version 0.14.0.
- gridbool
Whether to evaluate the results on a grid spanned by the input arrays, or at points specified by the input arrays.
Added in version 0.14.0.
Examples
Suppose that we want to bilinearly interpolate an exponentially decaying function in 2 dimensions.
>>> import numpy as np >>> from scipy.interpolate import RectBivariateSpline
We sample the function on a coarse grid. Note that the default indexing=”xy” of meshgrid would result in an unexpected (transposed) result after interpolation.
>>> xarr = np.linspace(-3, 3, 100) >>> yarr = np.linspace(-3, 3, 100) >>> xgrid, ygrid = np.meshgrid(xarr, yarr, indexing="ij")
The function to interpolate decays faster along one axis than the other.
>>> zdata = np.exp(-np.sqrt((xgrid / 2) ** 2 + ygrid**2))
Next we sample on a finer grid using interpolation (kx=ky=1 for bilinear).
>>> rbs = RectBivariateSpline(xarr, yarr, zdata, kx=1, ky=1) >>> xarr_fine = np.linspace(-3, 3, 200) >>> yarr_fine = np.linspace(-3, 3, 200) >>> xgrid_fine, ygrid_fine = np.meshgrid(xarr_fine, yarr_fine, indexing="ij") >>> zdata_interp = rbs(xgrid_fine, ygrid_fine, grid=False)
And check that the result agrees with the input by plotting both.
>>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> ax1 = fig.add_subplot(1, 2, 1, aspect="equal") >>> ax2 = fig.add_subplot(1, 2, 2, aspect="equal") >>> ax1.imshow(zdata) >>> ax2.imshow(zdata_interp) >>> plt.show()
Methods
__init__
(x, y, z[, bbox, kx, ky, s, fill_value])ev
(xi, yi[, dx, dy])Evaluate the spline at points
Return spline coefficients.
Return a tuple (tx,ty) where tx,ty contain knots positions of the spline with respect to x-, y-variable, respectively.
Return weighted sum of squared residuals of the spline approximation: sum ((w[i]*(z[i]-s(x[i],y[i])))**2,axis=0)
integral
(xa, xb, ya, yb)Evaluate the integral of the spline over area [xa,xb] x [ya,yb].
partial_derivative
(dx, dy)Construct a new spline representing a partial derivative of this spline.
- ev(xi, yi, dx=0, dy=0)[source]
Evaluate the spline at points
Returns the interpolated value at
(xi[i], yi[i]), i=0,...,len(xi)-1
.- Parameters:
- xi, yiarray_like
Input coordinates. Standard Numpy broadcasting is obeyed. The ordering of axes is consistent with
np.meshgrid(..., indexing="ij")
and inconsistent with the default orderingnp.meshgrid(..., indexing="xy")
.- dxint, optional
Order of x-derivative
Added in version 0.14.0.
- dyint, optional
Order of y-derivative
Added in version 0.14.0.
Examples
Suppose that we want to bilinearly interpolate an exponentially decaying function in 2 dimensions.
>>> import numpy as np >>> from scipy.interpolate import RectBivariateSpline >>> def f(x, y): ... return np.exp(-np.sqrt((x / 2) ** 2 + y**2))
We sample the function on a coarse grid and set up the interpolator. Note that the default
indexing="xy"
of meshgrid would result in an unexpected (transposed) result after interpolation.>>> xarr = np.linspace(-3, 3, 21) >>> yarr = np.linspace(-3, 3, 21) >>> xgrid, ygrid = np.meshgrid(xarr, yarr, indexing="ij") >>> zdata = f(xgrid, ygrid) >>> rbs = RectBivariateSpline(xarr, yarr, zdata, kx=1, ky=1)
Next we sample the function along a diagonal slice through the coordinate space on a finer grid using interpolation.
>>> xinterp = np.linspace(-3, 3, 201) >>> yinterp = np.linspace(3, -3, 201) >>> zinterp = rbs.ev(xinterp, yinterp)
And check that the interpolation passes through the function evaluations as a function of the distance from the origin along the slice.
>>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> ax1 = fig.add_subplot(1, 1, 1) >>> ax1.plot(np.sqrt(xarr**2 + yarr**2), np.diag(zdata), "or") >>> ax1.plot(np.sqrt(xinterp**2 + yinterp**2), zinterp, "-b") >>> plt.show()
- get_knots()[source]
Return a tuple (tx,ty) where tx,ty contain knots positions of the spline with respect to x-, y-variable, respectively. The position of interior and additional knots are given as t[k+1:-k-1] and t[:k+1]=b, t[-k-1:]=e, respectively.
- get_residual()[source]
Return weighted sum of squared residuals of the spline approximation: sum ((w[i]*(z[i]-s(x[i],y[i])))**2,axis=0)
- integral(xa, xb, ya, yb)[source]
Evaluate the integral of the spline over area [xa,xb] x [ya,yb].
- Parameters:
- xa, xbfloat
The end-points of the x integration interval.
- ya, ybfloat
The end-points of the y integration interval.
- Returns:
- integfloat
The value of the resulting integral.
- partial_derivative(dx, dy)[source]
Construct a new spline representing a partial derivative of this spline.
- Parameters:
- dx, dyint
Orders of the derivative in x and y respectively. They must be non-negative integers and less than the respective degree of the original spline (self) in that direction (
kx
,ky
).
- Returns:
- spline
A new spline of degrees (
kx - dx
,ky - dy
) representing the derivative of this spline.
Notes
Added in version 1.9.0.