Skip to content

Commit

Permalink
PY23: optimize: callable() + exec + builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
pv committed Jan 5, 2013
1 parent 155cd94 commit c1cbb7d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
6 changes: 4 additions & 2 deletions scipy/optimize/_minimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from warnings import warn

from numpy import any

from scipy.lib.six import callable

# unconstrained minimization
from .optimize import (_minimize_neldermead, _minimize_powell, _minimize_cg,
_minimize_bfgs, _minimize_newtoncg,
Expand All @@ -26,7 +29,6 @@
from .tnc import _minimize_tnc
from .cobyla import _minimize_cobyla
from .slsqp import _minimize_slsqp
import collections

def minimize(fun, x0, args=(), method='BFGS', jac=None, hess=None,
hessp=None, bounds=None, constraints=(), tol=None,
Expand Down Expand Up @@ -315,7 +317,7 @@ def minimize(fun, x0, args=(), method='BFGS', jac=None, hess=None,
RuntimeWarning)

# fun also returns the jacobian
if not isinstance(jac, collections.Callable):
if not callable(jac):
if bool(jac):
fun = MemoizeJac(fun)
jac = fun.derivative
Expand Down
5 changes: 3 additions & 2 deletions scipy/optimize/_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

import numpy as np

from scipy.lib.six import callable

from warnings import warn

from .optimize import MemoizeJac, Result, _check_unknown_options
from .minpack import _root_hybr, leastsq
from . import nonlin
import collections

def root(fun, x0, args=(), method='hybr', jac=None, tol=None, callback=None,
options=None):
Expand Down Expand Up @@ -146,7 +147,7 @@ def root(fun, x0, args=(), method='hybr', jac=None, tol=None, callback=None,
RuntimeWarning)

# fun also returns the jacobian
if not isinstance(jac, collections.Callable) and meth in ('hybr', 'lm'):
if not callable(jac) and meth in ('hybr', 'lm'):
if bool(jac):
fun = MemoizeJac(fun)
jac = fun.derivative
Expand Down
6 changes: 3 additions & 3 deletions scipy/optimize/cobyla.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"""

import numpy as np
from scipy.lib.six import callable
from scipy.optimize import _cobyla
from .optimize import Result, _check_unknown_options
from warnings import warn
import collections

__all__ = ['fmin_cobyla']

Expand Down Expand Up @@ -138,14 +138,14 @@ def fmin_cobyla(func, x0, cons, args=(), consargs=None, rhobeg=1.0, rhoend=1e-4,
try:
m = len(cons)
except TypeError:
if isinstance(cons, collections.Callable):
if callable(cons):
m = 1
cons = [cons]
else:
raise TypeError(err)
else:
for thisfunc in cons:
if not isinstance(thisfunc, collections.Callable):
if not callable(thisfunc):
raise TypeError(err)

if consargs is None:
Expand Down
5 changes: 3 additions & 2 deletions scipy/optimize/nonlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def residual(P):

import sys
import numpy as np
from scipy.lib.six import callable, exec_
from scipy.linalg import norm, solve, inv, qr, svd, lstsq, LinAlgError
from numpy import asarray, dot, vdot
import scipy.sparse.linalg
Expand Down Expand Up @@ -588,7 +589,7 @@ def asjacobian(J):
setup=getattr(J, 'setup'),
dtype=J.dtype,
shape=J.shape)
elif isinstance(J, collections.Callable):
elif callable(J):
# Assume it's a function J(x) that returns the Jacobian
class Jac(Jacobian):
def update(self, x, F):
Expand Down Expand Up @@ -1492,7 +1493,7 @@ def %(name)s(F, xin, iter=None %(kw)s, verbose=False, maxiter=None,
kwkw=kwkw_str)
ns = {}
ns.update(globals())
exec(wrapper, ns)
exec_(wrapper, ns)
func = ns[name]
func.__doc__ = jac.__doc__
_set_doc(func)
Expand Down
7 changes: 4 additions & 3 deletions scipy/optimize/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@

import warnings
import numpy
from scipy.lib.six import callable
from numpy import atleast_1d, eye, mgrid, argmin, zeros, shape, \
squeeze, vectorize, asarray, absolute, sqrt, Inf, asfarray, isinf
from .linesearch import \
line_search_BFGS, line_search_wolfe1, line_search_wolfe2, \
line_search_wolfe2 as line_search
import collections


# standard status messages of optimizers
Expand Down Expand Up @@ -143,7 +143,8 @@ def is_array_scalar(x):
return len(atleast_1d(x) == 1)

abs = absolute
import builtins

from scipy.lib.six.moves import builtins
pymin = builtins.min
pymax = builtins.max
__version__ = "0.7"
Expand Down Expand Up @@ -2253,7 +2254,7 @@ def _scalarfunc(*params):
if (N == 1):
grid = grid[0]
xmin = xmin[0]
if isinstance(finish, collections.Callable):
if callable(finish):
vals = finish(func, xmin, args=args, full_output=1, disp=0)
xmin = vals[0]
Jmin = vals[1]
Expand Down

0 comments on commit c1cbb7d

Please sign in to comment.