-2

My code is too long and I don't want to solve this error, because of the flaws of original data.
The error is:
RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 50000.
This error come out when iterations = maxfev. What I want to do is: when the iterations = maxfev, don't shut down the code, but to continue operating next pack of data. For example:

if raise RuntimeError :
   data = data
else:
   data = data-1

Something like that.
I just don't want the program to stop.
I don't know did I say clearly? Ask me if you need any details.

lpr
  • 1
  • 3
  • `try: some_operation(); except RuntimeError: data = ...`…? Are you asking how to catch exceptions? – deceze Jan 11 '21 at 05:47
  • you would want to use [`try-except-finally`](https://docs.python.org/3/tutorial/errors.html#handling-exceptions) – anurag Jan 11 '21 at 05:48
  • Also, read up on: the [textbook](https://python-textbok.readthedocs.io/en/1.0/Errors_and_Exceptions.html#runtime-errors) – anurag Jan 11 '21 at 05:59

2 Answers2

0

You can do it like this.

try:
    somecode
except RuntimeError as err:
    print('error')
    data = data
    raise err
else:
    print('no error')
    data = data - 1
0

Yes, i have know the solution.

try:
    popt,pcov = curve_fit(gaussian,bins[1:],n)
except RuntimeError:
    hdu2 = hdu
else:
    plt.plot(bins,gaussian(bins,popt[0],popt[1],popt[2]))
    hdu2 = hdu-popt[1]
lpr
  • 1
  • 3