comparison tests/RecomTST_test.py @ 4:4393ad5bffc1

Implemented the RecommendedTST algorithm Test file written, but test not yet working.
author nikcleju
date Mon, 24 Oct 2011 23:39:53 +0000
parents
children 4a4e5204ecf5
comparison
equal deleted inserted replaced
3:537f7798e186 4:4393ad5bffc1
1 # -*- coding: utf-8 -*-
2 """
3 Created on Mon Oct 24 21:17:49 2011
4
5 @author: Nic
6
7 Test RecommendedTST algorithm
8 """
9
10 import numpy as np
11 import numpy.linalg
12 import scipy.io
13 import unittest
14 from pyCSalgos.RecomTST.RecommendedTST import RecommendedTST
15
16 class RecomTSTresults(unittest.TestCase):
17 def testResults(self):
18 mdict = scipy.io.loadmat('RecomTSTtestdata.mat')
19
20 # A = system matrix
21 # Y = matrix with measurements (on columns)
22 # X0 = matrix with initial solutions (on columns)
23 # Eps = vector with epsilon
24 # Xr = matrix with correct solutions (on columns)
25 for A,Y,X0,Tol,Xr in zip(mdict['cellA'].squeeze(),mdict['cellY'].squeeze(),mdict['cellX0'].squeeze(),mdict['cellTol'].squeeze(),mdict['cellXr'].squeeze()):
26 for i in np.arange(Y.shape[1]):
27 xr = RecommendedTST(A, Y[:,i], nsweep=300, tol=Tol.squeeze()[i], xinitial=X0[:,i])
28
29 # check if found solution is the same as the correct cslution
30 diff = numpy.linalg.norm(xr - Xr[:,i])
31 self.assertTrue(diff < 1e-12)
32 #err1 = numpy.linalg.norm(Y[:,i] - np.dot(A,xr))
33 #err2 = numpy.linalg.norm(Y[:,i] - np.dot(A,Xr[:,i]))
34 #norm1 = numpy.linalg.norm(xr,1)
35 #norm2 = numpy.linalg.norm(Xr[:,i],1)
36 #print 'diff = ',diff
37 #print 'err1 = ',err1
38 #print 'err2 = ',err2
39 #print 'norm1 = ',norm1
40 #print 'norm2 = ',norm2
41 #
42 # It seems Matlab's linsolve and scipy solve are slightly different
43 # Therefore make a more robust condition:
44 # OK; if solutions are close enough (diff < 1e-6)
45 # or
46 # (
47 # they fulfill the constraint close enough (differr < 1e-6)
48 # and
49 # Python solution has l1 norm no more than 1e-6 larger as the reference solution
50 # (i.e. either norm1 < norm2 or norm1>norm2 not by more than 1e-6)
51 # )
52 #
53 # ERROR: else
54 #differr = abs((err1 - err2))
55 #diffnorm = norm1 - norm2 # intentionately no abs(), since norm1 < norm2 is good
56 #if diff < 1e-6 or (differr < 1e-6 and (diffnorm < 1e-6)):
57 # isok = True
58 #else:
59 # isok = False
60 #if not isok:
61 # print "should raise"
62 # #self.assertTrue(isok)
63 #self.assertTrue(isok)
64
65 if __name__ == "__main__":
66 unittest.main(verbosity=2)
67 #suite = unittest.TestLoader().loadTestsFromTestCase(CompareResults)
68 #unittest.TextTestRunner(verbosity=2).run(suite)