webbpsf_ext.maths.jl_poly_fit

webbpsf_ext.maths.jl_poly_fit(x, yvals, deg=1, QR=True, robust_fit=False, niter=25, use_legendre=False, lxmap=None, **kwargs)[source]

Fast polynomial fitting

Fit a polynomial to a function using linear least-squares. This function is particularly useful if you have a data cube and want to simultaneously fit a slope to all pixels in order to produce a slope image.

Gives the option of performing QR decomposition, which provides a considerable speed-up compared to simply using np.linalg.lstsq(). In addition to being fast, it has better numerical stability than linear regressions that involve matrix inversions (ie., dot(x.T,x)).

Returns the coefficients of the fit for each pixel.

Parameters
  • x (ndarray) – X-values of the data array (1D).

  • yvals (ndarray) – Y-values (1D, 2D, or 3D) where the first dimension must have equal length of x. For instance, if x is a time series of a data cube with size NZ, then the data cube must follow the Python convention (NZ,NY,NZ).

Keyword Arguments
  • deg (int) – Degree of polynomial to fit to the data.

  • QR (bool) – Perform QR decomposition? Default=True.

  • robust_fit (bool) – Perform robust fitting, iteratively kicking out outliers until convergence.

  • niter (int) – Maximum number of iterations for robust fitting. If convergence is attained first, iterations will stop.

  • use_legendre (bool) – Fit with Legendre polynomials, an orthonormal basis set.

  • lxmap (ndarray or None) – Legendre polynomials are normally mapped to xvals of [-1,+1]. lxmap gives the option to supply the values for xval that should get mapped to [-1,+1]. If set to None, then assumes [xvals.min(),xvals.max()].

Example

Fit all pixels in a data cube to get slope image in terms of ADU/sec

>>> nz, ny, nx = cube.shape
>>> tvals = (np.arange(nz) + 1) * 10.737
>>> coeff = jl_poly_fit(tvals, cube, deg=1)
>>> bias = coeff[0]  # Bias image (y-intercept)
>>> slope = coeff[1] # Slope image (DN/sec)