示例#1
0
def test_bpdn(par):
    """Basis pursuit denoise (BPDN) problem:

    minimize ||x||_1 subject to ||Ax - b||_2 <= 0.1

    """
    # Create random m-by-n encoding matrix
    n = par['n']
    m = par['m']
    k = par['k']

    A, A1 = np.linalg.qr(np.random.randn(n, m), 'reduced')
    if m > n:
        A = A1.copy()

    # Create sparse vector
    p = np.random.permutation(m)
    p = p[0:k]
    x = np.zeros(m)
    x[p] = np.random.randn(k)

    # Set up vector b, and run solver
    b = A.dot(x) + np.random.randn(n) * 0.075
    sigma = 0.10
    xinv, resid, _, _ = spg_bpdn(A, b, sigma, iter_lim=1000, verbosity=0)
    assert np.linalg.norm(resid) < sigma*1.1 # need to check why resid is slighly bigger than sigma

    # Run solver with subspace minimization
    xinv, _, _, _ = spg_bpdn(A, b, sigma, subspace_min=True, verbosity=0)
    assert np.linalg.norm(resid) < sigma*1.1 # need to check why resid is slighly bigger than sigma
def spgl1_foopsi(fluor, b, c1, g, sn, p, bas_nonneg, verbosity, thr = 1e-2,debug=False):
    
    if 'spg' not in globals():
        raise Exception('The SPGL package could not be loaded, use a different method')
    
    if b is None:
        bas_flag = True
        b = 0
    else:
        bas_flag = False
        
    if c1 is None:
        c1_flag = True
        c1 = 0
    else:
        c1_flag = False

    if bas_nonneg:
        b_lb = 0
    else:
        b_lb = np.min(fluor)
        
    T = len(fluor)
    w = np.ones(np.shape(fluor))
    if bas_flag:
        w = np.hstack((w,1e-10))
        
    if c1_flag:
        w = np.hstack((w,1e-10))
        
    gr = np.roots(np.concatenate([np.array([1]),-g.flatten()])) 
    gd_vec = np.max(gr)**np.arange(T)  # decay vector for initial fluorescence
    
    options = {'project' : spg.NormL1NN_project ,
               'primal_norm' : spg.NormL1NN_primal ,
               'dual_norm' : spg.NormL1NN_dual,
               'weights'   : w,
               'verbosity' : verbosity,
               'iterations': T}
    
    opA = lambda x,mode: G_inv_mat(x,mode,T,g,gd_vec,bas_flag,c1_flag)
    

    spikes,_,_,info = spg_bpdn(opA,np.squeeze(fluor)-bas_nonneg*b_lb - (1-bas_flag)*b -(1-c1_flag)*c1*gd_vec, sn*np.sqrt(T))
    if np.min(spikes)<-thr*np.max(spikes) and not debug:
        spikes[:T][spikes[:T]<0]=0
        spikes,_,_,info = spg_bpdn(opA,np.squeeze(fluor)-bas_nonneg*b_lb - (1-bas_flag)*b -(1-c1_flag)*c1*gd_vec, sn*np.sqrt(T), options)
        
    #print [np.min(spikes[:T]),np.max(spikes[:T])]
    spikes[:T][spikes[:T]<0]=0    

    c = opA(np.hstack((spikes[:T],0)),1)
    if bas_flag:
        b = spikes[T] + b_lb
    
    if c1_flag:
        c1 = spikes[-1]
        
    return c,b,c1,g,sn,spikes
示例#3
0
def spgl1_solve(A, b, epsilon, obj):
    W = [w + 1.01 for w in obj]
    x, o1, o2, o3 = spg_bpdn(np.array(A),
                             np.array(b),
                             epsilon,
                             weights=np.array(W),
                             verbosity=1)
    return x, sum(x), np.linalg.norm(np.subtract(np.dot(A, x), np.array(b)), 1)
示例#4
0
def spgl1_solve(A, b, epsilon, obj):
    W = [w + 1.01 for w in obj]
    #    print len(A), len(A[0])
    #    print len(b)
    x, o1, o2, o3 = spg_bpdn(np.array(A),
                             np.array(b),
                             epsilon,
                             weights=np.array(W))
    return x, sum(x), np.linalg.norm(np.subtract(np.dot(A, x), np.array(b)), 1)
