Extraction of variable percentages and other values from fit report

84 views
Skip to first unread message

Beet Root

unread,
Jul 11, 2023, 3:34:26 AM7/11/23
to lmfit-py
Hello,

This is my first conversation here so I hope I am doing this correctly. I don't have a specific code example to show, I am simply interested in how to know how to extract all values from the fit report.
The current value I am struggling with is the percentage of the standard deviation of the fitted centerpoint of a pseudo-Voigt fit compared to the value of the center. I will attach a fit report to illustrate further, the text marked with red background is what I am trying to extract:

"""
(100, 3000) gotcha i: 20 gotcha i: 40 [[Model]] (Model(pvoigt, prefix='psvo_') + Model(constant, prefix='con_')) [[Fit Statistics]] # fitting method = leastsq # function evals = 887 # data points = 15 # variables = 5 chi-square = 821231.503 reduced chi-square = 82123.1503 Akaike info crit = 173.657652 Bayesian info crit = 177.197903 R-squared = 0.98888572 [[Variables]] psvo_amplitude: 428759.332 +/- 4114896.39 (959.72%) (init = 80256.14) psvo_center: 21.9727742 +/- 102.511023 (466.54%) (init = 11) psvo_sigma: 16.2882819 +/- 37.7669367 (231.87%) (init = 3) psvo_fraction: 7.2527e-10 +/- 27.6672227 (3814746179333.35%) (init = 0.5) psvo_fwhm: 32.5765638 +/- 75.5338735 (231.87%) == '2.0000000*psvo_sigma' psvo_height: 12364.4870 +/- 2473137.12 (20001.94%) == '(((1-psvo_fraction)*psvo_amplitude)/max(1e-15, (psvo_sigma*sqrt(pi/log(2))))+(psvo_fraction*psvo_amplitude)/max(1e-15, (pi*psvo_sigma)))' con_c: 7313.17065 +/- 46640.3624 (637.76%) (init = 11161.56) [[Correlations]] (unreported correlations are < 0.250) C(psvo_amplitude, con_c) = -0.9987 C(psvo_fraction, con_c) = +0.9955 C(psvo_center, psvo_fraction) = +0.9945 C(psvo_amplitude, psvo_fraction) = -0.9901 C(psvo_center, con_c) = +0.9806 C(psvo_center, psvo_sigma) = +0.9785 C(psvo_amplitude, psvo_center) = -0.9701 C(psvo_sigma, psvo_fraction) = +0.9520 C(psvo_sigma, con_c) = +0.9192 C(psvo_amplitude, psvo_sigma) = -0.8998
"""

Additionally, it took me a very long time to figure out how to extract the actual standard deviation of the center of the fit (out.params['psvo_center'].stderr). Is there any sort of list or table anywhere of how to reach the different values printed in the fit report?
I figured the following link should be the place to find it: Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python (lmfit.github.io), but I am not able to find what I am looking for there. 

Matt Newville

unread,
Jul 11, 2023, 11:51:07 AM7/11/23
to lmfi...@googlegroups.com
Attaching a minimal example of what you are doing is always helpful. 

On Tue, Jul 11, 2023 at 2:34 AM Beet Root <cha...@live.se> wrote:
Hello,

This is my first conversation here so I hope I am doing this correctly. I don't have a specific code example to show, I am simply interested in how to know how to extract all values from the fit report.

Well, without example code, we are not sure how you got the fit report.
The current value I am struggling with is the percentage of the standard deviation of the fitted centerpoint of a pseudo-Voigt fit compared to the value of the center. I will attach a fit report to illustrate further, the text marked with red background is what I am trying to extract:

It looks like your (unfortunately formatted) report is from a Model fit, quite likely doing something like

     result = my_model.fit(ydata, params, ...)
     print(result.fit_report())

The `result` there is a ModelResult (https://lmfit.github.io/lmfit-py/model.html#the-modelresult-class).  It has several methods, including `result.fit_report()`, and many attributes.  One of those attributes will be `result.params`, which is a Parameters object:  a dictionary with keys of Parameter names, and values of Parameter objects.  You almost certainly created a Parameters object before doing the fit.

After a fit, each Parameter will have several attributes (https://lmfit.github.io/lmfit-py/parameters.html#the-parameter-class).    The "percent" value shown in the " "abs(Parameter.stderr/Parameter.value)" as a percentage:

          f'({abs(par.stderr/par.value):.2%})'



"""
(100, 3000) gotcha i: 20 gotcha i: 40 [[Model]] (Model(pvoigt, prefix='psvo_') + Model(constant, prefix='con_')) [[Fit Statistics]] # fitting method = leastsq # function evals = 887 # data points = 15 # variables = 5 chi-square = 821231.503 reduced chi-square = 82123.1503 Akaike info crit = 173.657652 Bayesian info crit = 177.197903 R-squared = 0.98888572 [[Variables]] psvo_amplitude: 428759.332 +/- 4114896.39 (959.72%) (init = 80256.14) psvo_center: 21.9727742 +/- 102.511023 (466.54%) (init = 11) psvo_sigma: 16.2882819 +/- 37.7669367 (231.87%) (init = 3) psvo_fraction: 7.2527e-10 +/- 27.6672227 (3814746179333.35%) (init = 0.5) psvo_fwhm: 32.5765638 +/- 75.5338735 (231.87%) == '2.0000000*psvo_sigma' psvo_height: 12364.4870 +/- 2473137.12 (20001.94%) == '(((1-psvo_fraction)*psvo_amplitude)/max(1e-15, (psvo_sigma*sqrt(pi/log(2))))+(psvo_fraction*psvo_amplitude)/max(1e-15, (pi*psvo_sigma)))' con_c: 7313.17065 +/- 46640.3624 (637.76%) (init = 11161.56) [[Correlations]] (unreported correlations are < 0.250) C(psvo_amplitude, con_c) = -0.9987 C(psvo_fraction, con_c) = +0.9955 C(psvo_center, psvo_fraction) = +0.9945 C(psvo_amplitude, psvo_fraction) = -0.9901 C(psvo_center, con_c) = +0.9806 C(psvo_center, psvo_sigma) = +0.9785 C(psvo_amplitude, psvo_center) = -0.9701 C(psvo_sigma, psvo_fraction) = +0.9520 C(psvo_sigma, con_c) = +0.9192 C(psvo_amplitude, psvo_sigma) = -0.8998
"""

Additionally, it took me a very long time to figure out how to extract the actual standard deviation of the center of the fit (out.params['psvo_center'].stderr). Is there any sort of list or table anywhere of how to reach the different values printed in the fit report?
I figured the following link should be the place to find it: Modeling Data and Curve Fitting — Non-Linear Least-Squares Minimization and Curve-Fitting for Python (lmfit.github.io), but I am not able to find what I am looking for there. 

Yes, the documentation lists all of the attributes of `ModelResult`.  Several of the examples also access the attributes and properties of Parameter objects.    
If you are interested in more details of what `fit_report()` is doing, you can also read the source code, for example at https://github.com/lmfit/lmfit-py/blob/master/lmfit/printfuncs.py#L84


Reply all
Reply to author
Forward
0 new messages