Line/Spectrum Fitting

One of the primary tasks in spectroscopic analysis is fitting models of spectra. This concept is often applied mainly to line-fitting, but the same general approach applies to continuum fitting or even full-spectrum fitting. specutils provides conveniences that aim to leverage the general fitting framework of astropy.modeling to spectral-specific tasks.

At a high level, this fitting takes the Spectrum1D object and a list of Model objects that have initial guesses for each of the parameters. these are used to create a compound model created from the model initial guesses. This model is then actually fit to the spectrum’s flux, yielding a single composite model result (which can be split back into its components if desired).

Line Finding

There are two techniques implemented in order to find emission and/or absorption lines in a Spectrum1D spectrum.

The first technique is find_lines_threshold that will find lines by thresholding the flux based on a factor applied to the spectrum uncertainty. The second technique is find_lines_derivative that will find the lines based on calculating the derivative and then thresholding based on it. Both techniques return an QTable that contains columns line_center, line_type and line_center_index.

We start with a synthetic spectrum:

While we know the true uncertainty here, this is often not the case with real data. Therefore, since find_lines_threshold requires an uncertainty, we will produce an estimate of the uncertainty by calling the noise_region_uncertainty function:

>>> from specutils.manipulation import noise_region_uncertainty
>>> noise_region = SpectralRegion(0*u.um, 3*u.um)
>>> spectrum = noise_region_uncertainty(spectrum, noise_region)

>>> from specutils.fitting import find_lines_threshold
>>> lines = find_lines_threshold(spectrum, noise_factor=3)

>>> lines[lines['line_type'] == 'emission']  
<QTable length=4>
   line_center    line_type line_center_index
        um
     float64        str10         int64
----------------- --------- -----------------
4.572864321608041  emission                91
4.824120603015076  emission                96
5.477386934673367  emission               109
 8.99497487437186  emission               179

>>> lines[lines['line_type'] == 'absorption']  
<QTable length=1>
   line_center    line_type  line_center_index
        um
     float64        str10          int64
----------------- ---------- -----------------
8.190954773869347 absorption               163

An example using the find_lines_derivative:

>>> # Define a noise region for adding the uncertainty
>>> noise_region = SpectralRegion(0*u.um, 3*u.um)

>>> # Derivative technique
>>> from specutils.fitting import find_lines_derivative
>>> lines = find_lines_derivative(spectrum, flux_threshold=0.75)

>>> lines[lines['line_type'] == 'emission']  
<QTable length=2>
   line_center    line_type line_center_index
        um
     float64        str10         int64
----------------- --------- -----------------
4.522613065326634  emission                90
5.477386934673367  emission               109

>>> lines[lines['line_type'] == 'absorption']  
<QTable length=1>
   line_center    line_type  line_center_index
        um
     float64        str10          int64
----------------- ---------- -----------------
8.190954773869347 absorption               163

While it might be surprising that these tables do not contain more information about the lines, this is because the “toolbox” philosophy of specutils aims to keep such functionality in separate distinct functions. See Analysis for functions that can be used to fill out common line measurements more completely.

Parameter Estimation

Given a spectrum with a set of lines, the estimate_line_parameters can be called to estimate the Model parameters given a spectrum.

For the Gaussian1D, Voigt1D, and Lorentz1D models, there are predefined estimators for each of the parameters. For all other models one must define the estimators (see example below). Note that in many (most?) cases where another model is needed, it may be better to create your own template models tailored to your specific spectra and skip this function entirely.

For example, based on the spectrum defined above we can first select a region:

>>> from specutils import SpectralRegion
>>> from specutils.fitting import estimate_line_parameters
>>> from specutils.manipulation import extract_region

>>> sub_region = SpectralRegion(4*u.um, 5*u.um)
>>> sub_spectrum = extract_region(spectrum, sub_region)

Then estimate the line parameters it it for a Gaussian line profile:

