annotate scripts/ABSapproxPP.py @ 22:2dd78e37b23a

ABS approx script is working Started working on parallel
author nikcleju
date Wed, 09 Nov 2011 00:11:14 +0000
parents
children 1a88766113a9
rev   line source
nikcleju@22 1 # -*- coding: utf-8 -*-
nikcleju@22 2 """
nikcleju@22 3 Created on Sat Nov 05 18:08:40 2011
nikcleju@22 4
nikcleju@22 5 @author: Nic
nikcleju@22 6 """
nikcleju@22 7
nikcleju@22 8 import numpy as np
nikcleju@22 9 import scipy.io
nikcleju@22 10 import math
nikcleju@22 11 import matplotlib.pyplot as plt
nikcleju@22 12 import matplotlib.cm as cm
nikcleju@22 13 import pp
nikcleju@22 14 import pyCSalgos
nikcleju@22 15 import pyCSalgos.GAP.GAP
nikcleju@22 16 import pyCSalgos.SL0.SL0_approx
nikcleju@22 17
nikcleju@22 18 # Define functions that prepare arguments for each algorithm call
nikcleju@22 19 def run_gap(y,M,Omega,epsilon):
nikcleju@22 20 gapparams = {"num_iteration" : 1000,\
nikcleju@22 21 "greedy_level" : 0.9,\
nikcleju@22 22 "stopping_coefficient_size" : 1e-4,\
nikcleju@22 23 "l2solver" : 'pseudoinverse',\
nikcleju@22 24 "noise_level": epsilon}
nikcleju@22 25 return pyCSalgos.GAP.GAP.GAP(y,M,M.T,Omega,Omega.T,gapparams,np.zeros(Omega.shape[1]))[0]
nikcleju@22 26
nikcleju@22 27 def run_sl0(y,M,Omega,D,U,S,Vt,epsilon,lbd):
nikcleju@22 28
nikcleju@22 29 N,n = Omega.shape
nikcleju@22 30 #D = np.linalg.pinv(Omega)
nikcleju@22 31 #U,S,Vt = np.linalg.svd(D)
nikcleju@22 32 aggDupper = np.dot(M,D)
nikcleju@22 33 aggDlower = Vt[-(N-n):,:]
nikcleju@22 34 aggD = np.concatenate((aggDupper, lbd * aggDlower))
nikcleju@22 35 aggy = np.concatenate((y, np.zeros(N-n)))
nikcleju@22 36
nikcleju@22 37 sigmamin = 0.001
nikcleju@22 38 sigma_decrease_factor = 0.5
nikcleju@22 39 mu_0 = 2
nikcleju@22 40 L = 10
nikcleju@22 41 return pyCSalgos.SL0.SL0_approx.SL0_approx(aggD,aggy,epsilon,sigmamin,sigma_decrease_factor,mu_0,L)
nikcleju@22 42
nikcleju@22 43 # Define tuples (algorithm setup function, algorithm function, name)
nikcleju@22 44 gap = (run_gap, 'GAP')
nikcleju@22 45 sl0 = (run_sl0, 'SL0_approx')
nikcleju@22 46
nikcleju@22 47 # Define which algorithms to run
nikcleju@22 48 # 1. Algorithms not depending on lambda
nikcleju@22 49 algosN = gap, # tuple
nikcleju@22 50 # 2. Algorithms depending on lambda (our ABS approach)
nikcleju@22 51 algosL = sl0, # tuple
nikcleju@22 52
nikcleju@22 53 def mainrun():
nikcleju@22 54
nikcleju@22 55 nalgosN = len(algosN)
nikcleju@22 56 nalgosL = len(algosL)
nikcleju@22 57
nikcleju@22 58 #Set up experiment parameters
nikcleju@22 59 d = 50;
nikcleju@22 60 sigma = 2.0
nikcleju@22 61 #deltas = np.arange(0.05,0.95,0.05)
nikcleju@22 62 #rhos = np.arange(0.05,0.95,0.05)
nikcleju@22 63 deltas = np.array([0.05,0.95])
nikcleju@22 64 rhos = np.array([0.05,0.95])
nikcleju@22 65 #deltas = np.array([0.05])
nikcleju@22 66 #rhos = np.array([0.05])
nikcleju@22 67 #delta = 0.8;
nikcleju@22 68 #rho = 0.15;
nikcleju@22 69 numvects = 10; # Number of vectors to generate
nikcleju@22 70 SNRdb = 20.; # This is norm(signal)/norm(noise), so power, not energy
nikcleju@22 71 # Values for lambda
nikcleju@22 72 #lambdas = [0 10.^linspace(-5, 4, 10)];
nikcleju@22 73 lambdas = np.concatenate((np.array([0]), 10**np.linspace(-5, 4, 10)))
nikcleju@22 74
nikcleju@22 75 meanmatrix = dict()
nikcleju@22 76 for i,algo in zip(np.arange(nalgosN),algosN):
nikcleju@22 77 meanmatrix[algo[1]] = np.zeros((rhos.size, deltas.size))
nikcleju@22 78 for i,algo in zip(np.arange(nalgosL),algosL):
nikcleju@22 79 meanmatrix[algo[1]] = np.zeros((lambdas.size, rhos.size, deltas.size))
nikcleju@22 80
nikcleju@22 81 # PP: start job server
nikcleju@22 82 job_server = pp.Server(ncpus = 1)
nikcleju@22 83 idx = 0
nikcleju@22 84 jobparams = []
nikcleju@22 85 for idelta,delta in zip(np.arange(deltas.size),deltas):
nikcleju@22 86 for irho,rho in zip(np.arange(rhos.size),rhos):
nikcleju@22 87
nikcleju@22 88 # Generate data and operator
nikcleju@22 89 Omega,x0,y,M,realnoise = genData(d,sigma,delta,rho,numvects,SNRdb)
nikcleju@22 90
nikcleju@22 91 jobparams.append((algosN,algosL, Omega,y,lambdas,realnoise,M,x0))
nikcleju@22 92
nikcleju@22 93 idx = idx + 1
nikcleju@22 94
nikcleju@22 95 # Run algorithms
nikcleju@22 96 jobs = [job_server.submit(runonce, jobparam, (run_gap,run_sl0), ('numpy',)) for jobparam in jobparams]
nikcleju@22 97 #funcarray[idelta,irho] = job_server.submit(runonce,(algosN,algosL, Omega,y,lambdas,realnoise,M,x0), (run_gap,run_sl0))
nikcleju@22 98 #mrelerrN,mrelerrL = runonce(algosN,algosL,Omega,y,lambdas,realnoise,M,x0)
nikcleju@22 99
nikcleju@22 100 # Get data from jobs
nikcleju@22 101 idx = 0
nikcleju@22 102 for idelta,delta in zip(np.arange(deltas.size),deltas):
nikcleju@22 103 for irho,rho in zip(np.arange(rhos.size),rhos):
nikcleju@22 104 mrelerrN,mrelerrL = jobs[idx]()
nikcleju@22 105 for algotuple in algosN:
nikcleju@22 106 meanmatrix[algotuple[1]][irho,idelta] = 1 - mrelerrN[algotuple[1]]
nikcleju@22 107 if meanmatrix[algotuple[1]][irho,idelta] < 0 or math.isnan(meanmatrix[algotuple[1]][irho,idelta]):
nikcleju@22 108 meanmatrix[algotuple[1]][irho,idelta] = 0
nikcleju@22 109 for algotuple in algosL:
nikcleju@22 110 for ilbd in np.arange(lambdas.size):
nikcleju@22 111 meanmatrix[algotuple[1]][ilbd,irho,idelta] = 1 - mrelerrL[algotuple[1]][ilbd]
nikcleju@22 112 if meanmatrix[algotuple[1]][ilbd,irho,idelta] < 0 or math.isnan(meanmatrix[algotuple[1]][ilbd,irho,idelta]):
nikcleju@22 113 meanmatrix[algotuple[1]][ilbd,irho,idelta] = 0
nikcleju@22 114 idx = idx + 1
nikcleju@22 115
nikcleju@22 116 # # Prepare matrices to show
nikcleju@22 117 # showmats = dict()
nikcleju@22 118 # for i,algo in zip(np.arange(nalgosN),algosN):
nikcleju@22 119 # showmats[algo[1]] = np.zeros(rhos.size, deltas.size)
nikcleju@22 120 # for i,algo in zip(np.arange(nalgosL),algosL):
nikcleju@22 121 # showmats[algo[1]] = np.zeros(lambdas.size, rhos.size, deltas.size)
nikcleju@22 122
nikcleju@22 123 # Save
nikcleju@22 124 tosave = dict()
nikcleju@22 125 tosave['meanmatrix'] = meanmatrix
nikcleju@22 126 tosave['d'] = d
nikcleju@22 127 tosave['sigma'] = sigma
nikcleju@22 128 tosave['deltas'] = deltas
nikcleju@22 129 tosave['rhos'] = rhos
nikcleju@22 130 tosave['numvects'] = numvects
nikcleju@22 131 tosave['SNRdb'] = SNRdb
nikcleju@22 132 tosave['lambdas'] = lambdas
nikcleju@22 133 try:
nikcleju@22 134 scipy.io.savemat('ABSapprox.mat',tosave)
nikcleju@22 135 except TypeError:
nikcleju@22 136 print "Oops, Type Error"
nikcleju@22 137 raise
nikcleju@22 138 # Show
nikcleju@22 139 for algotuple in algosN:
nikcleju@22 140 plt.figure()
nikcleju@22 141 plt.imshow(meanmatrix[algotuple[1]], cmap=cm.gray, interpolation='nearest')
nikcleju@22 142 for algotuple in algosL:
nikcleju@22 143 for ilbd in np.arange(lambdas.size):
nikcleju@22 144 plt.figure()
nikcleju@22 145 plt.imshow(meanmatrix[algotuple[1]][ilbd], cmap=cm.gray, interpolation='nearest')
nikcleju@22 146 plt.show()
nikcleju@22 147 print "Finished."
nikcleju@22 148
nikcleju@22 149 def genData(d,sigma,delta,rho,numvects,SNRdb):
nikcleju@22 150
nikcleju@22 151 # Process parameters
nikcleju@22 152 noiselevel = 1.0 / (10.0**(SNRdb/10.0));
nikcleju@22 153 p = round(sigma*d);
nikcleju@22 154 m = round(delta*d);
nikcleju@22 155 l = round(d - rho*m);
nikcleju@22 156
nikcleju@22 157 # Generate Omega and data based on parameters
nikcleju@22 158 Omega = pyCSalgos.GAP.GAP.Generate_Analysis_Operator(d, p);
nikcleju@22 159 # Optionally make Omega more coherent
nikcleju@22 160 U,S,Vt = np.linalg.svd(Omega);
nikcleju@22 161 Sdnew = S * (1+np.arange(S.size)) # Make D coherent, not Omega!
nikcleju@22 162 Snew = np.vstack((np.diag(Sdnew), np.zeros((Omega.shape[0] - Omega.shape[1], Omega.shape[1]))))
nikcleju@22 163 Omega = np.dot(U , np.dot(Snew,Vt))
nikcleju@22 164
nikcleju@22 165 # Generate data
nikcleju@22 166 x0,y,M,Lambda,realnoise = pyCSalgos.GAP.GAP.Generate_Data_Known_Omega(Omega, d,p,m,l,noiselevel, numvects,'l0');
nikcleju@22 167
nikcleju@22 168 return Omega,x0,y,M,realnoise
nikcleju@22 169
nikcleju@22 170 def runonce(algosN,algosL,Omega,y,lambdas,realnoise,M,x0):
nikcleju@22 171
nikcleju@22 172 d = Omega.shape[1]
nikcleju@22 173
nikcleju@22 174 nalgosN = len(algosN)
nikcleju@22 175 nalgosL = len(algosL)
nikcleju@22 176
nikcleju@22 177 xrec = dict()
nikcleju@22 178 err = dict()
nikcleju@22 179 relerr = dict()
nikcleju@22 180
nikcleju@22 181 # Prepare storage variables for algorithms non-Lambda
nikcleju@22 182 for i,algo in zip(np.arange(nalgosN),algosN):
nikcleju@22 183 xrec[algo[1]] = np.zeros((d, y.shape[1]))
nikcleju@22 184 err[algo[1]] = np.zeros(y.shape[1])
nikcleju@22 185 relerr[algo[1]] = np.zeros(y.shape[1])
nikcleju@22 186 # Prepare storage variables for algorithms with Lambda
nikcleju@22 187 for i,algo in zip(np.arange(nalgosL),algosL):
nikcleju@22 188 xrec[algo[1]] = np.zeros((lambdas.size, d, y.shape[1]))
nikcleju@22 189 err[algo[1]] = np.zeros((lambdas.size, y.shape[1]))
nikcleju@22 190 relerr[algo[1]] = np.zeros((lambdas.size, y.shape[1]))
nikcleju@22 191
nikcleju@22 192 # Run algorithms non-Lambda
nikcleju@22 193 for iy in np.arange(y.shape[1]):
nikcleju@22 194 for algofunc,strname in algosN:
nikcleju@22 195 epsilon = 1.1 * np.linalg.norm(realnoise[:,iy])
nikcleju@22 196 xrec[strname][:,iy] = algofunc(y[:,iy],M,Omega,epsilon)
nikcleju@22 197 err[strname][iy] = np.linalg.norm(x0[:,iy] - xrec[strname][:,iy])
nikcleju@22 198 relerr[strname][iy] = err[strname][iy] / np.linalg.norm(x0[:,iy])
nikcleju@22 199 for algotuple in algosN:
nikcleju@22 200 print algotuple[1],' : avg relative error = ',np.mean(relerr[strname])
nikcleju@22 201
nikcleju@22 202 # Run algorithms with Lambda
nikcleju@22 203 for ilbd,lbd in zip(np.arange(lambdas.size),lambdas):
nikcleju@22 204 for iy in np.arange(y.shape[1]):
nikcleju@22 205 D = np.linalg.pinv(Omega)
nikcleju@22 206 U,S,Vt = np.linalg.svd(D)
nikcleju@22 207 for algofunc,strname in algosL:
nikcleju@22 208 epsilon = 1.1 * np.linalg.norm(realnoise[:,iy])
nikcleju@22 209 gamma = algofunc(y[:,iy],M,Omega,D,U,S,Vt,epsilon,lbd)
nikcleju@22 210 xrec[strname][ilbd,:,iy] = np.dot(D,gamma)
nikcleju@22 211 err[strname][ilbd,iy] = np.linalg.norm(x0[:,iy] - xrec[strname][ilbd,:,iy])
nikcleju@22 212 relerr[strname][ilbd,iy] = err[strname][ilbd,iy] / np.linalg.norm(x0[:,iy])
nikcleju@22 213 print 'Lambda = ',lbd,' :'
nikcleju@22 214 for algotuple in algosL:
nikcleju@22 215 print ' ',algotuple[1],' : avg relative error = ',np.mean(relerr[strname][ilbd,:])
nikcleju@22 216
nikcleju@22 217 # Prepare results
nikcleju@22 218 mrelerrN = dict()
nikcleju@22 219 for algotuple in algosN:
nikcleju@22 220 mrelerrN[algotuple[1]] = np.mean(relerr[algotuple[1]])
nikcleju@22 221 mrelerrL = dict()
nikcleju@22 222 for algotuple in algosL:
nikcleju@22 223 mrelerrL[algotuple[1]] = np.mean(relerr[algotuple[1]],1)
nikcleju@22 224
nikcleju@22 225 return mrelerrN,mrelerrL
nikcleju@22 226
nikcleju@22 227 # Script main
nikcleju@22 228 if __name__ == "__main__":
nikcleju@22 229 mainrun()