XaBla
XaBla

Reputation: 43

How to solve NaN values error using Lmfit with Python

I'm trying to fit a set of data taken by an external simulation, and stored in a vector, with the Lmfit library.

Below there's my code:

import numpy as np
import matplotlib.pyplot as plt
from lmfit import Model
from lmfit import Parameters

def DGauss3Par(x,I1,sigma1,sigma2):
    I2 = 2.63 - I1
    return (I1/np.sqrt(2*np.pi*sigma1))*np.exp(-(x*x)/(2*sigma1*sigma1)) + (I2/np.sqrt(2*np.pi*sigma2))*np.exp(-(x*x)/(2*sigma2*sigma2))

#TAKE DATA
xFull = []
yFull = []

fileTypex = np.dtype([('xFull', np.float)])
fileTypey = np.dtype([('yFull', np.float)])
fDatax = "xValue.dat"
fDatay = "yValue.dat"
xFull = np.loadtxt(fDatax, dtype=fileTypex)
yFull = np.loadtxt(fDatay, dtype=fileTypey)

xGauss = xFull[:]["xFull"]
yGauss = yFull[:]["yFull"]

#MODEL'S DEFINITION
gmodel = Model(DGauss3Par)
params = Parameters()
params.add('I1', value=1.66)
params.add('sigma1', value=1.04)
params.add('sigma2', value=1.2)
result3 = gmodel.fit(yGauss, x=xGauss, params=params)

#PLOTS
plt.plot(xGauss, result3.best_fit, 'y-')
plt.show()

When I run it, I get this error:

File "Overlap.py", line 133, in <module>
    result3 = gmodel.fit(yGauss, x=xGauss, params=params)
ValueError: The input contains nan values

These are the values of the data contained in the vector xGauss (related to the x axis):

[-3.88 -3.28 -3.13 -3.08 -3.03 -2.98 -2.93 -2.88 -2.83 -2.78 -2.73 -2.68
 -2.63 -2.58 -2.53 -2.48 -2.43 -2.38 -2.33 -2.28 -2.23 -2.18 -2.13 -2.08
 -2.03 -1.98 -1.93 -1.88 -1.83 -1.78 -1.73 -1.68 -1.63 -1.58 -1.53 -1.48
 -1.43 -1.38 -1.33 -1.28 -1.23 -1.18 -1.13 -1.08 -1.03 -0.98 -0.93 -0.88
 -0.83 -0.78 -0.73 -0.68 -0.63 -0.58 -0.53 -0.48 -0.43 -0.38 -0.33 -0.28
 -0.23 -0.18 -0.13 -0.08 -0.03  0.03  0.08  0.13  0.18  0.23  0.28  0.33
  0.38  0.43  0.48  0.53  0.58  0.63  0.68  0.73  0.78  0.83  0.88  0.93
  0.98  1.03  1.08  1.13  1.18  1.23  1.28  1.33  1.38  1.43  1.48  1.53
  1.58  1.63  1.68  1.73  1.78  1.83  1.88  1.93  1.98  2.03  2.08  2.13
  2.18  2.23  2.28  2.33  2.38  2.43  2.48  2.53  2.58  2.63  2.68  2.73
  2.78  2.83  2.88  2.93  2.98  3.03  3.08  3.13  3.28  3.88]

And these ones the ones in the vector yGauss (related to y axis):

