Skip to content

Commit 45ba40d

Browse files
committed
MAINT: integrate: break dependancy on optimize
1 parent f709ce9 commit 45ba40d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+126
-104
lines changed

scipy/_lib/_util.py

+45
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,51 @@ def __dir__(self):
11891189
return list(self.keys())
11901190

11911191

1192+
class OptimizeResult(_RichResult):
1193+
"""
1194+
Represents the optimization result.
1195+
1196+
Attributes
1197+
----------
1198+
x : ndarray
1199+
The solution of the optimization.
1200+
success : bool
1201+
Whether or not the optimizer exited successfully.
1202+
status : int
1203+
Termination status of the optimizer. Its value depends on the
1204+
underlying solver. Refer to `message` for details.
1205+
message : str
1206+
Description of the cause of the termination.
1207+
fun : float
1208+
Value of objective function at `x`.
1209+
jac, hess : ndarray
1210+
Values of objective function's Jacobian and its Hessian at `x` (if
1211+
available). The Hessian may be an approximation, see the documentation
1212+
of the function in question.
1213+
hess_inv : object
1214+
Inverse of the objective function's Hessian; may be an approximation.
1215+
Not available for all solvers. The type of this attribute may be
1216+
either np.ndarray or scipy.sparse.linalg.LinearOperator.
1217+
nfev, njev, nhev : int
1218+
Number of evaluations of the objective functions and of its
1219+
Jacobian and Hessian.
1220+
nit : int
1221+
Number of iterations performed by the optimizer.
1222+
maxcv : float
1223+
The maximum constraint violation.
1224+
1225+
Notes
1226+
-----
1227+
Depending on the specific solver being used, `OptimizeResult` may
1228+
not have all attributes listed here, and they may have additional
1229+
attributes not listed here. Since this class is essentially a
1230+
subclass of dict with attribute accessors, one can see which
1231+
attributes are available using the `OptimizeResult.keys` method.
1232+
1233+
"""
1234+
pass
1235+
1236+
11921237
def _indenter(s, n=0):
11931238
"""
11941239
Ensures that lines after the first are indented by the specified amount

scipy/differentiate/tests/test_differentiate.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import numpy as np
55

66
import scipy._lib._elementwise_iterative_method as eim
7+
from scipy._lib._util import OptimizeResult
78
import scipy._lib.array_api_extra as xpx
89
from scipy._lib._array_api_no_0d import xp_assert_close, xp_assert_equal, xp_assert_less
910
from scipy._lib._array_api import is_numpy, is_torch
@@ -593,14 +594,14 @@ def df1_1xy(x, y):
593594
res01 = jacobian(lambda y: df1_0xy(z[0], y), z[1:2], initial_step=10)
594595
res10 = jacobian(lambda x: df1_1xy(x, z[1]), z[0:1], initial_step=10)
595596
res11 = jacobian(lambda y: df1_1xy(z[0], y), z[1:2], initial_step=10)
596-
ref = optimize.OptimizeResult()
597+
ref = OptimizeResult()
597598
for attr in ['success', 'status', 'df', 'nit', 'nfev']:
598599
ref_attr = xp.asarray([[getattr(res00, attr), getattr(res01, attr)],
599600
[getattr(res10, attr), getattr(res11, attr)]])
600601
ref[attr] = xp.squeeze(
601602
ref_attr,
602603
axis=tuple(ax for ax, size in enumerate(ref_attr.shape) if size == 1)
603-
)
604+
)
604605
rtol = 1.5e-5 if res[attr].dtype == xp.float32 else 1.5e-14
605606
xp_assert_close(res[attr], ref[attr], rtol=rtol)
606607

scipy/integrate/_bvp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from scipy.sparse import coo_matrix, csc_matrix
88
from scipy.sparse.linalg import splu
9-
from scipy.optimize import OptimizeResult
9+
from scipy._lib._util import OptimizeResult
1010

1111

1212
EPS = np.finfo(float).eps

scipy/integrate/_ivp/ivp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .radau import Radau
55
from .rk import RK23, RK45, DOP853
66
from .lsoda import LSODA
7-
from scipy.optimize import OptimizeResult
7+
from scipy._lib._util import OptimizeResult
88
from .common import EPS, OdeSolution
99
from .base import OdeSolver
1010

scipy/optimize/_cobyla_py.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
from threading import RLock
1515

1616
import numpy as np
17+
from .._lib._util import OptimizeResult
1718
from scipy.optimize import _cobyla as cobyla
18-
from ._optimize import (OptimizeResult, _check_unknown_options,
19+
from ._optimize import (_check_unknown_options,
1920
_prepare_scalar_function)
2021
try:
2122
from itertools import izip

scipy/optimize/_differentialevolution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
import numpy as np
88

9-
from scipy.optimize import OptimizeResult, minimize
9+
from scipy.optimize import minimize
1010
from scipy.optimize._constraints import (Bounds, new_bounds_to_old,
1111
NonlinearConstraint, LinearConstraint)
1212
from scipy.optimize._optimize import _status_message, _wrap_callback
13-
from scipy._lib._util import (check_random_state, MapWrapper, _FunctionWrapper,
13+
from scipy._lib._util import (OptimizeResult, check_random_state, MapWrapper, _FunctionWrapper,
1414
rng_integers, _transition_to_rng)
1515
from scipy._lib._sparse import issparse
1616

scipy/optimize/_direct_py.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
)
44

55
import numpy as np
6-
from scipy.optimize import OptimizeResult
6+
from scipy._lib._util import OptimizeResult
77
from ._constraints import old_bound_to_new, Bounds
88
from ._direct import direct as _direct # type: ignore
99

scipy/optimize/_dual_annealing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"""
99

