Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def residual(pars, x, sigma=None, data=None):
"""Define objective function."""
yg = gaussian(x, pars['amp_g'], pars['cen_g'], pars['wid_g'])
yl = lorentzian(x, pars['amp_l'], pars['cen_l'], pars['wid_l'])
model = yg + yl + pars['line_off'] + x * pars['line_slope']
if data is None:
return model
if sigma is None:
return model - data
return (model-data) / sigma
model = yg + yl + pars['line_off'] + x * pars['line_slope']
if data is None:
return model
if sigma is None:
return (model - data)
return (model - data) / sigma
n = 201
xmin = 0.
xmax = 20.0
x = linspace(xmin, xmax, n)
data = (gaussian(x, 21, 8.1, 1.2) +
lorentzian(x, 10, 9.6, 2.4) +
random.normal(scale=0.23, size=n) +
x*0.5)
pfit = Parameters()
pfit.add(name='amp_g', value=10)
pfit.add(name='cen_g', value=9)
pfit.add(name='wid_g', value=1)
pfit.add(name='amp_tot', value=20)
pfit.add(name='amp_l', expr='amp_tot - amp_g')
pfit.add(name='cen_l', expr='1.5+cen_g')
pfit.add(name='wid_l', expr='2*wid_g')
pfit.add(name='line_slope', value=0.0)
pfit.add(name='line_off', value=0.0)
def residual(pars, x, sigma=None, data=None):
yg = gaussian(x, pars['amp_g'], pars['cen_g'], pars['wid_g'])
yl = lorentzian(x, pars['amp_l'], pars['cen_l'], pars['wid_l'])
model = yg + yl + pars['line_off'] + x * pars['line_slope']
if data is None:
return model
if sigma is None:
return (model - data)
return (model - data) / sigma
except ImportError:
HASPYLAB = False
def residual(pars, x, data):
model = (gaussian(x, pars['amp_g'], pars['cen_g'], pars['wid_g']) +
lorentzian(x, pars['amp_l'], pars['cen_l'], pars['wid_l']))
return model - data
n = 601
random.seed(0)
x = linspace(0, 20.0, n)
data = (gaussian(x, 21, 6.1, 1.2) +
lorentzian(x, 10, 9.6, 1.3) +
random.normal(scale=0.1, size=n))
pfit = Parameters()
pfit.add(name='amp_g', value=10)
pfit.add(name='amp_l', value=10)
pfit.add(name='cen_g', value=5)
pfit.add(name='peak_split', value=2.5, min=0, max=5, vary=True)
pfit.add(name='cen_l', expr='peak_split+cen_g')
pfit.add(name='wid_g', value=1)
pfit.add(name='wid_l', expr='wid_g')
mini = Minimizer(residual, pfit, fcn_args=(x, data))
out = mini.leastsq()
report_fit(out.params)
out = mod.fit(yn, pars, x=xs)
out2 = mod.fit(yn, pars, x=xs, fit_kws={'Dfun': dfunc_gaussian,
'col_deriv': 1})
print('lmfit without dfunc **************')
print('number of function calls: ', out.nfev)
print('params', out.best_values)
print('lmfit with dfunc *****************')
print('number of function calls: ', out2.nfev)
print('params', out2.best_values)
print('\n \n')
out2.plot(datafmt='.')
print('**********************************')
print('***** Test Lorentzian ************')
print('**********************************')
ys = lorentzian(xs, 2.5, 0, 0.5)
yn = ys + 0.1*np.random.normal(size=len(xs))
mod = LorentzianModel()
pars = mod.guess(yn, xs)
out = mod.fit(yn, pars, x=xs)
out2 = mod.fit(yn, pars, x=xs, fit_kws={'Dfun': dfunc_lorentzian, 'col_deriv': 1})
print('lmfit without dfunc **************')
print('number of function calls: ', out.nfev)
print('params', out.best_values)
print('lmfit with dfunc *****************')
print('number of function calls: ', out2.nfev)
print('params', out2.best_values)
print('\n \n')
out2.plot(datafmt='.')
plt.show()
def residual(pars, x, data):
model = (gaussian(x, pars['amp_g'], pars['cen_g'], pars['wid_g']) +
lorentzian(x, pars['amp_l'], pars['cen_l'], pars['wid_l']))
return model - data
slope = pars['line_slope']
offset = pars['line_off']
model = yg + yl + offset + x*slope
if data is None:
return model
if sigma is None:
return model - data
return (model - data) / sigma
random.seed(0)
x = linspace(0.0, 20.0, 601)
data = (gaussian(x, 21, 8.1, 1.2) +
lorentzian(x, 10, 9.6, 2.4) +
random.normal(scale=0.23, size=x.size) +
x*0.5)
pfit = Parameters()
pfit.add(name='amp_g', value=10)
pfit.add(name='cen_g', value=9)
pfit.add(name='wid_g', value=1)
pfit.add(name='amp_tot', value=20)
pfit.add(name='amp_l', expr='amp_tot - amp_g')
pfit.add(name='cen_l', expr='1.5+cen_g')
pfit.add(name='wid_l', expr='2*wid_g')
pfit.add(name='line_slope', value=0.0)
pfit.add(name='line_off', value=0.0)
sigma = 0.021 # estimate of data error (for all data points)
def __init__(self, independent_vars=['x'], prefix='', nan_policy='raise',
**kwargs):
kwargs.update({'prefix': prefix, 'nan_policy': nan_policy,
'independent_vars': independent_vars})
super(LorentzianModel, self).__init__(lorentzian, **kwargs)
self._set_paramhints_prefix()
def residual(pars, x, sigma=None, data=None):
yg = gaussian(x, pars['amp_g'], pars['cen_g'], pars['wid_g'])
yl = lorentzian(x, pars['amp_l'], pars['cen_l'], pars['wid_l'])
slope = pars['line_slope']
offset = pars['line_off']
model = yg + yl + offset + x*slope
if data is None:
return model
if sigma is None:
return model - data
return (model - data) / sigma