A class for multi-component fitting to spectroscopic data of ices.
This is the heart of Omnifit, which receives spectra from the spectrum module, and is then capable of fitting an arbitrary number of different components to the target spectrum thus designated.
Attributes
target_x | (numpy.ndarray) The x axis of the target spectrum, e.g. the wavelength. |
target_y | (numpy.ndarray) The y axis of the target spectrum, e.g. the optical depth. |
target_dy | (float) A single number expressing the average uncertainty of the y axis data. |
modelname | (string) A human-readable name for the model being fitted. |
psf | (Nonetype, numpy.ndarray, or astropy.convolution.Kernel) If set, this attribute can be used to give a kernel which should be used to convolve all the fitted data with. |
fitrange | (Nonetype or list) If set, this specifies the inclusive limits to which the fitting should be performed in x axis coordinates. For example a fitrange of [[200,250],[300,350]] sets two fitting windows of 200 to 250, and 300 to 350. |
color | (string) A string inidcating the desired plotting color of the target data, in a format understandable by matplotlib. |
funclist | (list) A list containing all the fittable functions. Each list entry is a dictionary containing the following keys and values: * ‘name’ : A human-readable name for the function being fitted, in string format. * ‘color’ : A string inidcating the desired plotting color of the data, in a format understandable by matplotlib. * ‘type’ : A string indicating what type of data the function consists of. It can be either ‘analytical’ or ‘empirical’, indicating an analytical function or empirical spectrum, respectively. * ‘shape’ : The shape of the function being fitted. In the case of an analytical function, this is a string indicating the callable name of the function. In the case of an empirical spectrum, this is the y-axis data from the spectrum. * ‘params’ : an lmfit Parameters instance containing the fitting parameters appropriate to the data being fitted. |
fitpars | (Parameters) This is where the fitting parameters are stored during and after minimization. |
fitres | (Minimizer) The fitting results are stored in this class, as documented in lmfit. |
Constructor for the Fitter class. Initialisation happens by designating the target spectrum.
Parameters: | x : numpy.ndarray
y : numpy.ndarray
dy : float, optional
modelname : string, optional
psf : Nonetype or numpy.ndarray or astropy.convolution.Kernel, optional
fitrange : Nonetype or list, optional
color : string, optional
|
---|
Methods Summary
add_analytical(shape,params,funcname=None,color=) | Add analytical data in the form of a callable function to the fitting list. |
add_empirical(...) | Add empirical data in the form of a spectrum to the fitting list. |
chisq([checkrange]) | Return chi squared of fit, either in a custom range or in the range used by the fit. |
fit_results() | Return the fitting results as a dictionary. |
fitresults_tofile(filename) | Export fit results to two output files which are intended to be easily readable and paraseable with other software. |
fromspectrum(spectrum,**kwargs) | An alternate way to initialise Fitter, by directly giving it a spectrum. |
is_nondet([sigma]) | Determines whether the fitted functions in the function list can be considered detections or non-detections using the given detection thereshold. |
perform_fit(**kwargs) | Uses minimize in lmfit to perform least-squares fitting of all the functions in the function list to the target data. |
plot_fitresults(...) | Plot the fitting results to the given matplotlib axis, with a number of optional parameters specifying how the different plottable components are presented. |
Methods Documentation
Add analytical data in the form of a callable function to the fitting list.
Parameters: | shape : string
params : Parameters
funcname : string, optional
color : string, optional
|
---|
Add empirical data in the form of a spectrum to the fitting list. The spectrum must be interpolated to match the target x axis.
Parameters: | spectrum : spectrum.BaseSpectrum
params : Parameters
funcname : Nonetype or string, optional
color : string, optional
|
---|
Return chi squared of fit, either in a custom range or in the range used by the fit.
Parameters: | checkrange : Nonetype or list, optional
|
---|---|
Returns: | The chi squared within the desired ranged. |
Return the fitting results as a dictionary.
Parameters: | None |
---|---|
Returns: | A dictionary containing all the individual functions which were fitted. The key-value combinations of this dictionary consist of the function name, and its lmfit Parameters instance, which contains the best-fit results. |
Export fit results to two output files which are intended to be easily readable and paraseable with other software.
The first file is filename.csv, which contains x and y data of the fitted models, as would be visualized in a plotted fit result. The first column of the csv is the x value, which is shared by all models. The second column is the y value of data that was being fitted to. The third column is total sum of fitted models. The fourth to Nth columns are the individual models, in the order described in the second file, filename.xml.
The second file, filename.xml is an XML file containing additional information about the fitted data and the fit results which are not easily representable in a csv-formatted file. This data is formatted using the following XML elements:
- INFO : Contains all the other elements described below, and has the attribute “file”, which is the name of the csv file pair of this xml file.
- MODELNAME : Contains the name of the model.
- HAVEPSF : A boolean value indicating whether there is a PSF associated with the model.
- RMS_DATA : The uncertainty of the data.
- NUMBER_FUNCTIONS : An integer indicating how many functions have been fitted to the total data.
In addition to the above elements, each fitted function has its own element, designated FUNCTION, having the attribute “name” which is the name of the function. FUNCTION contains the following elements:
- TYPE : If the function is an empirical one, this contains the string “empirical”. Otherwise it contains the name of the called analytical function.
- DETECTION : When generating the contents of this element, The method is_nondet with the detection threshold designated by the parameter detection_threshold. The result given by the method is indicated here with a “True” or “False” depending on whether the result is considered a detection.
- CSV_COLUMN : Indicates which column in the CSV contains the fitted data for this function.
- NUMBER_PARAMS : Inidicates how many parameters are used by this function i.e. the number of PARAMETER elements.
Finally, contained within each FUNCTION element is a number of PARAMETER elements, which list the best-fit data for each fitted parameter pertaining to that function. Each PARAMETER element contains the attribute “name”, which tells the name of the parameter. In addition the following elements are contained by each PARAMETER element:
- VALUE : The best-fit value for this parameter.
Parameters: | filename : string
detection_threshold : float, optional
|
---|
An alternate way to initialise Fitter, by directly giving it a spectrum. Extracted data from the spectrum are the x, y, and (if the spectrum has been baselined) dy parameters.
Parameters: | spectrum : omnifit.spectrum.BaseSpectrum or its child class
**kwargs : Arguments, optional
|
---|
Determines whether the fitted functions in the function list can be considered detections or non-detections using the given detection thereshold. This is done by comparing the peak of the fitted function within the fitting range to a multiple (set by the parameter sigma) of the RMS noise in the target data. It should be emphasized that unless the dy attribute has been set during the fitter class initialisation, the results returned by this method are meaningless.
Parameters: | sigma : float, optional
|
---|---|
Returns: | A dictionary containing boolean values for each function (with their names as the keys) and the total fit (key ‘total’), with True indicating that the function is considered a non-detection using the criteria outlined above. |
Uses minimize in lmfit to perform least-squares fitting of all the functions in the function list to the target data.
Parameters: | **kwargs : Arguments, optional
|
---|
Plot the fitting results to the given matplotlib axis, with a number of optional parameters specifying how the different plottable components are presented.
Parameters: | axis : matplotlib.axis
lw : list, optional
color_total : string, optional
legend : bool, optional
**kwargs : Arguments, optional
|
---|