Skip to content
Snippets Groups Projects
Commit b16d3c06 authored by Laurent Mercadier's avatar Laurent Mercadier
Browse files

Handles case where fit does not work

parent 77c198d1
No related branches found
No related tags found
1 merge request!75Handles case where fit does not work
...@@ -76,20 +76,28 @@ def knife_edge(nrun, axisKey='scannerX', signalKey='FastADC4peaks', ...@@ -76,20 +76,28 @@ def knife_edge(nrun, axisKey='scannerX', signalKey='FastADC4peaks',
funcStr = 'a*erfc(-np.sqrt(2)*(x-x0)/w0) + b' funcStr = 'a*erfc(-np.sqrt(2)*(x-x0)/w0) + b'
if p0 is None: if p0 is None:
p0 = [np.mean(pos_sel), 0.1, np.max(int_sel)/2, 0] p0 = [np.mean(pos_sel), 0.1, np.max(int_sel)/2, 0]
popt, pcov = curve_fit(func, pos_sel, int_sel, p0=p0) try:
print('fitting function:', funcStr) popt, pcov = curve_fit(func, pos_sel, int_sel, p0=p0)
print('w0 = (%.1f +/- %.1f) um'%(popt[1]*1e3, pcov[1,1]**0.5*1e3)) print('fitting function:', funcStr)
print('x0 = (%.3f +/- %.3f) mm'%(popt[0], pcov[0,0]**0.5)) print('w0 = (%.1f +/- %.1f) um'%(popt[1]*1e3, pcov[1,1]**0.5*1e3))
print('a = %e +/- %e '%(popt[2], pcov[2,2]**0.5)) print('x0 = (%.3f +/- %.3f) mm'%(popt[0], pcov[0,0]**0.5))
print('b = %e +/- %e '%(popt[3], pcov[3,3]**0.5)) print('a = %e +/- %e '%(popt[2], pcov[2,2]**0.5))
print('b = %e +/- %e '%(popt[3], pcov[3,3]**0.5))
fitSuccess = True
except:
print('Could not fit the data with ercf function.' +
' Try adjusting the axisRange and the initial parameters p0')
fitSuccess = False
if plot: if plot:
xfit = np.linspace(positions.min(), positions.max(), 1000)
yfit = func(xfit, *popt)
plt.figure(figsize=(7,4)) plt.figure(figsize=(7,4))
plt.scatter(positions, intensities, color='C1', label='exp', s=2, alpha=0.1) plt.scatter(positions, intensities, color='C1', label='exp', s=2, alpha=0.1)
plt.plot(xfit, yfit, color='C4', if fitSuccess:
label=r'fit $\rightarrow$ $w_0=$(%.1f $\pm$ %.1f) $\mu$m'%(popt[1]*1e3, pcov[1,1]**0.5*1e3)) xfit = np.linspace(positions.min(), positions.max(), 1000)
yfit = func(xfit, *popt)
plt.plot(xfit, yfit, color='C4',
label=r'fit $\rightarrow$ $w_0=$(%.1f $\pm$ %.1f) $\mu$m'%(popt[1]*1e3,
pcov[1,1]**0.5*1e3))
leg = plt.legend() leg = plt.legend()
for lh in leg.legendHandles: for lh in leg.legendHandles:
lh.set_alpha(1) lh.set_alpha(1)
...@@ -98,6 +106,12 @@ def knife_edge(nrun, axisKey='scannerX', signalKey='FastADC4peaks', ...@@ -98,6 +106,12 @@ def knife_edge(nrun, axisKey='scannerX', signalKey='FastADC4peaks',
plt.title(nrun.attrs['runFolder']) plt.title(nrun.attrs['runFolder'])
plt.tight_layout() plt.tight_layout()
if full: if full:
return popt, pcov, func if fitSuccess:
return popt, pcov, func
else:
return np.zeros(4), np.zeros(2), None
else: else:
return np.array([popt[1], pcov[1,1]**0.5]) if fitSuccess:
return np.array([popt[1], pcov[1,1]**0.5])
else:
return np.zeros(2)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment