mangadap.proc.elric module

Implements an emission-line profile fitting class.


Although it may still be possible to use Elric, it is currently not actively used by the survey-level operation of the MaNGA DAP. See instead mangadap.proc.sasuke.Sasuke.

Revision history

26 Apr 2016: Original implementation by K. Westfall (KBW)
13 Jul 2016: (KBW) Include log_bounds determining whether or not a returned parameters is near its boundary.
19 Jul 2016: (KBW) Changed file name
19 Oct 2016: (KBW) Added Elric.reset_continuum_mask_window() function to deal with the subtraction of the continuum over the fully viable spectral range, and ignoring small spectral regions ignored during the stellar continuum fit. Changed the initial creation of the continuum mask to include the emission line regions to avoid any polynomial extrapolation errors, in case the emission line happens to fall exactly at the edge of the valid spectral range.
08 Nov 2016: (KBW) Moved the StellarContinuumModel.reset_continuum_mask_window() function from Elric to be a member of StellarContinuumModel.
24 May 2017: (KBW) Moved _check_db to mangadap.proc.spectralfitting.EmissionLineFit.check_emission_line_database() and moved fill_equivalent_width to mangadap.proc.spectralfitting.EmissionLineFit.measure_equivalent_width(). Moved _per_fitting_window_dtype from EmissionLineFit to Elric.
24 Aug 2017: (KBW) Use new mangadap.proc.util.sample_growth() instead of old residual_growth function.


Copyright © 2019, SDSS-IV/MaNGA Pipeline Group

class mangadap.proc.elric.Elric(bitmask, wave=None, flux=None, emission_lines=None, error=None, mask=None, stellar_continuum=None, base_order=-1, window_buffer=25, guess_redshift=None, guess_dispersion=None, default_dispersion=20.0, run_fit=False, loggers=None, quiet=False)[source]

Bases: mangadap.proc.spectralfitting.EmissionLineFit

ELRIC: Emission-Line Regression and Inference Class

Use LineProfileFit to fit the emission-line properties in a set of spectra.


  • Implement some scheme to penalize multicomponent fits at low S/N
_assess_and_save_fit(i, j, model_fit_par, model_eml_par)[source]

Assess the result of the LineProfileFit results.

  • (DONE) Check the success failure
  • (DONE) Calculate the chi2, rchi2, rms, fractional_rms, residuals, fractional_residuals
  • (DONE) Compare the fit parameters to the bounds
  • Check the chi-square and residuals?
  • Check the velocity offset wrt the input
  • For multiple lines, check the order of the lines matches the guess parameters
  • For multiple component lines, check the ordering of the subcomponents
static _correct_subeml_par(restwave_0, restwave_k, par, err=None)[source]

Correct the parameters fit assuming a rest wavelength of restwave_0, when it’s actually restwave_k. Input parameters are expected to be flux, velocity, velocity dispersion.

static _find_tied_index(index, emission_lines)[source]
static _fit_masks(wave, fitting_window, redshift, window_buffer)[source]
static _is_near_bound(par, lbnd, ubnd, logbounded, rtol=0.01, atol=0.0001)[source]

Determine of any of the parameters within start and start+npar are “near” an imposed boundary.

static _line_velocity_offset(restwave, restwave_primary)[source]

Parse the input emission-line file into a set of windows — the number of windows is \(N_{\rm win}\)) — that are fit for each spectrum.

static _per_fitting_window_dtype(nwin, max_npar, mask_dtype)[source]

Construct the record array data type for the output fits extension.

static _set_profile_ties(base_profiles, base_restwave, base_fixed_par, tied_profiles, tied_restwave, tied_fixed_par, mode, flux)[source]
static _velocity_vectors(wave, fitting_window)[source]
fit(wave, flux, emission_lines, ivar=None, mask=None, sres=None, continuum=None, base_order=-1, window_buffer=25, guess_redshift=None, guess_dispersion=None, loggers=None, quiet=False)[source]

The flux array is expected to have size Nspec x Nwave.

