Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_expr_and_constraints_GH265(self):
# test that parameters are reevaluated if they have bounds and expr
# see GH265
p = Parameters()
p['a'] = Parameter('a', 10, True)
p['b'] = Parameter('b', 10, True, 0, 20)
assert_equal(p['b'].min, 0)
assert_equal(p['b'].max, 20)
p['a'].expr = '2 * b'
assert_almost_equal(p['a'].value, 20)
p['b'].value = 15
assert_almost_equal(p['b'].value, 15)
assert_almost_equal(p['a'].value, 30)
p['b'].value = 30
assert_almost_equal(p['b'].value, 20)
assert_almost_equal(p['a'].value, 40)
def parameter():
"""Initialize parameter for tests."""
param = lmfit.Parameter(name='a', value=10.0, vary=True, min=-100.0,
max=100.0, expr=None, brute_step=5.0, user_data=1)
expected_attribute_values = ('a', 10.0, True, -100.0, 100.0, None, 5.0, 1)
assert_parameter_attributes(param, expected_attribute_values)
return param, expected_attribute_values
def test_init_bounds():
"""Tests to make sure that initial bounds are consistent.
Only for specific cases not tested above with the initializations of the
Parameter class.
"""
# test 1: min > max; should swap min and max
par = lmfit.Parameter(name='a', value=0.0, min=10.0, max=-10.0)
assert par.min == -10.0
assert par.max == 10.0
# test 2: min == max; should raise a ValueError
msg = r"Parameter 'a' has min == max"
with pytest.raises(ValueError, match=msg):
par = lmfit.Parameter(name='a', value=0.0, min=10.0, max=10.0)
# FIXME: ideally this should be impossible to happen ever....
# perhaps we should add a setter method for MIN and MAX as well?
# test 3: max or min is equal to None
par.min = None
par._init_bounds()
assert par.min == -np.inf
par.max = None
par._init_bounds()
assert par.max == np.inf
def _fit_2d_gaussian(self):
model = lmfit.Model(self._gauss2d, independent_vars=['xy'])
result = model.fit(data=self._xy_optim_scan_data.flatten(),
xy=np.meshgrid(np.linspace(self.__optim_xy_scan_range[0][0],
self.__optim_xy_scan_range[0][1],
self._optim_xy_resolution),
np.linspace(self.__optim_xy_scan_range[1][0],
self.__optim_xy_scan_range[1][1],
self._optim_xy_resolution),
indexing='ij'),
amp=lmfit.Parameter('amp', value=self._xy_optim_scan_data.max(), min=0),
x0=lmfit.Parameter(
'x0',
value=self.__current_target['x'],
min=self.__current_target['x'] - 3 * self._optim_xy_scan_range / 4,
max=self.__current_target['x'] + 3 * self._optim_xy_scan_range / 4),
y0=lmfit.Parameter(
'y0',
value=self.__current_target['y'],
min=self.__current_target['y'] - 3 * self._optim_xy_scan_range / 4,
max=self.__current_target['y'] + 3 * self._optim_xy_scan_range / 4),
sigma_x=lmfit.Parameter('sigma_x',
value=self._optim_xy_scan_range/2,
min=0,
max=self._optim_xy_scan_range),
sigma_y=lmfit.Parameter('sigma_y',
value=self._optim_xy_scan_range / 2,
min=0,
a0 = args[0]
if isinstance(a0, str):
kws.update({'expr': a0})
elif isinstance(a0, (int, float)):
kws.update({'value': a0})
else:
raise ValueError("first argument to param() must be string or number")
args = args[1:]
if '_larch' in kws:
kws.pop('_larch')
if 'vary' not in kws:
kws['vary'] = False
if 'name' not in kws:
kws['name'] = '_tmp_param_'
return Parameter(*args, **kws)
amp=lmfit.Parameter('amp', value=self._xy_optim_scan_data.max(), min=0),
x0=lmfit.Parameter(
'x0',
value=self.__current_target['x'],
min=self.__current_target['x'] - 3 * self._optim_xy_scan_range / 4,
max=self.__current_target['x'] + 3 * self._optim_xy_scan_range / 4),
y0=lmfit.Parameter(
'y0',
value=self.__current_target['y'],
min=self.__current_target['y'] - 3 * self._optim_xy_scan_range / 4,
max=self.__current_target['y'] + 3 * self._optim_xy_scan_range / 4),
sigma_x=lmfit.Parameter('sigma_x',
value=self._optim_xy_scan_range/2,
min=0,
max=self._optim_xy_scan_range),
sigma_y=lmfit.Parameter('sigma_y',
value=self._optim_xy_scan_range / 2,
min=0,
max=self._optim_xy_scan_range),
offset=lmfit.Parameter('offset',
value=self._xy_optim_scan_data.min(),
min=0),
theta=lmfit.Parameter('theta', value=0, min=-np.pi/2, max=np.pi/2))
return result
if name in kwargs:
# kw parameter names with prefix
par.value = kwargs[name]
params.add(par)
if verbose:
print(' - Adding parameter "%s"' % name)
# next build parameters defined in param_hints
# note that composites may define their own additional
# convenience parameters here
for basename, hint in self.param_hints.items():
name = "%s%s" % (self._prefix, basename)
if name in params:
par = params[name]
else:
par = Parameter(name=name)
params.add(par)
if verbose:
print(' - Adding parameter for hint "%s"' % name)
par._delay_asteval = True
for item in self._hint_names:
if item in hint:
setattr(par, item, hint[item])
if basename in kwargs:
par.value = kwargs[basename]
# Add the new parameter to self._param_names
if name not in self._param_names:
self._param_names.append(name)
for p in params.values():
p._delay_asteval = False
return params
par.expr = hints['expr']
pwids = ParameterWidgets(panel, par, name_size=100, expr_size=150,
float_size=80, prefix=prefix,
widgets=('name', 'value', 'minval',
'maxval', 'vary', 'expr'))
parwids[par.name] = pwids
panel.Add(pwids.name, newrow=True)
panel.AddMany((pwids.value, pwids.vary, pwids.bounds,
pwids.minval, pwids.maxval, pwids.expr))
for sname, hint in minst.param_hints.items():
pname = "%s%s" % (prefix, sname)
if 'expr' in hint and pname not in parnames:
par = Parameter(name=pname, value=0, expr=hint['expr'])
pwids = ParameterWidgets(panel, par, name_size=100, expr_size=400,
float_size=80, prefix=prefix,
widgets=('name', 'value', 'expr'))
parwids[par.name] = pwids
panel.Add(pwids.name, newrow=True)
panel.Add(pwids.value)
panel.Add(pwids.expr, dcol=5, style=wx.ALIGN_RIGHT)
pwids.value.Disable()
fgroup = Group(prefix=prefix, title=title, mclass=mclass,
mclass_kws=mclass_kws, usebox=usebox, panel=panel,
parwids=parwids, float_size=65, expr_size=150,
pick2_msg=pick2msg, bkgbox=bkgbox)
self.fit_components[prefix] = fgroup
def __init__(self, parent=None, size=(-1, -1)):
wx.Frame.__init__(self, parent, -1, 'Parameter Panel Test',
size=size, style=wx.DEFAULT_FRAME_STYLE)
panel = GridPanel(self)
param1 = Parameter(value=99.0, vary=True, min=0, name='peak1_amplitude')
param2 = Parameter(value=110.2, vary=True, min=100, max=120, name='peak1_center')
param3 = Parameter(value=1.23, vary=True, min=0.5, max=2.0, name='peak1_sigma')
panel.Add(ParameterPanel(panel, param1, show_name=True), style=LEFT)
# panel.NewRow()
panel.Add(ParameterPanel(panel, param2, show_name=True), style=LEFT)
panel.Add(ParameterPanel(panel, param3, show_name=True), style=LEFT)
panel.pack()
self.createMenus()
self.SetSize((700, 200))
self.Show()
self.Raise()
fit_values=self.fit_parm)
self.fit_bound = con.update_fit_bounds()
if con.parameters[0]["id"] == pp:
kws = con.get_lmfit_parameter_kwargs()
if kws is not None:
kwarglist += kws
if len(kwarglist) == 0:
# normal parameter
kwarglist += [{"name": "parm{:04d}".format(pp),
"value": self.fit_parm[pp],
"vary": True,
"min": self.fit_bound[pp][0],
"max": self.fit_bound[pp][1],
}]
for kw in kwarglist:
params.add(lmfit.Parameter(**kw))
return params