Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fit_params = Parameters()
fit_params.add('amp', value=13.0, max=20, min=0.0)
fit_params.add('period', value=2, max=10)
fit_params.add('shift', value=0.0, max=pi/2., min=-pi/2.)
fit_params.add('decay', value=0.02, max=0.10, min=0.00)
out = minimize(residual, fit_params, args=(x,), kws={'data':data})
fit = residual(out.params, x)
assert(out.nfev > 10)
assert(out.nfree > 50)
assert(out.chisqr > 1.0)
print(fit_report(out, show_correl=True, modelpars=p_true))
assert_paramval(out.params['decay'], 0.01, tol=1.e-2)
assert_paramval(out.params['shift'], 0.123, tol=1.e-2)
def lmfit(self, star, logger=None):
"""Fit parameters of the given star using lmfit (Levenberg-Marquardt minimization
algorithm).
:param star: A Star to fit.
:param logger: A logger object for logging debug info. [default: None]
:returns: (flux, dx, dy, scale, g1, g2, flag)
"""
import lmfit
logger = galsim.config.LoggerWrapper(logger)
params = self._lmfit_params(star)
results = self._lmfit_minimize(params, star, logger=logger)
if logger:
logger.debug(lmfit.fit_report(results))
flux, du, dv, scale, g1, g2 = results.params.valuesdict().values()
if not results.success:
raise RuntimeError("Error fitting with lmfit.")
try:
params_var = np.diag(results.covar)
except (ValueError, AttributeError) as e:
logger.warning("Failed to get params_var")
logger.warning(" -- Caught exception: %s",e)
# results.covar is either None or does not exist
params_var = np.zeros(6)
return flux, du, dv, scale, g1, g2, params_var
def lmfit(self, star, logger=None):
"""Fit parameters of the given star using lmfit (Levenberg-Marquardt minimization
algorithm).
:param star: A Star to fit.
:param logger: A logger object for logging debug info. [default: None]
:returns: (flux, cenx, ceny, sigma, g1, g2, flag)
"""
params = self._lmfit_params(star)
results = self._lmfit_minimize(params, star, logger=logger)
if logger:
import lmfit
logger.debug(lmfit.fit_report(results))
dflux, dcenu, dcenv, dsize, dg1, dg2 = results.params.valuesdict().values()
flag = 0 if results.success else 1
return dflux, dcenu, dcenv, dsize, dg1, dg2, flag
random.seed(0)
x = linspace(0.0, 250., 1001)
noise = random.normal(scale=0.7215, size=x.size)
data = residual(p_true, x) + noise
fit_params = Parameters()
fit_params.add('amp', value=13.0)
fit_params.add('period', value=2)
fit_params.add('shift', value=0.0)
fit_params.add('decay', value=0.02)
out = minimize(residual, fit_params, args=(x,), kws={'data': data})
print(fit_report(out))
#
def fit_report(self, params=None, **kws):
if params is None:
params = self.params
return lmfit_report(params, **kws)
show_correl=show_correl,
min_correl=min_correl, sort_pars=sort_pars)
elif isinstance(result, ModelResult):
return result.fit_report(modelpars=modelpars,
show_correl=show_correl,
min_correl=min_correl, sort_pars=sort_pars)
else:
result = getattr(fit_result, 'params', fit_result)
if isinstance(result, Parameters):
return lmfit.fit_report(result, modelpars=modelpars,
show_correl=show_correl,
min_correl=min_correl, sort_pars=sort_pars)
else:
try:
result = group2params(fit_result, _larch=_larch)
return lmfit.fit_report(result, modelpars=modelpars,
show_correl=show_correl,
min_correl=min_correl, sort_pars=sort_pars)
except (ValueError, AttributeError):
pass
return "Cannot make fit report with %s" % repr(fit_result)
"""
real_data = create_real_data()
fit_objective = ba.FitObjective()
fit_objective.addSimulationAndData(get_simulation, real_data, 1.0)
fit_objective.initPrint(10)
params = lmfit.Parameters()
params.add('radius', value=7*nm, min=5*nm, max=8*nm)
params.add('length', value=10*nm, min=8*nm, max=14*nm)
result = lmfit.minimize(fit_objective.evaluate_residuals, params)
fit_objective.finalize(result)
print(result.params.pretty_print())
print(lmfit.fit_report(result))
pvalues = np.where(np.isnan(p.values), None, p.values)
parameters.add(k, value=pvalues[0], min=pvalues[1],
max=pvalues[2], vary=pvalues[3])
if self.noisemodel is not None:
for k in self.noisemodel.parameters.index:
p = self.noisemodel.parameters.loc[k]
pvalues = np.where(np.isnan(p.values), None,
p.values) # needed because lmfit doesn't
# take nan as input
parameters.add(k, value=pvalues[0], min=pvalues[1],
max=pvalues[2], vary=pvalues[3])
self.lmfit_params = parameters
self.fit = lmfit.minimize(fcn=self.residuals, params=parameters,
ftol=1e-3, epsfcn=1e-4,
args=(tmin, tmax, self.solvemethod, noise))
if report: print lmfit.fit_report(self.fit)
self.parameters = np.array([p.value for p in self.fit.params.values()])
self.paramdict = self.fit.params.valuesdict()
# Return parameters to tseries
for ts in self.tserieslist:
for k in ts.parameters.index:
ts.parameters.loc[k].value = self.paramdict[k]
if self.noisemodel is not None:
for k in self.noisemodel.parameters.index:
self.noisemodel.parameters.loc[k].value = self.paramdict[k]
# Make the Statistics class available after optimization
self.stats = Statistics(self)
real_data = create_real_data()
fit_objective = ba.FitObjective()
fit_objective.addSimulationAndData(get_simulation, real_data, 1.0)
fit_objective.initPrint(10)
params = lmfit.Parameters()
params.add('radius', value=7*nm, min=5*nm, max=8*nm)
params.add('length', value=10*nm, min=8*nm, max=14*nm)
plotter = Plotter(fit_objective)
result = lmfit.minimize(fit_objective.evaluate_residuals, params, iter_cb=plotter)
fit_objective.finalize(result)
result.params.pretty_print()
print(lmfit.fit_report(result))
x = np.linspace(0.3, 10, 100)
np.random.seed(0)
y = 1/(0.1*x) + 2 + 0.1*np.random.randn(x.size)
pars = lmfit.Parameters()
pars.add_many(('a', 0.1), ('b', 1))
def residual(p):
return 1/(p['a']*x) + p['b'] - y
mini = lmfit.Minimizer(residual, pars)
result = mini.minimize()
print(lmfit.fit_report(result.params))
ci = lmfit.conf_interval(mini, result)
lmfit.printfuncs.report_ci(ci)
#