annotate pyCSalgos/ABS/ABSexact.py @ 68:cab8a215f9a1 tip

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