Package pypower :: Package t :: Module t_is
[hide private]
[frames] | no frames]

Source Code for Module pypower.t.t_is

  1  # Copyright (C) 2004-2011 Power System Engineering Research Center (PSERC) 
  2  # Copyright (C) 2011 Richard Lincoln 
  3  # 
  4  # PYPOWER is free software: you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published 
  6  # by the Free Software Foundation, either version 3 of the License, 
  7  # or (at your option) any later version. 
  8  # 
  9  # PYPOWER is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY], without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with PYPOWER. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  """Tests if two matrices are identical to some tolerance. 
 18  """ 
 19   
 20  from numpy import array, max, abs, nonzero, argmax, zeros 
 21   
 22  from pypower.t.t_ok import t_ok 
 23  from pypower.t.t_globals import TestGlobals 
 24   
 25   
26 -def t_is(got, expected, prec=5, msg=''):
27 """Tests if two matrices are identical to some tolerance. 28 29 Increments the global test count and if the maximum difference 30 between corresponding elements of C{got} and C{expected} is less 31 than 10**(-C{prec}) then it increments the passed tests count, 32 otherwise increments the failed tests count. Prints 'ok' or 'not ok' 33 followed by the MSG, unless the global variable t_quiet is true. 34 Intended to be called between calls to C{t_begin} and C{t_end}. 35 36 @author: Ray Zimmerman (PSERC Cornell) 37 @author: Richard Lincoln 38 """ 39 if isinstance(got, int) or isinstance(got, float): 40 got = array([got], float) 41 elif isinstance(got, list) or isinstance(got, tuple): 42 got = array(got, float) 43 44 if isinstance(expected, int) or isinstance(expected, float): 45 expected = array([expected], float) 46 elif isinstance(expected, list) or isinstance(expected, tuple): 47 expected = array(expected, float) 48 49 if (got.shape == expected.shape) or (expected.shape == (0,)): 50 got_minus_expected = got - expected 51 max_diff = max(max(abs(got_minus_expected))) 52 condition = ( max_diff < 10**(-prec) ) 53 else: 54 condition = False 55 max_diff = 0 56 57 t_ok(condition, msg) 58 if (not condition and not TestGlobals.t_quiet): 59 s = '' 60 if max_diff != 0: 61 idx = nonzero(abs(got_minus_expected) >= 10**(-prec)) 62 if len(idx) == 1: # 1D array 63 idx = (idx[0], zeros( len(got_minus_expected) )) 64 i, j = idx 65 66 k = i + (j-1) * expected.shape[0] 67 68 got = got.flatten() 69 expected = expected.flatten() 70 got_minus_expected = got_minus_expected.flatten() 71 72 kk = argmax( abs(got_minus_expected[ k.astype(int) ]) ) 73 74 s += ' row col got expected got - exp\n' 75 s += '------- ------ ---------------- ---------------- ----------------' 76 for u in range(len(i)): 77 s += '\n%6d %6d %16g %16g %16g' % \ 78 (i[u], j[u], got[k[u]], expected[k[u]], got_minus_expected[k[u]]) 79 if u == kk: 80 s += ' *' 81 s += '\nmax diff @ (%d,%d) = %g > allowed tol of %g\n\n' % \ 82 (i[kk], j[kk], max_diff, 10**(-prec)) 83 else: 84 s += ' dimension mismatch:\n' 85 86 if len(got.shape) == 1: # 1D array 87 s += ' got: %d\n' % got.shape 88 else: 89 s += ' got: %d x %d\n' % got.shape 90 91 if len(expected.shape) == 1: # 1D array 92 s += ' expected: %d\n' % expected.shape 93 else: 94 s += ' expected: %d x %d\n' % expected.shape 95 96 print s
97 98 if __name__ == '__main__': 99 a = array([[1,2,3], [4,5,6]]) 100 b = array([[2,3,4], [5,6,7]]) 101 TestGlobals.t_quiet = False 102 t_is(a, b) 103