-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Use warnings instead of print statements #14069
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
Conversation
Partially addresses: scipy#1953
I have tried the approach suggested by @mdhaber, i.e. creating a separate test with an expected failure, but passing a |
Interesting. Can you tell how many iterations are occurring when you give it |
@StefanoFrazzetto would you be interested in continuing this if I were to help fix merge conflicts? |
@mdhaber it's been a while! If it adds any value to the project, I'll try to find some time and help you getting this merged. By the way, while setting up my env, I took the chance to resolve the conflicts, so hopefully this won't take too much effort. Let me know how I can help. |
Hmm oops I didn't notice that gh-1953 was already resolved by gh-15259. This may not be needed anymore. The refactoring of the It looks like gh-15259 didn't change @AtsushiSakai can I get your opinion here, since you worked on this before? What should this PR do now that the main issue (gh-1953) is resolved? |
I realized now that #15259 fixed almost bare print statements for warnings, but the scipy/scipy/optimize/_optimize.py Lines 1441 to 1445 in 2f36c58
So, I think it would be great that this PR fixs these. I reopened #1953. |
@AtsushiSakai what warning class should be used? You used |
Yeah. I didn't know the |
In that case, do you think we should replace the RuntimeWarnings with OptimizeWarnnings, then? It's slightly backward incompatible, but I think if an email were sent to the mailing list suggesting the change, it probably wouldn't bother people. |
@AtsushiSakai I was hoping to get your opinion on #14069 (comment) when you have a moment. |
Let's go ahead and leave the existing You can test locally with: import numpy as np
from scipy import optimize
def f(x):
return np.sum(x**2)
method = 'newton-cg' # PR changed bfgs, cg, newton-cg, and powell
maxiter = 1000 # if 10, warning will be raised
options={'disp': True, 'maxiter': maxiter}
optimize.minimize(lambda x: optimize.rosen(x), [0, 0], method=method,
jac=optimize.rosen_der, options=options) @StefanoFrazzetto would you be willing to add unit tests that try to raise the warning and check that it occurs? Note: CirrusCI failures unrelated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
OK, I fixed two little bugs and added tests. refguide_check failure was related before; now it isn't. |
Reference issue
Partially addresses: #1953
What does this implement/fix?
Use
warnings.warn
instead of print statements in scipy/optimize/optimize.py, specifically withindef _minimize_bfgs
anddef _minimize_powell
Additional information
Related PR with partial fixes for another function: #14050
As discussed with @WarrenWeckesser, it might be worth having a look at changes in
scipy/optimize/tests/test_optimize.py
, to understand if those tests always generate a warning and, if that's the case, use something likewith pytest.warns(OptimizeWarning):
, instead of just filtering the warnings.