Error while using lmfit-fit class

84 views
Skip to first unread message

Shankar Dutt

unread,
Apr 24, 2020, 6:20:49 PM4/24/20
to lmfit-py
Hi All,

I am more used to use minimize-function of lmfit but now I am thinking to use fit-class because of ease of use. Please see the following code and let me know what mistake am I making?

from lmfit import Minimizer, Parameters, fit_report, minimize,  Model, report_fit

filepath = "FILE PATH"
data = np.loadtxt(filepath)
q = data[:, 0]
r = data[:, 1]
s = data[:, 2]

p = Parameters()

p.add('Amplitude', value=1, vary=True)
p.add('Radius', value=40, vary=True)
p.add('Polydispersity', value=0.1, vary=True)
p.add('Background', value=0, vary=False)
p.add('Roughness', value=0, vary=False)

def mdl(x,p):
    amp = p['Amplitude']
    radius = p['Radius']
    pd = p['Polydispersity']
    background = p['Background']
    roughness = p['Roughness']
    return mdls.hc_r_model(x, amp, background, radius, pd, roughness, wavelength)

fmodel = Model(mdl)
result = fmodel.fit(r, params=p, x=q)
fr = result.fit_report()
print(fr)
plt.loglog(q, r,  label='DATA',**marker_style,linewidth=0.7)
plt.loglog(q, result.best_fit, 'r', label='FIT',linewidth=1.5)
plt.xlabel(r'q', fontdict=font)
plt.ylabel(r'I', fontdict=font)
plt.title('Hard Cylinder Model', fontdict=font)
plt.legend()

I am getting the following error:

ValueError: Assign each parameter an initial value by passing Parameters or keyword arguments to fit.
Missing parameters: ['p']
Non initialized parameters: []


Please help :)

Matt Newville

unread,
Apr 24, 2020, 6:40:15 PM4/24/20
to lmfit-py

A model function for Model() is different from an objective function for Minimize().
For a model function, each function argument to the model function is expected to be an independent variable or a Parameter, so you would do something more like

    def mdl(x, amplitude, background, radius, polydispersity, roughness, wavelength):
        return mdls.hc_r_model(x, amplitude, background, radius, polydispersity, roughness, wavelength)

or just have `mdls.hc_r_model` be your model function.  Then do:

    fmodel = Model(mdl)  # or Model(mdls_hc_r_model)
    params = fmodel.make_params(amplitude=1, background=0, radius=40,  polydispersity=0.1, roughness=0)
    params['background'].vary = False
    params['roughness'].vary = False

(not sure what to do about 'wavelength' - is it meant to be a parameter or an independent variable).   Anyway, then do
 
    result = fmodel.fit(r, params, x=q)

If wavelength is an independent variable, read about setting that in the docs.

--Matt


--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/c8349e6f-6eec-4572-bfc4-63948b11bfa7%40googlegroups.com.


Shankar Dutt

unread,
Apr 24, 2020, 6:48:48 PM4/24/20
to lmfit-py
Thanks a lot for your reply and yes wavelength is an independent variable. Actually, I really like the parameter class(the way I used to add parameters). Is there no way to use those defined parameters with model function?
To unsubscribe from this group and stop receiving emails from it, send an email to lmfi...@googlegroups.com.

Matt Newville

unread,
Apr 24, 2020, 7:50:10 PM4/24/20
to lmfit-py
On Fri, Apr 24, 2020 at 5:48 PM Shankar Dutt <shankar...@gmail.com> wrote:
Thanks a lot for your reply and yes wavelength is an independent variable. Actually, I really like the parameter class(the way I used to add parameters). Is there no way to use those defined parameters with model function?

You can create a Parameters object explicitly, as you did.  The Model.make_params() creates a Parameters object with the needed parameters (and, like, correctly spelled) based on the arguments to the model function, and takes initial values.  The two approaches can be mixed. 

An important feature is that the model function itself does not use a lmfit.Parameters() object but just takes values and computes the result.   That means that just about many more functions can be used as model functions. 

--Matt
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/8e38833f-4afd-485b-a5d8-9e11adf6d1d7%40googlegroups.com.

Laurence Lurio

unread,
Apr 24, 2020, 9:30:10 PM4/24/20
to lmfi...@googlegroups.com
I don't think "p" should be the parameter.  You need to write the function as 

def mdl(x,amp, radius, pd, background, roughness):

--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages