Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
mpmdean committed Sep 11, 2020
1 parent 73ffa3d commit b5169ae
Show file tree
Hide file tree
Showing 7 changed files with 503 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# fitting.git
Some example fits
Work with this by installing [docker](https://www.docker.com/) and pip and then running

~~~
pip install jupyter-repo2docker
jupyter-repo2docker --editable .
~~~

Change `tree` to `lab` in the URL for JupyterLab.
1 change: 1 addition & 0 deletions binder/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM bnlxray/main:172e41f3bd7d
1 change: 1 addition & 0 deletions examples/LSCO_30_LH_grazout.txt

Large diffs are not rendered by default.

263 changes: 263 additions & 0 deletions examples/example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import lmfit\n",
"import numpy as np\n",
"import copy\n",
"import matplotlib.pyplot as plt\n",
"from fitting_functions import paramagnon\n",
"from matplotlib.ticker import AutoMinorLocator\n",
"\n",
"%matplotlib widget"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"E_, I_ = np.loadtxt('LSCO_30_LH_grazout.txt', unpack=True, skiprows=1)\n",
"E_ *= -1\n",
"choose = np.logical_and(E_>-.5, E_<2.5)\n",
"E = E_[choose]\n",
"I = I_[choose]\n",
"dd_onset = 1."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"model = (lmfit.models.GaussianModel(prefix='el_') + lmfit.Model(paramagnon, prefix='mag_')\n",
" + lmfit.models.PseudoVoigtModel(prefix='dd0_')\n",
" + lmfit.models.PseudoVoigtModel(prefix='dd1_')\n",
" + lmfit.models.PseudoVoigtModel(prefix='dd2_')\n",
" + lmfit.models.ConstantModel())\n",
"params = model.make_params()\n",
"\n",
"fwhm = 2*np.sqrt(2*np.log(2))\n",
"res = 0.13/fwhm\n",
" \n",
"params['el_center'].set(value=0, vary=False)\n",
"params['el_amplitude'].set(value=100, min=0)\n",
"params['el_sigma'].set(value=res, vary=False)\n",
"\n",
"params['mag_center'].set(value=.35)\n",
"params['mag_sigma'].set(value=.05, min=0)\n",
"params['mag_amplitude'].set(value=20, min=0)\n",
"\n",
"params['mag_res'].set(value=res, vary=False)\n",
"params['mag_kBT'].set(value=8.617e-5*25, vary=False)\n",
"\n",
"params['dd0_center'].set(value=1.6, min=1, max=3)\n",
"params['dd0_sigma'].set(value=0.1, min=0)\n",
"params['dd0_amplitude'].set(value=300)\n",
"\n",
"params['dd1_center'].set(value=1.8, min=1, max=3)\n",
"params['dd1_sigma'].set(value=0.1, min=0)\n",
"params['dd1_amplitude'].set(value=300)\n",
"\n",
"params['dd2_center'].set(value=2, min=1, max=3)\n",
"params['dd2_sigma'].set(value=0.1, min=0)\n",
"params['dd2_amplitude'].set(value=300)\n",
"\n",
"params_dd = copy.deepcopy(params)\n",
"\n",
"for key in params_dd.keys():\n",
" if key[:2] in ['el', 'ma']:\n",
" params_dd[key].set(vary=False)\n",
" else:\n",
" params_dd[key].set(vary=True)\n",
"\n",
"# Fit dds and force leading edge accuracy by artificial weighting\n",
"dd_region = np.logical_or(E<-.3, E>dd_onset)\n",
"weights = .1 + np.exp(-1*((E-1.1)/.3)**2)\n",
"params_dd['c'].set(value=I.min(), vary=False) \n",
"result_dds = model.fit(I[dd_region], x=E[dd_region], params=params_dd,\n",
" weights=weights[dd_region])\n",
"\n",
"#fig, ax = plt.subplots()\n",
"#result_dds.plot_fit(ax=ax, show_init=True)\n",
"\n",
"# assign and fix values for dds \n",
"for key in params.keys():\n",
" if key[:2] == 'dd':\n",
" params[key].set(value=result_dds.params[key].value, vary=False)\n",
"\n",
"params['c'].set(value=I.min(), vary=False) \n",
"result = model.fit(I, x=E, params=params)\n",
"\n",
"# fig, ax = plt.subplots()\n",
"# result.plot_fit(ax=ax, show_init=True)\n",
"# \n",
"# print(result.fit_report())"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "6f43ec06c63d4193aa1ccdeaf15bd7a1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[Model]]\n",
" (((((Model(gaussian, prefix='el_') + Model(paramagnon, prefix='mag_')) + Model(pvoigt, prefix='dd0_')) + Model(pvoigt, prefix='dd1_')) + Model(pvoigt, prefix='dd2_')) + Model(constant))\n",
"[[Fit Statistics]]\n",
" # fitting method = leastsq\n",
" # function evals = 63\n",
" # data points = 98\n",
" # variables = 4\n",
" chi-square = 10179.8497\n",
" reduced chi-square = 108.296273\n",
" Akaike info crit = 463.033409\n",
" Bayesian info crit = 473.373278\n",
"[[Variables]]\n",
" el_amplitude: 8.72381742 +/- 0.86393752 (9.90%) (init = 100)\n",
" el_center: 0 (fixed)\n",
" el_sigma: 0.05520592 (fixed)\n",
" mag_amplitude: 33.4456677 +/- 0.94889600 (2.84%) (init = 20)\n",
" mag_center: 0.26702044 +/- 0.00309973 (1.16%) (init = 0.35)\n",
" mag_sigma: 0.22941715 +/- 0.01224635 (5.34%) (init = 0.05)\n",
" mag_res: 0.05520592 (fixed)\n",
" mag_kBT: 0.00215425 (fixed)\n",
" dd0_amplitude: 682.9913 (fixed)\n",
" dd0_center: 1.80157 (fixed)\n",
" dd0_sigma: 0.2914458 (fixed)\n",
" dd0_fraction: 1.772421e-11 (fixed)\n",
" dd1_amplitude: 227.6857 (fixed)\n",
" dd1_center: 1.717436 (fixed)\n",
" dd1_sigma: 0.1024631 (fixed)\n",
" dd1_fraction: 3.824163e-13 (fixed)\n",
" dd2_amplitude: 568.9392 (fixed)\n",
" dd2_center: 2.099117 (fixed)\n",
" dd2_sigma: 0.2531614 (fixed)\n",
" dd2_fraction: 0.9321681 (fixed)\n",
" c: 7.519076 (fixed)\n",
" el_fwhm: 0.13000000 +/- 0.00000000 (0.00%) == '2.3548200*el_sigma'\n",
" el_height: 63.0421515 +/- 6.24319350 (9.90%) == '0.3989423*el_amplitude/max(2.220446049250313e-16, el_sigma)'\n",
" dd0_fwhm: 0.2 (fixed)\n",
" dd0_height: 1182.043 (fixed)\n",
" dd1_fwhm: 0.2 (fixed)\n",
" dd1_height: 1182.043 (fixed)\n",
" dd2_fwhm: 0.2 (fixed)\n",
" dd2_height: 1182.043 (fixed)\n",
"[[Correlations]] (unreported correlations are < 0.100)\n",
" C(mag_amplitude, mag_sigma) = 0.830\n",
" C(mag_center, mag_sigma) = 0.443\n",
" C(mag_amplitude, mag_center) = 0.343\n",
" C(el_amplitude, mag_amplitude) = -0.289\n",
" C(el_amplitude, mag_sigma) = -0.272\n"
]
},
{
"data": {
"text/plain": [
"16336"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result = model.fit(I, x=E, params=params)\n",
"\n",
"fig, ax = plt.subplots()\n",
"\n",
"x_fit = np.linspace(E.min(), E.max(), 1000)\n",
"\n",
"components = result.eval_components(x=x_fit)\n",
"constant = components.pop('constant')\n",
"dd0 = components.pop('dd0_')\n",
"dd1 = components.pop('dd1_')\n",
"dd2 = components.pop('dd2_')\n",
"\n",
"BG = constant + dd0 + dd1 + dd2\n",
"\n",
"ax.plot(x_fit, BG, 'k:', label='BG')\n",
"for model_name, model_value in components.items():\n",
" ax.plot(x_fit, model_value + BG, '-', label=model_name.strip('_'))\n",
"\n",
"y_fit = result.eval(**result.best_values, x=x_fit)\n",
"ax.plot(x_fit, y_fit, color=[0.5]*3, label='fit', lw=3, alpha=0.5)\n",
"ax.plot(E, I, 'k.', label='data')\n",
"\n",
"ax.set_xlabel('Energy loss (eV)')\n",
"ax.set_ylabel('I')\n",
"ax.legend()\n",
"ax.axis([-.4, dd_onset, 0, 400])\n",
"\n",
"ax.xaxis.set_minor_locator(AutoMinorLocator(2))\n",
"ax.yaxis.set_minor_locator(AutoMinorLocator(2))\n",
"\n",
"print(result.fit_report())\n",
"\n",
"result.dump(open('fit_info.json','w'))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"ci = result.ci_report()\n",
"with open('ci_info.text','w') as f:\n",
" f.write(ci)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
4 changes: 4 additions & 0 deletions fitting_functions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .lineshapes import (paramagnon, magnon, bose, make_gaussian_kernal,
convolve, zero2Linear, zero2Quad, antisymlorz,
plane2D, plane3D, plane3Dcentered, lorentzianSq2D,
lorentzianSq2DRot, lorentzianSq3D, error)
Loading

0 comments on commit b5169ae

Please sign in to comment.