annotate tests/RecomTST_test.py @ 68:cab8a215f9a1 tip

Minor
author Nic Cleju <nikcleju@gmail.com>
date Tue, 09 Jul 2013 14:50:09 +0300
parents 9079a9f7c4cf
children
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@5 8 u"""
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@5 31 err1 = numpy.linalg.norm(Y[:,i] - np.dot(A,xr))
nikcleju@5 32 err2 = numpy.linalg.norm(Y[:,i] - np.dot(A,Xr[:,i]))
nikcleju@5 33 norm1 = numpy.linalg.norm(xr,1)
nikcleju@5 34 norm2 = numpy.linalg.norm(Xr[:,i],1)
nikcleju@5 35
nikcleju@5 36 # Make a more robust condition:
nikcleju@4 37 # OK; if solutions are close enough (diff < 1e-6)
nikcleju@4 38 # or
nikcleju@4 39 # (
nikcleju@5 40 # Python solution fulfills the constraint better (or up to 1e-6 worse)
nikcleju@4 41 # and
nikcleju@4 42 # Python solution has l1 norm no more than 1e-6 larger as the reference solution
nikcleju@4 43 # (i.e. either norm1 < norm2 or norm1>norm2 not by more than 1e-6)
nikcleju@4 44 # )
nikcleju@4 45 #
nikcleju@5 46 # ERROR: else
nikcleju@5 47 differr = err1 - err2 # intentionately no abs(), since err1` < err2 is good
nikcleju@5 48 diffnorm = norm1 - norm2 # intentionately no abs(), since norm1 < norm2 is good
nikcleju@5 49 if diff < 1e-6 or (differr < 1e-6 and (diffnorm < 1e-6)):
nikcleju@5 50 isok = True
nikcleju@5 51 else:
nikcleju@5 52 isok = False
nikcleju@5 53 self.assertTrue(isok)
nikcleju@5 54
nikcleju@5 55 #diff = numpy.linalg.norm(xr - Xr[:,i])
nikcleju@5 56 #if diff > 1e-6:
nikcleju@5 57 # self.assertTrue(diff < 1e-6)
nikcleju@5 58
nikcleju@4 59
nikcleju@4 60 if __name__ == "__main__":
nikcleju@7 61 #import cProfile
nikcleju@7 62 #cProfile.run('unittest.main()', 'profres')
nikcleju@7 63 unittest.main()
nikcleju@4 64 #suite = unittest.TestLoader().loadTestsFromTestCase(CompareResults)
nikcleju@7 65 #unittest.TextTestRunner(verbosity=2).run(suite)