Skip to content

Commit

Permalink
Merge pull request #4570 from nmayorov/filter_convergence_warnings
Browse files Browse the repository at this point in the history
MNT: Suppress LineSearchWarning's in scipy.optimize tests
  • Loading branch information
dlax committed Mar 2, 2015
2 parents fe74166 + 267e1d8 commit b3a3f94
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
9 changes: 6 additions & 3 deletions scipy/optimize/optimize.py
Expand Up @@ -35,7 +35,8 @@
vectorize, asarray, sqrt, Inf, asfarray, isinf)
import numpy as np
from .linesearch import (line_search_wolfe1, line_search_wolfe2,
line_search_wolfe2 as line_search)
line_search_wolfe2 as line_search,
LineSearchWarning)


# standard status messages of optimizers
Expand Down Expand Up @@ -693,8 +694,10 @@ def _line_search_wolfe12(f, fprime, xk, pk, gfk, old_fval, old_old_fval,

if ret[0] is None:
# line search failed: try different one.
ret = line_search_wolfe2(f, fprime, xk, pk, gfk,
old_fval, old_old_fval)
with warnings.catch_warnings():
warnings.simplefilter('ignore', LineSearchWarning)
ret = line_search_wolfe2(f, fprime, xk, pk, gfk,
old_fval, old_old_fval)

if ret[0] is None:
raise _LineSearchError()
Expand Down
11 changes: 8 additions & 3 deletions scipy/optimize/tests/test_linesearch.py
Expand Up @@ -3,9 +3,12 @@
"""
from __future__ import division, print_function, absolute_import

import warnings

from numpy.testing import assert_, assert_equal, \
assert_array_almost_equal, assert_array_almost_equal_nulp
import scipy.optimize.linesearch as ls
from scipy.optimize.linesearch import LineSearchWarning
import numpy as np


Expand Down Expand Up @@ -190,9 +193,11 @@ def test_line_search_wolfe2(self):
f0 = f(x)
g0 = fprime(x)
self.fcount = 0
s, fc, gc, fv, ofv, gv = ls.line_search_wolfe2(f, fprime, x, p,
g0, f0, old_f,
amax=smax)
with warnings.catch_warnings():
warnings.simplefilter('ignore', LineSearchWarning)
s, fc, gc, fv, ofv, gv = ls.line_search_wolfe2(f, fprime, x, p,
g0, f0, old_f,
amax=smax)
assert_equal(self.fcount, fc+gc)
assert_fp_equal(ofv, f(x))
assert_fp_equal(fv, f(x + s*p))
Expand Down
5 changes: 2 additions & 3 deletions scipy/optimize/tests/test_optimize.py
Expand Up @@ -527,9 +527,9 @@ def dfunc(z):
else:
jac = dfunc

sol1 = optimize.minimize(func, [1,1], jac=jac, tol=1e-10,
sol1 = optimize.minimize(func, [1, 1], jac=jac, tol=1e-10,
method=method)
sol2 = optimize.minimize(func, [1,1], jac=jac, tol=1.0,
sol2 = optimize.minimize(func, [1, 1], jac=jac, tol=1.0,
method=method)
assert_(func(sol1.x) < func(sol2.x),
"%s: %s vs. %s" % (method, func(sol1.x), func(sol2.x)))
Expand Down Expand Up @@ -915,4 +915,3 @@ def test_attributes_present(self):

if __name__ == "__main__":
run_module_suite()

0 comments on commit b3a3f94

Please sign in to comment.