mangadap.proc.elric module¶
Implements an emission-line profile fitting class.
- License:
- Copyright (c) 2015, SDSS-IV/MaNGA Pipeline Group
- Licensed under BSD 3-clause license - see LICENSE.rst
- Source location:
- $MANGADAP_DIR/python/mangadap/proc/elric.py
- Imports and python version compliance:
from __future__ import division from __future__ import print_function from __future__ import absolute_import from __future__ import unicode_literals import sys import warnings if sys.version > '3': long = int import time import os import logging import numpy from scipy import interpolate, integrate, optimize from scipy.special import erf import astropy.constants from astropy.modeling import FittableModel, Parameter from ..par.parset import ParSet from ..par.emissionlinedb import EmissionLineDB from ..util.fileio import init_record_array from ..util.log import log_output from ..util.pixelmask import SpectralPixelMask from .spatiallybinnedspectra import SpatiallyBinnedSpectra from .stellarcontinuummodel import StellarContinuumModel from .spectralfitting import EmissionLineFit from .util import sample_growth
- Class usage examples:
- Add examples
- 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 name19 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 theStellarContinuumModel.reset_continuum_mask_window()
function fromElric
to be a member ofStellarContinuumModel
.24 May 2017: (KBW) Moved _check_db tomangadap.proc.spectralfitting.EmissionLineFit.check_emission_line_database()
and moved fill_equivalent_width tomangadap.proc.spectralfitting.EmissionLineFit.measure_equivalent_width()
. Moved _per_fitting_window_dtype from EmissionLineFit toElric
.24 Aug 2017: (KBW) Use newmangadap.proc.util.sample_growth()
instead of old residual_growth function.02 Feb 2018: (KBW) Adjust for change tomangadap.proc.stellarcontinuummodel.StellarContinuumModel.fill_to_match()
.
-
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
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
_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.
-
_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.
-
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]¶
-
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.
-
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.
-
class
mangadap.proc.elric.
ElricPar
(emission_lines, base_order, window_buffer, guess_redshift, guess_dispersion, minimum_snr=None, pixelmask=None, stellar_continuum=None)[source]¶ Bases:
mangadap.par.parset.ParSet
-
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 (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 peformed 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.
-
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
-
mask
¶ 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
¶ 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: 2-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 ofNCompLineProfile
.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.
-
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.