1

I am trying to work through an example using lmfit from the documentation website:

import sys

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.backends.backend_pdf import PdfPages

from lmfit import minimize, Parameters, Parameter, report_fit, Minimizer, conf_interval, conf_interval2d, printfuncs
from lmfit import Model

def main():
    def decay(t, N, tau):
        return N*np.exp(-t/tau)

    decay_model = Model(decay)
    print decay_model.independent_vars
    for pname, par in decay_model.params.items():
        print pname, par

if ___name___ == "___main___":
    main()

When I execute the last command, I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Model' object has no attribute 'params'

It seems that params is not an attribute of Model. Can someone explain why this code does not work? I tried this on multiple computers and versions of Python. Running on Python 2.7.9 32 bit, Windows 7.

grover
  • 927
  • 1
  • 10
  • 21
  • Could you post your entire code including your import statement? Which version of lmfit are you using? – Cleb Jan 19 '16 at 15:48
  • I updated the post so you can see the full code. I downloaded lmfit from github here: https://github.com/lmfit/lmfit-py – grover Jan 19 '16 at 20:18
  • Ok, I added a minimal example below which hopefully helps you. Since you do not provide any data, I generated some fake data which you can easily replace by the actual ones. Hope that helps! – Cleb Jan 19 '16 at 21:44
  • Could you get it working? – Cleb Jan 22 '16 at 09:17

1 Answers1

1

Things that might cause problems are - assuming this is really your entire code - that you do not use any data for the fit and you do not initialize your parameter dictionary anywhere. Below is a minimal example which works fine for version 0.8.3 (see the output below); if you use 0.9.x you will have to adjust one part a bit (check here which changes have been made from 0.8.3 to 0.9.x).

import numpy as np
import matplotlib.pyplot as plt
from lmfit import minimize, Parameters, Parameter, report_fit

# generate some data with noise
# replace xData and yData with your data
xData = np.linspace(0., 100., 50.)
Nf = 5.
tauf = 6.5
yData = Nf * np.exp(-xData / tauf) + np.random.normal(0, 0.5, len(xData))
# plt.plot(xData, yData, 'bo')
# plt.show()

def decay(params, x, data):

    N = params['N'].value
    tau = params['tau'].value

    model = N * np.exp(-x/tau)
    return model - data # that's what you want to minimize

# create a set of Parameters
params = Parameters()
params.add('N', value=10) # value is the initial value
params.add('tau', value=8.)

# do fit, here with leastsq model
result = minimize(decay, params, args=(xData, yData))

# calculate final result
final = yData + result.residual

# write error report
report_fit(params)

# plot the data
plt.plot(xData, yData, 'bo')
plt.plot(xData, final, 'r')
plt.show()

First, I create some data using your decay function and add some noise to it; this part should be replaced by your data. The remaining part is straightforward and you get the following output (the exact values might differ since noise is added to the data):

[[Variables]]
    N:     5.15685000 +/- 0.419115 (8.13%) (init= 10)
    tau:   6.58557758 +/- 0.877337 (13.32%) (init= 8)

As you can see, the determined parameters are very close to the ones I chose for the data generation. Plotting the data and the fit gives the following:

enter image description here

The nice thing about lmfit is that you can use boundaries for your parameter values. You can check the example here.

Hope that gets you started and let me know if you have any additional questions.

Community
  • 1
  • 1
Cleb
  • 25,102
  • 20
  • 116
  • 151