annotate scripts/ABSapprox.py @ 24:c07440417bd8

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