[SciPy-dev] scipy_test/testing.py: assert_approx_equal -- array version (patch)

krivilli at unberwoot.net krivilli at unberwoot.net
Sat Oct 23 01:11:34 CDT 2004


HI,

-I'm using scipy_test/testing.py as a 1st try to add tests units into my scripts and reuse some of
it in scipy.optimize.

-When dealing with something like optimize.leastsq, I needed to compare its resulted array (i.e.
optimize.leastsq(...)[0]) against its accuracy. I didn't find anything I could use for this matter
in 'testing.py'. I was looking for a n-dimensional (n>1) version of assert_approx_equal. So I
wrote one (in fact, i just extented 'assert_approx_equal' for n-arrays); I'm not sure if there is
similar code lying around somewhere in scipy_test.


-Bellow goes the patch against testing.py (cvs):



*** testing.py	2004-10-06 20:03:34.000000000 -0300
--- testing.py	2004-10-23 02:06:34.000000000 -0300
***************
*** 10,16 ****
      # If Numeric and scipy_base  are not available, then some of the
      # functions below will not be available.
      from Numeric import alltrue,equal,shape,ravel,around,zeros,Float64,asarray,\
!          less_equal,array2string,less
      import scipy_base.fastumath as math
  except ImportError:
      pass
--- 10,16 ----
      # If Numeric and scipy_base  are not available, then some of the
      # functions below will not be available.
      from Numeric import alltrue,equal,shape,ravel,around,zeros,Float64,asarray,\
!          less_equal,array2string,less,floor,fabs,log10,Float
      import scipy_base.fastumath as math
  except ImportError:
      pass
***************
*** 689,694 ****
--- 689,735 ----
          print x, y
          raise ValueError, 'arrays are not almost equal'

+ __all__.append('assert_array_approx_equal')
+ def assert_array_approx_equal(x,y,significant=7,err_msg=''):
+     x, y = asarray((x, y),Float)
+     msg = '\nItems are not equal to %d significant digits:\n' % significant
+     msg += err_msg
+
+     try:
+         cond = alltrue(equal(shape(x),shape(y)))
+         if not cond:
+             msg = msg + ' (shapes mismatch):\n\t'\
+                   'Shape of array 1: %s\n\tShape of array 2: %s' % (shape(x),shape(y))
+         assert cond, msg + '\n\t' + err_msg
+
+         # Normalized the numbers to be in range (-10.0,10.0)
+         scale = pow(10,floor(log10(0.5*(abs(x)+abs(y)))))
+         try:
+             sc_x = x/scale
+         except ZeroDivisionError:
+             sc_x = zeros(shape(x))
+         try:
+             sc_y = y/scale
+         except ZeroDivisionError:
+             sc_y = zeros(shape(y))
+
+         reduced = fabs(sc_x - sc_y) <= pow(10.,-1*significant)
+         cond = alltrue(reduced)
+         if not cond:
+             s1 = array2string(x,precision=significant+1)
+             s2 = array2string(y,precision=significant+1)
+             if len(s1)>120: s1 = s1[:120] + '...'
+             if len(s2)>120: s2 = s2[:120] + '...'
+             match = 100-100.0*reduced.tolist().count(1)/len(reduced)
+             msg = msg + ' (mismatch %s%%):\n\tArray 1: %s\n\tArray 2: %s' % (match,s1,s2)
+         assert cond,\
+                msg + '\n\t' + err_msg
+     except ValueError:
+         print sys.exc_value
+         print shape(x),shape(y)
+         print x, y
+         raise ValueError, 'arrays are not approximately equal'
+
  __all__.append('assert_array_less')
  def assert_array_less(x,y,err_msg=''):
      x,y = asarray(x), asarray(y)





regards,

-- roberto (krivilli)




More information about the Scipy-dev mailing list