Raises:ValueError – Raised if the length of the spectra, errors, or mask does not match the length of the wavelength array; raised if the wavelength, redshift, or dispersion arrays are not 1D vectors; and raised if the number of redshifts or dispersions is not a single value or the same as the number of input spectra.
fit_SpatiallyBinnedSpectra(binned_spectra, par=None, loggers=None, quiet=False)[source]

This is a basic interface that is geared for the DAP that interacts with the rest of the, more general, parts of the class.

This should not declare anything to self!

class mangadap.proc.elric.ElricFittingWindow(nlines=None, db_indx=None, line_index=None, restwave=None, profile_set=None, fixed_par=None, bounds=None, log_bounds=None, output_model=None, tied_pairs=None, tied_funcs=None)[source]

Bases: object

A utility class for the fitting windows used by Elric

The class can be instantiated as fully None.

  • nlines (int) – (Optional) The number of lines to be fit simultaneously.
  • db_indx (numpy.ndarray) – (Optional) An array with the database index (0-based) of each line to be fit.
  • line_index (numpy.ndarray) – (Optional) An array with the index numbers, read from the database, of each line to be fit.
  • restwave (numpy.ndarray) – (Optional) An array with the rest wavelengths of each line to be fit.
  • profile_set (numpy.ndarray) – (Optional) An array with the profile objects that define the functional form of each line.
  • fixed_par (numpy.ndarray) – (Optional) An array with the fixed parameters for all parameters in the model to be fit.
  • bounds (numpy.ndarray) – (Optional) A two-column array with the lower (first column) and upper (second column) bounds for the fit parameters.
  • log_bounds (numpy.ndarray) – (Optional) The range of the boundary should be considered as logarithmic when testing if a parameter is near its boundary.
  • output_model (bool) – (Optional) Include the best-fitting model in the composite emission-line model for each spectrum. This is only flagged as true if ALL the emission lines in the fitting window are to be included according to the emission-line database.
  • tied_pairs (numpy.ndarray) – (Optional) The series of tied profiles (TiedLineProfile objects) that are used to tie parameters of the model.
  • tied_funcs (numpy.ndarray) – (Optional) The member functions of the TiedLineProfile objects that should be called sequentially to tie model parameters.
append(db_indx, line_index, restwave, profile, fixed_par, bounds, log_bounds, output_model)[source]

Append another line to the fitting window. Must be a single line!

reset_init_mean(indx, newmean)[source]
class mangadap.proc.elric.ElricPar(emission_lines=None, base_order=None, window_buffer=None, guess_redshift=None, guess_dispersion=None, minimum_snr=None, pixelmask=None, stellar_continuum=None)[source]

Bases: mangadap.par.parset.KeywordParSet

Elric emission-line fitting parameters.

The defined parameters are:

Key Type Options Default Description
emission_lines EmissionLineDB  
base_order int -1  
window_buffer int, float 25.0  
guess_redshift ndarray, list, int, float  
guess_dispersion ndarray, list, int, float  
minimum_snr int, float 0.0  
pixelmask SpectralPixelMask  
stellar_continuum StellarContinuumModel  
class mangadap.proc.elric.LineProfileFit(x, y, profile_list, base_order=None, error=None, mask=None, par=None, fixed=None, bounds=None, run_fit=True, construct_covariance=True, verbose=0)[source]

Bases: object

Simultaneously fit multiple line profiles. Currently only allows one to fit using an NCompLineProfile object. The fitting algorithm used is scipy.optimize.least_squares with fitting method ‘trf’ to allow for bounds.


