nikcleju@10
|
1 # -*- coding: utf-8 -*-
|
nikcleju@10
|
2 """
|
nikcleju@10
|
3 Created on Sat Nov 05 18:08:40 2011
|
nikcleju@10
|
4
|
nikcleju@10
|
5 @author: Nic
|
nikcleju@10
|
6 """
|
nikcleju@10
|
7
|
nikcleju@19
|
8 import numpy as np
|
nikcleju@22
|
9 import scipy.io
|
nikcleju@22
|
10 import math
|
nikcleju@33
|
11 import os
|
nikcleju@29
|
12
|
nikcleju@10
|
13 import pyCSalgos
|
nikcleju@19
|
14 import pyCSalgos.GAP.GAP
|
nikcleju@19
|
15 import pyCSalgos.SL0.SL0_approx
|
nikcleju@30
|
16 import pyCSalgos.OMP.omp_QR
|
nikcleju@30
|
17 import pyCSalgos.RecomTST.RecommendedTST
|
nikcleju@10
|
18
|
nikcleju@29
|
19 #==========================
|
nikcleju@29
|
20 # Algorithm functions
|
nikcleju@29
|
21 #==========================
|
nikcleju@22
|
22 def run_gap(y,M,Omega,epsilon):
|
nikcleju@19
|
23 gapparams = {"num_iteration" : 1000,\
|
nikcleju@19
|
24 "greedy_level" : 0.9,\
|
nikcleju@19
|
25 "stopping_coefficient_size" : 1e-4,\
|
nikcleju@19
|
26 "l2solver" : 'pseudoinverse',\
|
nikcleju@19
|
27 "noise_level": epsilon}
|
nikcleju@22
|
28 return pyCSalgos.GAP.GAP.GAP(y,M,M.T,Omega,Omega.T,gapparams,np.zeros(Omega.shape[1]))[0]
|
nikcleju@29
|
29
|
nikcleju@22
|
30 def run_sl0(y,M,Omega,D,U,S,Vt,epsilon,lbd):
|
nikcleju@19
|
31
|
nikcleju@19
|
32 N,n = Omega.shape
|
nikcleju@22
|
33 #D = np.linalg.pinv(Omega)
|
nikcleju@22
|
34 #U,S,Vt = np.linalg.svd(D)
|
nikcleju@19
|
35 aggDupper = np.dot(M,D)
|
nikcleju@19
|
36 aggDlower = Vt[-(N-n):,:]
|
nikcleju@19
|
37 aggD = np.concatenate((aggDupper, lbd * aggDlower))
|
nikcleju@19
|
38 aggy = np.concatenate((y, np.zeros(N-n)))
|
nikcleju@19
|
39
|
nikcleju@22
|
40 sigmamin = 0.001
|
nikcleju@22
|
41 sigma_decrease_factor = 0.5
|
nikcleju@20
|
42 mu_0 = 2
|
nikcleju@20
|
43 L = 10
|
nikcleju@22
|
44 return pyCSalgos.SL0.SL0_approx.SL0_approx(aggD,aggy,epsilon,sigmamin,sigma_decrease_factor,mu_0,L)
|
nikcleju@10
|
45
|
nikcleju@27
|
46 def run_bp(y,M,Omega,D,U,S,Vt,epsilon,lbd):
|
nikcleju@27
|
47
|
nikcleju@27
|
48 N,n = Omega.shape
|
nikcleju@27
|
49 #D = np.linalg.pinv(Omega)
|
nikcleju@27
|
50 #U,S,Vt = np.linalg.svd(D)
|
nikcleju@27
|
51 aggDupper = np.dot(M,D)
|
nikcleju@27
|
52 aggDlower = Vt[-(N-n):,:]
|
nikcleju@27
|
53 aggD = np.concatenate((aggDupper, lbd * aggDlower))
|
nikcleju@27
|
54 aggy = np.concatenate((y, np.zeros(N-n)))
|
nikcleju@27
|
55
|
nikcleju@27
|
56 sigmamin = 0.001
|
nikcleju@27
|
57 sigma_decrease_factor = 0.5
|
nikcleju@27
|
58 mu_0 = 2
|
nikcleju@27
|
59 L = 10
|
nikcleju@27
|
60 return pyCSalgos.SL0.SL0_approx.SL0_approx(aggD,aggy,epsilon,sigmamin,sigma_decrease_factor,mu_0,L)
|
nikcleju@27
|
61
|
nikcleju@30
|
62 def run_ompeps(y,M,Omega,D,U,S,Vt,epsilon,lbd):
|
nikcleju@30
|
63
|
nikcleju@30
|
64 N,n = Omega.shape
|
nikcleju@30
|
65 #D = np.linalg.pinv(Omega)
|
nikcleju@30
|
66 #U,S,Vt = np.linalg.svd(D)
|
nikcleju@30
|
67 aggDupper = np.dot(M,D)
|
nikcleju@30
|
68 aggDlower = Vt[-(N-n):,:]
|
nikcleju@30
|
69 aggD = np.concatenate((aggDupper, lbd * aggDlower))
|
nikcleju@30
|
70 aggy = np.concatenate((y, np.zeros(N-n)))
|
nikcleju@30
|
71
|
nikcleju@30
|
72 opts = dict()
|
nikcleju@30
|
73 opts['stopCrit'] = 'mse'
|
nikcleju@30
|
74 opts['stopTol'] = epsilon**2 / aggy.size
|
nikcleju@30
|
75 return pyCSalgos.OMP.omp_QR.greed_omp_qr(aggy,aggD,aggD.shape[1],opts)[0]
|
nikcleju@30
|
76
|
nikcleju@30
|
77 def run_tst(y,M,Omega,D,U,S,Vt,epsilon,lbd):
|
nikcleju@30
|
78
|
nikcleju@30
|
79 N,n = Omega.shape
|
nikcleju@30
|
80 #D = np.linalg.pinv(Omega)
|
nikcleju@30
|
81 #U,S,Vt = np.linalg.svd(D)
|
nikcleju@30
|
82 aggDupper = np.dot(M,D)
|
nikcleju@30
|
83 aggDlower = Vt[-(N-n):,:]
|
nikcleju@30
|
84 aggD = np.concatenate((aggDupper, lbd * aggDlower))
|
nikcleju@30
|
85 aggy = np.concatenate((y, np.zeros(N-n)))
|
nikcleju@30
|
86
|
nikcleju@32
|
87 nsweep = 300
|
nikcleju@32
|
88 tol = epsilon / np.linalg.norm(aggy)
|
nikcleju@32
|
89 return pyCSalgos.RecomTST.RecommendedTST.RecommendedTST(aggD, aggy, nsweep=nsweep, tol=tol)
|
nikcleju@30
|
90
|
nikcleju@29
|
91 #==========================
|
nikcleju@29
|
92 # Define tuples (algorithm function, name)
|
nikcleju@29
|
93 #==========================
|
nikcleju@22
|
94 gap = (run_gap, 'GAP')
|
nikcleju@30
|
95 sl0 = (run_sl0, 'SL0a')
|
nikcleju@29
|
96 bp = (run_bp, 'BP')
|
nikcleju@30
|
97 ompeps = (run_ompeps, 'OMPeps')
|
nikcleju@30
|
98 tst = (run_tst, 'TST')
|
nikcleju@10
|
99
|
nikcleju@32
|
100 #==========================
|
nikcleju@32
|
101 # Pool initializer function (multiprocessing)
|
nikcleju@32
|
102 # Needed to pass the shared variable to the worker processes
|
nikcleju@32
|
103 # The variables must be global in the module in order to be seen later in run_once_tuple()
|
nikcleju@32
|
104 # see http://stackoverflow.com/questions/1675766/how-to-combine-pool-map-with-array-shared-memory-in-python-multiprocessing
|
nikcleju@32
|
105 #==========================
|
nikcleju@32
|
106 def initProcess(share, njobs):
|
nikcleju@32
|
107 import sys
|
nikcleju@32
|
108 currmodule = sys.modules[__name__]
|
nikcleju@32
|
109 currmodule.proccount = share
|
nikcleju@32
|
110 currmodule.njobs = njobs
|
nikcleju@29
|
111
|
nikcleju@29
|
112 #==========================
|
nikcleju@33
|
113 # Standard parameters
|
nikcleju@29
|
114 #==========================
|
nikcleju@33
|
115 # Standard parameters 1
|
nikcleju@33
|
116 # All algorithms
|
nikcleju@33
|
117 # d=50, sigma = 2, delta and rho full resolution (0.05 step), lambdas = 0, 1e-4, 1e-2, 1, 100, 10000
|
nikcleju@33
|
118 # Do save data, do save plots, don't show plots
|
nikcleju@33
|
119 def std1():
|
nikcleju@33
|
120 # Define which algorithms to run
|
nikcleju@33
|
121 algosN = gap, # tuple of algorithms not depending on lambda
|
nikcleju@33
|
122 algosL = sl0,bp,ompeps,tst # tuple of algorithms depending on lambda (our ABS approach)
|
nikcleju@33
|
123
|
nikcleju@33
|
124 d = 50.0;
|
nikcleju@33
|
125 sigma = 2.0
|
nikcleju@33
|
126 deltas = np.arange(0.05,1.,0.05)
|
nikcleju@33
|
127 rhos = np.arange(0.05,1.,0.05)
|
nikcleju@33
|
128 #deltas = np.array([0.05, 0.45, 0.95])
|
nikcleju@33
|
129 #rhos = np.array([0.05, 0.45, 0.95])
|
nikcleju@33
|
130 numvects = 100; # Number of vectors to generate
|
nikcleju@33
|
131 SNRdb = 20.; # This is norm(signal)/norm(noise), so power, not energy
|
nikcleju@33
|
132 # Values for lambda
|
nikcleju@33
|
133 #lambdas = [0 10.^linspace(-5, 4, 10)];
|
nikcleju@33
|
134 lambdas = np.array([0., 0.0001, 0.01, 1, 100, 10000])
|
nikcleju@33
|
135
|
nikcleju@33
|
136 dosavedata = True
|
nikcleju@33
|
137 savedataname = 'approx_pt_std1.mat'
|
nikcleju@33
|
138 doshowplot = False
|
nikcleju@33
|
139 dosaveplot = True
|
nikcleju@33
|
140 saveplotbase = 'approx_pt_std1_'
|
nikcleju@33
|
141 saveplotexts = ('png','pdf','eps')
|
nikcleju@33
|
142
|
nikcleju@33
|
143 return algosN,algosL,d,sigma,deltas,rhos,lambdas,numvects,SNRdb,dosavedata,savedataname,\
|
nikcleju@33
|
144 doshowplot,dosaveplot,saveplotbase,saveplotexts
|
nikcleju@33
|
145
|
nikcleju@33
|
146 # Standard parameters 2
|
nikcleju@33
|
147 # Algorithms: GAP, SL0 and BP
|
nikcleju@33
|
148 # d=50, sigma = 2, delta and rho only 3 x 3, lambdas = 0, 1e-4, 1e-2, 1, 100, 10000
|
nikcleju@33
|
149 # Do save data, do save plots, don't show plots
|
nikcleju@33
|
150 # Useful for short testing
|
nikcleju@33
|
151 def std2():
|
nikcleju@33
|
152 # Define which algorithms to run
|
nikcleju@33
|
153 algosN = gap, # tuple of algorithms not depending on lambda
|
nikcleju@33
|
154 algosL = sl0,bp # tuple of algorithms depending on lambda (our ABS approach)
|
nikcleju@33
|
155
|
nikcleju@33
|
156 d = 50.0
|
nikcleju@33
|
157 sigma = 2.0
|
nikcleju@33
|
158 deltas = np.array([0.05, 0.45, 0.95])
|
nikcleju@33
|
159 rhos = np.array([0.05, 0.45, 0.95])
|
nikcleju@35
|
160 numvects = 10; # Number of vectors to generate
|
nikcleju@33
|
161 SNRdb = 20.; # This is norm(signal)/norm(noise), so power, not energy
|
nikcleju@33
|
162 # Values for lambda
|
nikcleju@33
|
163 #lambdas = [0 10.^linspace(-5, 4, 10)];
|
nikcleju@33
|
164 lambdas = np.array([0., 0.0001, 0.01, 1, 100, 10000])
|
nikcleju@33
|
165
|
nikcleju@33
|
166 dosavedata = True
|
nikcleju@33
|
167 savedataname = 'approx_pt_std2.mat'
|
nikcleju@33
|
168 doshowplot = False
|
nikcleju@33
|
169 dosaveplot = True
|
nikcleju@33
|
170 saveplotbase = 'approx_pt_std2_'
|
nikcleju@33
|
171 saveplotexts = ('png','pdf','eps')
|
nikcleju@33
|
172
|
nikcleju@33
|
173 return algosN,algosL,d,sigma,deltas,rhos,lambdas,numvects,SNRdb,dosavedata,savedataname,\
|
nikcleju@33
|
174 doshowplot,dosaveplot,saveplotbase,saveplotexts
|
nikcleju@33
|
175
|
nikcleju@33
|
176 #==========================
|
nikcleju@33
|
177 # Interface run functions
|
nikcleju@33
|
178 #==========================
|
nikcleju@33
|
179 def run_mp(std=std2,ncpus=None):
|
nikcleju@33
|
180
|
nikcleju@33
|
181 algosN,algosL,d,sigma,deltas,rhos,lambdas,numvects,SNRdb,dosavedata,savedataname,doshowplot,dosaveplot,saveplotbase,saveplotexts = std()
|
nikcleju@29
|
182 run_multi(algosN, algosL, d,sigma,deltas,rhos,lambdas,numvects,SNRdb,dosavedata=dosavedata,savedataname=savedataname,\
|
nikcleju@30
|
183 doparallel=True, ncpus=ncpus,\
|
nikcleju@30
|
184 doshowplot=doshowplot,dosaveplot=dosaveplot,saveplotbase=saveplotbase,saveplotexts=saveplotexts)
|
nikcleju@22
|
185
|
nikcleju@33
|
186 def run(std=std2):
|
nikcleju@33
|
187 algosN,algosL,d,sigma,deltas,rhos,lambdas,numvects,SNRdb,dosavedata,savedataname,doshowplot,dosaveplot,saveplotbase,saveplotexts = std()
|
nikcleju@29
|
188 run_multi(algosN, algosL, d,sigma,deltas,rhos,lambdas,numvects,SNRdb,dosavedata=dosavedata,savedataname=savedataname,\
|
nikcleju@30
|
189 doparallel=False,\
|
nikcleju@33
|
190 doshowplot=doshowplot,dosaveplot=dosaveplot,saveplotbase=saveplotbase,saveplotexts=saveplotexts)
|
nikcleju@29
|
191 #==========================
|
nikcleju@29
|
192 # Main functions
|
nikcleju@29
|
193 #==========================
|
nikcleju@29
|
194 def run_multi(algosN, algosL, d, sigma, deltas, rhos, lambdas, numvects, SNRdb,
|
nikcleju@29
|
195 doparallel=False, ncpus=None,\
|
nikcleju@29
|
196 doshowplot=False, dosaveplot=False, saveplotbase=None, saveplotexts=None,\
|
nikcleju@29
|
197 dosavedata=False, savedataname=None):
|
nikcleju@30
|
198
|
nikcleju@30
|
199 print "This is analysis recovery ABS approximation script by Nic"
|
nikcleju@30
|
200 print "Running phase transition ( run_multi() )"
|
nikcleju@29
|
201
|
nikcleju@29
|
202 if doparallel:
|
nikcleju@32
|
203 import multiprocessing
|
nikcleju@32
|
204 # Shared value holding the number of finished processes
|
nikcleju@32
|
205 # Add it as global of the module
|
nikcleju@32
|
206 import sys
|
nikcleju@32
|
207 currmodule = sys.modules[__name__]
|
nikcleju@32
|
208 currmodule.proccount = multiprocessing.Value('I', 0) # 'I' = unsigned int, see docs (multiprocessing, array)
|
nikcleju@29
|
209
|
nikcleju@30
|
210 if dosaveplot or doshowplot:
|
nikcleju@30
|
211 try:
|
nikcleju@30
|
212 import matplotlib
|
nikcleju@33
|
213 if doshowplot or os.name == 'nt':
|
nikcleju@30
|
214 print "Importing matplotlib with default (GUI) backend... ",
|
nikcleju@30
|
215 else:
|
nikcleju@30
|
216 print "Importing matplotlib with \"Cairo\" backend... ",
|
nikcleju@30
|
217 matplotlib.use('Cairo')
|
nikcleju@30
|
218 import matplotlib.pyplot as plt
|
nikcleju@30
|
219 import matplotlib.cm as cm
|
nikcleju@30
|
220 print "OK"
|
nikcleju@30
|
221 except:
|
nikcleju@30
|
222 print "FAIL"
|
nikcleju@30
|
223 print "Importing matplotlib.pyplot failed. No figures at all"
|
nikcleju@30
|
224 print "Try selecting a different backend"
|
nikcleju@30
|
225 doshowplot = False
|
nikcleju@30
|
226 dosaveplot = False
|
nikcleju@30
|
227
|
nikcleju@30
|
228 # Print summary of parameters
|
nikcleju@30
|
229 print "Parameters:"
|
nikcleju@30
|
230 if doparallel:
|
nikcleju@30
|
231 if ncpus is None:
|
nikcleju@30
|
232 print " Running in parallel with default threads using \"multiprocessing\" package"
|
nikcleju@30
|
233 else:
|
nikcleju@30
|
234 print " Running in parallel with",ncpus,"threads using \"multiprocessing\" package"
|
nikcleju@30
|
235 else:
|
nikcleju@30
|
236 print "Running single thread"
|
nikcleju@30
|
237 if doshowplot:
|
nikcleju@30
|
238 print " Showing figures"
|
nikcleju@30
|
239 else:
|
nikcleju@30
|
240 print " Not showing figures"
|
nikcleju@30
|
241 if dosaveplot:
|
nikcleju@30
|
242 print " Saving figures as "+saveplotbase+"* with extensions ",saveplotexts
|
nikcleju@30
|
243 else:
|
nikcleju@30
|
244 print " Not saving figures"
|
nikcleju@30
|
245 print " Running algorithms",[algotuple[1] for algotuple in algosN],[algotuple[1] for algotuple in algosL]
|
nikcleju@29
|
246
|
nikcleju@29
|
247 nalgosN = len(algosN)
|
nikcleju@29
|
248 nalgosL = len(algosL)
|
nikcleju@29
|
249
|
nikcleju@22
|
250 meanmatrix = dict()
|
nikcleju@22
|
251 for i,algo in zip(np.arange(nalgosN),algosN):
|
nikcleju@22
|
252 meanmatrix[algo[1]] = np.zeros((rhos.size, deltas.size))
|
nikcleju@22
|
253 for i,algo in zip(np.arange(nalgosL),algosL):
|
nikcleju@22
|
254 meanmatrix[algo[1]] = np.zeros((lambdas.size, rhos.size, deltas.size))
|
nikcleju@22
|
255
|
nikcleju@29
|
256 # Prepare parameters
|
nikcleju@29
|
257 jobparams = []
|
nikcleju@30
|
258 print " (delta, rho) pairs to be run:"
|
nikcleju@22
|
259 for idelta,delta in zip(np.arange(deltas.size),deltas):
|
nikcleju@22
|
260 for irho,rho in zip(np.arange(rhos.size),rhos):
|
nikcleju@22
|
261
|
nikcleju@22
|
262 # Generate data and operator
|
nikcleju@29
|
263 Omega,x0,y,M,realnoise = generateData(d,sigma,delta,rho,numvects,SNRdb)
|
nikcleju@22
|
264
|
nikcleju@29
|
265 #Save the parameters, and run after
|
nikcleju@30
|
266 print " delta = ",delta," rho = ",rho
|
nikcleju@29
|
267 jobparams.append((algosN,algosL, Omega,y,lambdas,realnoise,M,x0))
|
nikcleju@32
|
268
|
nikcleju@32
|
269 if doparallel:
|
nikcleju@32
|
270 currmodule.njobs = deltas.size * rhos.size
|
nikcleju@30
|
271 print "End of parameters"
|
nikcleju@30
|
272
|
nikcleju@29
|
273 # Run
|
nikcleju@29
|
274 jobresults = []
|
nikcleju@32
|
275
|
nikcleju@29
|
276 if doparallel:
|
nikcleju@32
|
277 pool = multiprocessing.Pool(4,initializer=initProcess,initargs=(currmodule.proccount,currmodule.njobs))
|
nikcleju@32
|
278 jobresults = pool.map(run_once_tuple, jobparams)
|
nikcleju@29
|
279 else:
|
nikcleju@29
|
280 for jobparam in jobparams:
|
nikcleju@29
|
281 jobresults.append(run_once(algosN,algosL,Omega,y,lambdas,realnoise,M,x0))
|
nikcleju@29
|
282
|
nikcleju@29
|
283 # Read results
|
nikcleju@29
|
284 idx = 0
|
nikcleju@29
|
285 for idelta,delta in zip(np.arange(deltas.size),deltas):
|
nikcleju@29
|
286 for irho,rho in zip(np.arange(rhos.size),rhos):
|
nikcleju@29
|
287 mrelerrN,mrelerrL = jobresults[idx]
|
nikcleju@29
|
288 idx = idx+1
|
nikcleju@22
|
289 for algotuple in algosN:
|
nikcleju@22
|
290 meanmatrix[algotuple[1]][irho,idelta] = 1 - mrelerrN[algotuple[1]]
|
nikcleju@22
|
291 if meanmatrix[algotuple[1]][irho,idelta] < 0 or math.isnan(meanmatrix[algotuple[1]][irho,idelta]):
|
nikcleju@22
|
292 meanmatrix[algotuple[1]][irho,idelta] = 0
|
nikcleju@22
|
293 for algotuple in algosL:
|
nikcleju@22
|
294 for ilbd in np.arange(lambdas.size):
|
nikcleju@22
|
295 meanmatrix[algotuple[1]][ilbd,irho,idelta] = 1 - mrelerrL[algotuple[1]][ilbd]
|
nikcleju@22
|
296 if meanmatrix[algotuple[1]][ilbd,irho,idelta] < 0 or math.isnan(meanmatrix[algotuple[1]][ilbd,irho,idelta]):
|
nikcleju@22
|
297 meanmatrix[algotuple[1]][ilbd,irho,idelta] = 0
|
nikcleju@22
|
298
|
nikcleju@32
|
299 # Save
|
nikcleju@32
|
300 if dosavedata:
|
nikcleju@32
|
301 tosave = dict()
|
nikcleju@32
|
302 tosave['meanmatrix'] = meanmatrix
|
nikcleju@32
|
303 tosave['d'] = d
|
nikcleju@32
|
304 tosave['sigma'] = sigma
|
nikcleju@32
|
305 tosave['deltas'] = deltas
|
nikcleju@32
|
306 tosave['rhos'] = rhos
|
nikcleju@32
|
307 tosave['numvects'] = numvects
|
nikcleju@32
|
308 tosave['SNRdb'] = SNRdb
|
nikcleju@32
|
309 tosave['lambdas'] = lambdas
|
nikcleju@35
|
310 # Save algo names as cell array
|
nikcleju@35
|
311 obj_arr = np.zeros((len(algosN)+len(algosL),), dtype=np.object)
|
nikcleju@35
|
312 idx = 0
|
nikcleju@35
|
313 for algotuple in algosN+algosL:
|
nikcleju@35
|
314 obj_arr[idx] = algotuple[1]
|
nikcleju@35
|
315 idx = idx+1
|
nikcleju@35
|
316 tosave['algonames'] = obj_arr
|
nikcleju@32
|
317 try:
|
nikcleju@32
|
318 scipy.io.savemat(savedataname, tosave)
|
nikcleju@32
|
319 except:
|
nikcleju@32
|
320 print "Save error"
|
nikcleju@22
|
321 # Show
|
nikcleju@29
|
322 if doshowplot or dosaveplot:
|
nikcleju@27
|
323 for algotuple in algosN:
|
nikcleju@29
|
324 algoname = algotuple[1]
|
nikcleju@27
|
325 plt.figure()
|
nikcleju@29
|
326 plt.imshow(meanmatrix[algoname], cmap=cm.gray, interpolation='nearest',origin='lower')
|
nikcleju@29
|
327 if dosaveplot:
|
nikcleju@29
|
328 for ext in saveplotexts:
|
nikcleju@34
|
329 plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight')
|
nikcleju@27
|
330 for algotuple in algosL:
|
nikcleju@29
|
331 algoname = algotuple[1]
|
nikcleju@27
|
332 for ilbd in np.arange(lambdas.size):
|
nikcleju@27
|
333 plt.figure()
|
nikcleju@29
|
334 plt.imshow(meanmatrix[algoname][ilbd], cmap=cm.gray, interpolation='nearest',origin='lower')
|
nikcleju@29
|
335 if dosaveplot:
|
nikcleju@29
|
336 for ext in saveplotexts:
|
nikcleju@34
|
337 plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
|
nikcleju@29
|
338 if doshowplot:
|
nikcleju@29
|
339 plt.show()
|
nikcleju@29
|
340
|
nikcleju@22
|
341 print "Finished."
|
nikcleju@22
|
342
|
nikcleju@29
|
343 def run_once_tuple(t):
|
nikcleju@32
|
344 results = run_once(*t)
|
nikcleju@32
|
345 import sys
|
nikcleju@32
|
346 currmodule = sys.modules[__name__]
|
nikcleju@32
|
347 currmodule.proccount.value = currmodule.proccount.value + 1
|
nikcleju@32
|
348 print "================================"
|
nikcleju@32
|
349 print "Finished job",currmodule.proccount.value,"of",currmodule.njobs
|
nikcleju@32
|
350 print "================================"
|
nikcleju@32
|
351 return results
|
nikcleju@10
|
352
|
nikcleju@29
|
353 def run_once(algosN,algosL,Omega,y,lambdas,realnoise,M,x0):
|
nikcleju@22
|
354
|
nikcleju@22
|
355 d = Omega.shape[1]
|
nikcleju@22
|
356
|
nikcleju@22
|
357 nalgosN = len(algosN)
|
nikcleju@22
|
358 nalgosL = len(algosL)
|
nikcleju@10
|
359
|
nikcleju@19
|
360 xrec = dict()
|
nikcleju@19
|
361 err = dict()
|
nikcleju@19
|
362 relerr = dict()
|
nikcleju@22
|
363
|
nikcleju@22
|
364 # Prepare storage variables for algorithms non-Lambda
|
nikcleju@22
|
365 for i,algo in zip(np.arange(nalgosN),algosN):
|
nikcleju@22
|
366 xrec[algo[1]] = np.zeros((d, y.shape[1]))
|
nikcleju@22
|
367 err[algo[1]] = np.zeros(y.shape[1])
|
nikcleju@22
|
368 relerr[algo[1]] = np.zeros(y.shape[1])
|
nikcleju@22
|
369 # Prepare storage variables for algorithms with Lambda
|
nikcleju@22
|
370 for i,algo in zip(np.arange(nalgosL),algosL):
|
nikcleju@22
|
371 xrec[algo[1]] = np.zeros((lambdas.size, d, y.shape[1]))
|
nikcleju@22
|
372 err[algo[1]] = np.zeros((lambdas.size, y.shape[1]))
|
nikcleju@22
|
373 relerr[algo[1]] = np.zeros((lambdas.size, y.shape[1]))
|
nikcleju@19
|
374
|
nikcleju@22
|
375 # Run algorithms non-Lambda
|
nikcleju@22
|
376 for iy in np.arange(y.shape[1]):
|
nikcleju@22
|
377 for algofunc,strname in algosN:
|
nikcleju@22
|
378 epsilon = 1.1 * np.linalg.norm(realnoise[:,iy])
|
nikcleju@22
|
379 xrec[strname][:,iy] = algofunc(y[:,iy],M,Omega,epsilon)
|
nikcleju@22
|
380 err[strname][iy] = np.linalg.norm(x0[:,iy] - xrec[strname][:,iy])
|
nikcleju@22
|
381 relerr[strname][iy] = err[strname][iy] / np.linalg.norm(x0[:,iy])
|
nikcleju@32
|
382 for algofunc,strname in algosN:
|
nikcleju@32
|
383 print strname,' : avg relative error = ',np.mean(relerr[strname])
|
nikcleju@22
|
384
|
nikcleju@22
|
385 # Run algorithms with Lambda
|
nikcleju@19
|
386 for ilbd,lbd in zip(np.arange(lambdas.size),lambdas):
|
nikcleju@19
|
387 for iy in np.arange(y.shape[1]):
|
nikcleju@22
|
388 D = np.linalg.pinv(Omega)
|
nikcleju@22
|
389 U,S,Vt = np.linalg.svd(D)
|
nikcleju@22
|
390 for algofunc,strname in algosL:
|
nikcleju@19
|
391 epsilon = 1.1 * np.linalg.norm(realnoise[:,iy])
|
nikcleju@22
|
392 gamma = algofunc(y[:,iy],M,Omega,D,U,S,Vt,epsilon,lbd)
|
nikcleju@22
|
393 xrec[strname][ilbd,:,iy] = np.dot(D,gamma)
|
nikcleju@19
|
394 err[strname][ilbd,iy] = np.linalg.norm(x0[:,iy] - xrec[strname][ilbd,:,iy])
|
nikcleju@19
|
395 relerr[strname][ilbd,iy] = err[strname][ilbd,iy] / np.linalg.norm(x0[:,iy])
|
nikcleju@19
|
396 print 'Lambda = ',lbd,' :'
|
nikcleju@32
|
397 for algofunc,strname in algosL:
|
nikcleju@32
|
398 print ' ',strname,' : avg relative error = ',np.mean(relerr[strname][ilbd,:])
|
nikcleju@10
|
399
|
nikcleju@22
|
400 # Prepare results
|
nikcleju@22
|
401 mrelerrN = dict()
|
nikcleju@22
|
402 for algotuple in algosN:
|
nikcleju@22
|
403 mrelerrN[algotuple[1]] = np.mean(relerr[algotuple[1]])
|
nikcleju@22
|
404 mrelerrL = dict()
|
nikcleju@22
|
405 for algotuple in algosL:
|
nikcleju@22
|
406 mrelerrL[algotuple[1]] = np.mean(relerr[algotuple[1]],1)
|
nikcleju@22
|
407
|
nikcleju@22
|
408 return mrelerrN,mrelerrL
|
nikcleju@29
|
409
|
nikcleju@29
|
410 def generateData(d,sigma,delta,rho,numvects,SNRdb):
|
nikcleju@29
|
411
|
nikcleju@29
|
412 # Process parameters
|
nikcleju@29
|
413 noiselevel = 1.0 / (10.0**(SNRdb/10.0));
|
nikcleju@29
|
414 p = round(sigma*d);
|
nikcleju@29
|
415 m = round(delta*d);
|
nikcleju@29
|
416 l = round(d - rho*m);
|
nikcleju@29
|
417
|
nikcleju@29
|
418 # Generate Omega and data based on parameters
|
nikcleju@29
|
419 Omega = pyCSalgos.GAP.GAP.Generate_Analysis_Operator(d, p);
|
nikcleju@29
|
420 # Optionally make Omega more coherent
|
nikcleju@29
|
421 U,S,Vt = np.linalg.svd(Omega);
|
nikcleju@29
|
422 Sdnew = S * (1+np.arange(S.size)) # Make D coherent, not Omega!
|
nikcleju@29
|
423 Snew = np.vstack((np.diag(Sdnew), np.zeros((Omega.shape[0] - Omega.shape[1], Omega.shape[1]))))
|
nikcleju@29
|
424 Omega = np.dot(U , np.dot(Snew,Vt))
|
nikcleju@29
|
425
|
nikcleju@29
|
426 # Generate data
|
nikcleju@29
|
427 x0,y,M,Lambda,realnoise = pyCSalgos.GAP.GAP.Generate_Data_Known_Omega(Omega, d,p,m,l,noiselevel, numvects,'l0');
|
nikcleju@29
|
428
|
nikcleju@29
|
429 return Omega,x0,y,M,realnoise
|
nikcleju@22
|
430
|
nikcleju@19
|
431 # Script main
|
nikcleju@19
|
432 if __name__ == "__main__":
|
nikcleju@27
|
433 #import cProfile
|
nikcleju@27
|
434 #cProfile.run('mainrun()', 'profile')
|
nikcleju@29
|
435 run()
|