Implements an emission-line profile fitting class.

Warning

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. ELRIC: Emission-Line Regression and Inference Class

https://en.wikipedia.org/wiki/Edward_Elric

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

Todo

• 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 _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_emission_line_models()[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.

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.

Parameters:
• 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!

reinit_profiles()[source]
reset_init_mean(indx, newmean)[source]

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

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.

Todo

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.

Parameters:
• x (array-like) – 1D vector with the independent variable

• y (array-like) – 1D vector with the dependent variable

• profile_list (list, 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 (array-like, optional) – 1D vector with the 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 (array-like, optional) – 1D boolean array used to ignore values in y during the fit.

• par (array-like, optional) – 1D vector with the 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 (array-like, optional) – 1D vector with 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 (tuple) – (Optional) 2-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.

x

Independent variable of length $$M$$.

Type:

numpy.ndarray

y

Dependent variable to be fit of length $$M$$.

Type:

numpy.ndarray

err

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

Type:

numpy.ndarray

Flag to fit dependent variables of length $$M$$.

Type:

numpy.ndarray

nlines

Number of lines being fit.

Type:

int

base_order

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

Type:

int

model

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})$$.

Type:

astropy.modeling.models.CompoundModel

npar

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

Type:

int

nfitpar

Number of free parameters, defined as $$N$$.

Type:

int

par

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

Type:

numpy.ndarray

fixed

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

Type:

numpy.ndarray

bounds

2-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).

Type:

tuple

result

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

Type:

scipy.optimize.OptimizeResult

cov

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}$$.

Type:

numpy.ndarray

Raises:
• 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.

Todo

• Provide a better initialization for the parameters.

• Need to provide the best-fitting parameters and errors as full vectors, including the fixed parameters.

_assign_par(par)[source]

Fixed parameters are ignored!

_calculate_covariance_matrix(verbose=0)[source]
_chi(par)[source]
_quick_sample(x)[source]

Sample without providing/checking new input parameters.

_resid(par)[source]
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

tie_flux()[source]
tie_mean()[source]
tie_stddev()[source]