Skip to content

Commit b5169ae

Browse files
committed
init
1 parent 73ffa3d commit b5169ae

File tree

7 files changed

+503
-0
lines changed

7 files changed

+503
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# fitting.git
2+
Some example fits
3+
Work with this by installing [docker](https://www.docker.com/) and pip and then running
4+
5+
~~~
6+
pip install jupyter-repo2docker
7+
jupyter-repo2docker --editable .
8+
~~~
9+
10+
Change `tree` to `lab` in the URL for JupyterLab.

binder/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM bnlxray/main:172e41f3bd7d

examples/LSCO_30_LH_grazout.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

examples/example.ipynb

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import lmfit\n",
10+
"import numpy as np\n",
11+
"import copy\n",
12+
"import matplotlib.pyplot as plt\n",
13+
"from fitting_functions import paramagnon\n",
14+
"from matplotlib.ticker import AutoMinorLocator\n",
15+
"\n",
16+
"%matplotlib widget"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 2,
22+
"metadata": {},
23+
"outputs": [],
24+
"source": [
25+
"E_, I_ = np.loadtxt('LSCO_30_LH_grazout.txt', unpack=True, skiprows=1)\n",
26+
"E_ *= -1\n",
27+
"choose = np.logical_and(E_>-.5, E_<2.5)\n",
28+
"E = E_[choose]\n",
29+
"I = I_[choose]\n",
30+
"dd_onset = 1."
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 3,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"model = (lmfit.models.GaussianModel(prefix='el_') + lmfit.Model(paramagnon, prefix='mag_')\n",
40+
" + lmfit.models.PseudoVoigtModel(prefix='dd0_')\n",
41+
" + lmfit.models.PseudoVoigtModel(prefix='dd1_')\n",
42+
" + lmfit.models.PseudoVoigtModel(prefix='dd2_')\n",
43+
" + lmfit.models.ConstantModel())\n",
44+
"params = model.make_params()\n",
45+
"\n",
46+
"fwhm = 2*np.sqrt(2*np.log(2))\n",
47+
"res = 0.13/fwhm\n",
48+
" \n",
49+
"params['el_center'].set(value=0, vary=False)\n",
50+
"params['el_amplitude'].set(value=100, min=0)\n",
51+
"params['el_sigma'].set(value=res, vary=False)\n",
52+
"\n",
53+
"params['mag_center'].set(value=.35)\n",
54+
"params['mag_sigma'].set(value=.05, min=0)\n",
55+
"params['mag_amplitude'].set(value=20, min=0)\n",
56+
"\n",
57+
"params['mag_res'].set(value=res, vary=False)\n",
58+
"params['mag_kBT'].set(value=8.617e-5*25, vary=False)\n",
59+
"\n",
60+
"params['dd0_center'].set(value=1.6, min=1, max=3)\n",
61+
"params['dd0_sigma'].set(value=0.1, min=0)\n",
62+
"params['dd0_amplitude'].set(value=300)\n",
63+
"\n",
64+
"params['dd1_center'].set(value=1.8, min=1, max=3)\n",
65+
"params['dd1_sigma'].set(value=0.1, min=0)\n",
66+
"params['dd1_amplitude'].set(value=300)\n",
67+
"\n",
68+
"params['dd2_center'].set(value=2, min=1, max=3)\n",
69+
"params['dd2_sigma'].set(value=0.1, min=0)\n",
70+
"params['dd2_amplitude'].set(value=300)\n",
71+
"\n",
72+
"params_dd = copy.deepcopy(params)\n",
73+
"\n",
74+
"for key in params_dd.keys():\n",
75+
" if key[:2] in ['el', 'ma']:\n",
76+
" params_dd[key].set(vary=False)\n",
77+
" else:\n",
78+
" params_dd[key].set(vary=True)\n",
79+
"\n",
80+
"# Fit dds and force leading edge accuracy by artificial weighting\n",
81+
"dd_region = np.logical_or(E<-.3, E>dd_onset)\n",
82+
"weights = .1 + np.exp(-1*((E-1.1)/.3)**2)\n",
83+
"params_dd['c'].set(value=I.min(), vary=False) \n",
84+
"result_dds = model.fit(I[dd_region], x=E[dd_region], params=params_dd,\n",
85+
" weights=weights[dd_region])\n",
86+
"\n",
87+
"#fig, ax = plt.subplots()\n",
88+
"#result_dds.plot_fit(ax=ax, show_init=True)\n",
89+
"\n",
90+
"# assign and fix values for dds \n",
91+
"for key in params.keys():\n",
92+
" if key[:2] == 'dd':\n",
93+
" params[key].set(value=result_dds.params[key].value, vary=False)\n",
94+
"\n",
95+
"params['c'].set(value=I.min(), vary=False) \n",
96+
"result = model.fit(I, x=E, params=params)\n",
97+
"\n",
98+
"# fig, ax = plt.subplots()\n",
99+
"# result.plot_fit(ax=ax, show_init=True)\n",
100+
"# \n",
101+
"# print(result.fit_report())"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": 4,
107+
"metadata": {},
108+
"outputs": [
109+
{
110+
"data": {
111+
"application/vnd.jupyter.widget-view+json": {
112+
"model_id": "6f43ec06c63d4193aa1ccdeaf15bd7a1",
113+
"version_major": 2,
114+
"version_minor": 0
115+
},
116+
"text/plain": [
117+
"Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
118+
]
119+
},
120+
"metadata": {},
121+
"output_type": "display_data"
122+
},
123+
{
124+
"name": "stdout",
125+
"output_type": "stream",
126+
"text": [
127+
"[[Model]]\n",
128+
" (((((Model(gaussian, prefix='el_') + Model(paramagnon, prefix='mag_')) + Model(pvoigt, prefix='dd0_')) + Model(pvoigt, prefix='dd1_')) + Model(pvoigt, prefix='dd2_')) + Model(constant))\n",
129+
"[[Fit Statistics]]\n",
130+
" # fitting method = leastsq\n",
131+
" # function evals = 63\n",
132+
" # data points = 98\n",
133+
" # variables = 4\n",
134+
" chi-square = 10179.8497\n",
135+
" reduced chi-square = 108.296273\n",
136+
" Akaike info crit = 463.033409\n",
137+
" Bayesian info crit = 473.373278\n",
138+
"[[Variables]]\n",
139+
" el_amplitude: 8.72381742 +/- 0.86393752 (9.90%) (init = 100)\n",
140+
" el_center: 0 (fixed)\n",
141+
" el_sigma: 0.05520592 (fixed)\n",
142+
" mag_amplitude: 33.4456677 +/- 0.94889600 (2.84%) (init = 20)\n",
143+
" mag_center: 0.26702044 +/- 0.00309973 (1.16%) (init = 0.35)\n",
144+
" mag_sigma: 0.22941715 +/- 0.01224635 (5.34%) (init = 0.05)\n",
145+
" mag_res: 0.05520592 (fixed)\n",
146+
" mag_kBT: 0.00215425 (fixed)\n",
147+
" dd0_amplitude: 682.9913 (fixed)\n",
148+
" dd0_center: 1.80157 (fixed)\n",
149+
" dd0_sigma: 0.2914458 (fixed)\n",
150+
" dd0_fraction: 1.772421e-11 (fixed)\n",
151+
" dd1_amplitude: 227.6857 (fixed)\n",
152+
" dd1_center: 1.717436 (fixed)\n",
153+
" dd1_sigma: 0.1024631 (fixed)\n",
154+
" dd1_fraction: 3.824163e-13 (fixed)\n",
155+
" dd2_amplitude: 568.9392 (fixed)\n",
156+
" dd2_center: 2.099117 (fixed)\n",
157+
" dd2_sigma: 0.2531614 (fixed)\n",
158+
" dd2_fraction: 0.9321681 (fixed)\n",
159+
" c: 7.519076 (fixed)\n",
160+
" el_fwhm: 0.13000000 +/- 0.00000000 (0.00%) == '2.3548200*el_sigma'\n",
161+
" el_height: 63.0421515 +/- 6.24319350 (9.90%) == '0.3989423*el_amplitude/max(2.220446049250313e-16, el_sigma)'\n",
162+
" dd0_fwhm: 0.2 (fixed)\n",
163+
" dd0_height: 1182.043 (fixed)\n",
164+
" dd1_fwhm: 0.2 (fixed)\n",
165+
" dd1_height: 1182.043 (fixed)\n",
166+
" dd2_fwhm: 0.2 (fixed)\n",
167+
" dd2_height: 1182.043 (fixed)\n",
168+
"[[Correlations]] (unreported correlations are < 0.100)\n",
169+
" C(mag_amplitude, mag_sigma) = 0.830\n",
170+
" C(mag_center, mag_sigma) = 0.443\n",
171+
" C(mag_amplitude, mag_center) = 0.343\n",
172+
" C(el_amplitude, mag_amplitude) = -0.289\n",
173+
" C(el_amplitude, mag_sigma) = -0.272\n"
174+
]
175+
},
176+
{
177+
"data": {
178+
"text/plain": [
179+
"16336"
180+
]
181+
},
182+
"execution_count": 4,
183+
"metadata": {},
184+
"output_type": "execute_result"
185+
}
186+
],
187+
"source": [
188+
"result = model.fit(I, x=E, params=params)\n",
189+
"\n",
190+
"fig, ax = plt.subplots()\n",
191+
"\n",
192+
"x_fit = np.linspace(E.min(), E.max(), 1000)\n",
193+
"\n",
194+
"components = result.eval_components(x=x_fit)\n",
195+
"constant = components.pop('constant')\n",
196+
"dd0 = components.pop('dd0_')\n",
197+
"dd1 = components.pop('dd1_')\n",
198+
"dd2 = components.pop('dd2_')\n",
199+
"\n",
200+
"BG = constant + dd0 + dd1 + dd2\n",
201+
"\n",
202+
"ax.plot(x_fit, BG, 'k:', label='BG')\n",
203+
"for model_name, model_value in components.items():\n",
204+
" ax.plot(x_fit, model_value + BG, '-', label=model_name.strip('_'))\n",
205+
"\n",
206+
"y_fit = result.eval(**result.best_values, x=x_fit)\n",
207+
"ax.plot(x_fit, y_fit, color=[0.5]*3, label='fit', lw=3, alpha=0.5)\n",
208+
"ax.plot(E, I, 'k.', label='data')\n",
209+
"\n",
210+
"ax.set_xlabel('Energy loss (eV)')\n",
211+
"ax.set_ylabel('I')\n",
212+
"ax.legend()\n",
213+
"ax.axis([-.4, dd_onset, 0, 400])\n",
214+
"\n",
215+
"ax.xaxis.set_minor_locator(AutoMinorLocator(2))\n",
216+
"ax.yaxis.set_minor_locator(AutoMinorLocator(2))\n",
217+
"\n",
218+
"print(result.fit_report())\n",
219+
"\n",
220+
"result.dump(open('fit_info.json','w'))"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": 5,
226+
"metadata": {},
227+
"outputs": [],
228+
"source": [
229+
"ci = result.ci_report()\n",
230+
"with open('ci_info.text','w') as f:\n",
231+
" f.write(ci)"
232+
]
233+
},
234+
{
235+
"cell_type": "code",
236+
"execution_count": null,
237+
"metadata": {},
238+
"outputs": [],
239+
"source": []
240+
}
241+
],
242+
"metadata": {
243+
"kernelspec": {
244+
"display_name": "Python 3",
245+
"language": "python",
246+
"name": "python3"
247+
},
248+
"language_info": {
249+
"codemirror_mode": {
250+
"name": "ipython",
251+
"version": 3
252+
},
253+
"file_extension": ".py",
254+
"mimetype": "text/x-python",
255+
"name": "python",
256+
"nbconvert_exporter": "python",
257+
"pygments_lexer": "ipython3",
258+
"version": "3.8.3"
259+
}
260+
},
261+
"nbformat": 4,
262+
"nbformat_minor": 4
263+
}

fitting_functions/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .lineshapes import (paramagnon, magnon, bose, make_gaussian_kernal,
2+
convolve, zero2Linear, zero2Quad, antisymlorz,
3+
plane2D, plane3D, plane3Dcentered, lorentzianSq2D,
4+
lorentzianSq2DRot, lorentzianSq3D, error)

0 commit comments

Comments
 (0)