fitspy.spectrum module
Class dedicated to spectrum processing
- fitspy.spectrum.create_model(model, model_name, prefix=None)
Return a ‘model’ (peak_model or ‘bkg_model’) object
- class fitspy.spectrum.Spectrum
Bases:
object
Class dedicated to spectrum processing
- fname
Filename associated to spectrum to handle
- Type:
str
- range_min, range_max
Range associated to the spectrum support to work with
- Type:
floats
- x0, y0
Arrays related to the spectrum raw support and intensity (resp.)
- Type:
numpy.array((n0))
- x, y
Arrays related to spectrum modified support and intensity (resp.)
- Type:
numpy.array((n))
- normalize
Activation keyword for the spectrum profile normalization
- Type:
bool
- normalize_range_min, normalize_range_max
Ranges for searching the maximum value used in the normalization
- Type:
floats
- outliers_limit
Array related to the outliers limit associated to the raw data (x0, y0)
- Type:
numpy.array((n0))
- baseline
Baseline associated to the spectrum (to subract)
- Type:
Baseline object
- baseline_history
Concatenation list of all baselines applied to the spectrum. Each item of the list consist in the max order of the baseline polynom and the (x, y) baseline points coordinates.
- Type:
list of list - DEPRECATED from v2024.2
- bkg_model
Background model to fit with the composite peaks models, among : [None, ‘ConstantModel’, ‘LinearModel’, ‘ParabolicModel’, ‘ExponentialModel’]
- Type:
lmfit.Model
- peak_models
List of peak models
- Type:
list of lmfit.Model
- peak_labels
List of labels associated to the peak models. Default is [‘1’, ‘2’, …]
- Type:
list of str
- peak_index
Counter used for peak models indexing when creating a lmfit.Model
- Type:
itertools.count
- fit_params
Dictionary used to manage the fit parameters: * method: str
Method used for fitting. See lmfit.Model.fit(). Default method is ‘Leastsq’.
- fit_negative: bool
Activation keyword to take into account negative values when fitting. Default is False.
- fit_outliers: bool
Activation keyword to take into account outliers points when fitting. Default is False.
- max_ite: int
Maximum number of iteration associated to the fitting process. An iteration consists in evaluating all the ‘free’ parameters once. Default is 200.
- coef_noise: float
Coefficient applied to the estimated noise amplitude to define a threshold below which the fit weights are set to 0, and local peak models are disabled . Default is 2.
- xtol: float
Relative error desired in the solution approximated by the ‘Leastsq’ or the ‘Least_square’ algorithm. Default is 1e-4.
- Type:
dict
- result_fit
Object resulting from lmfit fitting. Default value is a ‘None’ object (function) that enables to address a ‘result_fit.success’ status.
- Type:
lmfit.ModelResult
- reinit()
Reinitialize the main attributes
- set_attributes(model_dict)
Set attributes from a dictionary (obtained from a .json reloading)
- preprocess()
Preprocess the spectrum: call successively load_profile(), apply_range(), eval_baseline(), subtract_baseline() and normalization()
- load_profile(fname)
Load profile from ‘fname’ with 1 header line and 2 (x,y) columns
- apply_range(range_min=None, range_max=None)
Apply range to the raw spectrum
- calculate_outliers()
Return outliers points (x,y) coordinates
- property y_no_outliers
Return spectrum profile where outliers have been removed and replaced by interpolated values
- normalization()
Normalize spectrum according to the ‘Maximum’ value or the nearest ‘Attractor’ value from reference position, assuming that the baseline has been correctly defined (y_min value ~ 0)
- static create_peak_model(index, model_name, x0, ampli, fwhm=1.0, fwhm_l=1.0, fwhm_r=1.0, alpha=0.5)
Create a ‘lmfit’ model associated to one peak
- Parameters:
index (int) – Index used to create the ‘prefix’ associated to the peak model
model_name (str) – Model name among ‘Gaussian’, ‘Lorentzian’, ‘PseudoVoigt’, ‘GaussianAsym’, ‘LorentzianAsym’
x0 (float) – Position of the peak model
ampli (float) – Amplitude of the peak model.
fwhm (floats, optional) – Optional parameters passed to the model related to the Full Width at Half Maximum. Default values are 1.
fwhm_l (floats, optional) – Optional parameters passed to the model related to the Full Width at Half Maximum. Default values are 1.
fwhm_r (floats, optional) – Optional parameters passed to the model related to the Full Width at Half Maximum. Default values are 1.
alpha (float, optional) – Optional parameter passed to the ‘PseudoVoigt’ model. Default value is 0.5.
- Returns:
peak_model
- Return type:
lmfit.Model
- reassign_params()
Reassign fitted ‘params’ values to the ‘models’
- add_peak_model(model_name, x0, ampli=None, fwhm=None, fwhm_l=None, fwhm_r=None, alpha=0.5)
Add a peak model passing model_name and indice position or parameters
- Parameters:
model_name (str) – Model name among ‘Gaussian’, ‘Lorentzian’, ‘GaussianAsym’, ‘LorentzianAsym’
x0 (float) – Position of the peak model
ampli (float, Optional) – Amplitude of the peak model. If None, consider the amplitude of the spectrum profile at position x0
fwhm (floats, optional) – Optional parameters passed to the model related to the Full Width at Half Maximum. Default values are x-step size (x[1]-x[0]).
fwhm_l (floats, optional) – Optional parameters passed to the model related to the Full Width at Half Maximum. Default values are x-step size (x[1]-x[0]).
fwhm_r (floats, optional) – Optional parameters passed to the model related to the Full Width at Half Maximum. Default values are x-step size (x[1]-x[0]).
alpha (float, optional) – Optional parameter passed to the ‘PseudoVoigt’ model. Default values is 0.5.
- del_peak_model(i)
Delete the ith-peak model
- reorder(key='x0', reverse=False)
Return increasing or decreasing ordered models list from ‘key’
- static get_model_name(model)
from model class attribute return the function name associated Ex: Model(‘LorentzianAsym’…) -> ‘lorentzian_asym’
- remove_models()
Remove all the models
- set_bkg_model(bkg_name)
Set the ‘bkg_model’ attribute from ‘bkg_name’
- fit(fit_method=None, fit_negative=None, fit_outliers=None, max_ite=None, coef_noise=None, xtol=None, reinit_guess=True, **kwargs)
Fit the peaks and background models
- Parameters:
fit_method (str, optional) – Method passed to lmfit.fit() like ‘leastsq’, ‘least_squares’, ‘nelder’, ‘slsqp’, … (see the lmfit documentation). Default value is ‘leastsq’.
fit_negative (bool, optional) – Activation key to take into account negative values during the fit. Default value is False.
fit_outliers (bool, optional) – Activation key to take into account outliers during the fit. Default value is False.
max_ite (int, optional) – Number of maximum iterations (1 iteration corresponds to 1 gradient descent of all the variables). Default value is 200.
coef_noise (float, optional) – Multiplication factor associated with the estimated noise level. Default value is 1.
xtol (float, optional) – Relative tolerance associated with the ‘leastsq’ and the ‘least_squares’ fit algorithm. Default value is 0.0001.
reinit_guess (bool, optional) – Key to adapt initial values for ‘ampli’ and ‘fwhm’, ‘fwhm_l’ or ‘fwhm_r’ to the spectrum intensity at the corresponding point ‘x0’. Default value is True.
kwargs (dict, optional) – Dictionary of optional arguments passed to lmfit.fit()
- auto_baseline()
set baseline.mode to ‘Semi-Auto
- eval_baseline()
Evaluate baseline profile
- subtract_baseline()
Subtract the baseline to the spectrum profile
- auto_peaks(model_name)
Create automatically ‘model_name’ peak-models in the limit of 5% of the maximum intensity peaks and nmax_nfev=400
- plot(ax, show_outliers=True, show_outliers_limit=True, show_negative_values=True, show_noise_level=True, show_baseline=True, show_background=True, show_peak_models=True, show_result=True, subtract_baseline=True)
Plot the spectrum with the peak models
- plot_residual(ax, factor=1)
Plot the residual x factor obtained after fitting
- save_params(dirname_params)
Save fit parameters in a ‘.csv’ file located in ‘dirname_params’
- save_stats(dirname_stats)
Save statistics in a ‘.txt’ file located in ‘dirname_stats’
- save(fname_json=None)
Return a ‘model_dict’ dictionary from the spectrum attributes and Save it if a ‘fname_json’ is given
- static load(fname_json, preprocess=False)
Return a Spectrum object created from a .json file reloading