annotate var/omp_app.py @ 68:cab8a215f9a1 tip

Minor
author Nic Cleju <nikcleju@gmail.com>
date Tue, 09 Jul 2013 14:50:09 +0300
parents 020399d027b1
children
rev   line source
nikcleju@55 1 """
nikcleju@55 2 #=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
nikcleju@55 3 # Bob L. Sturm <bst@create.aau.dk> 20111018
nikcleju@55 4 # Department of Architecture, Design and Media Technology
nikcleju@55 5 # Aalborg University Copenhagen
nikcleju@55 6 # Lautrupvang 15, 2750 Ballerup, Denmark
nikcleju@55 7 #=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
nikcleju@55 8 """
nikcleju@55 9
nikcleju@55 10 import numpy as np
nikcleju@55 11 from sklearn.utils import check_random_state
nikcleju@55 12 import time
nikcleju@55 13
nikcleju@55 14 from omp_sk_bugfix import orthogonal_mp
nikcleju@55 15 from omp_QR import greed_omp_qr
nikcleju@55 16 from omp_QR import omp_qr
nikcleju@55 17
nikcleju@55 18 """
nikcleju@55 19 Run a problem suite involving sparse vectors in
nikcleju@55 20 ambientDimension dimensional space, with a resolution
nikcleju@55 21 in the phase plane of numGradations x numGradations,
nikcleju@55 22 and at each indeterminacy and sparsity pair run
nikcleju@55 23 numTrials independent trials.
nikcleju@55 24
nikcleju@55 25 Outputs a text file denoting successes at each phase point.
nikcleju@55 26 For more on phase transitions, see:
nikcleju@55 27 D. L. Donoho and J. Tanner, "Precise undersampling theorems,"
nikcleju@55 28 Proc. IEEE, vol. 98, no. 6, pp. 913-924, June 2010.
nikcleju@55 29 """
nikcleju@55 30
nikcleju@55 31 def runProblemSuite(ambientDimension,numGradations,numTrials):
nikcleju@55 32
nikcleju@55 33 idx = np.arange(ambientDimension)
nikcleju@55 34 phaseDelta = np.linspace(0.05,1,numGradations)
nikcleju@55 35 phaseRho = np.linspace(0.05,1,numGradations)
nikcleju@55 36 success = np.zeros((numGradations, numGradations))
nikcleju@55 37
nikcleju@55 38 #Nic: init timers
nikcleju@55 39 t1all = 0
nikcleju@55 40 t2all = 0
nikcleju@55 41 t3all = 0
nikcleju@55 42
nikcleju@55 43 deltaCounter = 0
nikcleju@55 44 # delta is number of measurements/
nikcleju@55 45 for delta in phaseDelta[:17]:
nikcleju@55 46 rhoCounter = 0
nikcleju@55 47 for rho in phaseRho:
nikcleju@55 48 print(deltaCounter,rhoCounter)
nikcleju@55 49 numMeasurements = int(delta*ambientDimension)
nikcleju@55 50 sparsity = int(rho*numMeasurements)
nikcleju@55 51 # how do I set the following to be random each time?
nikcleju@55 52 generator = check_random_state(100)
nikcleju@55 53 # create unit norm dictionary
nikcleju@55 54 D = generator.randn(numMeasurements, ambientDimension)
nikcleju@55 55 D /= np.sqrt(np.sum((D ** 2), axis=0))
nikcleju@55 56 # compute Gramian (for efficiency)
nikcleju@55 57 DTD = np.dot(D.T,D)
nikcleju@55 58
nikcleju@55 59 successCounter = 0
nikcleju@55 60 trial = numTrials
nikcleju@55 61 while trial > 0:
nikcleju@55 62 # generate sparse signal with a minimum non-zero value
nikcleju@55 63 x = np.zeros((ambientDimension, 1))
nikcleju@55 64 idx2 = idx
nikcleju@55 65 generator.shuffle(idx2)
nikcleju@55 66 idx3 = idx2[:sparsity]
nikcleju@55 67 while np.min(np.abs(x[idx3,0])) < 1e-10 :
nikcleju@55 68 x[idx3,0] = generator.randn(sparsity)
nikcleju@55 69 # sense sparse signal
nikcleju@55 70 y = np.dot(D, x)
nikcleju@55 71
nikcleju@55 72 # Nic: Use sparsify OMP function (translated from Matlab)
nikcleju@55 73 ompopts = dict({'stopCrit':'M', 'stopTol':2*sparsity})
nikcleju@55 74 starttime = time.time() # start timer
nikcleju@55 75 x_r2, errs, times = greed_omp_qr(y.squeeze().copy(), D.copy(), D.shape[1], ompopts)
nikcleju@55 76 t2all = t2all + time.time() - starttime # stop timer
nikcleju@55 77 idx_r2 = np.nonzero(x_r2)[0]
nikcleju@55 78
nikcleju@55 79 # run to two times expected sparsity, or tolerance
nikcleju@55 80 # why? Often times, OMP can retrieve the correct solution
nikcleju@55 81 # when it is run for more than the expected sparsity
nikcleju@55 82 #x_r, idx_r = omp_qr(y,D,DTD,2*sparsity,1e-5)
nikcleju@55 83 # Nic: adjust tolerance to match with other function
nikcleju@55 84 starttime = time.time() # start timer
nikcleju@55 85 x_r, idx_r = omp_qr(y.copy(),D.copy(),DTD.copy(),2*sparsity,numMeasurements*1e-14/np.vdot(y,y))
nikcleju@55 86 t1all = t1all + time.time() - starttime # stop timer
nikcleju@55 87
nikcleju@55 88 # Nic: test sklearn omp
nikcleju@55 89 starttime = time.time() # start timer
nikcleju@55 90 x_r3 = orthogonal_mp(D.copy(), y.copy(), n_nonzero_coefs=2*sparsity, tol=numMeasurements*1e-14, precompute_gram=False, copy_X=True)
nikcleju@55 91 idx_r3 = np.nonzero(x_r3)[0]
nikcleju@55 92 t3all = t3all + time.time() - starttime # stop timer
nikcleju@55 93
nikcleju@55 94 # Nic: compare results
nikcleju@55 95 print 'diff1 = ',np.linalg.norm(x_r.squeeze() - x_r2.squeeze())
nikcleju@55 96 print 'diff2 = ',np.linalg.norm(x_r.squeeze() - x_r3.squeeze())
nikcleju@55 97 print 'diff3 = ',np.linalg.norm(x_r2.squeeze() - x_r3.squeeze())
nikcleju@55 98 print "Bob's total time = ", t1all
nikcleju@55 99 print "Nic's total time = ", t2all
nikcleju@55 100 print "Skl's total time = ", t3all
nikcleju@55 101 if np.linalg.norm(x_r.squeeze() - x_r2.squeeze()) > 1e-10 or \
nikcleju@55 102 np.linalg.norm(x_r.squeeze() - x_r3.squeeze()) > 1e-10 or \
nikcleju@55 103 np.linalg.norm(x_r2.squeeze() - x_r3.squeeze()) > 1e-10:
nikcleju@55 104 print "STOP: Different results"
nikcleju@55 105 print "Bob's residual: ||y - D x_r ||_2 = ",np.linalg.norm(y.squeeze() - np.dot(D,x_r).squeeze())
nikcleju@55 106 print "Nic's residual: ||y - D x_r ||_2 = ",np.linalg.norm(y.squeeze() - np.dot(D,x_r2).squeeze())
nikcleju@55 107 print "Skl's residual: ||y - D x_r ||_2 = ",np.linalg.norm(y.squeeze() - np.dot(D,x_r3).squeeze())
nikcleju@55 108 raise ValueError("Different results")
nikcleju@55 109
nikcleju@55 110 # debais to remove small entries
nikcleju@55 111 for nn in idx_r:
nikcleju@55 112 if abs(x_r[nn]) < 1e-10:
nikcleju@55 113 x_r[nn] = 0
nikcleju@55 114
nikcleju@55 115 # exact recovery condition using support
nikcleju@55 116 #if sorted(np.flatnonzero(x_r)) == sorted(np.flatnonzero(x)):
nikcleju@55 117 # successCounter += 1
nikcleju@55 118 # exact recovery condition using error in solution
nikcleju@55 119 error = x - x_r
nikcleju@55 120 """ the following is the exact recovery condition in: A. Maleki
nikcleju@55 121 and D. L. Donoho, "Optimally tuned iterative reconstruction
nikcleju@55 122 algorithms for compressed sensing," IEEE J. Selected Topics
nikcleju@55 123 in Signal Process., vol. 4, pp. 330-341, Apr. 2010. """
nikcleju@55 124 if np.vdot(error,error) < np.vdot(x,x)*1e-4:
nikcleju@55 125 successCounter += 1
nikcleju@55 126 trial -= 1
nikcleju@55 127
nikcleju@55 128 success[rhoCounter,deltaCounter] = successCounter
nikcleju@55 129 if successCounter == 0:
nikcleju@55 130 break
nikcleju@55 131
nikcleju@55 132 rhoCounter += 1
nikcleju@55 133 #np.savetxt('test.txt',success,fmt='#2.1d',delimiter=',')
nikcleju@55 134 deltaCounter += 1
nikcleju@55 135
nikcleju@55 136 if __name__ == '__main__':
nikcleju@55 137 print ('Running problem suite')
nikcleju@55 138 ambientDimension = 400
nikcleju@55 139 numGradations = 30
nikcleju@55 140 numTrials = 1
nikcleju@55 141
nikcleju@55 142 #import cProfile
nikcleju@55 143 #cProfile.run('runProblemSuite(ambientDimension,numGradations,numTrials)','profres')
nikcleju@55 144 runProblemSuite(ambientDimension,numGradations,numTrials)
nikcleju@55 145 print "Done"
nikcleju@55 146
nikcleju@55 147 #import pstats
nikcleju@55 148 #p = pstats.Stats('D:\Nic\Dev2\profres')
nikcleju@55 149 #p.sort_stats('cumulative').print_stats(10)