/usr/lib/python2.7/dist-packages/dipy/core/tests/test_optimize.py is in python-dipy 0.10.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | import numpy as np
import scipy.sparse as sps
import numpy.testing as npt
from dipy.core.optimize import Optimizer, SCIPY_LESS_0_12, sparse_nnls, spdot
import dipy.core.optimize as opt
def func(x):
return x[0]**2 + x[1]**2 + x[2]**2
def func2(x):
return x[0]**2 + 0.5 * x[1]**2 + 0.2 * x[2]**2 + 0.2 * x[3]**2
@npt.dec.skipif(SCIPY_LESS_0_12)
def test_optimize_new_scipy():
opt = Optimizer(fun=func, x0=np.array([1., 1., 1.]), method='Powell')
npt.assert_array_almost_equal(opt.xopt, np.array([0, 0, 0]))
npt.assert_almost_equal(opt.fopt, 0)
opt = Optimizer(fun=func, x0=np.array([1., 1., 1.]), method='L-BFGS-B',
options={'maxcor': 10, 'ftol': 1e-7,
'gtol': 1e-5, 'eps': 1e-8})
npt.assert_array_almost_equal(opt.xopt, np.array([0, 0, 0]))
npt.assert_almost_equal(opt.fopt, 0)
npt.assert_equal(opt.evolution, None)
npt.assert_equal(opt.evolution, None)
opt = Optimizer(fun=func, x0=np.array([1., 1., 1.]), method='L-BFGS-B',
options={'maxcor': 10, 'ftol': 1e-7,
'gtol': 1e-5, 'eps': 1e-8},
evolution=False)
npt.assert_array_almost_equal(opt.xopt, np.array([0, 0, 0]))
npt.assert_almost_equal(opt.fopt, 0)
opt.print_summary()
opt = Optimizer(fun=func2, x0=np.array([1., 1., 1., 5.]),
method='L-BFGS-B',
options={'maxcor': 10, 'ftol': 1e-7,
'gtol': 1e-5, 'eps': 1e-8},
evolution=True)
npt.assert_equal(opt.evolution.shape, (opt.nit, 4))
opt = Optimizer(fun=func2, x0=np.array([1., 1., 1., 5.]),
method='Powell',
options={'xtol': 1e-6, 'ftol': 1e-6, 'maxiter': 1e6},
evolution=True)
npt.assert_array_almost_equal(opt.xopt, np.array([0, 0, 0, 0.]))
@npt.dec.skipif(not SCIPY_LESS_0_12)
def test_optimize_old_scipy():
opt = Optimizer(fun=func, x0=np.array([1., 1., 1.]),
method='L-BFGS-B',
options={'maxcor': 10, 'ftol': 1e-7,
'gtol': 1e-5, 'eps': 1e-8})
npt.assert_array_almost_equal(opt.xopt, np.array([0, 0, 0]))
npt.assert_almost_equal(opt.fopt, 0)
opt = Optimizer(fun=func2, x0=np.array([1., 1., 1., 5.]),
method='Powell',
options={'xtol': 1e-6, 'ftol': 1e-6, 'maxiter': 1e6},
evolution=True)
npt.assert_array_almost_equal(opt.xopt, np.array([0, 0, 0, 0.]))
opt = Optimizer(fun=func, x0=np.array([1., 1., 1.]),
method='L-BFGS-B',
options={'maxcor': 10, 'eps': 1e-8})
npt.assert_array_almost_equal(opt.xopt, np.array([0, 0, 0]))
npt.assert_almost_equal(opt.fopt, 0)
opt = Optimizer(fun=func, x0=np.array([1., 1., 1.]),
method='L-BFGS-B',
options=None)
npt.assert_array_almost_equal(opt.xopt, np.array([0, 0, 0]))
npt.assert_almost_equal(opt.fopt, 0)
opt = Optimizer(fun=func2, x0=np.array([1., 1., 1., 5.]),
method='L-BFGS-B',
options={'gtol': 1e-7, 'ftol': 1e-7, 'maxiter': 10000})
npt.assert_array_almost_equal(opt.xopt, np.array([0, 0, 0, 0.]), 4)
npt.assert_almost_equal(opt.fopt, 0)
opt = Optimizer(fun=func2, x0=np.array([1., 1., 1., 5.]),
method='Powell',
options={'maxiter': 1e6},
evolution=True)
npt.assert_array_almost_equal(opt.xopt, np.array([0, 0, 0, 0.]))
opt = Optimizer(fun=func2, x0=np.array([1., 1., 1., 5.]),
method='Powell',
options={'maxiter': 1e6},
evolution=True)
npt.assert_array_almost_equal(opt.xopt, np.array([0, 0, 0, 0.]))
def test_sklearn_linear_solver():
class SillySolver(opt.SKLearnLinearSolver):
def fit(self, X, y):
self.coef_ = np.ones(X.shape[-1])
MySillySolver = SillySolver()
n_samples = 100
n_features = 20
y = np.random.rand(n_samples)
X = np.ones((n_samples, n_features))
MySillySolver.fit(X, y)
npt.assert_equal(MySillySolver.coef_, np.ones(n_features))
npt.assert_equal(MySillySolver.predict(X), np.ones(n_samples) * 20)
def test_nonnegativeleastsquares():
n = 100
X = np.eye(n)
beta = np.random.rand(n)
y = np.dot(X, beta)
my_nnls = opt.NonNegativeLeastSquares()
my_nnls.fit(X, y)
npt.assert_equal(my_nnls.coef_, beta)
npt.assert_equal(my_nnls.predict(X), y)
def test_spdot():
n = 100
m = 20
k = 10
A = np.random.randn(n, m)
B = np.random.randn(m, k)
A_sparse = sps.csr_matrix(A)
B_sparse = sps.csr_matrix(B)
dense_dot = np.dot(A, B)
# Try all the different variations:
npt.assert_array_almost_equal(dense_dot,
spdot(A_sparse, B_sparse).todense())
npt.assert_array_almost_equal(dense_dot, spdot(A, B_sparse))
npt.assert_array_almost_equal(dense_dot, spdot(A_sparse, B))
def test_sparse_nnls():
# Set up the regression:
beta = np.random.rand(10)
X = np.random.randn(1000, 10)
y = np.dot(X, beta)
beta_hat = sparse_nnls(y, X)
beta_hat_sparse = sparse_nnls(y, sps.csr_matrix(X))
# We should be able to get back the right answer for this simple case
npt.assert_array_almost_equal(beta, beta_hat, decimal=1)
npt.assert_array_almost_equal(beta, beta_hat_sparse, decimal=1)
if __name__ == '__main__':
npt.run_module_suite()
|