示例#5
0
def spgl1_foopsi(fluor, b, c1, g, sn, p, bas_nonneg, verbosity):

    if "spg" not in globals():
        raise Exception("The SPGL package could not be loaded, use a different method")

    if b is None:
        bas_flag = True
        b = 0
    else:
        bas_flag = False

    if c1 is None:
        c1_flag = True
        c1 = 0
    else:
        c1_flag = False

    if bas_nonneg:
        b_lb = 0
    else:
        b_lb = np.min(fluor)

    T = len(fluor)
    w = np.ones(np.shape(fluor))
    if bas_flag:
        w = np.hstack((w, 1e-10))

    if c1_flag:
        w = np.hstack((w, 1e-10))

    gr = np.roots(np.concatenate([np.array([1]), -g.flatten()]))
    gd_vec = np.max(gr) ** np.arange(T)  # decay vector for initial fluorescence

    options = {
        "project": spg.NormL1NN_project,
        "primal_norm": spg.NormL1NN_primal,
        "dual_norm": spg.NormL1NN_dual,
        "weights": w,
        "verbosity": verbosity,
    }

    opA = lambda x, mode: G_inv_mat(x, mode, T, g, gd_vec, bas_flag, c1_flag)

    spikes = spg_bpdn(
        opA,
        np.squeeze(fluor) - bas_nonneg * b_lb - (1 - bas_flag) * b - (1 - c1_flag) * c1 * gd_vec,
        sn * np.sqrt(T),
        options,
    )[0]
    c = opA(np.hstack((spikes[:T], 0)), 1)
    if bas_flag:
        b = spikes[T] + b_lb

    if c1_flag:
        c1 = spikes[-1]

    return c, b, c1, g, sn, spikes
示例#6
0
def solveSPGL1(Psi, u_samps, N, sigma):
    delta = sigma * LA.norm(u_samps[:N])
    
    C_SPG, r, g, i = spg_bpdn(Psi[0:N,:], u_samps[0:N],
                                  delta, iter_lim=100000, verbosity=2,
                                  opt_tol=1e-9, bp_tol=1e-9)

    mean_SPG = C_SPG[0]
    var_SPG = np.sum(C_SPG[1:]**2)
    
    return C_SPG, mean_SPG, var_SPG, i
示例#7
0
def solveSPGL1(Psi, u_samps, Ntot, N_SPG, sigma):
    inds = np.random.choice(Ntot, size = N_SPG, replace = False)
    delta = sigma * LA.norm(u_samps[inds])
    
    C_SPG, r, g, i = spg_bpdn(Psi[inds,:], u_samps[inds],
                                  delta, iter_lim=100000, verbosity=2,
                                  opt_tol=1e-9, bp_tol=1e-9)

    mean_SPG = C_SPG[0]
    var_SPG = np.sum(C_SPG[1:]**2)
    
    return C_SPG, mean_SPG, var_SPG, i
def spgl1_foopsi(fluor, b, c1, g, sn, p, bas_nonneg, verbosity):
    
    if 'spg' not in globals():
        raise Exception('The SPGL package could not be loaded, use a different method')
    
    if b is None:
        bas_flag = True
        b = 0
    else:
        bas_flag = False
        
    if c1 is None:
        c1_flag = True
        c1 = 0
    else:
        c1_flag = False

    if bas_nonneg:
        b_lb = 0
    else:
        b_lb = np.min(fluor)
        
    T = len(fluor)
    w = np.ones(np.shape(fluor))
    if bas_flag:
        w = np.hstack((w,1e-10))
        
    if c1_flag:
        w = np.hstack((w,1e-10))
        
    gr = np.roots(np.concatenate([np.array([1]),-g.flatten()])) 
    gd_vec = np.max(gr)**np.arange(T)  # decay vector for initial fluorescence
    
    options = {'project' : spg.NormL1NN_project ,
               'primal_norm' : spg.NormL1NN_primal ,
               'dual_norm' : spg.NormL1NN_dual,
               'weights'   : w, 
               'verbosity' : verbosity}
    
    opA = lambda x,mode: G_inv_mat(x,mode,T,g,gd_vec,bas_flag,c1_flag)
    
    spikes = spg_bpdn(opA,np.squeeze(fluor)-bas_nonneg*b_lb - (1-bas_flag)*b -(1-c1_flag)*c1*gd_vec, sn*np.sqrt(T), options)[0]
    c = opA(np.hstack((spikes[:T],0)),1)
    if bas_flag:
        b = spikes[T] + b_lb
    
    if c1_flag:
        c1 = spikes[-1]
        
    return c,b,c1,g,sn,spikes
