1

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!

iasonas
  • 895
  • 7
  • 12

1 Answers1

0

The code seems very weird, but I assume it comes from a larger design. I think this must be a bug, though I'm not certain what that is. I will create an Issue on the lmfit github site.

M Newville
  • 7,486
  • 2
  • 16
  • 29
  • Hay thanks for your comment, Indeed it looks very weird the code, since I made a simplified version of my whole code. I have made a global fitting script which fits a different number of decaying components to transient spectroscopy data. This is why I need a keyword to set the parameter hints according to number of components. The code works fine now (when using the commented line above) and the results are good. Its just that this problem messed with my head for 2 days straight. – iasonas Mar 13 '16 at 18:55
  • Btw I checked the issue you raised at github. Thank you for simplifying my code. I was so tired to think of a simpler version before posting it :) – iasonas Mar 13 '16 at 19:38
  • I believe this is now fixed in the latest master branch on github. – M Newville Mar 14 '16 at 02:33
  • 1
    That's great to hear. Thanks. And very good job btw with the LMFIT package! – iasonas Mar 14 '16 at 18:00