>>> print(estimate_line_parameters(sub_spectrum, models.Gaussian1D()))  
   Model: Gaussian1D
   Inputs: ('x',)
   Outputs: ('y',)
   Model set size: 1
   Parameters:
           amplitude            mean             stddev
               Jy                um                um
       ------------------ ---------------- -------------------
       1.1845669151078486 4.57517271067525 0.19373372929165977

If an Model is used that does not have the predefined parameter estimators, or if one wants to use different parameter estimators then one can create a dictionary where the key is the parameter name and the value is a function that operates on a spectrum (lambda functions are very useful for this purpose). For example if one wants to estimate the line parameters of a line fit for a RickerWavelet1D one can define the estimators dictionary and use it to populate the estimator attribute of the model’s parameters:

>>> from specutils import SpectralRegion
>>> from specutils.fitting import estimate_line_parameters
>>> from specutils.manipulation import extract_region
>>> from specutils.analysis import centroid, fwhm

>>> sub_region = SpectralRegion(4*u.um, 5*u.um)
>>> sub_spectrum = extract_region(spectrum, sub_region)

>>> ricker = models.RickerWavelet1D()
>>> ricker.amplitude.estimator = lambda s: max(s.flux)
>>> ricker.x_0.estimator = lambda s: centroid(s, region=None)
>>> ricker.sigma.estimator = lambda s: fwhm(s)

>>> print(estimate_line_parameters(spectrum, ricker))  
Model: RickerWavelet1D
Inputs: ('x',)
Outputs: ('y',)
Model set size: 1
Parameters:
        amplitude             x_0                sigma
           Jy                 um                  um
   ------------------ ------------------ -------------------
   2.4220683957581444 3.6045476935889367 0.24416769183724707

Model (Line) Fitting

The generic model fitting machinery is well-suited to fitting spectral lines. The first step is to create a set of models with initial guesses as the parameters. To achieve better fits it may be wise to include a set of bounds for each parameter, but that is optional.

Note

A method to make plausible initial guesses will be provided in a future version, but user defined initial guesses are required at present.

Below are a series of examples of this sort of fitting.

Simple Example

Below is a simple example to demonstrate how to use the fit_lines method to fit a spectrum to an Astropy model initial guess.

Simple Example with Different Units

Similar fit example to above, but the Gaussian model initial guess has different units. The fit will convert the initial guess to the spectral units, fit and then output the fitted model in the spectrum units.

Single Peak Fit Within a Window (Defined by Center)

Single peak fit with a window of 2*u.um around the center of the mean of the model initial guess (so 2*u.um around 5.5*u.um).

Single Peak Fit Within a Window (Defined by Left and Right)

Single peak fit using spectral data only within the window 6*u.um to 7*u.um, all other data will be ignored.

Double Peak Fit

Double peak fit compound model initial guess in and compound model out.

Double Peak Fit Within a Window

Double peak fit using data in the spectrum from 4.3*u.um to 5.3*u.um, only.

Double Peak Fit Within Around a Center Window

Double peak fit using data in the spectrum centered on 4.7*u.um +/- 0.3*u.um.

Double Peak Fit - Two Separate Peaks

Double peak fit where each model gl_init and gr_init is fit separately, each within 0.2*u.um of the model’s mean.

Double Peak Fit - Two Separate Peaks With Two Windows

Double peak fit where each model gl_init and gr_init is fit within the corresponding window.

Double Peak Fit - Exclude One Region

Double peak fit where each model gl_init and gr_init is fit using all the data except between 5.2*u.um and 5.8*u.um.

Continuum Fitting

While the line-fitting machinery can be used to fit continuua at the same time as models, often it is convenient to subtract or normalize a spectrum by its continuum before other processing is done. specutils provides some convenience functions to perform exactly this task. An example is shown below.

The normalized spectrum is simply the old spectrum devided by the fitted continuum, which returns a new object:

Reference/API