示例#9
0
    # % Solve the basis pursuit denoise (BPDN) problem:
    # %
    # %    minimize ||x||_1 subject to ||Ax - b||_2 <= 0.1
    # %
    # % -----------------------------------------------------------
    print('%s ' % ('-'*78))
    print('Solve the basis pursuit denoise (BPDN) problem:      ')
    print('                                              ')
    print('  minimize ||x||_1 subject to ||Ax - b||_2 <= 0.1')
    print('                                              ')
    print('%s%s ' % ('-'*78, '\n'))

    # % Set up vector b, and run solver
    b = A.dot(x0) + np.random.randn(m) * 0.075
    sigma = 0.10  #     % Desired ||Ax - b||_2
    x,resid,grad,info = spg_bpdn(A, b, sigma)

    figure()
    plot(x,'b')
    hold(True)
    plot(x0,'ro')
    legend(('Recovered coefficients','Original coefficients'))
    title('(b) Basis Pursuit Denoise')

    print('%s%s%s' % ('-'*35,' Solution ','-'*35))
    print('See figure 1(b)')
    print('%s%s ' % ('-'*78, '\n'))


    # % -----------------------------------------------------------
    # % Solve the basis pursuit (BP) problem in COMPLEX variables:
示例#10
0
        if mode == 1:
            return problem.Jvec(mwav, iwavmap * x)
        else:
            return wavmap * problem.Jtvec(mwav, x)

    b = survey_r.dpred(mwav) - survey_r.dpred(mtrue)

    print "# of data: ", b.shape

    opts = spgl1.spgSetParms({'iterations': 100, 'verbosity': 2})
    sigtol = np.linalg.norm(b) / np.maximum(ratio[it], min_progress)
    #tautol = 20000.

    tic = time.clock()

    x, resid, grad, info = spgl1.spg_bpdn(JS, b, sigma=sigtol, options=opts)

    toc = time.clock()
    print 'SPGL1 chronometer: ', toc - tic
    timespgl1.append(toc - tic)

    #x,resid,grad,info = spgl1.spg_lasso(JS,b,tautol,opts)
    #assert dmis.eval(mwav) > dmis.eval(mwav - iwavmap*x)
    mwav = mwav - iwavmap * x
    xlist.append(x)
    it += 1
    print "end iteration: ", it, '; Subsample Normalized Misfit: ', dmis.eval(
        mwav) / survey_r.nD
    dmisfitsub.append(dmis.eval(mwav) / survey_r.nD)

    problem.unpair()
示例#11
0
    b = Xb_Xa[:, i]
    A = Int_Phi

    Coef['ls'][:, i], res, rnk, s = lstsq(A, b)

##### Solve the coefficient with a regulaized least-square problem
##### with a constraint  "min|Coef|_1 s.t. b - A*Coef <= tolerance"
Coef['bpdn'] = np.zeros((P, n_var))  ### basis pursuit denoising (BPDN)
tol_bpdn = 1e-3
for i in range(n_var):
    b = Xb_Xa[:, i]
    A = Int_Phi

    Coef['bpdn'][:, i], resid, grad, info = spg_bpdn(A,
                                                     b,
                                                     tol_bpdn,
                                                     iter_lim=200,
                                                     verbosity=0)

##### Calculate approximation for dx from the coefficients achieved
for method in ['ls', 'bpdn']:
    DATA['dx:' + method] = np.zeros(DATA['dx:true'].shape)
    for i in range(n_var):
        DATA['dx:' + method][:, i] = np.dot(Phi['approx'], Coef[method][:, i])

##### Plot: Compare coefficients
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 5))
ax1.plot(Coef['true'][:, 0], 'k')
ax1.plot(Coef['ls'][:, 0], 'ro')
ax1.plot(Coef['bpdn'][:, 0], 'bo')
ax1.legend(('True', 'LS', 'BPDN'))
示例#12
0
         label=r'$||x||_1$')
plt.xlabel(r'#iter')
plt.ylabel(r'$||r||_2/||x||_1$')
plt.title('Norms')
plt.legend()

###############################################################################
# Solve the basis pursuit denoise (BPDN) problem:
#
# .. math::
#     min. ||\mathbf{x}||_1 \quad subject \quad to \quad ||\mathbf{Ax} -
#     \mathbf{b}||_2 <= 0.1

b = A.dot(x0) + np.random.randn(m) * 0.075
sigma = 0.10  # Desired ||Ax - b||_2
x, resid, grad, info = spgl1.spg_bpdn(A, b, sigma, iter_lim=100, verbosity=2)

plt.figure()
plt.plot(x, 'b')
plt.plot(x0, 'ro')
plt.legend(('Recovered coefficients', 'Original coefficients'))
plt.title('Basis Pursuit Denoise')


###############################################################################
# Solve the basis pursuit (BP) problem in complex variables:
#
#  .. math::
#      min. ||\mathbf{z}||_1 \quad subject \quad to \quad
#      \mathbf{Az} = \mathbf{b}
class partialFourier(LinearOperator):