The SpecModel Module
This module introduces the SpecModel class and its functionality. The SpecModel class is designed to fit models to an astronomical spectrum using LMFIT.
The SpecModel is always associated with a SpecFit object, which provides the foundational functionality for the fitting.
Notes
This module is in active development.
- class sculptor.specmodel.SpecModel(specfit, spectrum=None, redshift=0)
Class holding information on models for the SpecFit class
- xlim
Wavelength limits for plotting
- Type
list of float
- ylim
Flux density limits for plotting
- Type
list of float
- redshift
Cosmological redshift of the astronomical object
- Type
float
- use_weights
Boolean to indicate whether fluxden errors will be used as weights for the fit.
- Type
bool
- model_list
List of LMFIT models
- Type
list of Models
- params_list
List of LMFIT Parameters for all LMFIT models.
- Type
list of Parameters
- global_params
Global parameters to be added to the all models in the Specmodel. Their main use is to provide variables and constraints for multiple individual models.
- Type
Parameters
- color
Color to use in the SpecModel plot.
- Type
str
- model
LMFIT SpecModel model. The global model including all models in the model_list.
- Type
Model
- params
LMFIT SpecModel parameters. The global parameter list including all parameters from all models.
- Type
Parameters
- fit_result
LMFIT ModelResult for the fit to the SpecModel
- Type
ModelResult
- add_global_param(param_name, value=None, vary=True, min=- inf, max=inf, expr=None)
Adding a “Global Parameter” to the SpecModel object
- Parameters
param_name (str) – Name of the global parameter
value (float,optional) – Initial value of the global parameter
vary (bool,optional) – Boolean to indicate whether the global parameter should be varied during the fit
min (float,optional) – Minimum value for the global parameter
max (float,optional) – Maximum value for the global parameter
expr (str, optional) – Optional expression for the global parameter
- Returns
None
- add_mask_preset_to_fit_mask(mask_preset_key)
Adding a preset mask from the models_and_masks module to the fit.
- Parameters
mask_preset_key (str) – Name of the preset mask in the mask_preset dictionary.
- Returns
None
- add_model(model_name, prefix, **kwargs)
Add a model to the SpecModel by using the built-in Sculptor models.
- Parameters
model_name –
prefix –
- Returns
- add_wavelength_range_to_fit_mask(disp_x1, disp_x2)
Adding a wavelength region to the fit mask.
The dispersion region between the two dispersion values will be added to the fit mask.
- Parameters
disp_x1 ((float)) – Dispersion value 1
disp_x2 ((float)) – Dispersion value 2
- Returns
- build_model()
Build the Specmodel model and parameters for the fit
- Returns
None
- delete_model(index=None)
Delete model (Model, Parameters) from the SpecModel object.
- Parameters
index ((int)) – Index of model to remove from model_list and Parameters to remove from params_list (default index==”None”). If the index is None the last added model will be removed.
- Returns
None
- fit()
Fit the SpecModel to the astronomical spectrum
- Returns
None
- load(foldername, specmodel_id)
Load a SpecModel from the specified folder.
- Parameters
foldername (str) – Specified folder in which the SpecModel will be saved.
specmodel_id (str) – Unique SpecModel identifier used in creating the filenames for the save files.
- Returns
None
- plot(xlim=None, ylim=None)
Plot the SpecModel
- Returns
None
- remove_global_param(param_name)
Remove “Global Parameter” from SpecModel object
- Parameters
param_name (str) – Parameter name of the global parameter to remove.
- Returns
None
- reset_fit_mask()
Reset the fit mask based on the supplied astronomical spectrum.
- Returns
None
- reset_plot_limits(fluxden=True, dispersion=True)
Reset the plot limits based on the dispersion and flux density ranges of the spectrum.
- Parameters
fluxden (boolean) – Boolean to indicate whether to reset the flux density axis limits (default: True).
dispersion (boolean) – Boolean to indicate whether to reset the dispersion axis limits (default: True).
- Returns
None
- save(foldername, specmodel_id=0)
Save the SpecModel object to a specified folder
- Parameters
foldername (str) – Specified folder in which the SpecModel will be saved.
specmodel_id (str) – Unique SpecModel identifier used in creating the filenames for the save files.
- Returns
None
- save_fit_report(foldername, specmodel_id=None, show=False)
Save the fit report to a file in the specified folder
- Parameters
foldername (str) – Specified folder in which the fit report will be saved.
specmodel_id (str) – Unique SpecModel identifier used in creating the filename for the fit report.
show (bool) – Boolean to indicate whether the fit report should also be printed to the screen.
- Returns
None
- save_mcmc_chain(foldername, specmodel_id=None)
Save the values of the MCMC flat chain as an hdf5 file.
Fixed parameters in the model fit will be automatically added to the output file.
- Parameters
foldername (str) – Specified folder in which the fit report will be saved.
specmodel_id (str) – Unique SpecModel identifier used in creating the filename for the fit report.
- Returns
None
- update_model_params_for_global_params()
Global parameters are added to the Model parameters.
- Returns
None
- update_params_from_fit_result()
Update all parameter values in the parameter list based on the fit result.
Individual model parameter, global parameters and even the super parameters of the associated SpecFit object will be updated based on the fit.
- Returns
None
- sculptor.specmodel.fitting_methods = {'Adaptive Memory Programming for Global Optimization': 'ampgo', 'BFGS': 'bfgs', 'Basinhopping': 'basinhopping', 'Brute force method': 'brute', 'Cobyla': 'cobyla', 'Conjugate-Gradient': 'cg', 'Differential evolution': 'differential_evolution', 'Dual Annealing Optimization': 'dual_annealing', 'L-BFGS-B': 'lbfgsb', 'Least-Squares minimization': 'least_squares', 'Levenberg-Marquardt': 'leastsq', 'Maximum likelihood via Monte-Carlo Markov Chain': 'emcee', 'Nelder-Mead': 'nelder', 'Newton GLTR trust-region': 'trust-krylov', 'Powell': 'powell', 'Sequential Linear Squares Programming': 'slsqp', 'Simplicial Homology Global Optimization': 'shgo', 'Truncated Newton': 'tnc', 'Trust-region for constrained obtimization': 'trust-constr'}
Dictionary of fitting methods
Fitting methods available for fitting in SpecFit based on the list of methods in LMFIT.
- Type
dict