Reputation: 495
How can I turn off this error using the SciPy curve fitting function fitting a Gaussian curve? In other words, if it doesn't fit a model peak then its not a peak so I don't want to return anything. Also, are there faster approaches? curve_fit may be too slow for my application looking at huge data.
RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 800.
from scipy.optimize import curve_fit
from scipy import asarray as ar,exp
import matplotlib.pyplot as plt
from numpy import sqrt, pi, exp, loadtxt
data = loadtxt('data/model1d_gauss.dat')
x = data[:, 0]
y = data[:, 1]
n = len(x) #the number of data
mean = sum(x*y)/n #note this correction
sigma = sum(y*(x-mean)**2)/n #note this correction
def gaus(x,a,x0,sigma):
return a*exp(-(x-x0)**2/(2*sigma**2))
def gaussian(x, amp, cen, wid):
"1-d gaussian: gaussian(x, amp, cen, wid)"
return (amp/(sqrt(2*pi)*wid)) * exp(-(x-cen)**2 /(2*wid**2))
popt,pcov = curve_fit(gaus,x,y,p0=[1,mean,sigma])
#popt,pcov = curve_fit(gaussian,x,y,p0=[5,1,1])
plt.plot(x,y,'bo:',label='data')
plt.plot(x,gaus(x,*popt),'ro:',label='fit')
plt.legend()
plt.show()`enter code here`
Upvotes: 2
Views: 2115
Reputation:
To handle the RuntimeError, use a try-except block:
try:
popt,pcov = curve_fit(gaus,x,y,p0=[1,mean,sigma])
except RuntimeError:
print("No fit found") # whatever you want to do here
Some ways to reduce the running time:
maxfev
, so that the routine will fail faster: e.g., curve_fit(gaus, x, y, p0=[1,0,1], maxfev=400)
Upvotes: 3