Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions docs/examples/example9_6.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Global Fitting of a series of spectra"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from spectrafit.plugins import notebook as nb"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\n",
" \"https://raw.githubusercontent.com/Anselmoo/spectrafit/1cda9e0d93f9d0536380075e75fac50459555b99/Examples/data_global.csv\"\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### This example shows how to fit a series of spectra with a common model\n",
"\n",
"To activate the _global fitting_ routine, `y_column` has to be defined to \n",
"a list of column names and not a single column name. The list of column names \n",
"will be selected for the fitting."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"spn = nb.SpectraFitNotebook(\n",
" df=df, x_column=\"energy\", y_column=[\"y_1\", \"y_2\", \"y_3\"], fname=\"example9_6\"\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Defining the model as a function of the parameters\n",
"\n",
"The `initial model` is defined as a function of the parameters as for 2D fitting."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"## Warning: uncertainties could not be estimated:\n"
]
}
],
"source": [
"initial_model = [\n",
" {\n",
" \"pseudovoigt\": {\n",
" \"amplitude\": {\"max\": 2, \"min\": 0, \"vary\": True, \"value\": 1},\n",
" \"center\": {\"max\": 2, \"min\": -2, \"vary\": True, \"value\": 0},\n",
" \"fwhmg\": {\"max\": 0.3, \"min\": 0.02, \"vary\": True, \"value\": 0.1},\n",
" \"fwhml\": {\"max\": 0.2, \"min\": 0.01, \"vary\": True, \"value\": 0.1},\n",
" }\n",
" },\n",
" {\n",
" \"gaussian\": {\n",
" \"amplitude\": {\"max\": 2, \"min\": 0, \"vary\": True, \"value\": 0.3},\n",
" \"center\": {\"max\": 2., \"min\": 0, \"vary\": True, \"value\": 2},\n",
" \"fwhmg\": {\"max\": 0.3, \"min\": 0.02, \"vary\": True, \"value\": 0.1},\n",
" }\n",
" },\n",
" {\n",
" \"gaussian\": {\n",
" \"amplitude\": {\"max\": 2, \"min\": 0, \"vary\": True, \"value\": 0.3},\n",
" \"center\": {\"max\": 3.5, \"min\": 1.5, \"vary\": True, \"value\": 2.5},\n",
" \"fwhmg\": {\"max\": 0.4, \"min\": 0.02, \"vary\": True, \"value\": 0.2},\n",
" }\n",
" },\n",
" {\n",
" \"gaussian\": {\n",
" \"amplitude\": {\"max\": 2, \"min\": 0, \"vary\": True, \"value\": 0.3},\n",
" \"center\": {\"max\": 3.5, \"min\": 2, \"vary\": True, \"value\": 2.5},\n",
" \"fwhmg\": {\"max\": 0.4, \"min\": 0.02, \"vary\": True, \"value\": 0.3},\n",
" }\n",
" },\n",
" {\n",
" \"gaussian\": {\n",
" \"amplitude\": {\"max\": 2, \"min\": 0, \"vary\": True, \"value\": 0.3},\n",
" \"center\": {\"max\": 4.5, \"min\": 3, \"vary\": True, \"value\": 2.5},\n",
" \"fwhmg\": {\"max\": 0.4, \"min\": 0.02, \"vary\": True, \"value\": 0.3},\n",
" }\n",
" },\n",
" {\n",
" \"gaussian\": {\n",
" \"amplitude\": {\"max\": 2, \"min\": 0, \"vary\": True, \"value\": 0.3},\n",
" \"center\": {\"max\": 4.7, \"min\": 3.7, \"vary\": True, \"value\": 3.8},\n",
" \"fwhmg\": {\"max\": 0.4, \"min\": 0.02, \"vary\": True, \"value\": 0.3},\n",
" }\n",
" },\n",
"]\n",
"spn.solver_model(initial_model=initial_model, show_plot=False, show_metric=False)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Note:\n",
"\n",
"Currently, the global fitting routine has no plot function. However, the\n",
"metric function can be used to plot the fit errors."
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "b199af289ce012bb6a24205fcd5edae736294423f0e29474acbaa2e2fdaf4f82"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 1 addition & 1 deletion spectrafit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ def __call__(self) -> Tuple[Minimizer, Any]:
self.solve_global_fitting,
params=self.params,
fcn_args=(self.x, self.data),
**self.args["minimizer"],
**self.args_solver["minimizer"],
)
else:
minimizer = Minimizer(
Expand Down