annotate test_approx.py @ 21:d395461b92ae tip

Lots and lots of modifications. Approximate recovery script working.
author Nic Cleju <nikcleju@gmail.com>
date Mon, 23 Apr 2012 10:54:57 +0300
parents 2837cfeaf353
children
rev   line source
nikcleju@0 1 # -*- coding: utf-8 -*-
nikcleju@0 2 """
nikcleju@17 3 Main script for approximate reconstruction tests.
nikcleju@17 4 Author: Nicolae Cleju
nikcleju@19 5
nikcleju@17 6 """
nikcleju@17 7 __author__ = "Nicolae Cleju"
nikcleju@17 8 __license__ = "GPL"
nikcleju@17 9 __email__ = "nikcleju@gmail.com"
nikcleju@0 10
nikcleju@0 11
nikcleju@15 12 import numpy
nikcleju@0 13 import scipy.io
nikcleju@0 14 import math
nikcleju@0 15 import os
nikcleju@0 16 import time
nikcleju@17 17 import multiprocessing
nikcleju@17 18 import sys
nikcleju@0 19
nikcleju@17 20 # Try to do smart importing of matplotlib
nikcleju@15 21 try:
nikcleju@15 22 import matplotlib
nikcleju@15 23 if os.name == 'nt':
nikcleju@15 24 print "Importing matplotlib with default (GUI) backend... "
nikcleju@15 25 else:
nikcleju@15 26 print "Importing matplotlib with \"Cairo\" backend... "
nikcleju@15 27 matplotlib.use('Cairo')
nikcleju@15 28 import matplotlib.pyplot as plt
nikcleju@15 29 import matplotlib.cm as cm
nikcleju@15 30 import matplotlib.colors as mcolors
nikcleju@15 31 except:
nikcleju@15 32 print "FAIL"
nikcleju@15 33 print "Importing matplotlib.pyplot failed. No figures at all"
nikcleju@15 34 print "Try selecting a different backend"
nikcleju@15 35
nikcleju@15 36 currmodule = sys.modules[__name__]
nikcleju@17 37 printLock = None # Lock for printing in a thread-safe way
nikcleju@17 38 # Thread-safe variable to store number of finished tasks
nikcleju@15 39 proccount = multiprocessing.Value('I', 0) # 'I' = unsigned int, see docs (multiprocessing, array)
nikcleju@15 40
nikcleju@17 41 # Contains pre-defined simulation parameters
nikcleju@13 42 import stdparams_approx
nikcleju@17 43
nikcleju@17 44 # Analysis operator and data generation functions
nikcleju@13 45 import AnalysisGenerate
nikcleju@0 46
nikcleju@13 47 # For exceptions
nikcleju@13 48 import pyCSalgos.BP.l1qec
nikcleju@13 49 import pyCSalgos.BP.l1qc
nikcleju@13 50 import pyCSalgos.NESTA.NESTA
nikcleju@21 51 import pyCSalgos.SL0.EllipseProj
nikcleju@15 52
nikcleju@19 53 # For plotting with right axes
nikcleju@19 54 import utils
nikcleju@19 55
nikcleju@17 56
nikcleju@17 57
nikcleju@15 58 def initProcess(share, ntasks, printLock):
nikcleju@15 59 """
nikcleju@15 60 Pool initializer function (multiprocessing)
nikcleju@15 61 Needed to pass the shared variable to the worker processes
nikcleju@15 62 The variables must be global in the module in order to be seen later in run_once_tuple()
nikcleju@15 63 see http://stackoverflow.com/questions/1675766/how-to-combine-pool-map-with-array-shared-memory-in-python-multiprocessing
nikcleju@15 64 """
nikcleju@0 65 currmodule = sys.modules[__name__]
nikcleju@0 66 currmodule.proccount = share
nikcleju@15 67 currmodule.ntasks = ntasks
nikcleju@21 68 currmodule.printLock = printLock
nikcleju@15 69
nikcleju@15 70 def generateTaskParams(globalparams):
nikcleju@15 71 """
nikcleju@17 72 Generate a list of task parameters (for parallel running)
nikcleju@15 73 """
nikcleju@15 74 taskparams = []
nikcleju@15 75 SNRdb = globalparams['SNRdb']
nikcleju@15 76 sigma = globalparams['sigma']
nikcleju@15 77 d = globalparams['d']
nikcleju@15 78 deltas = globalparams['deltas']
nikcleju@15 79 rhos = globalparams['rhos']
nikcleju@15 80 numvects = globalparams['numvects']
nikcleju@15 81 algosN = globalparams['algosN']
nikcleju@15 82 algosL = globalparams['algosL']
nikcleju@15 83 lambdas = globalparams['lambdas']
nikcleju@15 84
nikcleju@15 85 # Process parameters
nikcleju@21 86 # noiselevel = norm(noise)/norm(signal) = SNR**(-1/2) = 10**-(SNRdb/20)
nikcleju@21 87 noiselevel = 1.0 / (10.0**(SNRdb/20.0));
nikcleju@15 88
nikcleju@15 89 for delta in deltas:
nikcleju@15 90 for rho in rhos:
nikcleju@15 91 p = round(sigma*d);
nikcleju@15 92 m = round(delta*d);
nikcleju@15 93 l = round(d - rho*m);
nikcleju@15 94
nikcleju@15 95 # Generate Omega and data based on parameters
nikcleju@15 96 Omega = AnalysisGenerate.Generate_Analysis_Operator(d, p);
nikcleju@15 97 # Optionally make Omega more coherent
nikcleju@15 98 #U,S,Vt = numpy.linalg.svd(Omega);
nikcleju@15 99 #Sdnew = S * (1+numpy.arange(S.size)) # Make D coherent, not Omega!
nikcleju@15 100 #Snew = numpy.vstack((numpy.diag(Sdnew), numpy.zeros((Omega.shape[0] - Omega.shape[1], Omega.shape[1]))))
nikcleju@15 101 #Omega = numpy.dot(U , numpy.dot(Snew,Vt))
nikcleju@15 102
nikcleju@15 103 # Generate data
nikcleju@15 104 x0,y,M,Lambda,realnoise = AnalysisGenerate.Generate_Data_Known_Omega(Omega, d,p,m,l,noiselevel, numvects,'l0')
nikcleju@0 105
nikcleju@15 106 # Append task params
nikcleju@15 107 taskparams.append((algosN,algosL, Omega,y,lambdas,realnoise,M,x0))
nikcleju@0 108
nikcleju@15 109 return taskparams
nikcleju@0 110
nikcleju@19 111 def processResults(params, taskparams, taskresults):
nikcleju@15 112 """
nikcleju@15 113 Process the raw task results
nikcleju@15 114 """
nikcleju@15 115 deltas = params['deltas']
nikcleju@15 116 rhos = params['rhos']
nikcleju@15 117 algosN = params['algosN']
nikcleju@15 118 algosL = params['algosL']
nikcleju@15 119 lambdas = params['lambdas']
nikcleju@19 120 numvects = params['numvects']
nikcleju@0 121
nikcleju@19 122 err = dict()
nikcleju@19 123 relerr = dict()
nikcleju@19 124 mrelerrN = dict()
nikcleju@19 125 mrelerrL = dict()
nikcleju@0 126 meanmatrix = dict()
nikcleju@0 127 elapsed = dict()
nikcleju@19 128
nikcleju@19 129 # Prepare storage variables for algorithms non-Lambda
nikcleju@15 130 for algo in algosN:
nikcleju@19 131 err[algo[1]] = numpy.zeros(numvects)
nikcleju@19 132 relerr[algo[1]] = numpy.zeros(numvects)
nikcleju@15 133 meanmatrix[algo[1]] = numpy.zeros((rhos.size, deltas.size))
nikcleju@0 134 elapsed[algo[1]] = 0
nikcleju@19 135
nikcleju@19 136 # Prepare storage variables for algorithms with Lambda
nikcleju@15 137 for algo in algosL:
nikcleju@19 138 err[algo[1]] = numpy.zeros((lambdas.size, numvects))
nikcleju@19 139 relerr[algo[1]] = numpy.zeros((lambdas.size, numvects))
nikcleju@15 140 meanmatrix[algo[1]] = numpy.zeros((lambdas.size, rhos.size, deltas.size))
nikcleju@15 141 elapsed[algo[1]] = numpy.zeros(lambdas.size)
nikcleju@0 142
nikcleju@15 143 # Process results
nikcleju@0 144 idx = 0
nikcleju@15 145 for idelta,delta in zip(numpy.arange(deltas.size),deltas):
nikcleju@15 146 for irho,rho in zip(numpy.arange(rhos.size),rhos):
nikcleju@19 147 algosN,algosL,Omega,y,lambdas,realnoise,M,x0 = taskparams[idx]
nikcleju@19 148 xrec,addelapsed = taskresults[idx]
nikcleju@0 149 idx = idx+1
nikcleju@19 150
nikcleju@19 151 # Optionally compare not with original signal x0, but with solution of
nikcleju@19 152 # another algorithm (e.g. GAP, NESTA etc)
nikcleju@19 153 if 'reference_signal' in params:
nikcleju@19 154 xref = xrec[params['reference_signal']]
nikcleju@19 155 else:
nikcleju@19 156 xref = x0
nikcleju@19 157
nikcleju@19 158 # Compute errors and relative errors
nikcleju@19 159 for iy in numpy.arange(y.shape[1]):
nikcleju@19 160 for algofunc,algoname in algosN:
nikcleju@19 161 err[algoname][iy] = numpy.linalg.norm(xref[:,iy] - xrec[algoname][:,iy])
nikcleju@19 162 relerr[algoname][iy] = err[algoname][iy] / numpy.linalg.norm(xref[:,iy])
nikcleju@19 163 for ilbd,lbd in zip(numpy.arange(lambdas.size),lambdas):
nikcleju@19 164 for iy in numpy.arange(y.shape[1]):
nikcleju@19 165 for algofunc,algoname in algosL:
nikcleju@19 166 err[algoname][ilbd,iy] = numpy.linalg.norm(xref[:,iy] - xrec[algoname][ilbd,:,iy])
nikcleju@19 167 relerr[algoname][ilbd,iy] = err[algoname][ilbd,iy] / numpy.linalg.norm(xref[:,iy])
nikcleju@19 168
nikcleju@19 169 # Compute average relative errors and prepare matrix to plot
nikcleju@19 170 for algofunc,algoname in algosN:
nikcleju@19 171 mrelerrN[algoname] = numpy.mean(relerr[algoname])
nikcleju@19 172 meanmatrix[algoname][irho,idelta] = 1 - mrelerrN[algoname]
nikcleju@19 173 if meanmatrix[algoname][irho,idelta] < 0 or math.isnan(meanmatrix[algoname][irho,idelta]):
nikcleju@19 174 meanmatrix[algoname][irho,idelta] = 0
nikcleju@19 175 elapsed[algoname] = elapsed[algoname] + addelapsed[algoname]
nikcleju@19 176 for algofunc, algoname in algosL:
nikcleju@15 177 for ilbd in numpy.arange(lambdas.size):
nikcleju@19 178 mrelerrL[algoname] = numpy.mean(relerr[algoname],1)
nikcleju@19 179 meanmatrix[algoname][ilbd,irho,idelta] = 1 - mrelerrL[algoname][ilbd]
nikcleju@19 180 if meanmatrix[algoname][ilbd,irho,idelta] < 0 or math.isnan(meanmatrix[algoname][ilbd,irho,idelta]):
nikcleju@19 181 meanmatrix[algoname][ilbd,irho,idelta] = 0
nikcleju@19 182 elapsed[algoname][ilbd] = elapsed[algoname][ilbd] + addelapsed[algoname][ilbd]
nikcleju@0 183
nikcleju@15 184 procresults = dict()
nikcleju@15 185 procresults['meanmatrix'] = meanmatrix
nikcleju@15 186 procresults['elapsed'] = elapsed
nikcleju@15 187 return procresults
nikcleju@15 188
nikcleju@15 189 def saveSim(params, procresults):
nikcleju@15 190 """
nikcleju@15 191 Save simulation to mat file
nikcleju@15 192 """
nikcleju@15 193 tosave = dict()
nikcleju@15 194 tosave['meanmatrix'] = procresults['meanmatrix']
nikcleju@15 195 tosave['elapsed'] = procresults['elapsed']
nikcleju@15 196 tosave['d'] = params['d']
nikcleju@15 197 tosave['sigma'] = params['sigma']
nikcleju@15 198 tosave['deltas'] = params['deltas']
nikcleju@15 199 tosave['rhos'] = params['rhos']
nikcleju@15 200 tosave['numvects'] = params['numvects']
nikcleju@15 201 tosave['SNRdb'] = params['SNRdb']
nikcleju@15 202 tosave['lambdas'] = params['lambdas']
nikcleju@15 203 tosave['saveplotbase'] = params['saveplotbase']
nikcleju@15 204 tosave['saveplotexts'] = params['saveplotexts']
nikcleju@15 205
nikcleju@15 206 # Save algo names as cell array
nikcleju@15 207 obj_arr = numpy.zeros((len(params['algosN']),), dtype=numpy.object)
nikcleju@15 208 idx = 0
nikcleju@15 209 for algotuple in params['algosN']:
nikcleju@15 210 obj_arr[idx] = algotuple[1]
nikcleju@15 211 idx = idx+1
nikcleju@15 212 tosave['algosNnames'] = obj_arr
nikcleju@15 213
nikcleju@15 214 obj_arr = numpy.zeros((len(params['algosL']),), dtype=numpy.object)
nikcleju@15 215 idx = 0
nikcleju@15 216 for algotuple in params['algosL']:
nikcleju@15 217 obj_arr[idx] = algotuple[1]
nikcleju@15 218 idx = idx+1
nikcleju@15 219 tosave['algosLnames'] = obj_arr
nikcleju@15 220
nikcleju@15 221 try:
nikcleju@15 222 scipy.io.savemat(params['savedataname'], tosave)
nikcleju@15 223 except:
nikcleju@15 224 print "Save error"
nikcleju@15 225
nikcleju@15 226 def loadSim(savedataname):
nikcleju@15 227 """
nikcleju@15 228 Load simulation from mat file
nikcleju@15 229 """
nikcleju@15 230 mdict = scipy.io.loadmat(savedataname)
nikcleju@15 231
nikcleju@15 232 params = dict()
nikcleju@15 233 procresults = dict()
nikcleju@15 234
nikcleju@15 235 procresults['meanmatrix'] = mdict['meanmatrix'][0,0]
nikcleju@15 236 procresults['elapsed'] = mdict['elapsed']
nikcleju@15 237 params['d'] = mdict['d']
nikcleju@15 238 params['sigma'] = mdict['sigma']
nikcleju@15 239 params['deltas'] = mdict['deltas']
nikcleju@15 240 params['rhos'] = mdict['rhos']
nikcleju@15 241 params['numvects'] = mdict['numvects']
nikcleju@15 242 params['SNRdb'] = mdict['SNRdb']
nikcleju@15 243 params['lambdas'] = mdict['lambdas']
nikcleju@15 244 params['saveplotbase'] = mdict['saveplotbase'][0]
nikcleju@15 245 params['saveplotexts'] = mdict['saveplotexts']
nikcleju@15 246
nikcleju@15 247 algonames = mdict['algosNnames'][:,0]
nikcleju@15 248 params['algosNnames'] = []
nikcleju@15 249 for algoname in algonames:
nikcleju@15 250 params['algosNnames'].append(algoname[0])
nikcleju@15 251
nikcleju@15 252 algonames = mdict['algosLnames'][:,0]
nikcleju@15 253 params['algosLnames'] = []
nikcleju@15 254 for algoname in algonames:
nikcleju@15 255 params['algosLnames'].append(algoname[0])
nikcleju@15 256
nikcleju@15 257 return params, procresults
nikcleju@15 258
nikcleju@15 259 def plot(savedataname):
nikcleju@15 260 """
nikcleju@17 261 Plot results from a mat file.
nikcleju@17 262 The files are saved in the current folder.
nikcleju@15 263 """
nikcleju@15 264 params, procresults = loadSim(savedataname)
nikcleju@15 265 meanmatrix = procresults['meanmatrix']
nikcleju@15 266 saveplotbase = params['saveplotbase']
nikcleju@15 267 saveplotexts = params['saveplotexts']
nikcleju@15 268 algosNnames = params['algosNnames']
nikcleju@15 269 algosLnames = params['algosLnames']
nikcleju@15 270 lambdas = params['lambdas']
nikcleju@15 271
nikcleju@15 272 for algoname in algosNnames:
nikcleju@15 273 plt.figure()
nikcleju@15 274 plt.imshow(meanmatrix[algoname], cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower')
nikcleju@15 275 for ext in saveplotexts:
nikcleju@15 276 plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight')
nikcleju@15 277 for algoname in algosLnames:
nikcleju@15 278 for ilbd in numpy.arange(lambdas.size):
nikcleju@15 279 plt.figure()
nikcleju@15 280 plt.imshow(meanmatrix[algoname][ilbd], cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower')
nikcleju@15 281 for ext in saveplotexts:
nikcleju@15 282 plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
nikcleju@15 283
nikcleju@19 284 def plotProveEll1(savedataname):
nikcleju@19 285 """
nikcleju@19 286 Plot ...
nikcleju@19 287 The files are saved in the current folder.
nikcleju@19 288 """
nikcleju@19 289 params, procresults = loadSim(savedataname)
nikcleju@19 290 meanmatrix = procresults['meanmatrix']
nikcleju@19 291 saveplotbase = params['saveplotbase']
nikcleju@19 292 saveplotexts = params['saveplotexts']
nikcleju@19 293 algosNnames = params['algosNnames']
nikcleju@19 294 algosLnames = params['algosLnames']
nikcleju@19 295 lambdas = params['lambdas']
nikcleju@19 296
nikcleju@19 297 toplot = numpy.zeros((len(lambdas),len(algosNnames) + len(algosLnames)))
nikcleju@19 298
nikcleju@19 299 idxcol = 0
nikcleju@19 300 for algoname in algosNnames:
nikcleju@19 301 toplot[:,idxcol] = (1 - meanmatrix[algoname][0,0]) * numpy.ones(len(lambdas))
nikcleju@19 302 idxcol = idxcol + 1
nikcleju@19 303 for algoname in algosLnames:
nikcleju@19 304 for ilbd in numpy.arange(len(lambdas)):
nikcleju@19 305 toplot[ilbd,idxcol] = 1 - meanmatrix[algoname][ilbd][0,0]
nikcleju@19 306 idxcol = idxcol + 1
nikcleju@19 307
nikcleju@19 308 plt.figure()
nikcleju@19 309 plt.plot(toplot)
nikcleju@19 310 for ext in saveplotexts:
nikcleju@19 311 plt.savefig(saveplotbase + '.' + ext, bbox_inches='tight')
nikcleju@19 312
nikcleju@15 313 #==========================
nikcleju@15 314 # Main function
nikcleju@15 315 #==========================
nikcleju@15 316 def run(params):
nikcleju@17 317 """
nikcleju@17 318 Run simulation with given parameters
nikcleju@17 319 """
nikcleju@17 320
nikcleju@15 321 print "This is analysis recovery ABS approximation script by Nic"
nikcleju@17 322 print "Running simulation"
nikcleju@15 323
nikcleju@15 324 algosN = params['algosN']
nikcleju@15 325 algosL = params['algosL']
nikcleju@15 326 d = params['d']
nikcleju@15 327 sigma = params['sigma']
nikcleju@15 328 deltas = params['deltas']
nikcleju@15 329 rhos = params['rhos']
nikcleju@15 330 lambdas = params['lambdas']
nikcleju@15 331 numvects = params['numvects']
nikcleju@15 332 SNRdb = params['SNRdb']
nikcleju@18 333 if 'ncpus' in params:
nikcleju@18 334 ncpus = params['ncpus']
nikcleju@18 335 else:
nikcleju@18 336 ncpus = None
nikcleju@15 337 savedataname = params['savedataname']
nikcleju@15 338
nikcleju@15 339 if ncpus is None:
nikcleju@15 340 print " Running in parallel with default",multiprocessing.cpu_count(),"threads using \"multiprocessing\" package"
nikcleju@15 341 if multiprocessing.cpu_count() == 1:
nikcleju@15 342 doparallel = False
nikcleju@15 343 else:
nikcleju@15 344 doparallel = True
nikcleju@15 345 elif ncpus > 1:
nikcleju@15 346 print " Running in parallel with",ncpus,"threads using \"multiprocessing\" package"
nikcleju@15 347 doparallel = True
nikcleju@15 348 elif ncpus == 1:
nikcleju@15 349 print "Running single thread"
nikcleju@15 350 doparallel = False
nikcleju@15 351 else:
nikcleju@15 352 print "Wrong number of threads, exiting"
nikcleju@15 353 return
nikcleju@15 354
nikcleju@15 355 # Print summary of parameters
nikcleju@15 356 print "Parameters:"
nikcleju@15 357 print " Running algorithms",[algotuple[1] for algotuple in algosN],[algotuple[1] for algotuple in algosL]
nikcleju@15 358
nikcleju@15 359 # Prepare parameters
nikcleju@18 360 print "Generating task parameters..."
nikcleju@15 361 taskparams = generateTaskParams(params)
nikcleju@15 362
nikcleju@15 363 # Store global variables
nikcleju@15 364 currmodule.ntasks = len(taskparams)
nikcleju@15 365
nikcleju@15 366 # Run
nikcleju@18 367 print "Running..."
nikcleju@15 368 taskresults = []
nikcleju@15 369 if doparallel:
nikcleju@15 370 currmodule.printLock = multiprocessing.Lock()
nikcleju@15 371 pool = multiprocessing.Pool(ncpus,initializer=initProcess,initargs=(currmodule.proccount,currmodule.ntasks,currmodule.printLock))
nikcleju@15 372 taskresults = pool.map(run_once_tuple, taskparams)
nikcleju@21 373 pool.close()
nikcleju@21 374 pool.join()
nikcleju@15 375 else:
nikcleju@15 376 for taskparam in taskparams:
nikcleju@15 377 taskresults.append(run_once_tuple(taskparam))
nikcleju@15 378
nikcleju@15 379 # Process results
nikcleju@19 380 procresults = processResults(params, taskparams, taskresults)
nikcleju@15 381
nikcleju@0 382 # Save
nikcleju@15 383 saveSim(params, procresults)
nikcleju@15 384
nikcleju@0 385 print "Finished."
nikcleju@0 386
nikcleju@0 387 def run_once_tuple(t):
nikcleju@17 388 """
nikcleju@17 389 Wrapper for run_once() that explodes the tuple argument t and shows
nikcleju@17 390 the number of finished / remaining tasks
nikcleju@17 391 """
nikcleju@17 392
nikcleju@17 393 # Call run_once() here
nikcleju@0 394 results = run_once(*t)
nikcleju@17 395
nikcleju@17 396 if currmodule.printLock:
nikcleju@17 397 currmodule.printLock.acquire()
nikcleju@17 398
nikcleju@17 399 currmodule.proccount.value = currmodule.proccount.value + 1
nikcleju@17 400 print "================================"
nikcleju@17 401 print "Finished task",currmodule.proccount.value,"of",currmodule.ntasks
nikcleju@17 402 print "================================"
nikcleju@17 403
nikcleju@17 404 currmodule.printLock.release()
nikcleju@17 405
nikcleju@0 406 return results
nikcleju@0 407
nikcleju@0 408 def run_once(algosN,algosL,Omega,y,lambdas,realnoise,M,x0):
nikcleju@17 409 """
nikcleju@17 410 Run single task (i.e. task function)
nikcleju@17 411 """
nikcleju@0 412
nikcleju@0 413 d = Omega.shape[1]
nikcleju@0 414
nikcleju@0 415 xrec = dict()
nikcleju@19 416 # err = dict()
nikcleju@19 417 # relerr = dict()
nikcleju@0 418 elapsed = dict()
nikcleju@0 419
nikcleju@0 420 # Prepare storage variables for algorithms non-Lambda
nikcleju@15 421 for algo in algosN:
nikcleju@15 422 xrec[algo[1]] = numpy.zeros((d, y.shape[1]))
nikcleju@0 423 elapsed[algo[1]] = 0
nikcleju@0 424 # Prepare storage variables for algorithms with Lambda
nikcleju@15 425 for algo in algosL:
nikcleju@15 426 xrec[algo[1]] = numpy.zeros((lambdas.size, d, y.shape[1]))
nikcleju@15 427 elapsed[algo[1]] = numpy.zeros(lambdas.size)
nikcleju@0 428
nikcleju@0 429 # Run algorithms non-Lambda
nikcleju@15 430 for iy in numpy.arange(y.shape[1]):
nikcleju@0 431 for algofunc,strname in algosN:
nikcleju@15 432 epsilon = 1.1 * numpy.linalg.norm(realnoise[:,iy])
nikcleju@0 433 try:
nikcleju@0 434 timestart = time.time()
nikcleju@0 435 xrec[strname][:,iy] = algofunc(y[:,iy],M,Omega,epsilon)
nikcleju@0 436 elapsed[strname] = elapsed[strname] + (time.time() - timestart)
nikcleju@0 437 except pyCSalgos.BP.l1qec.l1qecInputValueError as e:
nikcleju@0 438 print "Caught exception when running algorithm",strname," :",e.message
nikcleju@0 439 except pyCSalgos.NESTA.NESTA.NestaError as e:
nikcleju@0 440 print "Caught exception when running algorithm",strname," :",e.message
nikcleju@21 441 except pyCSalgos.SL0.EllipseProj.EllipseProjDaiError as e:
nikcleju@21 442 print "Caught exception when running algorithm",strname," :",e.message
nikcleju@0 443
nikcleju@0 444 # Run algorithms with Lambda
nikcleju@15 445 for ilbd,lbd in zip(numpy.arange(lambdas.size),lambdas):
nikcleju@15 446 for iy in numpy.arange(y.shape[1]):
nikcleju@15 447 D = numpy.linalg.pinv(Omega)
nikcleju@15 448 #U,S,Vt = numpy.linalg.svd(D)
nikcleju@0 449 for algofunc,strname in algosL:
nikcleju@15 450 epsilon = 1.1 * numpy.linalg.norm(realnoise[:,iy])
nikcleju@0 451 try:
nikcleju@0 452 timestart = time.time()
nikcleju@13 453 #gamma = algofunc(y[:,iy],M,Omega,D,U,S,Vt,epsilon,lbd)
nikcleju@19 454 xrec[strname][ilbd][:,iy] = algofunc(y[:,iy],M,Omega,epsilon,lbd)
nikcleju@0 455 elapsed[strname][ilbd] = elapsed[strname][ilbd] + (time.time() - timestart)
nikcleju@0 456 except pyCSalgos.BP.l1qc.l1qcInputValueError as e:
nikcleju@0 457 print "Caught exception when running algorithm",strname," :",e.message
nikcleju@21 458 except pyCSalgos.SL0.EllipseProj.EllipseProjDaiError as e:
nikcleju@21 459 print "Caught exception when running algorithm",strname," :",e.message
nikcleju@21 460
nikcleju@0 461
nikcleju@19 462 return xrec, elapsed
nikcleju@19 463
nikcleju@19 464 def generateFigProveEll1():
nikcleju@19 465 """
nikcleju@19 466 Generates figure xxx (prove theorem IV.2 in the ell_1 case).
nikcleju@19 467 Figures are saved in the current folder.
nikcleju@19 468 """
nikcleju@19 469 #paramsl1prove['reference_signal'] = nesta[1] # 'NESTA'
nikcleju@19 470 run(stdparams_approx.paramsl1prove)
nikcleju@21 471 #plotProveEll1(stdparams_approx.paramsl1prove['savedataname'])
nikcleju@21 472 utils.replot_ProveEll1(stdparams_approx.paramsl1prove['savedataname'],
nikcleju@21 473 algonames = None, # will read them from mat file
nikcleju@21 474 doshow=False,
nikcleju@21 475 dosave=True,
nikcleju@21 476 saveplotbase=stdparams_approx.paramsl1prove['saveplotbase'],
nikcleju@21 477 saveplotexts=stdparams_approx.paramsl1prove['saveplotexts'])
nikcleju@0 478
nikcleju@17 479 def generateFig():
nikcleju@17 480 """
nikcleju@17 481 Generates figures.
nikcleju@17 482 Figures are saved in the current folder.
nikcleju@17 483 """
nikcleju@21 484 #stdparams_approx.params1['ncpus'] = 1
nikcleju@21 485 run(stdparams_approx.params1)
nikcleju@21 486 utils.replot_approx(stdparams_approx.params1['savedataname'],
nikcleju@21 487 algonames = None, # will read them from mat file
nikcleju@21 488 doshow=False,
nikcleju@21 489 dosave=True,
nikcleju@21 490 saveplotbase=stdparams_approx.params1['saveplotbase'],
nikcleju@21 491 saveplotexts=stdparams_approx.params1['saveplotexts'])
nikcleju@21 492
nikcleju@21 493 #stdparams_approx.params2['ncpus'] = 1
nikcleju@21 494 run(stdparams_approx.params2)
nikcleju@21 495 utils.replot_approx(stdparams_approx.params2['savedataname'],
nikcleju@21 496 algonames = None, # will read them from mat file
nikcleju@21 497 doshow=False,
nikcleju@21 498 dosave=True,
nikcleju@21 499 saveplotbase=stdparams_approx.params2['saveplotbase'],
nikcleju@21 500 saveplotexts=stdparams_approx.params2['saveplotexts'])
nikcleju@21 501
nikcleju@21 502 #stdparams_approx.params3['ncpus'] = 1
nikcleju@21 503 run(stdparams_approx.params3)
nikcleju@21 504 utils.replot_approx(stdparams_approx.params3['savedataname'],
nikcleju@21 505 algonames = None, # will read them from mat file
nikcleju@21 506 doshow=False,
nikcleju@21 507 dosave=True,
nikcleju@21 508 saveplotbase=stdparams_approx.params3['saveplotbase'],
nikcleju@21 509 saveplotexts=stdparams_approx.params3['saveplotexts'])
nikcleju@21 510
nikcleju@21 511 #stdparams_approx.params4['ncpus'] = 1
nikcleju@21 512 run(stdparams_approx.params4)
nikcleju@21 513 utils.replot_approx(stdparams_approx.params4['savedataname'],
nikcleju@21 514 algonames = None, # will read them from mat file
nikcleju@21 515 doshow=False,
nikcleju@21 516 dosave=True,
nikcleju@21 517 saveplotbase=stdparams_approx.params4['saveplotbase'],
nikcleju@21 518 saveplotexts=stdparams_approx.params4['saveplotexts'])
nikcleju@21 519
nikcleju@0 520 # Script main
nikcleju@0 521 if __name__ == "__main__":
nikcleju@17 522
nikcleju@19 523 #stdparams_approx.paramsl1prove['ncpus'] = 1
nikcleju@19 524 #generateFigProveEll1()
nikcleju@19 525
nikcleju@19 526 #generateFig()
nikcleju@15 527
nikcleju@17 528 # Run test parameters
nikcleju@17 529 #stdparams_approx.paramstest['ncpus'] = 1
nikcleju@17 530 #run(stdparams_approx.paramstest)
nikcleju@17 531 #plot(stdparams_approx.paramstest['savedataname'])
nikcleju@21 532 #utils.replot_approx(stdparams_approx.paramstest['savedataname'], algonames = None,doshow=False,dosave=True,saveplotbase=stdparams_approx.paramstest['saveplotbase'],saveplotexts=stdparams_approx.paramstest['saveplotexts'])
nikcleju@21 533
nikcleju@21 534 #stdparams_approx.params5['ncpus'] = 3
nikcleju@21 535 #run(stdparams_approx.params5)
nikcleju@21 536 #utils.replot_approx(stdparams_approx.params5['savedataname'],
nikcleju@21 537 # algonames = None, # will read them from mat file
nikcleju@21 538 # doshow=False,
nikcleju@21 539 # dosave=True,
nikcleju@21 540 # saveplotbase=stdparams_approx.params5['saveplotbase'],
nikcleju@21 541 # saveplotexts=stdparams_approx.params5['saveplotexts'])
nikcleju@21 542
nikcleju@21 543 # stdparams_approx.params3sl0['ncpus'] = 1
nikcleju@21 544 # run(stdparams_approx.params3sl0)
nikcleju@21 545 # utils.replot_approx(stdparams_approx.params3sl0['savedataname'],
nikcleju@21 546 # algonames = None, # will read them from mat file
nikcleju@21 547 # doshow=False,
nikcleju@21 548 # dosave=True,
nikcleju@21 549 # saveplotbase=stdparams_approx.params3sl0['saveplotbase'],
nikcleju@21 550 # saveplotexts=stdparams_approx.params3sl0['saveplotexts'])
nikcleju@21 551
nikcleju@21 552 #stdparams_approx.params3['ncpus'] = 1
nikcleju@21 553 run(stdparams_approx.params3)
nikcleju@21 554 utils.replot_approx(stdparams_approx.params3['savedataname'],
nikcleju@19 555 algonames = None, # will read them from mat file
nikcleju@19 556 doshow=False,
nikcleju@19 557 dosave=True,
nikcleju@21 558 saveplotbase=stdparams_approx.params3['saveplotbase'],
nikcleju@21 559 saveplotexts=stdparams_approx.params3['saveplotexts'])
nikcleju@21 560
nikcleju@21 561 #stdparams_approx.params4['ncpus'] = 1
nikcleju@21 562 run(stdparams_approx.params4)
nikcleju@21 563 utils.replot_approx(stdparams_approx.params4['savedataname'],
nikcleju@21 564 algonames = None, # will read them from mat file
nikcleju@21 565 doshow=False,
nikcleju@21 566 dosave=True,
nikcleju@21 567 saveplotbase=stdparams_approx.params4['saveplotbase'],
nikcleju@21 568 saveplotexts=stdparams_approx.params4['saveplotexts'])