[0.00173977 0.00986279 0.01529543 0.0242624  0.0287456  0.03238484
 0.03285927 0.03945234 0.04615091 0.05701618 0.0637672  0.07194268
 0.07763934 0.08565687 0.09615262 0.1043281  0.11350606 0.1199406
 0.1260062  0.14093328 0.15079665 0.16651464 0.18065023 0.1938894
 0.2047541  0.21794024 0.22806706 0.23793043 0.25164404 0.2635118
 0.28075974 0.29568682 0.30871501 0.3311846  0.34648062 0.36984661
 0.38540666 0.40618835 0.4283945  0.45002014 0.48303911 0.50746062
 0.53167057 0.5548792  0.57835128 0.60256181 0.62566436 0.65704847
 0.68289386 0.71332794 0.73258027 0.769608   0.78769989 0.81407275
 0.83358852 0.85210239 0.87109068 0.89456217 0.91618782 0.93760247
 0.95680234 0.96919757 0.9783219  0.98486193 0.9931429  0.9931429
 0.98486193 0.9783219  0.96919757 0.95680234 0.93760247 0.91618782
 0.89456217 0.87109068 0.85210239 0.83358852 0.81407275 0.78769989
 0.769608   0.73258027 0.71332794 0.68289386 0.65704847 0.62566436
 0.60256181 0.57835128 0.5548792  0.53167057 0.50746062 0.48303911
 0.45002014 0.4283945  0.40618835 0.38540666 0.36984661 0.34648062
 0.3311846  0.30871501 0.29568682 0.28075974 0.2635118  0.25164404
 0.23793043 0.22806706 0.21794024 0.2047541  0.1938894  0.18065023
 0.16651464 0.15079665 0.14093328 0.1260062  0.1199406  0.11350606
 0.1043281  0.09615262 0.08565687 0.07763934 0.07194268 0.0637672
 0.05701618 0.04615091 0.03945234 0.03285927 0.03238484 0.0287456
 0.0242624  0.01529543 0.00986279 0.00173977]

I've also tried to print the values returned by my function, to see if there really were some NaN values:

params = Parameters()
params.add('I1', value=1.66)
params.add('sigma1', value=1.04)
params.add('sigma2', value=1.2)
func = DGauss3Par(xGauss,I1,sigma1,sigma2)
print func

but what I obtained is:

[0.04835225 0.06938855 0.07735839 0.08040181 0.08366964 0.08718237
 0.09096169 0.09503048 0.0994128  0.10413374 0.10921938 0.11469669
 0.12059333 0.12693754 0.13375795 0.14108333 0.14894236 0.15736337
 0.16637406 0.17600115 0.18627003 0.19720444 0.20882607 0.22115413
 0.23420498 0.24799173 0.26252377 0.27780639 0.29384037 0.3106216
 0.32814069 0.34638266 0.3653266  0.38494543 0.40520569 0.42606735
 0.44748374 0.46940149 0.49176057 0.51449442 0.5375301  0.56078857
 0.58418507 0.60762948 0.63102687 0.65427809 0.6772804  0.69992818
 0.72211377 0.74372824 0.76466232 0.78480729 0.80405595 0.82230355
 0.83944875 0.85539458 0.87004937 0.88332762 0.89515085 0.90544838
 0.91415806 0.92122688 0.92661155 0.93027889 0.93220625 0.93220625
 0.93027889 0.92661155 0.92122688 0.91415806 0.90544838 0.89515085
 0.88332762 0.87004937 0.85539458 0.83944875 0.82230355 0.80405595
 0.78480729 0.76466232 0.74372824 0.72211377 0.69992818 0.6772804
 0.65427809 0.63102687 0.60762948 0.58418507 0.56078857 0.5375301
 0.51449442 0.49176057 0.46940149 0.44748374 0.42606735 0.40520569
 0.38494543 0.3653266  0.34638266 0.32814069 0.3106216  0.29384037
 0.27780639 0.26252377 0.24799173 0.23420498 0.22115413 0.20882607
 0.19720444 0.18627003 0.17600115 0.16637406 0.15736337 0.14894236
 0.14108333 0.13375795 0.12693754 0.12059333 0.11469669 0.10921938
 0.10413374 0.0994128  0.09503048 0.09096169 0.08718237 0.08366964
 0.08040181 0.07735839 0.06938855 0.04835225]

So it doesn't seems that there are NaN values, I'm not understanding for which reason it returns me that error. Could anyone help me, please? Thanks!

Upvotes: 0

Views: 1384

Answers (1)

9769953
9769953

Reputation: 12221

If you add a print function to your fit function, printing out sigma1 and sigma2, you'll find that

  • DGauss3Par is evaluated already a few times before the error occurs.
  • Both sigma variables have a negative value at the time the error occurs.

Taking the square root of a negative value causes, of course, a NaN.

You should add a min bound or similar to your sigma1 and sigma2 parameters to prevent this. Using min=0.0 as an additional argument to params.add(...) will result in a good fit.

Be aware that for some analyses, setting explicit bounds to your fitting parameters may make these analyses invalid. For most cases, you'll be fine, but for some cases, you'll need to check whether the fitting parameters should be allowed to vary from negative infinity to positive infinity, or are allowed to be bounded.

Upvotes: 2

Related Questions