For multicomponent lines, set the first normalization to be the normalization for the sum of all components, then force the normalization of the subcomponents to be ordered from highest to lowest and bounded from 0 to 1.

  • x (1D array) – Independent variable
  • y (1D array) – Dependent variable
  • profile_list (list of or individual NCompLineProfile) – The profile(s) to fit to the dependent variable.
  • base_order (int) – (Optional) The order of the Legendre polynomial to include in the model for the baseline trend in y below the fitted line profile(s).
  • error (1D array) – (Optional) Error in the dependent variables. If not provided, no error weighting is performed during the fitting process, and the covariance will not be constructed.
  • mask (1D array) – (Optional) Boolean array used to ignore values in y during the fit.
  • par (1D array) – (Optional) Initial guess for model parameters. The number of parameters much match the expectation based on the provided list of profiles and the order of the baseline polynomial. If not provided, the parameters are initialized to 0.
  • fixed (1D array) – (Optional) Flags used to fix parameters during the fit. The number of parameters much match the expectation based on the provided list of profiles and the order of the baseline polynomial. If not provided, all parameters are freely fit.
  • bounds (2-tuple) – (Optional) Tuple with two array-like elements giving the upper and lower bound for each parameter. The length of each array element must match the number of parameters. For an unbounded problem, set bounds=None, or use numpy.inf with an appropriate sign to disable bounds on all or some variables.
  • run_fit (bool) – (Optional) Flag to run the fit upon instantiation of the object, which defaults to True. If set to False, the object is initialized but the fit is not executed, and can be executed later using fit().
  • construct_covariance (bool) – (Optional) Flag to construct the covariance matrix based on the result object provided by scipy.optimize.least_squares, which defaults to True. If set to False, the covariance matrix is set to None.
  • verbose (int) – (Optional) Verbosity level for scipy.optimize.least_squares; default is 0.

Independent variable of length \(M\).


Dependent variable to be fit of length \(M\).


Error, \(\sigma\), in the dependent variable of length \(M\).


Flag to fit dependent variables of length \(M\).


Number of lines being fit.


The order of the Legendre polynomial include in the model; see astropy.modeling.polynomial.Legendre1D.


The compound model being fit composed of the line profiles and the baseline (if a baseline is being fit). That is, this defines the function \(f(\mathbf{x}|\mathbf{\theta})\), where \(\mathbf{x}\) is the dependent variable and \(\mathbf{\theta}\) is the list of variables. The fitting algorithm minimizes the (error-weighted) residuals to approximate \(y=f(\mathbf{x}|\mathbf{\theta})\).


Total number of parameters in the model. The is the number of parameters per line and the number of parameters included in the baseline.


Number of free parameters, defined as \(N\).


Full list of model parameters, including those parameters that have been fixed.


Flags to fit (False) or fix (True) a given parameter.


Tuple with two array-like elements giving the upper and lower bound for each parameter. For an unbounded problem, this is set to bounds=(-numpy.inf,numpy.inf).


Object with the results from scipy.optimize.least_squares. The best-fitting parameters, \(\mathbf{\theta}\), is returned as result.x.


The formal covariance matrix for the fit. The scipy.optimize.OptimizeResult object provides the Jacobian of the model, an \(M \times N\) array with elements

\[J_{ij} = \left.\frac{\partial f_i}{\partial \theta_j}\right|_{\mathbf{\theta}}\]

at location in parameter space of the best-fitting model. This is used to construct the covariance matrix by taking the inverse of the curvature matrix:

\[\mathbf{\alpha}_{kl} = \left[\frac{1}{\sigma_{i}} J_{ik}\right]^{\rm T} \left[\frac{1}{\sigma_{i}} J_{il}\right].\]

That is, \(\mathbf{C} = \mathbf{\alpha}^{-1}\).

  • TypeError – Raised if the provided profile objects are not instances of NCompLineProfile.
  • ValueError – Raised if any of the provided parameter arrays (par, fixed) are not one-dimensional or the number of parameters is not as expected based on the number of profile and baseline parameters.


  • Provide a better initialization for the parameters.
  • Need to provide the best-fitting parameters and errors as full vectors, including the fixed parameters.

Fixed parameters are ignored!


Sample without providing/checking new input parameters.

fit(x, y, error=None, mask=None, construct_covariance=True, verbose=0)[source]

Fit the line profiles provided upon initialization to the data using scipy.optimize.least_squares.

sample(x, par=None)[source]
class mangadap.proc.elric.TiedLineProfile(profile, base_profile, relative_flux=None, mean_separation=None, relative_stddev=None)[source]

Bases: object