Mercurial > hg > pycsalgos
comparison tests/l1eq_test.py @ 62:e684f76c1969
Added l1eq_pd(), l1 minimizer with equality constraints from l1-magic
author | Nic Cleju <nikcleju@gmail.com> |
---|---|
date | Mon, 12 Mar 2012 11:29:32 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
61:15374d30fb87 | 62:e684f76c1969 |
---|---|
1 # -*- coding: utf-8 -*- | |
2 """ | |
3 Created on Fri Oct 21 14:28:08 2011 | |
4 | |
5 @author: Nic | |
6 | |
7 Test l1-magic l1eq_pd() algorithm | |
8 """ | |
9 | |
10 import numpy as np | |
11 import numpy.linalg | |
12 import scipy.io | |
13 import unittest | |
14 from pyCSalgos.BP.l1eq_pd import l1eq_pd | |
15 #from l1qc import l1qc_logbarrier | |
16 | |
17 class l1eq_results(unittest.TestCase): | |
18 def testResults(self): | |
19 mdict = scipy.io.loadmat('l1eq_testdata.mat') | |
20 | |
21 # A = system matrix | |
22 # Y = matrix with measurements (on columns) | |
23 # X0 = matrix with initial solutions (on columns) | |
24 # Xr = matrix with correct solutions (on columns) | |
25 for A,Y,X0,Xr in zip(mdict['cellA'].squeeze(),mdict['cellY'].squeeze(),mdict['X0'].squeeze(),mdict['Xr'].squeeze()): | |
26 for i in np.arange(Y.shape[1]): | |
27 xr = l1eq_pd(X0[:,i], A, np.array([]), Y[:,i]) | |
28 | |
29 # check if found solution is the same as the correct cslution | |
30 diff = numpy.linalg.norm(xr - Xr[:,i]) | |
31 err1 = numpy.linalg.norm(Y[:,i] - np.dot(A,xr)) | |
32 err2 = numpy.linalg.norm(Y[:,i] - np.dot(A,Xr[:,i])) | |
33 norm1 = numpy.linalg.norm(xr,1) | |
34 norm2 = numpy.linalg.norm(Xr[:,i],1) | |
35 print 'diff = ',diff | |
36 print 'err1 = ',err1 | |
37 print 'err2 = ',err2 | |
38 print 'norm1 = ',norm1 | |
39 print 'norm2 = ',norm2 | |
40 | |
41 # It seems Matlab's linsolve and scipy solve are slightly different | |
42 # Therefore make a more robust condition: | |
43 # OK; if solutions are close enough (diff < 1e-6) | |
44 # or | |
45 # ( | |
46 # they fulfill the constraint close enough (differr < 1e-6) | |
47 # and | |
48 # Python solution has l1 norm no more than 1e-6 larger as the reference solution | |
49 # (i.e. either norm1 < norm2 or norm1>norm2 not by more than 1e-6) | |
50 # ) | |
51 # | |
52 # ERROR: else | |
53 differr = abs((err1 - err2)) | |
54 diffnorm = norm1 - norm2 # intentionately no abs(), since norm1 < norm2 is good | |
55 if diff < 1e-6 or (differr < 1e-6 and (diffnorm < 1e-6)): | |
56 isok = True | |
57 else: | |
58 isok = False | |
59 | |
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) |