I want to set the parameter hints for models held in a dictionary. I have created a function which is called for setting the hints. First, a primary model is created and then I want to create different models, identical to the primary, but with different prefixes. The set_hints function accepts a parameter comp which defined what hints will be set. This is a simplified part of my code:
import lmfit
def foo (x, a):
return x + a
def set_hints(mod, comp="2"):
mod.set_param_hint("a", value=1, vary=True)
if comp == "2":
mod.set_param_hint("a", value=0, vary=False)
return mod.param_hints
m = lmfit.Model(foo)
models = {}
for i in range(2):
hints = set_hints(m, comp="2")
models["m%i" % i] = lmfit.Model(m.func, m.independent_vars,
prefix="m%i" %i,
param_names=m.param_names)
for par in m.param_names:
models["m%i" % i].param_hints[par] = hints[par]
# models["m%i" % i].param_hints = hints
for key in models.keys():
print key
print "value:"
print models[key].param_hints["a"]["value"]
print "vary:"
print models[key].param_hints["a"]["vary"]
which outputs:
m0
value:
1
vary:
True
m1
value:
0
vary:
False
Which doesn't make any sense to me! The value and vary hints should be 0 and False respectively in both cases. It is like at the second iteration of the loop, the condition comp == "2" of the set_hints function is not satisfied for the 1st iteration of the loop and the hints are changed retroactively! If I uncomment the commented line and not set the hints iteratively, the result is good. But what is happening now I find it completely absurd. Please help me understand what is happening!