mangadap.util.vander module

Implements a base class that performs linear least-squares fitting of 1D basis functions constructed via a pseudo-Vandermonde matrix.


License

Copyright © 2019, SDSS-IV/MaNGA Pipeline Group


class mangadap.util.vander.Legendre1D(x, order, rcond=None)[source]

Bases: mangadap.util.vander.Vander1D

Least-squares fitting of 1D Legendre functions.

Parameters
  • x (array-like) – Ordinate for fitting.

  • order (int) – Order of the fit.

  • rcond (float, optional) – Relative condition number of the fit. Singular values smaller than this relative to the largest singular value will be ignored. The default value is len(x)*eps, where eps is the relative precision of the float type, about 2e-16 in most cases.

class mangadap.util.vander.Vander1D(vander, x, order, rcond=None)[source]

Bases: object

Base class for least-squares fitting using linear combinations of 1D basis functions.

Parameters
  • vander (callable) – Function used to generate the pseudo-Vandermonde matrix of the basis functions; e.g., numpy.polynomial.legendre.legvander. Calling sequence must be vander(x,order).

  • x (array-like) – Ordinate for fitting.

  • order (int) – Order of the fit.

  • rcond (float, optional) – Relative condition number of the fit. Singular values smaller than this relative to the largest singular value will be ignored. The default value is len(x)*eps, where eps is the relative precision of the float type, about 2e-16 in most cases.

x

See the instantiation argument. Shape is \((N_{\rm x},)\).

Type

numpy.ndarray

v

Pseudo-Vandermonde matrix of the request fitting order, \(o\). Shape is \((o+1, N_{\rm x})\).

Type

numpy.ndarray

rcond

See the instantiation argument.

Type

float

static _reject(resid, lo, hi, boxcar)[source]

Reject fit residuals.

Parameters
  • resid (numpy.ndarray, numpy.ma.MaskedArray`_) – Weighted fit residuals. If input as a numpy.ndarray, all data is included in the calculation of the local or global standard deviation. To exclude values from this calculation, the input must be a masked array. Shape must be either \((N_x,)\) or \((N_{\rm vec},N_x})\). If 2D, the rejection is performed separately for each row.

  • lo (float) – Sigma rejection for low values.

  • hi (float) – Sigma rejection for high values.

  • boxcar (int) – Size of a boxcar to use if using local sigma rejection. If None, rejection is based on the global standard deviation.

Returns

Boolean array flagging values that were rejected.

Return type

numpy.ndarray

_single_fit(y, w)[source]

Perform a single fitting iteration.

Parameters
  • y (numpy.ndarray) – The data to fit. Shape is \((N_{\rm x},)\) or \((N_{\rm vec}, N_{\rm x})\). If the latter, a fit is done to each vector.

  • w (numpy.ndarray) – Weights. Shape is \((N_{\rm x},)\) or \((N_{\rm vec}, N_{\rm x})\). If the latter, this call is equivalent to looping over \(N_{\rm vec}\) and fitting each vector in y individually. Can be None, and if so the fit is unweighted.

Returns

The best-fitting coefficients for the basis functions with order \(o\). Shape is \((o+1,)\) or \((N_{\rm vec},o+1)\).

Return type

numpy.ndarray

fit(y, w=None, err=None, mask=None, rej_iter=0, rej_lo=3.0, rej_hi=3.0, rej_win=None, model=False)[source]

Fit the provided data.

The ordinate is provided on the class instantiation.

If provided, the errors, \(\epsilon\), are converted to weights to use during the fitting, where \(\omega_i = 1/\epsilon_i\). Both weights, \(w\), and errors can be provided; however, in this case, the weights used during the fit are are \(\omega_i = w_i / \epsilon_i\).

Todo

Allow for the nominal calculation of the errors in the coefficients.

Parameters
  • y (array-like) – A 1D or 2D array with the 1D functions to fit. Shape is \((N_{\rm vec},N_x)\), where \(N_x\) is the length of the ordinate (x). Input can be a masked array.

  • w (array-like, optional) – A 1D or 2D array with the weights for each pixel. Shape is \((N_{\rm vec},N_x)\), where \(N_x\) is the length of the ordinate (x). If w is 1D, but y is 2D, the same weights are applied to all vectors.

  • err (array-like, optional) – Error in y, used for weighting. Shape must match y.

  • mask (array-like, optional) – A boolean mask; values to ignore should have mask == True. Shape must match y. Treated identically as a mask provided by passing in a numpy.ma.MaskedArray for y. Note that providing a weight of 0 is identical to masking the value.

  • rej_iter (int, optional) – Number of fit rejection iterations. If 0, no rejection iterations are performed. If -1, rejections are performed until no points are rejected in a given iteration.

  • rej_lo (scalar-like, optional) – The lower sigma rejection threshold.

  • rej_hi (scalar-like, optional) – The higher sigma rejection threshold.

  • rej_win (int, optional) – The length of a boxcar window to use in a calculation of a local standard deviation used during the rejection iterations. This is useful if the S/N varies strongly within the provided vector being fit. If None, no windowing is applied, meaning a global standard deviation is used for rejection.

  • model (bool, optional) – If True, return the model as well as the best fitting coefficients.

Returns

Returns the vector(s) with the best-fitting coefficients. If model is True, a second object with the best-fitting models are also returned (same shape as y).

Return type

numpy.ndarray, tuple

model(c)[source]

Return the model function constructed using the provided coefficients.

property order
property shape
property size