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