im281
im281

Reputation: 495

RuntimeError using SciPy curve fitting library with a large data set

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

Answers (1)

user6655984
user6655984

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:

  • Reduce the maximal value of function calls maxfev, so that the routine will fail faster: e.g., curve_fit(gaus, x, y, p0=[1,0,1], maxfev=400)
  • Sample your data points. If you have 10000 points, pick 1000 of them at random, and find that there is a Gaussian curve that fits them well, it will probably fit well to the rest of data points. Or at least it will make a good starting point for subsequent refinement of parameters with the full data set.

Upvotes: 3

Related Questions