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: 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

_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_x,)\) or \((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_x,)\) or \((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:

As many as three objects can be returned. Regardless of the input, the function always returns an array with the best-fitting coefficients, with shape \((o+1,)\) or \((N_{\rm vec},o+1)\), depending on the input shape of y. If model is True, an array with the best-fitting models are also returned (same shape as y). If rejection operations are performed, a boolean array flagging the pixels excluded from the fit is also returned. Note that any data that will have been ignored based on the input (the element is masked or has 0 weight) is not flagged as having been rejected.

Return type:

numpy.ndarray, tuple

model(c)[source]

Return the model function constructed using the provided coefficients.

Parameters:

c (array-like) – The coefficients for the model with shape shape \((o+1,)\) or \((N_{\rm vec},o+1)\), where \(o\) is the order of the function.

Returns:

Array with the evaluated functions. The output shape is \((N_x,)\) or \((N_{\rm vec},N_x)\) depending on the input shape of c.

Return type:

numpy.ndarray

property order

The order of the function.

property shape

The shape of the coordinate vector.

property size

The size of the coordinate vector.