SpecViz utilizes Astropy Models and Fitting to fit models to its spectra. For example, you can fit one model to the continuum, another to an emission line of interest, and yet another to an absorption line.
Currently, the following models are available:
|SpecViz Model Name||Astropy Model Class|
The models can be fitted with the following fitters:
|SpecViz Fitter Name||Astropy Fitter Class|
To add a model:
- Select the desired layer from
Layers(left panel). For example, you can choose the layer containing your emission or absorption line. See Viewer on how to create a layer for ROI.
- Select the desired model name from the
Add Modeldrop-down box and click
Selectto add it to
- If desired, repeat the above step to add additional models.
- Scroll down (if needed) and click
- A new model layer will be created under
Layers(left panel) and it is attached to the selected data layer.
To fine-tune model parameters:
- Select the model layer under
Layers(left panel) that contains the desired model.
- If desired, double-click on the model name to rename it. When you see a blinking cursor, enter its new name and press “Enter”.
- Expand the model listing under
Current Modelson the right of the viewer.
- Double-click on the desired model parameter value in the listing.
When you see a blinking cursor, enter the new value and press
- Scroll down (if needed) and click
To fit a model:
- Select the model layer under
Layersthat contains the model(s) you wish to fit to your data.
- Select the desired fitter from
Fitting Routineusing its drop-down menu.
Perform Fit. This may take up to a few seconds, depending on the complexity of the fit.
- The associated model parameters will be adjusted accordingly.
Arithmetic Behavior text box is used to define the relationship between
different models for the same layer. If nothing is defined, the default is to
add all the models together. To describe a non-default model relationship,
enter the model names and math operators, as shown in the examples below and
Create Layer or
Update Layer to produce the compound model:
Linear1 + Gaussian1
Linear1 * Gaussian1
Gaussian1 - Gaussian2
The entity that results from lumping together all the models, and combining them either using the arithmetic behavior expression, or just adding them all together, is called a “compound model”.
When added to the
Current Models list, a model will receive a default name
that is generated from the model type (as listed in the drop down model selector)
plus a running numerical suffix.
These names can be re-defined by clicking on the default name and typing a new
name. Note that re-defining names will require that any eventual expression in
Arithmetic Behavior text box should be edited accordingly.
For now, we are limited to only alphanumeric characters (and no white spaces) when re-naming models.
Note that the Spline model is of an intrinsically different nature than the other models included in the drop down list of models. The Spline model, when added to a pre-existing list of models, or when added by itself to an empty list, will immediately be fitted to the data within the currently defined Regions Of Interest. That is, being a linear model, there is no need to iterate in search of a “best fit” spline. It is just computed once and for all, and kept as part of the compound model that is built from the models in the list and the arithmetic behavior expression.
This implies that, to change the regions of interest that define the spline,
one has no other way than removing the spline from the list of models. Then,
redefine the regions of interest, and add a new spline to the list. To change
a spline parameter, there is no need do discard the spline. Just do it in the
same way as with other models: just type in the new value for the parameter and
Subsequently, when the fitter iterates the compound model in search of a best solution, the spline model will act as a constant. That is, it will be used to compute the global result of the compound model, but its parameters won’t be accessed, and varied, by the fitter. Thus, the spline parameters are not fitted, they are just a convenient mechanism that enables user access to the parameter’s values.
The documentation for the spline model cane be seen here:
Note that SpecViz provides access, at this point, to just two of the parameters
in the scipy implementation of th spline function. Pay special attention to the
smooth parameter. SpecViz initializes it to a ‘best guess’ (
Too small of a value in here may cause the spline to enter an infinite loop.
smooth value with care, trying to stay close to the default
Model arithmetic is a work in progress.
Saving and Exporting Models to File¶
Selecting a model layer under “Layers” will enable the
Save (the “floppy disk” icon) and
Export (the “out the door” icon) buttons under
“Current Models” on the right of the viewer. Saving a model to a file will
enable SpecViz to read back that model into a new model layer. Exporting a model
to a file wil create a Python script in a
.py file. This file can be
directly imported by Python in a command-line session.
Click on either button to get a file dialog window. Type in a file name.
If this file name does not end with the correct suffix, the suffix will
automatically be appended. Click “Save”, or just the Return/Enter key.
The correct suffix for saved and exported files are
Save and Load¶
Saving the model to a file works in the same way as Export. The difference is that a saved model can be later read back into SpecViz via the “Load” button (the “folder” icon), also under “Current Models”.
For the “Load” button to be enabled, a data (spectrum) layer (not a model layer)
must be selected under “Layers” listing. The selected
.yaml model file will
generate a model that will be attached to a new model layer associated under the
selected data layer.
The file is writen using the YAML format. Being a plain text file with a self-explanatory structure, it can be edited at will by the user, e.g., to add bounds, fixed flags, and ties to the model parameters. Note that these extra, user-defined attributes, won’t be accessible from SpecViz’s user interface. They will however, be accessible by the fitter when a fit is run on the model. They will also be written ou correctly, either when saving or exporting the model.
YAML format for saved models and usage of advanced features like bounds and fixed flags are work in progress.
This will save the model in the currently selected model layer to a file
that can be directly imported by Python. The file is just a plain text
file with the model expressed recorded as a Python expression. The model
is associated to a variable named
The following example uses the
'test3.py' file name, and a model comprised
of a constant and a gaussian:
>>> import test3 >>> test3 <module 'test3' from '/my/saved/models/test3.py'> >>> test3.model1 <CompoundModel0(amplitude_0=0.297160787184, amplitude_1=2.25396100263, mean_1=15117.1710847, stddev_1=948.493577186)> >>> print(test3.model1) Model: CompoundModel0 Inputs: ('x',) Outputs: ('y',) Model set size: 1 Parameters: amplitude_0 amplitude_1 mean_1 stddev_1 -------------- ------------- ------------- ------------- 0.297160787184 2.25396100263 15117.1710847 948.493577186
The file can be edited at will by the user, e.g., to add bounds, fixed flags, and ties to the model parameters.
Security issues importing model this way into Python and usage of advanced features like bounds and fixed flags are work in progress.