annotate scripts/ABSapprox.py @ 27:1a88766113a9

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