Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def minimize_migrad(fun, x0, args=(), dx0=None, **kwargs):
"""Minimization function using MINUIT's MIGRAD minimizer."""
import iminuit
mfun = MinuitFunction(f=fun, dim=len(x0), args=args)
# bring the parameters in a suitable form
par = iminuit.util.describe(mfun)
x0_dict = {par[i]: x0i for i, x0i in enumerate(x0)}
if dx0 is None:
dx0 = np.ones(len(x0))
dx0_dict = {'error_' + par[i]: dx0i for i, dx0i in enumerate(dx0)}
# run
minuit_args={'errordef': 1}
minuit_args.update(kwargs)
minuit = iminuit.Minuit(mfun, **x0_dict, **dx0_dict, **minuit_args)
fmin, param = minuit.migrad()
# cast migrad result in terms of scipy-like result object
res = scipy.optimize.OptimizeResult()
res.success = fmin['is_valid']
res.fun = fmin['fval']
res.x = np.array([p['value'] for p in param])
res.nfev = fmin['nfcn']
return res
self.param_names = []
for parset in system.walk():
#-- for each parameterSet, walk to all the parameters
for qual in parset:
#-- extract those which need to be fitted
if parset.get_adjust(qual) and parset.has_prior(qual):
#-- ask a unique ID and update the value of the parameter
this_param = parset.get_parameter(qual)
myid = this_param.get_unique_label().replace('-','_')
if myid in had: continue
id_for_minuit = '{}_{}'.format(qual,myid)
setattr(self,id_for_minuit,this_param.get_value())
had.append(myid)
self.param_names.append(id_for_minuit)
self.func_code = iminuit.util.make_func_code(self.param_names)
self.func_defaults = None
#-- construct the iminuit Minimizer