Default Plot
Regular Usage of the Jupyter-Notebook extension for SpectraFit
¶
This notebook shows how to use the Jupyter-Notebook extension for SpectraFit
to fit a spectrum. In order to use the extension, you need to install the SpectraFit
like the following:
pip install spectrafit[jupyter]
or straight in the notebook:
! pip install spectrafit[jupyter]
Then, you need to enable the extension by running the following command in the terminal:
from spectrafit.plugins import notebook as nb
Other regular packages are already installed:
numpy
matplotlib
scipy
pandas
So that the data / spectra can be loaded and plotted.
import pandas as pd
import matplotlib
# %matplotlib inline
df = pd.read_csv(
"https://raw.githubusercontent.com/Anselmoo/spectrafit/main/Examples/data.csv"
)
df.plot(x="Energy", y="Noisy", kind="line", label="Spectra")
<Axes: xlabel='Energy'>
The spectra
aboive shows a pseudo 2p3d spectrum, see also: https://doi.org/10.1021/acs.inorgchem.8b01550
Load the data into the notebook-plugin of SpectraFit
¶
spn = nb.SpectraFitNotebook(df=df, x_column="Energy", y_column="Noisy")
Define the fitting model¶
initial_model = [
{
"pseudovoigt": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 1},
"center": {"max": 2, "min": -2, "vary": True, "value": 0},
"fwhmg": {"max": 0.3, "min": 0.02, "vary": True, "value": 0.1},
"fwhml": {"max": 0.2, "min": 0.01, "vary": True, "value": 0.1},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 2.0, "min": 0, "vary": True, "value": 2},
"fwhmg": {"max": 0.3, "min": 0.02, "vary": True, "value": 0.1},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 3.5, "min": 1.5, "vary": True, "value": 2.5},
"fwhmg": {"max": 0.4, "min": 0.02, "vary": True, "value": 0.2},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 3.5, "min": 2, "vary": True, "value": 2.5},
"fwhmg": {"max": 0.4, "min": 0.02, "vary": True, "value": 0.3},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 4.5, "min": 3, "vary": True, "value": 2.5},
"fwhmg": {"max": 0.4, "min": 0.02, "vary": True, "value": 0.3},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 4.7, "min": 3.7, "vary": True, "value": 3.8},
"fwhmg": {"max": 0.4, "min": 0.02, "vary": True, "value": 0.3},
}
},
]
Run the fitting with the proposed initial_model
¶
spn.solver_model(initial_model=initial_model)
/home/runner/work/spectrafit/spectrafit/spectrafit/report.py:342: UserWarning: ## WARNING ######################### Uncertainties could not be estimated #################################### /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ################################################################################## The parameter 'pseudovoigt_fwhmg_1' is at its boundary and uncertainties cannot be estimated! ############################################################################################# /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ################################################################################## The parameter 'pseudovoigt_fwhml_1' is at its boundary and uncertainties cannot be estimated! ############################################################################################# /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ############################################################################### The parameter 'gaussian_fwhmg_3' is at its boundary and uncertainties cannot be estimated! ########################################################################################## /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ############################################################################### The parameter 'gaussian_fwhmg_4' is at its boundary and uncertainties cannot be estimated! ########################################################################################## /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ############################################################################### The parameter 'gaussian_fwhmg_5' is at its boundary and uncertainties cannot be estimated! ########################################################################################## /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ################################################################################ The parameter 'gaussian_center_6' is at its boundary and uncertainties cannot be estimated! ########################################################################################### /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ############################################################################### The parameter 'gaussian_fwhmg_6' is at its boundary and uncertainties cannot be estimated! ##########################################################################################
Show the dataframe with the fitted parameters¶
spn.display_fit_df(mode="interactive")
energy | intensity | residual | fit | pseudovoigt_1 | gaussian_2 | gaussian_3 | gaussian_4 | gaussian_5 | gaussian_6 |
---|---|---|---|---|---|---|---|---|---|
Loading ITables v2.2.3 from the internet... (need help?) |
Metric Plot of the fitted model¶
SpectraFit
also shows the metric of the fitted model. The plot shows the metric of the fitted model for each run. The metric consits of the goodness of fit
and the regression metric
of the model. By default, the Akaike Information Criterion
and Bayesian Information Criteria
are used for the bar plot and the Mean Squared Error
for the line plot.
initial_model = [
{
"pseudovoigt": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 1},
"center": {"max": 2, "min": -2, "vary": True, "value": 0},
"fwhmg": {"max": 0.3, "min": 0.02, "vary": True, "value": 0.1},
"fwhml": {"max": 0.2, "min": 0.01, "vary": True, "value": 0.1},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 2.0, "min": 0, "vary": True, "value": 2},
"fwhmg": {"max": 0.3, "min": 0.02, "vary": True, "value": 0.1},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 3.5, "min": 1.5, "vary": True, "value": 2.5},
"fwhmg": {"max": 0.4, "min": 0.02, "vary": True, "value": 0.2},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 3.5, "min": 1.5, "vary": True, "value": 2.5},
"fwhmg": {"max": 0.4, "min": 0.02, "vary": True, "value": 0.2},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 3.25, "min": 1.8, "vary": True, "value": 2.5},
"fwhmg": {"max": 0.4, "min": 0.02, "vary": True, "value": 0.3},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 3.5, "min": 2, "vary": True, "value": 2.5},
"fwhmg": {"max": 0.4, "min": 0.02, "vary": True, "value": 0.3},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 4.5, "min": 3, "vary": True, "value": 2.5},
"fwhmg": {"max": 0.4, "min": 0.02, "vary": True, "value": 0.3},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 4.5, "min": 3, "vary": True, "value": 2.5},
"fwhmg": {"max": 0.4, "min": 0.02, "vary": True, "value": 0.3},
}
},
{
"gaussian": {
"amplitude": {"max": 2, "min": 0, "vary": True, "value": 0.3},
"center": {"max": 4.7, "min": 3.7, "vary": True, "value": 3.8},
"fwhmg": {"max": 0.4, "min": 0.02, "vary": True, "value": 0.3},
}
},
]
spn.solver_model(initial_model=initial_model, conf_interval=False)
/home/runner/work/spectrafit/spectrafit/spectrafit/report.py:342: UserWarning: ## WARNING ######################### Uncertainties could not be estimated #################################### /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ################################################################################## The parameter 'pseudovoigt_fwhmg_1' is at its boundary and uncertainties cannot be estimated! ############################################################################################# /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ################################################################################## The parameter 'pseudovoigt_fwhml_1' is at its boundary and uncertainties cannot be estimated! ############################################################################################# /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ############################################################################### The parameter 'gaussian_fwhmg_2' is at its boundary and uncertainties cannot be estimated! ########################################################################################## /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ############################################################################### The parameter 'gaussian_fwhmg_3' is at its boundary and uncertainties cannot be estimated! ########################################################################################## /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ############################################################################### The parameter 'gaussian_fwhmg_4' is at its boundary and uncertainties cannot be estimated! ########################################################################################## /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:358: UserWarning: ## WARNING ##################################################################################### The parameter 'gaussian_center_7' is at its initial value and uncertainties cannot be estimated! ################################################################################################ /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ################################################################################ The parameter 'gaussian_center_7' is at its boundary and uncertainties cannot be estimated! ########################################################################################### /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ############################################################################### The parameter 'gaussian_fwhmg_7' is at its boundary and uncertainties cannot be estimated! ########################################################################################## /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ############################################################################### The parameter 'gaussian_fwhmg_8' is at its boundary and uncertainties cannot be estimated! ########################################################################################## /home/runner/work/spectrafit/spectrafit/spectrafit/report.py:366: UserWarning: ## WARNING ############################################################################### The parameter 'gaussian_fwhmg_9' is at its boundary and uncertainties cannot be estimated! ##########################################################################################