nikcleju@32: # -*- coding: utf-8 -*- nikcleju@32: """ nikcleju@32: Created on Sat Nov 05 18:08:40 2011 nikcleju@32: nikcleju@32: @author: Nic nikcleju@32: """ nikcleju@32: nikcleju@32: import numpy as np nikcleju@32: import scipy.io nikcleju@32: import math nikcleju@32: doplot = True nikcleju@32: try: nikcleju@32: import matplotlib.pyplot as plt nikcleju@32: except: nikcleju@32: doplot = False nikcleju@32: if doplot: nikcleju@32: import matplotlib.cm as cm nikcleju@32: import pyCSalgos nikcleju@32: import pyCSalgos.GAP.GAP nikcleju@32: import pyCSalgos.SL0.SL0_approx nikcleju@32: nikcleju@32: # Define functions that prepare arguments for each algorithm call nikcleju@32: def run_gap(y,M,Omega,epsilon): nikcleju@32: gapparams = {"num_iteration" : 1000,\ nikcleju@32: "greedy_level" : 0.9,\ nikcleju@32: "stopping_coefficient_size" : 1e-4,\ nikcleju@32: "l2solver" : 'pseudoinverse',\ nikcleju@32: "noise_level": epsilon} nikcleju@32: return pyCSalgos.GAP.GAP.GAP(y,M,M.T,Omega,Omega.T,gapparams,np.zeros(Omega.shape[1]))[0] nikcleju@32: nikcleju@32: def run_sl0(y,M,Omega,D,U,S,Vt,epsilon,lbd): nikcleju@32: nikcleju@32: N,n = Omega.shape nikcleju@32: #D = np.linalg.pinv(Omega) nikcleju@32: #U,S,Vt = np.linalg.svd(D) nikcleju@32: aggDupper = np.dot(M,D) nikcleju@32: aggDlower = Vt[-(N-n):,:] nikcleju@32: aggD = np.concatenate((aggDupper, lbd * aggDlower)) nikcleju@32: aggy = np.concatenate((y, np.zeros(N-n))) nikcleju@32: nikcleju@32: sigmamin = 0.001 nikcleju@32: sigma_decrease_factor = 0.5 nikcleju@32: mu_0 = 2 nikcleju@32: L = 10 nikcleju@32: return pyCSalgos.SL0.SL0_approx.SL0_approx(aggD,aggy,epsilon,sigmamin,sigma_decrease_factor,mu_0,L) nikcleju@32: nikcleju@32: def run_bp(y,M,Omega,D,U,S,Vt,epsilon,lbd): nikcleju@32: nikcleju@32: N,n = Omega.shape nikcleju@32: #D = np.linalg.pinv(Omega) nikcleju@32: #U,S,Vt = np.linalg.svd(D) nikcleju@32: aggDupper = np.dot(M,D) nikcleju@32: aggDlower = Vt[-(N-n):,:] nikcleju@32: aggD = np.concatenate((aggDupper, lbd * aggDlower)) nikcleju@32: aggy = np.concatenate((y, np.zeros(N-n))) nikcleju@32: nikcleju@32: sigmamin = 0.001 nikcleju@32: sigma_decrease_factor = 0.5 nikcleju@32: mu_0 = 2 nikcleju@32: L = 10 nikcleju@32: return pyCSalgos.SL0.SL0_approx.SL0_approx(aggD,aggy,epsilon,sigmamin,sigma_decrease_factor,mu_0,L) nikcleju@32: nikcleju@32: nikcleju@32: # Define tuples (algorithm setup function, algorithm function, name) nikcleju@32: gap = (run_gap, 'GAP') nikcleju@32: sl0 = (run_sl0, 'SL0_approx') nikcleju@32: nikcleju@32: # Define which algorithms to run nikcleju@32: # 1. Algorithms not depending on lambda nikcleju@32: algosN = gap, # tuple nikcleju@32: # 2. Algorithms depending on lambda (our ABS approach) nikcleju@32: algosL = sl0, # tuple nikcleju@32: nikcleju@32: def mainrun(): nikcleju@32: nikcleju@32: nalgosN = len(algosN) nikcleju@32: nalgosL = len(algosL) nikcleju@32: nikcleju@32: #Set up experiment parameters nikcleju@32: d = 50.0; nikcleju@32: sigma = 2.0 nikcleju@32: #deltas = np.arange(0.05,1.,0.05) nikcleju@32: #rhos = np.arange(0.05,1.,0.05) nikcleju@32: deltas = np.array([0.05, 0.45, 0.95]) nikcleju@32: rhos = np.array([0.05, 0.45, 0.95]) nikcleju@32: #deltas = np.array([0.05]) nikcleju@32: #rhos = np.array([0.05]) nikcleju@32: #delta = 0.8; nikcleju@32: #rho = 0.15; nikcleju@32: numvects = 100; # Number of vectors to generate nikcleju@32: SNRdb = 20.; # This is norm(signal)/norm(noise), so power, not energy nikcleju@32: # Values for lambda nikcleju@32: #lambdas = [0 10.^linspace(-5, 4, 10)]; nikcleju@32: #lambdas = np.concatenate((np.array([0]), 10**np.linspace(-5, 4, 10))) nikcleju@32: lambdas = np.array([0., 0.0001, 0.01, 1, 100, 10000]) nikcleju@32: nikcleju@32: meanmatrix = dict() nikcleju@32: for i,algo in zip(np.arange(nalgosN),algosN): nikcleju@32: meanmatrix[algo[1]] = np.zeros((rhos.size, deltas.size)) nikcleju@32: for i,algo in zip(np.arange(nalgosL),algosL): nikcleju@32: meanmatrix[algo[1]] = np.zeros((lambdas.size, rhos.size, deltas.size)) nikcleju@32: nikcleju@32: for idelta,delta in zip(np.arange(deltas.size),deltas): nikcleju@32: for irho,rho in zip(np.arange(rhos.size),rhos): nikcleju@32: nikcleju@32: # Generate data and operator nikcleju@32: Omega,x0,y,M,realnoise = genData(d,sigma,delta,rho,numvects,SNRdb) nikcleju@32: nikcleju@32: # Run algorithms nikcleju@32: print "***** delta = ",delta," rho = ",rho nikcleju@32: mrelerrN,mrelerrL = runonce(algosN,algosL,Omega,y,lambdas,realnoise,M,x0) nikcleju@32: nikcleju@32: for algotuple in algosN: nikcleju@32: meanmatrix[algotuple[1]][irho,idelta] = 1 - mrelerrN[algotuple[1]] nikcleju@32: if meanmatrix[algotuple[1]][irho,idelta] < 0 or math.isnan(meanmatrix[algotuple[1]][irho,idelta]): nikcleju@32: meanmatrix[algotuple[1]][irho,idelta] = 0 nikcleju@32: for algotuple in algosL: nikcleju@32: for ilbd in np.arange(lambdas.size): nikcleju@32: meanmatrix[algotuple[1]][ilbd,irho,idelta] = 1 - mrelerrL[algotuple[1]][ilbd] nikcleju@32: if meanmatrix[algotuple[1]][ilbd,irho,idelta] < 0 or math.isnan(meanmatrix[algotuple[1]][ilbd,irho,idelta]): nikcleju@32: meanmatrix[algotuple[1]][ilbd,irho,idelta] = 0 nikcleju@32: nikcleju@32: # # Prepare matrices to show nikcleju@32: # showmats = dict() nikcleju@32: # for i,algo in zip(np.arange(nalgosN),algosN): nikcleju@32: # showmats[algo[1]] = np.zeros(rhos.size, deltas.size) nikcleju@32: # for i,algo in zip(np.arange(nalgosL),algosL): nikcleju@32: # showmats[algo[1]] = np.zeros(lambdas.size, rhos.size, deltas.size) nikcleju@32: nikcleju@32: # Save nikcleju@32: tosave = dict() nikcleju@32: tosave['meanmatrix'] = meanmatrix nikcleju@32: tosave['d'] = d nikcleju@32: tosave['sigma'] = sigma nikcleju@32: tosave['deltas'] = deltas nikcleju@32: tosave['rhos'] = rhos nikcleju@32: tosave['numvects'] = numvects nikcleju@32: tosave['SNRdb'] = SNRdb nikcleju@32: tosave['lambdas'] = lambdas nikcleju@32: try: nikcleju@32: scipy.io.savemat('ABSapprox.mat',tosave) nikcleju@32: except TypeError: nikcleju@32: print "Oops, Type Error" nikcleju@32: raise nikcleju@32: # Show nikcleju@32: if doplot: nikcleju@32: for algotuple in algosN: nikcleju@32: plt.figure() nikcleju@32: plt.imshow(meanmatrix[algotuple[1]], cmap=cm.gray, interpolation='nearest',origin='lower') nikcleju@32: for algotuple in algosL: nikcleju@32: for ilbd in np.arange(lambdas.size): nikcleju@32: plt.figure() nikcleju@32: plt.imshow(meanmatrix[algotuple[1]][ilbd], cmap=cm.gray, interpolation='nearest',origin='lower') nikcleju@32: plt.show() nikcleju@32: print "Finished." nikcleju@32: nikcleju@32: def genData(d,sigma,delta,rho,numvects,SNRdb): nikcleju@32: nikcleju@32: # Process parameters nikcleju@32: noiselevel = 1.0 / (10.0**(SNRdb/10.0)); nikcleju@32: p = round(sigma*d); nikcleju@32: m = round(delta*d); nikcleju@32: l = round(d - rho*m); nikcleju@32: nikcleju@32: # Generate Omega and data based on parameters nikcleju@32: Omega = pyCSalgos.GAP.GAP.Generate_Analysis_Operator(d, p); nikcleju@32: # Optionally make Omega more coherent nikcleju@32: U,S,Vt = np.linalg.svd(Omega); nikcleju@32: Sdnew = S * (1+np.arange(S.size)) # Make D coherent, not Omega! nikcleju@32: Snew = np.vstack((np.diag(Sdnew), np.zeros((Omega.shape[0] - Omega.shape[1], Omega.shape[1])))) nikcleju@32: Omega = np.dot(U , np.dot(Snew,Vt)) nikcleju@32: nikcleju@32: # Generate data nikcleju@32: x0,y,M,Lambda,realnoise = pyCSalgos.GAP.GAP.Generate_Data_Known_Omega(Omega, d,p,m,l,noiselevel, numvects,'l0'); nikcleju@32: nikcleju@32: return Omega,x0,y,M,realnoise nikcleju@32: nikcleju@32: def runonce(algosN,algosL,Omega,y,lambdas,realnoise,M,x0): nikcleju@32: nikcleju@32: d = Omega.shape[1] nikcleju@32: nikcleju@32: nalgosN = len(algosN) nikcleju@32: nalgosL = len(algosL) nikcleju@32: nikcleju@32: xrec = dict() nikcleju@32: err = dict() nikcleju@32: relerr = dict() nikcleju@32: nikcleju@32: # Prepare storage variables for algorithms non-Lambda nikcleju@32: for i,algo in zip(np.arange(nalgosN),algosN): nikcleju@32: xrec[algo[1]] = np.zeros((d, y.shape[1])) nikcleju@32: err[algo[1]] = np.zeros(y.shape[1]) nikcleju@32: relerr[algo[1]] = np.zeros(y.shape[1]) nikcleju@32: # Prepare storage variables for algorithms with Lambda nikcleju@32: for i,algo in zip(np.arange(nalgosL),algosL): nikcleju@32: xrec[algo[1]] = np.zeros((lambdas.size, d, y.shape[1])) nikcleju@32: err[algo[1]] = np.zeros((lambdas.size, y.shape[1])) nikcleju@32: relerr[algo[1]] = np.zeros((lambdas.size, y.shape[1])) nikcleju@32: nikcleju@32: # Run algorithms non-Lambda nikcleju@32: for iy in np.arange(y.shape[1]): nikcleju@32: for algofunc,strname in algosN: nikcleju@32: epsilon = 1.1 * np.linalg.norm(realnoise[:,iy]) nikcleju@32: xrec[strname][:,iy] = algofunc(y[:,iy],M,Omega,epsilon) nikcleju@32: err[strname][iy] = np.linalg.norm(x0[:,iy] - xrec[strname][:,iy]) nikcleju@32: relerr[strname][iy] = err[strname][iy] / np.linalg.norm(x0[:,iy]) nikcleju@32: for algotuple in algosN: nikcleju@32: print algotuple[1],' : avg relative error = ',np.mean(relerr[strname]) nikcleju@32: nikcleju@32: # Run algorithms with Lambda nikcleju@32: for ilbd,lbd in zip(np.arange(lambdas.size),lambdas): nikcleju@32: for iy in np.arange(y.shape[1]): nikcleju@32: D = np.linalg.pinv(Omega) nikcleju@32: U,S,Vt = np.linalg.svd(D) nikcleju@32: for algofunc,strname in algosL: nikcleju@32: epsilon = 1.1 * np.linalg.norm(realnoise[:,iy]) nikcleju@32: gamma = algofunc(y[:,iy],M,Omega,D,U,S,Vt,epsilon,lbd) nikcleju@32: xrec[strname][ilbd,:,iy] = np.dot(D,gamma) nikcleju@32: err[strname][ilbd,iy] = np.linalg.norm(x0[:,iy] - xrec[strname][ilbd,:,iy]) nikcleju@32: relerr[strname][ilbd,iy] = err[strname][ilbd,iy] / np.linalg.norm(x0[:,iy]) nikcleju@32: print 'Lambda = ',lbd,' :' nikcleju@32: for algotuple in algosL: nikcleju@32: print ' ',algotuple[1],' : avg relative error = ',np.mean(relerr[strname][ilbd,:]) nikcleju@32: nikcleju@32: # Prepare results nikcleju@32: mrelerrN = dict() nikcleju@32: for algotuple in algosN: nikcleju@32: mrelerrN[algotuple[1]] = np.mean(relerr[algotuple[1]]) nikcleju@32: mrelerrL = dict() nikcleju@32: for algotuple in algosL: nikcleju@32: mrelerrL[algotuple[1]] = np.mean(relerr[algotuple[1]],1) nikcleju@32: nikcleju@32: return mrelerrN,mrelerrL nikcleju@32: nikcleju@32: # Script main nikcleju@32: if __name__ == "__main__": nikcleju@32: nikcleju@32: #import cProfile nikcleju@32: #cProfile.run('mainrun()', 'profile') nikcleju@32: mainrun()