[Scipy-svn] r4137 - in trunk/scipy/optimize: . tests
scipy-svn at scipy.org
scipy-svn at scipy.org
Sun Apr 13 14:39:03 EDT 2008
Author: stefan
Date: 2008-04-13 13:38:35 -0500 (Sun, 13 Apr 2008)
New Revision: 4137
Modified:
trunk/scipy/optimize/minpack.py
trunk/scipy/optimize/tests/test_optimize.py
Log:
Add tests for leastsq [provided by Andrew Straw]. Fix: input parameters are no longer
modified. Closes #637.
Modified: trunk/scipy/optimize/minpack.py
===================================================================
--- trunk/scipy/optimize/minpack.py 2008-04-11 22:43:35 UTC (rev 4136)
+++ trunk/scipy/optimize/minpack.py 2008-04-13 18:38:35 UTC (rev 4137)
@@ -1,7 +1,7 @@
import _minpack
from numpy import atleast_1d, dot, take, triu, shape, eye, \
- transpose, zeros, product, greater
+ transpose, zeros, product, greater, array
error = _minpack.error
@@ -108,7 +108,7 @@
fixed_point -- scalar fixed-point finder
"""
- x0 = atleast_1d(x0)
+ x0 = array(x0,ndmin=1)
n = len(x0)
if type(args) != type(()): args = (args,)
check_func(func,x0,args,n,(n,))
@@ -262,7 +262,7 @@
fixed_point -- scalar fixed-point finder
"""
- x0 = atleast_1d(x0)
+ x0 = array(x0,ndmin=1)
n = len(x0)
if type(args) != type(()): args = (args,)
m = check_func(func,x0,args,n)[0]
Modified: trunk/scipy/optimize/tests/test_optimize.py
===================================================================
--- trunk/scipy/optimize/tests/test_optimize.py 2008-04-11 22:43:35 UTC (rev 4136)
+++ trunk/scipy/optimize/tests/test_optimize.py 2008-04-13 18:38:35 UTC (rev 4137)
@@ -1,17 +1,25 @@
""" Unit tests for optimization routines
-Author: Ed Schofield
-Nov 2005
+Authors:
+ Ed Schofield, Nov 2005
+ Andrew Straw, April 2008
+
+To run it in its simplest form::
+ nosetests test_optimize.py
+
+
"""
from scipy.testing import *
from scipy import optimize
-from numpy import array, zeros, float64, dot, log, exp, inf
+from scipy.optimize import leastsq
+from numpy import array, zeros, float64, dot, log, exp, inf, \
+ pi, sin, cos
+import numpy as np
from scipy.optimize.tnc import RCSTRINGS, MSG_NONE
+import numpy.random
+from math import pow
-
-from math import sin, cos, pow
-
class TestOptimize(TestCase):
""" Test case for a simple constrained entropy maximization problem
(the machine translation example of Berger et al in
@@ -192,11 +200,11 @@
def test5fg(x):
f = sin(x[0]+x[1])+pow(x[0]-x[1],2)-1.5*x[0]+2.5*x[1]+1.0
dif = [0,0]
- v1 = cos(x[0]+x[1]);
- v2 = 2.0*(x[0]-x[1]);
+ v1 = cos(x[0]+x[1])
+ v2 = 2.0*(x[0]-x[1])
- dif[0] = v1+v2-1.5;
- dif[1] = v1-v2+2.5;
+ dif[0] = v1+v2-1.5
+ dif[1] = v1-v2+2.5
return f, dif
self.tests.append((test5fg, [0,0], [(-1.5, 4),(-3,3)],
[-0.54719755119659763, -1.5471975511965976]))
@@ -241,6 +249,44 @@
if ef > 1e-8:
raise err
+class TestLeastSq(TestCase):
+ def setUp(self):
+ x = np.linspace(0, 10, 40)
+ a,b,c = 3.1, 42, -304.2
+ self.x = x
+ self.abc = a,b,c
+ y_true = a*x**2 + b*x + c
+ self.y_meas = y_true + 0.01*np.random.standard_normal( y_true.shape )
+ def residuals(self, p, y, x):
+ a,b,c = p
+ err = y-(a*x**2 + b*x + c)
+ return err
+
+ def test_basic(self):
+ p0 = numpy.array([0,0,0])
+ params_fit, ier = leastsq(self.residuals, p0,
+ args=(self.y_meas, self.x))
+ assert ier in (1,2,3,4), 'solution not found (ier=%d)'%ier
+ assert_array_almost_equal( params_fit, self.abc, decimal=2) # low precision due to random
+
+ def test_full_output(self):
+ p0 = numpy.array([0,0,0])
+ full_output = leastsq(self.residuals, p0,
+ args=(self.y_meas, self.x),
+ full_output=True)
+ params_fit, cov_x, infodict, mesg, ier = full_output
+ assert ier in (1,2,3,4), 'solution not found: %s'%mesg
+
+ def test_input_untouched(self):
+ p0 = numpy.array([0,0,0],dtype=numpy.float64)
+ p0_copy = numpy.array(p0, copy=True)
+ full_output = leastsq(self.residuals, p0,
+ args=(self.y_meas, self.x),
+ full_output=True)
+ params_fit, cov_x, infodict, mesg, ier = full_output
+ assert ier in (1,2,3,4), 'solution not found: %s'%mesg
+ assert_array_equal(p0, p0_copy)
+
if __name__ == "__main__":
nose.run(argv=['', __file__])
More information about the Scipy-svn
mailing list