Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed an error to warning for failure to optimise parameters in curve fit #13442

Closed
wants to merge 8 commits into from

Conversation

jagoosw
Copy link

@jagoosw jagoosw commented Jan 27, 2021

ENH: adjusted error conditions for too many iterations to optimise parameters in scipy optimise curve fit.

What does this implement/fix?

Often when the error "Optimal parameters not found: Number of calls to function has reached maxfev = ..." is given perfectly usable results have been produced so raising an error is unhelpful so I have changed it to message (I would have preferred to use the warnings module but it appears to just not be showing anything).

Additional information

This was also complained about here, here and others.

Changed error when too many many iterations have failed to optimise the parameters to a warning and returning the results since they will often be valid.
@@ -785,7 +785,10 @@ def curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False,
popt, pcov, infodict, errmsg, ier = res
ysize = len(infodict['fvec'])
cost = np.sum(infodict['fvec'] ** 2)
if ier not in [1, 2, 3, 4]:
if ier==5:
print("Optimal parameters not found: " + errmsg + " This can be increased by passing the parameter maxfev=n.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick note about two things:

  1. Understandable that sometimes Python warnings don't show up, but perhaps that is something we can help get working. I just grepped through the code a bit and I do see class OptimizeWarning(UserWarning) defined and used through the optimize machinery. Perhaps one of the optimize regulars can comment on using that, along with a filter if one is needed.

  2. Note that this part of the code is not covered by tests (codecov shows 0 % coverage on the diff). May be an opportunity to encode the desired warnings and exceptions situations in actual unit tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help.

When I set warnings.simplefilter('always') just before curve_fit then it shows and another warning about not being able to find the covariance. I tried going through how warnings is set up but couldn't find anywhere where they're filtered yet.

I have also now added a test but don't have much experience of unit testing so not sure its right/tests for the right stuff. I don't know if I need to change something else too to add it properly, I don't really understand the workflow but happy to try and work it out more.

Added test to check that warning is raised when maxfev is exceeded in curve_fit
@jagoosw
Copy link
Author

jagoosw commented Jan 27, 2021

Okay it seems I've broken something although the tests that failed seem to be unrelated to my changes
All seems to pass now

jagoosw and others added 2 commits February 1, 2021 23:50
Failed tests seem to be unrelated to my changes so trying to prompt them to re run
@mdhaber
Copy link
Contributor

mdhaber commented Sep 29, 2022

@jagoosw I resolved merge conflicts for you. If you're happy with what this does now, please send an email to the mailing list proposing the change, mentioning the possible concern that users might miss the warning and think that the returned solution is optimal, and asking for opinions about whether the benefits outweigh the risks.

What do you think @tylerjereddy?

@mdhaber
Copy link
Contributor

mdhaber commented Oct 8, 2022

Oops that push was accidental. In the future, it's best to create a new feature branch rather than working in master. Merging main one more time to re-run tests to make sure everything is OK.

@mdhaber
Copy link
Contributor

mdhaber commented Oct 15, 2022

I haven't heard back, and I cannot champion this change, so I'll go ahead and close this one.

I would support making this change in conjunction with:

  • deprecating full_output,
  • always returning all output in a bunch object, and
  • adding a success attribute to indicate whether fitting was successful or not.

But that would be a more substantial PR.

@mdhaber mdhaber closed this Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants