Fitting performance

44 views
Skip to first unread message

Antonio Bulgheroni (toto)

unread,
Feb 2, 2024, 3:36:49 PMFeb 2
to lmfit-py
dear all, 

first of all thanks a lot for this beautiful fitting tool. I used to work with scipy curve_fit and I find lmfit much more user friendly and easy to use. 

I moved from curve_fit to lmfit because of the simplicity of combining models. 

I need to fit several thousands of 'rectangle' function. Initially I was using curve_fit and a basic combination of erfs. 

From the physical point of view, the experimental curve is only in the first approximation an erf, and one should also include some additional 'shoulders' on both sides of the rectangle to account for the real physics. 

I have no idea which function to use to model these extra-shoulders and I decided to use a skewed gaussian. And while I was coding the new fitting function for curve_fit, I got to know that with lmfit, I just need to sum three predefined models. 

Actually I had to add some constrain on the relative positions of the various curve, but reading the doc, was rather easy.

The price to pay is that with this more advanced model, the fitting procedure is, for some profile, taking much longer than with the simple rectangle. In some cases even a few seconds. The fit is anyhow successful so I don't complain. 

I though that the reason for this long calculation could be related to the not optimal definition of the initial values, but not much. Instead I have seen that if I set some min and max value for some parameters in order to reduce their range, the computational time is increasing instead of decreasing.

Do you have any advice on how to improve the fitting performance? 

Thanks again,
toto

Matt Newville

unread,
Feb 2, 2024, 6:54:27 PMFeb 2
to lmfi...@googlegroups.com
Hi Toto,

It is difficult to give detailed responses without real example code
to see what you are doing, A few general comments would include:

In general, lmfit does not prioritize run-time. We don't
intentionally make things slow, but it is not something we emphasize.

I would not expect that setting bounds would improve performance,
though it certainly could prevent exploring parts of parameter space.

Better initial values can sometimes help improve run-time performance
for a fit, but getting those improved guesses might kill performance
;)

Since you are fitting hundreds or thousands of spectra, is it
reasonable to do multiple fits in parallel, say one per core? I think
that might be the easiest and cheapest route to improved performance.

I hope that helps.

--Matt

Antonio Bulgheroni

unread,
Feb 3, 2024, 8:25:12 AMFeb 3
to lmfi...@googlegroups.com
Dear Matt, 

thanks for your quick reply. Unfortunately I am running the code inside of a rather complicated infrastructure, so it is not so easy to copy paste here a minimalist working example. I will try to extract something that is reproducing the effect and also attaching two datasets, one 'fast' and one 'slow'. It will take a bit, so don't hold your breath! 

Meanwhile, the suggestion to run the code in parallel over multiple cores is smart, even if it requires a bit of coding. I will try to see what I can put together! 

Thanks again
toto

-----------------------------------------
Antonio Bulgheroni, PhD





--
You received this message because you are subscribed to a topic in the Google Groups "lmfit-py" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lmfit-py/j4O8J3PeLxc/unsubscribe.
To unsubscribe from this group and all its topics, 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/CA%2B7ESbrzH2N1BH-XoFMn87Wr85nBRXTM7v2t4tjxNDPM4-x0rw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages