Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SciPy curve_fit runtime error, stopping iteration

I am using scipy.optimize.curve_fit() in an iterative way.

My problem is that when ever it is unable to fit the parameters the whole program (and thus the iteration) stops, this is the error it gives:

RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 800.

I understand that why it has been unable to fit. My problem is that is there any way I can write the program in Python 3.2.2 that will ignore such occurrences and just continue?

like image 825
makhlaghi Avatar asked Feb 07 '12 07:02

makhlaghi


1 Answers

You can use standard Python exception handling to trap the error raised by curve_fit in cases where the optimization fails to find a solution. So something like:

try:
    popt,pcov = scipy.optimize.curve_fit(f, xdata, ydata, p0=None, sigma=None)

except RuntimeError:
    print("Error - curve_fit failed")

That construct will let you catch and handle the error condition raised by curve_fit without having your program abort.

like image 93
talonmies Avatar answered Sep 18 '22 01:09

talonmies