1010
import numpy as np
11-
from scipy.optimize import OptimizeResult
11+
from scipy._lib._util import OptimizeResult
1212
from scipy.optimize import minimize, Bounds
1313
from scipy.special import gammaln
1414
from scipy._lib._util import check_random_state, _transition_to_rng

scipy/optimize/_isotonic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import numpy as np
44

5-
from ._optimize import OptimizeResult
5+
from .._lib._util import OptimizeResult
66
from ._pava_pybind import pava
77

88
if TYPE_CHECKING:

scipy/optimize/_lbfgsb_py.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535

3636
import numpy as np
3737
from numpy import array, asarray, float64, zeros
38+
39+
from .._lib._util import OptimizeResult
3840
from . import _lbfgsb
39-
from ._optimize import (MemoizeJac, OptimizeResult, _call_callback_maybe_halt,
41+
from ._optimize import (MemoizeJac, _call_callback_maybe_halt,
4042
_wrap_callback, _check_unknown_options,
4143
_prepare_scalar_function)
4244
from ._constraints import old_bound_to_new

scipy/optimize/_linprog.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
import numpy as np
1818

19-
from ._optimize import OptimizeResult, OptimizeWarning
19+
from .._lib._util import OptimizeResult
20+
21+
from ._optimize import OptimizeWarning
2022
from warnings import warn
2123
from ._linprog_highs import _linprog_highs
2224
from ._linprog_ip import _linprog_ip

scipy/optimize/_linprog_highs.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
import inspect
1717
import numpy as np
18-
from ._optimize import OptimizeWarning, OptimizeResult
18+
19+
from .._lib._util import OptimizeResult
20+
from ._optimize import OptimizeWarning
1921
from warnings import warn
2022
from ._highspy._highs_wrapper import _highs_wrapper
2123
from ._highspy._core import(

scipy/optimize/_linprog_ip.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020

2121
import numpy as np
2222
import scipy as sp
23+
from .._lib._util import OptimizeResult
2324
import scipy.sparse as sps
2425
from warnings import warn
2526
from scipy.linalg import LinAlgError
26-
from ._optimize import OptimizeWarning, OptimizeResult, _check_unknown_options
27+
from ._optimize import OptimizeWarning, _check_unknown_options
2728
from ._linprog_util import _postsolve
2829
has_umfpack = True
2930
has_cholmod = True

scipy/optimize/_linprog_rs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from ._bglu_dense import LU
2626
from ._bglu_dense import BGLU as BGLU
2727
from ._linprog_util import _postsolve
28-
from ._optimize import OptimizeResult
28+
from .._lib._util import OptimizeResult
2929

3030

3131
def _phase_one(A, b, x0, callback, postsolve_args, maxiter, tol, disp,

scipy/optimize/_linprog_simplex.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030

3131
import numpy as np
3232
from warnings import warn
33-
from ._optimize import OptimizeResult, OptimizeWarning, _check_unknown_options
33+
34+
from .._lib._util import OptimizeResult
35+
from ._optimize import OptimizeWarning, _check_unknown_options
3436
from ._linprog_util import _postsolve
3537

3638

scipy/optimize/_lsq/bvls.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Bounded-variable least-squares algorithm."""
22
import numpy as np
33
from numpy.linalg import norm, lstsq
4-
from scipy.optimize import OptimizeResult
4+
from scipy._lib._util import OptimizeResult
55

66
from .common import print_header_linear, print_iteration_linear
77

scipy/optimize/_lsq/dogbox.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
from numpy.linalg import lstsq, norm
4545

4646
from scipy.sparse.linalg import LinearOperator, aslinearoperator, lsmr
47-
from scipy.optimize import OptimizeResult
47+
from scipy._lib._util import OptimizeResult
4848
from scipy._lib._util import _call_callback_maybe_halt
4949

5050

scipy/optimize/_lsq/least_squares.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
from numpy.linalg import norm
66

77
from scipy.sparse.linalg import LinearOperator
8-
from scipy.optimize import _minpack, OptimizeResult
8+
from scipy.optimize import _minpack
99
from scipy.optimize._numdiff import approx_derivative, group_columns
1010
from scipy.optimize._minimize import Bounds
1111
from scipy._lib._sparse import issparse
12-
from scipy._lib._util import _workers_wrapper
12+
from scipy._lib._util import OptimizeResult, _workers_wrapper
1313

1414
from .trf import trf
1515
from .dogbox import dogbox

scipy/optimize/_lsq/lsq_linear.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from numpy.linalg import norm
44
from scipy.sparse import issparse, csr_array
55
from scipy.sparse.linalg import LinearOperator, lsmr
6-
from scipy.optimize import OptimizeResult
6+
from scipy._lib._util import OptimizeResult
77
from scipy.optimize._minimize import Bounds
88

99
from .common import in_bounds, compute_grad

scipy/optimize/_lsq/trf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
from numpy.linalg import norm
9898
from scipy.linalg import svd, qr
9999
from scipy.sparse.linalg import lsmr
100-
from scipy.optimize import OptimizeResult
100+
from scipy._lib._util import OptimizeResult
101101

102102
from .common import (
103103
step_size_to_bound, find_active_constraints, in_bounds,

scipy/optimize/_lsq/trf_linear.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from numpy.linalg import norm
55
from scipy.linalg import qr, solve_triangular
66
from scipy.sparse.linalg import lsmr
7-
from scipy.optimize import OptimizeResult
7+
from scipy._lib._util import OptimizeResult
88

99
from .givens_elimination import givens_elimination
1010
from .common import (

scipy/optimize/_milp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from scipy.sparse import csc_array, vstack, issparse
77
from ._highspy._highs_wrapper import _highs_wrapper # type: ignore[import-not-found,import-untyped]
88
from ._constraints import LinearConstraint, Bounds
9-
from ._optimize import OptimizeResult
9+
from .._lib._util import OptimizeResult
1010
from ._linprog_highs import _highs_to_scipy_status_message
1111

1212

scipy/optimize/_minimize.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
import numpy as np
1717

18+
from .._lib._util import OptimizeResult
19+
1820
# unconstrained minimization
1921
from ._optimize import (_minimize_neldermead, _minimize_powell, _minimize_cg,
2022
_minimize_bfgs, _minimize_newtoncg,
2123
_minimize_scalar_brent, _minimize_scalar_bounded,
22-
_minimize_scalar_golden, MemoizeJac, OptimizeResult,
23-
_wrap_callback, _recover_from_bracket_error)
24+
_minimize_scalar_golden, MemoizeJac, _wrap_callback, _recover_from_bracket_error)
2425
from ._trustregion_dogleg import _minimize_dogleg
2526
from ._trustregion_ncg import _minimize_trust_ncg
2627
from ._trustregion_krylov import _minimize_trust_krylov

scipy/optimize/_minpack_py.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
finfo, inexact, issubdtype, dtype)
88
from scipy import linalg
99
from scipy.linalg import svd, cholesky, solve_triangular, LinAlgError
10-
from scipy._lib._util import _asarray_validated, _lazywhere, _contains_nan
10+
from scipy._lib._util import OptimizeResult, _asarray_validated, _lazywhere, _contains_nan
1111
from scipy._lib._util import getfullargspec_no_self as _getfullargspec
12-
from ._optimize import OptimizeResult, _check_unknown_options, OptimizeWarning
12+
from ._optimize import _check_unknown_options, OptimizeWarning
1313
from ._lsq import least_squares
1414
# from ._lsq.common import make_strictly_feasible
1515
from ._lsq.least_squares import prepare_bounds

scipy/optimize/_optimize.py

+2-47
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
import inspect
3232
from numpy import eye, argmin, zeros, shape, asarray, sqrt
3333
import numpy as np
34+
from scipy._lib._util import OptimizeResult
3435
from scipy.linalg import cholesky, issymmetric, LinAlgError
3536
from scipy.sparse.linalg import LinearOperator
3637
from ._linesearch import (line_search_wolfe1, line_search_wolfe2,
3738
line_search_wolfe2 as line_search,
3839
LineSearchWarning)
3940
from ._numdiff import approx_derivative
4041
from scipy._lib._util import getfullargspec_no_self as _getfullargspec
41-
from scipy._lib._util import (MapWrapper, check_random_state, _RichResult,
42-
_call_callback_maybe_halt, _transition_to_rng)
42+
from scipy._lib._util import (MapWrapper, check_random_state, _call_callback_maybe_halt, _transition_to_rng)
4343
from scipy.optimize._differentiable_functions import ScalarFunction, FD_METHODS
4444
from scipy._lib._array_api import array_namespace, xp_capabilities
4545
from scipy._lib import array_api_extra as xpx
@@ -109,51 +109,6 @@ def wrapped_callback(res):
109109
return wrapped_callback
110110

111111

112-
class OptimizeResult(_RichResult):
113-
"""
114-
Represents the optimization result.
115-
116-
Attributes
117-
----------
118-
x : ndarray
119-
The solution of the optimization.
120-
success : bool
121-
Whether or not the optimizer exited successfully.
122-
status : int
123-
Termination status of the optimizer. Its value depends on the
124-
underlying solver. Refer to `message` for details.
125-
message : str
126-
Description of the cause of the termination.
127-
fun : float
128-
Value of objective function at `x`.
129-
jac, hess : ndarray
130-
Values of objective function's Jacobian and its Hessian at `x` (if
131-
available). The Hessian may be an approximation, see the documentation
132-
of the function in question.
133-
hess_inv : object
134-
Inverse of the objective function's Hessian; may be an approximation.
135-
Not available for all solvers. The type of this attribute may be
136-
either np.ndarray or scipy.sparse.linalg.LinearOperator.
137-
nfev, njev, nhev : int
138-
Number of evaluations of the objective functions and of its
139-
Jacobian and Hessian.
140-
nit : int
141-
Number of iterations performed by the optimizer.
142-
maxcv : float
143-
The maximum constraint violation.
144-
145-
Notes
146-
-----
147-
Depending on the specific solver being used, `OptimizeResult` may
148-
not have all attributes listed here, and they may have additional
149-
attributes not listed here. Since this class is essentially a
150-
subclass of dict with attribute accessors, one can see which
151-
attributes are available using the `OptimizeResult.keys` method.
152-
153-
"""
154-
pass
155-
156-
157112
class OptimizeWarning(UserWarning):
158113
"""General warning for :mod:`scipy.optimize`."""
159114
pass

scipy/optimize/_qap.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import operator
33
import warnings
44
import numbers
5-
from . import (linear_sum_assignment, OptimizeResult)
5+
from . import (linear_sum_assignment)
66
from ._optimize import _check_unknown_options
77

8-
from scipy._lib._util import check_random_state
8+
from scipy._lib._util import OptimizeResult, check_random_state
99
import itertools
1010

1111
QUADRATIC_ASSIGNMENT_METHODS = ['faq', '2opt']

0 commit comments

Comments
 (0)