annotate ABSexact.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@13 1 # -*- coding: utf-8 -*-
nikcleju@13 2 """
nikcleju@17 3 Algorithms for exact analysis recovery based on synthesis solvers (a.k.a. Analysis by Synthesis, ABS).
nikcleju@17 4 Exact reconstruction.
nikcleju@13 5
nikcleju@17 6 Author: Nicolae Cleju
nikcleju@13 7 """
nikcleju@17 8 __author__ = "Nicolae Cleju"
nikcleju@17 9 __license__ = "GPL"
nikcleju@17 10 __email__ = "nikcleju@gmail.com"
nikcleju@17 11
nikcleju@13 12
nikcleju@13 13 import numpy
nikcleju@17 14
nikcleju@17 15 # Import synthesis solvers from pyCSalgos package
nikcleju@13 16 import pyCSalgos.BP.l1eq_pd
nikcleju@14 17 import pyCSalgos.BP.cvxopt_lp
nikcleju@13 18 import pyCSalgos.OMP.omp_QR
nikcleju@13 19 import pyCSalgos.SL0.SL0
nikcleju@13 20 import pyCSalgos.TST.RecommendedTST
nikcleju@13 21
nikcleju@17 22
nikcleju@14 23 def bp(y,M,Omega,x0, pdtol=1e-3, pdmaxiter=50, cgtol=1e-8, cgmaxiter=200, verbose=False):
nikcleju@17 24 """
nikcleju@17 25 ABS-exact: Basis Pursuit (based on l1magic toolbox)
nikcleju@17 26 """
nikcleju@13 27 N,n = Omega.shape
nikcleju@13 28 D = numpy.linalg.pinv(Omega)
nikcleju@13 29 U,S,Vt = numpy.linalg.svd(D)
nikcleju@13 30 Aextra = Vt[-(N-n):,:]
nikcleju@13 31
nikcleju@13 32 # Create aggregate problem
nikcleju@13 33 Atilde = numpy.vstack((numpy.dot(M,D), Aextra))
nikcleju@13 34 ytilde = numpy.concatenate((y,numpy.zeros(N-n)))
nikcleju@13 35
nikcleju@14 36 return numpy.dot(D , pyCSalgos.BP.l1eq_pd.l1eq_pd(x0,Atilde,Atilde.T,ytilde, pdtol, pdmaxiter, cgtol, cgmaxiter, verbose))
nikcleju@14 37
nikcleju@14 38 def bp_cvxopt(y,M,Omega):
nikcleju@17 39 """
nikcleju@17 40 ABS-exact: Basis Pursuit (based cvxopt)
nikcleju@17 41 """
nikcleju@14 42 N,n = Omega.shape
nikcleju@14 43 D = numpy.linalg.pinv(Omega)
nikcleju@14 44 U,S,Vt = numpy.linalg.svd(D)
nikcleju@14 45 Aextra = Vt[-(N-n):,:]
nikcleju@14 46
nikcleju@14 47 # Create aggregate problem
nikcleju@14 48 Atilde = numpy.vstack((numpy.dot(M,D), Aextra))
nikcleju@14 49 ytilde = numpy.concatenate((y,numpy.zeros(N-n)))
nikcleju@14 50
nikcleju@14 51 return numpy.dot(D , pyCSalgos.BP.cvxopt_lp.cvxopt_lp(ytilde, Atilde))
nikcleju@14 52
nikcleju@13 53
nikcleju@13 54 def ompeps(y,M,Omega,epsilon):
nikcleju@17 55 """
nikcleju@17 56 ABS-exact: OMP with stopping criterion residual < epsilon
nikcleju@17 57 """
nikcleju@13 58
nikcleju@13 59 N,n = Omega.shape
nikcleju@13 60 D = numpy.linalg.pinv(Omega)
nikcleju@13 61 U,S,Vt = numpy.linalg.svd(D)
nikcleju@13 62 Aextra = Vt[-(N-n):,:]
nikcleju@13 63
nikcleju@13 64 # Create aggregate problem
nikcleju@13 65 Atilde = numpy.vstack((numpy.dot(M,D), Aextra))
nikcleju@13 66 ytilde = numpy.concatenate((y,numpy.zeros(N-n)))
nikcleju@13 67
nikcleju@13 68 opts = dict()
nikcleju@13 69 opts['stopCrit'] = 'mse'
nikcleju@13 70 opts['stopTol'] = epsilon
nikcleju@14 71 return numpy.dot(D , pyCSalgos.OMP.omp_QR.greed_omp_qr(ytilde,Atilde,Atilde.shape[1],opts)[0])
nikcleju@13 72
nikcleju@13 73 def ompk(y,M,Omega,k):
nikcleju@17 74 """
nikcleju@17 75 ABS-exact: OMP with stopping criterion fixed number of atoms = k
nikcleju@17 76 """
nikcleju@13 77
nikcleju@13 78 N,n = Omega.shape
nikcleju@13 79 D = numpy.linalg.pinv(Omega)
nikcleju@13 80 U,S,Vt = numpy.linalg.svd(D)
nikcleju@13 81 Aextra = Vt[-(N-n):,:]
nikcleju@13 82
nikcleju@13 83 # Create aggregate problem
nikcleju@13 84 Atilde = numpy.vstack((numpy.dot(M,D), Aextra))
nikcleju@13 85 ytilde = numpy.concatenate((y,numpy.zeros(N-n)))
nikcleju@13 86
nikcleju@13 87 opts = dict()
nikcleju@13 88 opts['stopTol'] = k
nikcleju@14 89 return numpy.dot(D , pyCSalgos.OMP.omp_QR.greed_omp_qr(ytilde,Atilde,Atilde.shape[1],opts)[0])
nikcleju@13 90
nikcleju@13 91 def sl0(y,M,Omega, sigma_min, sigma_decrease_factor=0.5, mu_0=2, L=3, true_s=None):
nikcleju@17 92 """
nikcleju@17 93 ABS-exact: Smooth L0 (SL0)
nikcleju@17 94 """
nikcleju@13 95
nikcleju@13 96 N,n = Omega.shape
nikcleju@13 97 D = numpy.linalg.pinv(Omega)
nikcleju@13 98 U,S,Vt = numpy.linalg.svd(D)
nikcleju@13 99 Aextra = Vt[-(N-n):,:]
nikcleju@13 100
nikcleju@13 101 # Create aggregate problem
nikcleju@13 102 Atilde = numpy.vstack((numpy.dot(M,D), Aextra))
nikcleju@13 103 ytilde = numpy.concatenate((y,numpy.zeros(N-n)))
nikcleju@13 104
nikcleju@13 105 return numpy.dot(D, pyCSalgos.SL0.SL0.SL0(Atilde,ytilde,sigma_min,sigma_decrease_factor,mu_0,L,true_s))
nikcleju@13 106
nikcleju@13 107 def tst_recom(y,M,Omega, nsweep=300, tol=0.00001, xinitial=None, ro=None):
nikcleju@17 108 """
nikcleju@17 109 ABS-exact: Two Stage Thresholding (TST) with optimized parameters (see Maleki & Donoho)
nikcleju@17 110 """
nikcleju@13 111
nikcleju@13 112 N,n = Omega.shape
nikcleju@13 113 D = numpy.linalg.pinv(Omega)
nikcleju@13 114 U,S,Vt = numpy.linalg.svd(D)
nikcleju@13 115 Aextra = Vt[-(N-n):,:]
nikcleju@13 116
nikcleju@13 117 # Create aggregate problem
nikcleju@13 118 Atilde = numpy.vstack((numpy.dot(M,D), Aextra))
nikcleju@13 119 ytilde = numpy.concatenate((y,numpy.zeros(N-n)))
nikcleju@13 120
nikcleju@14 121 return numpy.dot(D, pyCSalgos.TST.RecommendedTST.RecommendedTST(Atilde, ytilde, nsweep, tol, xinitial, ro))
nikcleju@13 122