annotate algos.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 7fdf964f4edd
children
rev   line source
nikcleju@0 1 # -*- coding: utf-8 -*-
nikcleju@0 2 """
nikcleju@17 3 Define simple wrappers for algorithms, with similar header.
nikcleju@17 4 Specific algorithm parameters are defined inside here.
nikcleju@13 5
nikcleju@17 6 Author: Nicolae Cleju
nikcleju@17 7 """
nikcleju@17 8 __author__ = "Nicolae Cleju"
nikcleju@17 9 __license__ = "GPL"
nikcleju@17 10 __email__ = "nikcleju@gmail.com"
nikcleju@0 11
nikcleju@0 12
nikcleju@0 13 import numpy
nikcleju@17 14
nikcleju@17 15 # Module with algorithms implemented in Python
nikcleju@0 16 import pyCSalgos
nikcleju@0 17 import pyCSalgos.GAP.GAP
nikcleju@17 18
nikcleju@17 19 # Analysis by Synthesis - exact algorithms
nikcleju@13 20 import ABSexact
nikcleju@17 21 # Analysis by Synthesis - mixed algorithms
nikcleju@13 22 import ABSmixed
nikcleju@17 23 # Analysis by Synthesis - lambda algorithms
nikcleju@13 24 import ABSlambda
nikcleju@17 25
nikcleju@0 26
nikcleju@13 27 ###---------------------------------
nikcleju@13 28 ### Exact reconstruction algorithms
nikcleju@13 29 ###---------------------------------
nikcleju@13 30 def run_exact_gap(y,M,Omega):
nikcleju@13 31 """
nikcleju@13 32 Wrapper for GAP algorithm for exact analysis recovery
nikcleju@13 33 """
nikcleju@13 34 gapparams = {"num_iteration" : 1000,\
nikcleju@13 35 "greedy_level" : 0.9,\
nikcleju@13 36 "stopping_coefficient_size" : 1e-4,\
nikcleju@13 37 "l2solver" : 'pseudoinverse',\
nikcleju@13 38 "noise_level": 1e-10}
nikcleju@13 39 return pyCSalgos.GAP.GAP.GAP(y,M,M.T,Omega,Omega.T,gapparams,numpy.zeros(Omega.shape[1]))[0]
nikcleju@13 40
nikcleju@13 41 def run_exact_bp(y,M,Omega):
nikcleju@13 42 """
nikcleju@13 43 Wrapper for BP algorithm for exact analysis recovery
nikcleju@13 44 Algorithm implementation is l1eq_pd() from l1-magic toolbox
nikcleju@13 45 """
nikcleju@14 46 return ABSexact.bp(y,M,Omega,numpy.zeros(Omega.shape[0]), pdtol=1e-5, pdmaxiter = 100)
nikcleju@14 47
nikcleju@14 48 def run_exact_bp_cvxopt(y,M,Omega):
nikcleju@14 49 """
nikcleju@14 50 Wrapper for BP algorithm for exact analysis recovery
nikcleju@14 51 Algorithm implementation is using cvxopt linear programming
nikcleju@14 52 """
nikcleju@14 53 return ABSexact.bp_cvxopt(y,M,Omega)
nikcleju@14 54
nikcleju@13 55
nikcleju@13 56 def run_exact_ompeps(y,M,Omega):
nikcleju@13 57 """
nikcleju@13 58 Wrapper for OMP algorithm for exact analysis recovery, with stopping criterion = epsilon
nikcleju@13 59 """
nikcleju@13 60 return ABSexact.ompeps(y,M,Omega,1e-9)
nikcleju@13 61
nikcleju@13 62 #def run_exact_ompk(y,M,Omega)
nikcleju@13 63 # """
nikcleju@13 64 # Wrapper for OMP algorithm for exact analysis recovery, with stopping criterion = fixed no. of atoms
nikcleju@13 65 # """
nikcleju@13 66
nikcleju@13 67 def run_exact_sl0(y,M,Omega):
nikcleju@13 68 """
nikcleju@13 69 Wrapper for SL0 algorithm for exact analysis recovery
nikcleju@13 70 """
nikcleju@13 71 sigma_min = 1e-12
nikcleju@13 72 sigma_decrease_factor = 0.5
nikcleju@13 73 mu_0 = 2
nikcleju@13 74 L = 20
nikcleju@13 75 return ABSexact.sl0(y,M,Omega, sigma_min, sigma_decrease_factor, mu_0, L)
nikcleju@13 76
nikcleju@13 77 def run_exact_tst(y,M,Omega):
nikcleju@13 78 """
nikcleju@13 79 Wrapper for TST algorithm (with default optimized params) for exact analysis recovery
nikcleju@13 80 """
nikcleju@13 81 nsweep = 300
nikcleju@13 82 tol = 1e-5
nikcleju@13 83 return ABSexact.tst_recom(y,M,Omega, nsweep, tol)
nikcleju@13 84
nikcleju@13 85
nikcleju@13 86 ###---------------------------------------
nikcleju@13 87 ### Approximate reconstruction algorithms
nikcleju@13 88 ###---------------------------------------
nikcleju@13 89 # 1. Native
nikcleju@13 90
nikcleju@0 91 def run_gap(y,M,Omega,epsilon):
nikcleju@13 92 """
nikcleju@13 93 Wrapper for GAP algorithm for approximate analysis recovery
nikcleju@13 94 """
nikcleju@0 95 gapparams = {"num_iteration" : 1000,\
nikcleju@0 96 "greedy_level" : 0.9,\
nikcleju@0 97 "stopping_coefficient_size" : 1e-4,\
nikcleju@0 98 "l2solver" : 'pseudoinverse',\
nikcleju@0 99 "noise_level": epsilon}
nikcleju@0 100 return pyCSalgos.GAP.GAP.GAP(y,M,M.T,Omega,Omega.T,gapparams,numpy.zeros(Omega.shape[1]))[0]
nikcleju@0 101
nikcleju@0 102 def run_nesta(y,M,Omega,epsilon):
nikcleju@13 103 """
nikcleju@13 104 Wrapper for NESTA algorithm for approximate analysis recovery
nikcleju@13 105 """
nikcleju@0 106 U,S,V = numpy.linalg.svd(M, full_matrices = True)
nikcleju@0 107 V = V.T # Make like Matlab
nikcleju@0 108 m,n = M.shape # Make like Matlab
nikcleju@0 109 S = numpy.hstack((numpy.diag(S), numpy.zeros((m,n-m))))
nikcleju@0 110
nikcleju@0 111 opt_muf = 1e-3
nikcleju@0 112 optsUSV = {'U':U, 'S':S, 'V':V}
nikcleju@0 113 opts = {'U':Omega, 'Ut':Omega.T.copy(), 'USV':optsUSV, 'TolVar':1e-5, 'Verbose':0}
nikcleju@0 114 return pyCSalgos.NESTA.NESTA.NESTA(M, None, y, opt_muf, epsilon, opts)[0]
nikcleju@0 115
nikcleju@13 116 # 2. ABS-mixed
nikcleju@0 117
nikcleju@13 118 def run_mixed_sl0(y,M,Omega,epsilon):
nikcleju@13 119 """
nikcleju@13 120 Wrapper for SL0-mixed algorithm for approximate analysis recovery
nikcleju@13 121 """
nikcleju@13 122 sigma_min = 0.001
nikcleju@0 123 sigma_decrease_factor = 0.5
nikcleju@0 124 mu_0 = 2
nikcleju@0 125 L = 10
nikcleju@13 126 return ABSmixed.sl0(y,M,Omega,epsilon,sigma_min, sigma_decrease_factor, mu_0, L)
nikcleju@13 127
nikcleju@13 128 def run_mixed_bp(y,M,Omega,epsilon):
nikcleju@13 129 """
nikcleju@13 130 Wrapper for BP-mixed algorithm for approximate analysis recovery
nikcleju@13 131 """
nikcleju@13 132 return ABSmixed.bp(y,M,Omega,epsilon, numpy.zeros(Omega.shape[0]))
nikcleju@13 133
nikcleju@13 134 # 3. ABS-lambda
nikcleju@13 135
nikcleju@13 136 def run_lambda_sl0(y,M,Omega,epsilon,lbd):
nikcleju@13 137 """
nikcleju@13 138 Wrapper for SL0 algorithm within ABS-lambda approach for approximate analysis recovery
nikcleju@13 139 """
nikcleju@13 140 sigma_min = 0.001
nikcleju@13 141 sigma_decrease_factor = 0.5
nikcleju@13 142 mu_0 = 2
nikcleju@13 143 L = 10
nikcleju@13 144 return ABSlambda.sl0(y,M,Omega,epsilon, lbd, sigma_min, sigma_decrease_factor, mu_0, L)
nikcleju@13 145
nikcleju@13 146 def run_lambda_bp(y,M,Omega,epsilon,lbd):
nikcleju@13 147 """
nikcleju@13 148 Wrapper for BP algorithm within ABS-lambda approach for approximate analysis recovery
nikcleju@13 149 """
nikcleju@13 150 return ABSlambda.bp(y,M,Omega,epsilon,lbd,numpy.zeros(Omega.shape[0]))
nikcleju@13 151
nikcleju@13 152 def run_lambda_ompeps(y,M,Omega,epsilon,lbd):
nikcleju@13 153 """
nikcleju@13 154 Wrapper for OMP algorithm, with stopping criterion = epsilon,
nikcleju@13 155 for approximate analysis recovery within ABS-lambda approach
nikcleju@13 156 """
nikcleju@13 157 return ABSlambda.ompeps(y,M,Omega,epsilon,lbd)
nikcleju@13 158
nikcleju@13 159 def run_lambda_tst(y,M,Omega,epsilon,lbd):
nikcleju@13 160 """
nikcleju@13 161 Wrapper for TST algorithm (with default optimized params)
nikcleju@13 162 for approximate analysis recovery within ABS-lambda approach
nikcleju@13 163 """
nikcleju@13 164 nsweep = 300
nikcleju@13 165 return ABSlambda.tst_recom(y,M,Omega,epsilon,lbd, nsweep)
nikcleju@0 166
nikcleju@0 167
nikcleju@17 168 ### Define algorithm tuples: (function, name)
nikcleju@17 169 ### Will be used in stdparams and in test scripts
nikcleju@13 170 ## Exact recovery
nikcleju@13 171 exact_gap = (run_exact_gap, 'GAP')
nikcleju@14 172 exact_bp = (run_exact_bp, 'ABSexact_BP')
nikcleju@14 173 exact_bp_cvxopt = (run_exact_bp_cvxopt, 'ABSexact_BP_cvxopt')
nikcleju@14 174 exact_ompeps = (run_exact_ompeps, 'ABSexact_OMPeps')
nikcleju@14 175 exact_sl0 = (run_exact_sl0, 'ABSexact_SL0')
nikcleju@14 176 exact_tst = (run_exact_tst, 'ABSexact_TST')
nikcleju@13 177 ## Approximate recovery
nikcleju@13 178 # Native
nikcleju@13 179 gap = (run_gap, 'GAP')
nikcleju@13 180 nesta = (run_nesta, 'NESTA')
nikcleju@13 181 # ABS-mixed
nikcleju@14 182 mixed_sl0 = (run_mixed_sl0, 'ABSmixed_SL0')
nikcleju@14 183 mixed_bp = (run_mixed_bp, 'ABSmixed_BP')
nikcleju@13 184 # ABS-lambda
nikcleju@14 185 lambda_sl0 = (run_lambda_sl0, 'ABSlambda_SL0')
nikcleju@14 186 lambda_bp = (run_lambda_bp, 'ABSlambda_BP')
nikcleju@14 187 lambda_ompeps = (run_lambda_ompeps, 'ABSlambda_OMPeps')
nikcleju@14 188 lambda_tst = (run_lambda_tst, 'ABSlambda_TST')