示例#1
0
def test_beta():
    for matrix in [real_matrix(), complex_matrix()]:
        for beta in [0, 1, 3.4]:
            matrix_plus_beta = matrix + beta * sparse.eye(*matrix.shape)
            for mode in ["auto", "supernodal", "simplicial"]:
                L = cholesky(matrix, beta=beta).L()
                assert factor_of(cholesky(matrix, beta=beta), matrix_plus_beta)
示例#2
0
文件: geodesic.py 项目: caomw/cmm
 def __init__(self, verts, tris, m=1.0):
     self._verts = verts
     self._tris = tris
     # precompute some stuff needed later on
     e01 = verts[tris[:, 1]] - verts[tris[:, 0]]
     e12 = verts[tris[:, 2]] - verts[tris[:, 1]]
     e20 = verts[tris[:, 0]] - verts[tris[:, 2]]
     self._triangle_area = .5 * veclen(np.cross(e01, e12))
     unit_normal = normalized(np.cross(normalized(e01), normalized(e12)))
     self._un = unit_normal
     self._unit_normal_cross_e01 = np.cross(unit_normal, -e01)
     self._unit_normal_cross_e12 = np.cross(unit_normal, -e12)
     self._unit_normal_cross_e20 = np.cross(unit_normal, -e20)
     # parameters for heat method
     h = np.mean(map(veclen, [e01, e12, e20]))
     t = m * h**2
     # pre-factorize poisson systems
     Lc, vertex_area = compute_mesh_laplacian(verts,
                                              tris,
                                              area_type='lumped_mass')
     A = sparse.spdiags(vertex_area, 0, len(verts), len(verts))
     #self._factored_AtLc = splu((A - t * Lc).tocsc()).solve
     self._factored_AtLc = cholesky((A - t * Lc).tocsc(), mode='simplicial')
     #self._factored_L = splu(Lc.tocsc()).solve
     self._factored_L = cholesky(Lc.tocsc(), mode='simplicial')
示例#3
0
def test_beta():
    for matrix in [real_matrix(), complex_matrix()]:
        for beta in [0, 1, 3.4]:
            matrix_plus_beta = matrix + beta * sparse.eye(*matrix.shape)
            for mode in ["auto", "supernodal", "simplicial"]:
                L = cholesky(matrix, beta=beta).L()
                assert factor_of(cholesky(matrix, beta=beta),
                                 matrix_plus_beta)
示例#4
0
def sparse_build_np_models(kernel, trans_samples, ter_samples, ter_rew_samples, lamb):
    Xa, Ra, Xpa = zip(*trans_samples)
    Xa_term, Ra_term = zip(*ter_samples)
    Xa = [ np.vstack((xa, xa_term)) if xa_term.size > 0 else xa for xa, xa_term in izip(Xa, Xa_term) ]
    Ra = [ np.hstack((ra, ra_term)) if ra_term.size > 0 else ra for ra, ra_term in izip(Ra, Ra_term) ] 
    
    k = len(trans_samples) 
    
    # build the K_a,b matrices
    Kab = dict()
    KabT = dict()
    for a,b in product(xrange(k), xrange(k)):
        if Xa_term[b].size > 0:
            Kab[(a,b)] = np.hstack((kernel(Xa[a], Xpa[b]), 
                                    np.zeros((Xa[a].shape[0], Xa_term[b].shape[0]))))
        else:
            Kab[(a,b)] = kernel(Xa[a], Xpa[b])
        Kab[(a,b)] = csr_matrix(Kab[(a,b)] * (np.abs(Kab[(a,b)]) > 1e-3))
        KabT[(a,b)] = Kab[(a,b)].T.tocsr()
    
    # build the K_a, D_a matrices
    Ka = [kernel(Xa[i], Xa[i])  for i in xrange(k)]
    Dainv = [csc_matrix(Ka[i] * (np.abs(Ka[i]) > 1e-3)) + lamb*scipy.sparse.eye(*Ka[i].shape) for i in xrange(k)]
#    print np.linalg.matrix_rank(Dainv[2].toarray()), Dainv[2].shape, np.linalg.cond(Dainv[2].toarray())
#    print np.linalg.eig(Dainv[2].toarray())[0]
#    print [np.linalg.eig(Dainv[i].toarray())[0].min() for i in xrange(3)]
#    plt.spy(Dainv[2].toarray())
#    plt.show()  
#    print Dainv[2].shape

    index = (squareform(pdist(Xa[2])) == 0.0).nonzero()    
    
#    print (squareform(pdist(Xa[2])) == 0.0).nonzero()
#    print Xa[2][index[0][:5],:]
#    print Xa[2][index[1][:5],:]
#    
#    splu(Dainv[0])    
#    cholesky(Dainv[0])
#    splu(Dainv[1])
#    cholesky(Dainv[1])
#    splu(Dainv[2])
    cholesky(Dainv[2])
        
    
    Da= [cholesky(Dainv[i]) for i in xrange(k)]
#    Da = [splu(Dainv[i]) for i in xrange(k)]
        
    # build K_ter matrix
    Kterma = [ np.hstack((kernel(ter_rew_samples[0], Xpa[i]),
                          np.zeros((ter_rew_samples[0].shape[0], Xa_term[i].shape[0])))) if Xa_term[i].size > 0
                else kernel(ter_rew_samples[0], Xpa[i]) for i in xrange(k)]
    K_ter = kernel(ter_rew_samples[0], ter_rew_samples[0])
    D_ter = cholesky(csc_matrix(K_ter*(np.abs(K_ter) > 1e-3)) + lamb*scipy.sparse.eye(*K_ter.shape))
#    D_ter = splu(csc_matrix(K_ter*(np.abs(K_ter) > 1e-3)) + lamb*scipy.sparse.eye(*K_ter.shape))
    R_ter = ter_rew_samples[1]
    
    return kernel, Kab, KabT, Da, Dainv, Ra, Kterma, D_ter, R_ter, Xa
示例#5
0
def getModel(img,
             var,
             omat,
             cmat,
             rmat,
             reg,
             niter=10,
             fixreg=False,
             isin=None,
             removeedges=True,
             regmin=1e-5,
             regacc=0.001):
    from scikits.sparse.cholmod import cholesky
    from scikits.sparse.cholmod import CholmodError
    import numpy

    if ((removeedges) & (isin != None)):
        omat = omat.tocsr()[:, isin]
        rmat = rmat[isin][:, isin]

    rhs = omat.T * (img / var)

    B = omat.T * cmat * omat

    res = 0.
    if fixreg == False:
        regs = [reg]
    else:
        regs = [fixreg]

    lhs = B + regs[-1] * rmat

    F = cholesky(lhs)
    fit = F(rhs)

    if fixreg != False:
        finalreg = fixreg
    else:
        finalreg = solveSPregularization(B,
                                         rmat,
                                         reg,
                                         rhs,
                                         niter=niter,
                                         regmin=regmin,
                                         regacc=regacc)

    lhs = B + finalreg * rmat
    F = cholesky(lhs)
    fit = F(rhs)

    model = omat * fit

    return model, fit, finalreg, B
示例#6
0
def get_potentials(conductivity_laplacian, io_index_pair):
    """
    Recovers voltages based on the conductivity Laplacian and the IO array

    :param conductivity_laplacian:
    :param io_index_pair:

    :return: array of potential in each node
    """
    # TODO: technically, Cholesky is not the best solver. Change is needed, but in approximation
    # it should be good enough

    if switch_to_splu:
        io_array = build_sink_source_current_array(
            io_index_pair, conductivity_laplacian.shape, splu=True)
        local_conductivity_laplacian = trim_matrix(conductivity_laplacian,
                                                   io_index_pair[1])
        log.info('starting splu computation')
        solver = splu(csc_matrix(local_conductivity_laplacian))
        log.info('splu computation done')
        return solver.solve(io_array.toarray())
    else:
        io_array = build_sink_source_current_array(
            io_index_pair, conductivity_laplacian.shape)
        solver = cholesky(csc_matrix(conductivity_laplacian), fudge)
        return solver(io_array)
示例#7
0
def test_spinv():
    X = sparse.rand(500,500,density=0.03)
    K = (X.T * X).tocsc() + sparse.identity(500)
    for mode in ("simplicial", "supernodal"):
        L = cholesky(K, mode=mode)
        # Full inverse
        inv_K = L.inv().todense()
        for form in ("lower", "upper", "full"):
            # Sparse inverse
            spinv_K = L.spinv(form=form)
            # 1) Check that the non-zero elements of spinv_K are
            # correct
            spinv_K = spinv_K.tocoo()
            i = spinv_K.row
            j = spinv_K.col
            x = spinv_K.data
            assert(np.allclose(x, inv_K[i,j]))
            # 2) Check that the elements that are non-zero in K
            # are correct in spinv_K
            K = K.tocoo()
            i = K.row
            j = K.col
            spinv_K = spinv_K.todense()
            if form == "lower":
                Z = np.tril(inv_K)
            elif form == "upper":
                Z = np.triu(inv_K)
            else:
                Z = inv_K
            assert(np.allclose(spinv_K[i,j], Z[i,j]))
def getModel(img,var,lmat,pmat,cmat,rmat,reg,niter=10,onlyRes=False):
    from scikits.sparse.cholmod import cholesky
    import numpy

    omat = pmat*lmat
    rhs = omat.T*(img/var)

    B = omat.T*cmat*omat

    res = 0.
    regs = [reg]
    lhs = B+regs[-1]*rmat

    F = cholesky(lhs)
    fit = F(rhs)
    for i in range(niter):
        res = fit.dot(rmat*fit)

        delta = reg*1e3
        lhs2 = B+(reg+delta)*rmat

        T = (2./delta)*(numpy.log(F.cholesky(lhs2).L().diagonal()).sum()-numpy.log(F.L().diagonal()).sum())
        reg = (omat.shape[0]-T*reg)/res
        if abs(reg-regs[-1])/reg<0.005:
            break
        regs.append(reg)
        lhs = B+regs[-1]*rmat
        F = F.cholesky(lhs)
        fit = F(rhs)
    print reg,regs
    res = -0.5*res*regs[-1] + -0.5*((omat*fit-img)**2/var).sum()
    if onlyRes:
        return res,reg
    model = (omat*fit)
    return res,reg,fit,model
示例#9
0
def test_complex():
    c = complex_matrix()
    fc = cholesky(c)
    r = real_matrix()
    fr = cholesky(r)
    
    assert factor_of(fc, c)

    assert np.allclose(fc(np.arange(4)),
                       (c.todense().I * np.arange(4)[:, np.newaxis]).ravel())
    assert np.allclose(fc(np.arange(4) * 1j),
                       (c.todense().I * (np.arange(4) * 1j)[:, np.newaxis]).ravel())
    assert np.allclose(fr(np.arange(4)),
                       (r.todense().I * np.arange(4)[:, np.newaxis]).ravel())
    # If we did a real factorization, we can't do solves on complex arrays:
    assert_raises(CholmodError, fr, np.arange(4) * 1j)
示例#10
0
def test_spinv():
    X = sparse.rand(500, 500, density=0.03)
    K = (X.T * X).tocsc() + sparse.identity(500)
    for mode in ("simplicial", "supernodal"):
        L = cholesky(K, mode=mode)
        # Full inverse
        inv_K = L.inv().todense()
        for form in ("lower", "upper", "full"):
            # Sparse inverse
            spinv_K = L.spinv(form=form)
            # 1) Check that the non-zero elements of spinv_K are
            # correct
            spinv_K = spinv_K.tocoo()
            i = spinv_K.row
            j = spinv_K.col
            x = spinv_K.data
            assert (np.allclose(x, inv_K[i, j]))
            # 2) Check that the elements that are non-zero in K
            # are correct in spinv_K
            K = K.tocoo()
            i = K.row
            j = K.col
            spinv_K = spinv_K.todense()
            if form == "lower":
                Z = np.tril(inv_K)
            elif form == "upper":
                Z = np.triu(inv_K)
            else:
                Z = inv_K
            assert (np.allclose(spinv_K[i, j], Z[i, j]))
def sample_mult_normal_given_precision(mu, precision, num_of_samples):
	from scikits.sparse import cholmod
	factor = cholmod.cholesky(precision)
	D = factor.D(); D = numpy.reshape(D, (len(D),1))
	samples = factor.solve_Lt(numpy.random.normal(size=(len(mu),num_of_samples))/numpy.sqrt(D))
	samples = numpy.repeat(mu, num_of_samples, axis=1)+ factor.apply_Pt(samples)
	return samples
示例#12
0
def manifold_harmonics(verts,
                       tris,
                       K,
                       scaled=True,
                       return_D=False,
                       return_eigenvalues=False):
    Q, vertex_area = compute_mesh_laplacian(verts,
                                            tris,
                                            'cotangent',
                                            return_vertex_area=True,
                                            area_type='lumped_mass')
    if scaled:
        D = sparse.spdiags(vertex_area, 0, len(verts), len(verts))
    else:
        D = sparse.spdiags(np.ones_like(vertex_area), 0, len(verts),
                           len(verts))

    try:
        lambda_dense, Phi_dense = eigsh(-Q, M=D, k=K, sigma=0)
    except RuntimeError, e:
        if e.message == 'Factor is exactly singular':
            logging.warn(
                "factor is singular, trying some regularization and cholmod")
            chol_solve = cholesky(-Q + sparse.eye(Q.shape[0]) * 1.e-9,
                                  mode='simplicial')
            OPinv = sparse.linalg.LinearOperator(Q.shape, matvec=chol_solve)
            lambda_dense, Phi_dense = eigsh(-Q, M=D, k=K, sigma=0, OPinv=OPinv)
        else:
            raise e
示例#13
0
def test_complex():
    c = complex_matrix()
    fc = cholesky(c)
    r = real_matrix()
    fr = cholesky(r)

    assert factor_of(fc, c)

    assert np.allclose(fc(np.arange(4)),
                       (c.todense().I * np.arange(4)[:, np.newaxis]).ravel())
    assert np.allclose(fc(np.arange(4) * 1j),
                       (c.todense().I *
                        (np.arange(4) * 1j)[:, np.newaxis]).ravel())
    assert np.allclose(fr(np.arange(4)),
                       (r.todense().I * np.arange(4)[:, np.newaxis]).ravel())
    # If we did a real factorization, we can't do solves on complex arrays:
    assert_raises(CholmodError, fr, np.arange(4) * 1j)
示例#14
0
def IRLS(Y, W, nore, reData, estCov, curr_bbeta):
    N = W.shape[0]
    nofe = W.shape[1] - nore
    err = 1
    logistic_input = W*curr_bbeta;
    curr_pred = np.asarray(np.divide(np.exp(logistic_input),(1+np.exp(logistic_input))))
    inv_curr_covar = invrecovar(reData, estCov); # Construct the covariance matrix b given the estimate of covariance matrix of random effects
    hessian_from_logprior = block_diag((inv_curr_covar,np.zeros((nofe,nofe))))
    start_time = time.time()
    curr_b = curr_bbeta[np.arange(nore)] 
    while err>0.00001:
      old_b = curr_b;
      old_bbeta = curr_bbeta; 
      curr_gradient = -((W.T)*(Y-curr_pred) -sp.vstack(((inv_curr_covar*curr_b), np.zeros((nofe,1)))));
      hessain_from_loglik = (W.T)*(sp.csr_matrix(((curr_pred*(1-curr_pred)).ravel(), (np.arange(N), np.arange(N)))))*W;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
      curr_hessian = hessain_from_loglik+hessian_from_logprior;
      factor = cholesky(curr_hessian);
      delta_bbeta = factor(curr_gradient);
      delta_bbeta = np.reshape(delta_bbeta, (len(delta_bbeta),1));
      curr_bbeta = curr_bbeta - delta_bbeta;
      logistic_input = W*curr_bbeta;
      curr_pred = np.asarray(np.divide(np.exp(logistic_input),(1+np.exp(logistic_input))));
      curr_b = curr_bbeta[0:nore];
      curr_beta = curr_bbeta[nore:];
      err = np.sqrt(sum(np.square(curr_bbeta - old_bbeta))/len(old_bbeta));
      #print repr(err)
    pred_error = np.sqrt(sum(np.square(curr_pred - Y))/len(curr_pred)); # current prediction mean square error
    print 'Current training RMSE = '+repr(pred_error);
    hessian_from_loglik = (W.T)*(sp.csr_matrix(((curr_pred*(1-curr_pred)).ravel(), (np.arange(N), np.arange(N)))))*W;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
    curr_hessian = hessian_from_logprior+hessian_from_loglik;
    curr_b = sp.csr_matrix(curr_b);
    log_c1 = sp.csr_matrix((W*curr_bbeta).T)*Y - sum(np.log(1+np.exp(W*curr_bbeta))) - 0.5*curr_b.T*(inv_curr_covar)*(curr_b); # refer to sampling paper
    # log_lik_part = - 0.5*inv_curr_covar.shape[1]*np.log(2*np.pi);

    # for i in range(len(reData)):
    #   p = len(np.unique(reData[i][1]));
    #   (sign, logdet) = np.linalg.slogdet(estCov[i]);
    #   log_lik_part = log_lik_part - p*logdet;

    # log_lik = log_c1 + log_lik_part;
    factor1 = cholesky(curr_hessian);
    factor2 = cholesky(inv_curr_covar);
    log_lik = -factor1.logdet()+factor2.logdet();

    return ([curr_bbeta, curr_pred, curr_hessian, log_c1, log_lik])
示例#15
0
def logdet_cholmod(R,U,rows,cols, psd_tolerance=1e-6, factor=None):
  from scikits.sparse.cholmod import cholesky
  if not is_psd_cholmod(R, U, rows, cols, psd_tolerance, factor):
    return -np.Inf
  X = build_sparse(R, U, rows, cols)
#  print factor
  filled_factor = cholesky(X) if factor is None else factor.cholesky(X)
  D = filled_factor.D()
  return np.sum(np.log(D))
 def cholsolve(A, b, fac=None):
     if fac is None:
         fac = cholmod.cholesky(A)
     else:
         # store a representation of the factorised matrix and update it in
         # place
         fac.cholesky_inplace(A)
     x = fac.solve_A(b)
     return x, fac
def block_cov_matrix(W, s):
    """
    extract block inverse from W. 
    the indices of the block elements are specified in the array s
    """
    A, B, C, D = extract_blocks(W, s)
    f = cholesky(D)
    w = A - B * f(C)
    return np.linalg.inv(w.todense())
示例#18
0
def is_psd_cholmod(R,U,rows,cols,tolerance=1e-6,factor=None):
  X = build_sparse(R, U, rows, cols)
  from scikits.sparse.cholmod import cholesky
  try:
    full_factor = cholesky(X) if factor is None else factor.cholesky(X)
  except:
    return False
  D = full_factor.D()
  return np.all(D>tolerance)
示例#19
0
def adjust_pose_graph(x_hat, constraints):
    num_of_nodes = len(x_hat)
    jacobian = np.empty((3, 6), dtype=float)

    F = 0
    threshold = 0.1

    while not (F < threshold):
        hessian = np.empty((num_of_nodes * 3, num_of_nodes * 3), dtype=float)
        info_vector = np.empty((num_of_nodes * 3), dtype=float)

        F = 0

        for c in constraints:
            i = c[0]._prev_node._id
            j = c[0]._post_node._id

            node_i = x_hat[i]
            node_j = x_hat[j]

            # Following section is programmed with motion model
            calc_jacobian(c, jacobian)

            A_ij = jacobian[0:3, 0:3]
            B_ij = jacobian[0:3, 3:6]
            Sigma_ij = c[1]

            # update H
            hessian[i * 3:(i + 1) * 3,
                    i * 3:(i + 1) * 3] += np.dot(np.dot(A_ij.T, Sigma_ij),
                                                 A_ij)
            hessian[i * 3:(i + 1) * 3,
                    j * 3:(j + 1) * 3] += np.dot(np.dot(A_ij.T, Sigma_ij),
                                                 B_ij)
            hessian[j * 3:(j + 1) * 3,
                    i * 3:(i + 1) * 3] += np.dot(np.dot(B_ij.T, Sigma_ij),
                                                 A_ij)
            hessian[j * 3:(j + 1) * 3,
                    j * 3:(j + 1) * 3] += np.dot(np.dot(B_ij.T, Sigma_ij),
                                                 B_ij)

            # update b
            info_vector[i * 3:(i + 1) * 3] += np.dot(np.dot(A_ij.T, Sigma_ij),
                                                     A_ij)
            info_vector[j * 3:(j + 1) * 3] += np.dot(np.dot(A_ij.T, Sigma_ij),
                                                     A_ij)

        hessian[0][0] += 10000  # to keep initial coordinates

        factor = cholesky(hessian)
        delta_x = factor(info_vector)
        x_hat += delta_x

        # calculate F(x) = e.T * Omega * e
        error = (node_i.pose - node_j.pose) - c[0].edge.pose
        F += np.dot(np.dot(error.T, c[1]), error)
示例#20
0
def test_deprecation():
    f = cholesky(sparse.eye(5, 5))
    b = np.ones(5)
    for dep_method in "solve_P", "solve_Pt":
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            getattr(f, dep_method)(b)
            assert len(w) == 1
            assert issubclass(w[-1].category, DeprecationWarning)
            assert "deprecated" in str(w[-1].message)
示例#21
0
def test_deprecation():
    f = cholesky(sparse.eye(5, 5))
    b = np.ones(5)
    for dep_method in "solve_P", "solve_Pt":
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            getattr(f, dep_method)(b)
            assert len(w) == 1
            assert issubclass(w[-1].category, DeprecationWarning)
            assert "deprecated" in str(w[-1].message)
示例#22
0
def _random_correlated_image(mean, sigma, image_shape, alpha=0.3, rng=None):
    """
    Creates a random image with correlated neighbors.
    pixel covariance is sigma^2, direct neighors pixel covariance is alpha * sigma^2.

    Parameters
    ----------
    mean : the mean value of the image pixel values.
    sigma : the std dev of image pixel values.
    image_shape : tuple, shape = (3, )
    alpha : the neighbors correlation factor.
    rng : random number generator (a numpy.random.RandomState instance).
    """
    dim_x, dim_y, dim_z = image_shape
    dim_image = dim_x * dim_y * dim_z

    correlated_image = 0
    for neighbor in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
        corr_data = []
        corr_i = []
        corr_j = []
        for i, j, k in [(0, 0, 0), neighbor]:
            d2 = 1.0 * (i*i + j*j + k*k)
            ind = np.asarray(np.mgrid[0:dim_x-i, 0:dim_y-j, 0:dim_z-k], dtype=np.int)
            ind = ind.reshape((3, (dim_x - i) * (dim_y - j) * (dim_z - k)))
            corr_i.extend(np.ravel_multi_index(ind, (dim_x, dim_y, dim_z)).tolist())
            corr_j.extend(np.ravel_multi_index(ind + np.asarray([i, j, k])[:, None],
                                          (dim_x, dim_y, dim_z)).tolist())
            if i>0 or j>0 or k>0:
                corr_i.extend(np.ravel_multi_index(ind + np.asarray([i, j, k])[:, None],
                                              (dim_x, dim_y, dim_z)).tolist())
                corr_j.extend(np.ravel_multi_index(ind, (dim_x, dim_y, dim_z)).tolist())
            if i==0 and j==0 and k==0:
                corr_data.extend([3.0] * ind.shape[1])
            else:
                corr_data.extend([alpha * 3.0] * 2 * ind.shape[1])

        correlation = scisp.csc_matrix((corr_data, (corr_i, corr_j)), shape=(dim_image, dim_image))

        factor = cholesky(correlation)
        L = factor.L()
        P = factor.P()[None, :]
        P = scisp.csc_matrix((np.ones(dim_image),
                              np.vstack((P, np.asarray(range(dim_image))[None, :]))),
                             shape=(dim_image, dim_image))

        sq_correlation = P.dot(L)
        X = rng.normal(0, 1, dim_image)
        Y = sq_correlation.dot(X)
        Y = Y.reshape((dim_x, dim_y, dim_z))
        X = X.reshape((dim_x, dim_y, dim_z))
        correlated_image += Y
    correlated_image /= 3

    return correlated_image * sigma + mean
示例#23
0
def test_spinv_complex():
    # Complex matrices do not work yet, but this test checks that some
    # reasonable error is produced
    C = complex_matrix()
    L = cholesky(C)
    try:
        # This should result CholmodError because spinv is not
        # implemented for complex matrices - yet
        invC = L.spinv()
        assert (False)
    except CholmodError:
        assert (True)
def get_posterior_params(W, V, M, K_inv):
	"""
	INPUT: 	W, sparse matrix format, same dimension as K_inv
			V, a numpy vector of size K_inv.shape[0]
			M: the mean vector in numpy format
			K_inv: precision matrix in sparse form
	"""
	from scikits.sparse import cholmod
	posterior_precision = K_inv + W
	posterior_precision_factor = cholmod.cholesky(posterior_precision)
	posterior_mean = posterior_precision_factor(V + K_inv*M)
	return (posterior_mean, posterior_precision) 
示例#25
0
def test_spinv_complex():
    # Complex matrices do not work yet, but this test checks that some
    # reasonable error is produced
    C = complex_matrix()
    L = cholesky(C)
    try:
        # This should result CholmodError because spinv is not
        # implemented for complex matrices - yet
        invC = L.spinv()
        assert(False)
    except CholmodError:
        assert(True)
示例#26
0
def test_update_downdate():
    m = real_matrix()
    f = cholesky(m)
    L = f.L()[f.P(), :]
    assert factor_of(f, m)
    f.update_inplace(L)
    assert factor_of(f, 2 * m)
    f.update_inplace(L)
    assert factor_of(f, 3 * m)
    f.update_inplace(L, subtract=True)
    assert factor_of(f, 2 * m)
    f.update_inplace(L, subtract=True)
    assert factor_of(f, m)
示例#27
0
def test_update_downdate():
    m = real_matrix()
    f = cholesky(m)
    L = f.L()[f.P(), :]
    assert factor_of(f, m)
    f.update_inplace(L)
    assert factor_of(f, 2 * m)
    f.update_inplace(L)
    assert factor_of(f, 3 * m)
    f.update_inplace(L, subtract=True)
    assert factor_of(f, 2 * m)
    f.update_inplace(L, subtract=True)
    assert factor_of(f, m)
示例#28
0
def test_convenience():
    A_dense_seed = np.array([[10, 0, 3, 0], [0, 5, 0, -2], [3, 0, 5, 0],
                             [0, -2, 0, 2]])
    for mode in ("simplicial", "supernodal"):
        for dtype in (float, complex):
            A_dense = np.array(A_dense_seed, dtype=dtype)
            A_sp = sparse.csc_matrix(A_dense)
            f = cholesky(A_sp, mode=mode)
            assert np.allclose(f.det(), np.linalg.det(A_dense))
            assert np.allclose(f.logdet(), np.log(np.linalg.det(A_dense)))
            assert np.allclose(f.slogdet(),
                               [1, np.log(np.linalg.det(A_dense))])
            assert np.allclose((f.inv() * A_sp).todense(), np.eye(4))
示例#29
0
def get_potentials(conductivity_laplacian, io_index_pair):
    """
    Recovers voltages based on the conductivity Laplacian and the IO array

    :param conductivity_laplacian:
    :param io_index_pair:

    :return: array of potential in each node
    """
    # TODO: technically, Cholesky is not the best solver => change it if needed
    solver = cholesky(csc_matrix(conductivity_laplacian), fudge)
    io_array = build_sink_source_current_array(io_index_pair,
                                               conductivity_laplacian.shape)
    return solver(io_array)
示例#30
0
def test_convenience():
    A_dense_seed = np.array([[10, 0, 3, 0],
                             [0, 5, 0, -2],
                             [3, 0, 5, 0],
                             [0, -2, 0, 2]])
    for mode in ("simplicial", "supernodal"):
        for dtype in (float, complex):
            A_dense = np.array(A_dense_seed, dtype=dtype)
            A_sp = sparse.csc_matrix(A_dense)
            f = cholesky(A_sp, mode=mode)
            assert np.allclose(f.det(), np.linalg.det(A_dense))
            assert np.allclose(f.logdet(), np.log(np.linalg.det(A_dense)))
            assert np.allclose(f.slogdet(), [1, np.log(np.linalg.det(A_dense))])
            assert np.allclose((f.inv() * A_sp).todense(), np.eye(4))
示例#31
0
文件: geodesic.py 项目: KeeganRen/cmm
 def __init__(self, verts, tris, m=1.0):
     self._verts = verts
     self._tris = tris
     # precompute some stuff needed later on
     e01 = verts[tris[:,1]] - verts[tris[:,0]]
     e12 = verts[tris[:,2]] - verts[tris[:,1]]
     e20 = verts[tris[:,0]] - verts[tris[:,2]]
     self._triangle_area = .5 * veclen(np.cross(e01, e12))
     unit_normal = normalized(np.cross(normalized(e01), normalized(e12)))
     self._un = unit_normal
     self._unit_normal_cross_e01 = np.cross(unit_normal, -e01)
     self._unit_normal_cross_e12 = np.cross(unit_normal, -e12)
     self._unit_normal_cross_e20 = np.cross(unit_normal, -e20)
     # parameters for heat method
     h = np.mean(map(veclen, [e01, e12, e20]))
     t = m * h ** 2
     # pre-factorize poisson systems
     Lc, vertex_area = compute_mesh_laplacian(verts, tris, area_type='lumped_mass')
     A = sparse.spdiags(vertex_area, 0, len(verts), len(verts))
     #self._factored_AtLc = splu((A - t * Lc).tocsc()).solve
     self._factored_AtLc = cholesky((A - t * Lc).tocsc(), mode='simplicial')
     #self._factored_L = splu(Lc.tocsc()).solve
     self._factored_L = cholesky(Lc.tocsc(), mode='simplicial')
示例#32
0
def random_projection_cholmod_csc(X,k,factor=None):
  from scikits.sparse.cholmod import cholesky
  # TODO: add factor update
  full_factor = cholesky(X) if factor is None else factor.cholesky(X)
  (n,_) = X.shape
  D = full_factor.D()
  D12 = np.sqrt(D)
  Q = np.random.rand(k,n)
  Q[Q<0.5] = -1.0/np.sqrt(k)
  Q[Q>=0.5] = 1.0/np.sqrt(k)
  zA = (Q / D12).T
  zB = full_factor.solve_Lt(zA)
  A = full_factor.apply_Pt(zB).T
  return A
示例#33
0
def test_cholesky_smoke_test():
    f = cholesky(sparse.eye(10, 10) * 1.)
    d = np.arange(20).reshape(10, 2)
    print "dense"
    assert np.allclose(f(d), d)
    print "sparse"
    s_csc = sparse.csc_matrix(np.eye(10)[:, :2] * 1.)
    assert sparse.issparse(f(s_csc))
    assert np.allclose(f(s_csc).todense(), s_csc.todense())
    print "csr"
    s_csr = s_csc.tocsr()
    assert sparse.issparse(f(s_csr))
    assert np.allclose(f(s_csr).todense(), s_csr.todense())
    print "extract"
    assert np.all(f.P() == np.arange(10))
示例#34
0
def test_cholesky_smoke_test():
    f = cholesky(sparse.eye(10, 10) * 1.)
    d = np.arange(20).reshape(10, 2)
    print "dense"
    assert np.allclose(f(d), d)
    print "sparse"
    s_csc = sparse.csc_matrix(np.eye(10)[:, :2] * 1.)
    assert sparse.issparse(f(s_csc))
    assert np.allclose(f(s_csc).todense(), s_csc.todense())
    print "csr"
    s_csr = s_csc.tocsr()
    assert sparse.issparse(f(s_csr))
    assert np.allclose(f(s_csr).todense(), s_csr.todense())
    print "extract"
    assert np.all(f.P() == np.arange(10))
示例#35
0
    def solve (self, recover_cov=False):
        n = len (self.nodes)
        L = sparse.coo_matrix ((self._data, (self._rows,self._cols)), 
                shape=(self._Ns*n,self._Ns*n))
        e = np.asarray (self._vector)
        factor = cholmod.cholesky (L.tocsc ())

        x = factor (e)
        self.x = x.reshape (n,self._Ns)

        if recover_cov:
            S = factor (np.eye (self._Ns*n))
            Smarginal = [S[ii:ii+2,ii:ii+2].ravel ().tolist () for ii in range (0,self._Ns*n,self._Ns)]
            self.cov = np.asarray (Smarginal)
        else: self.cov = np.zeros ((n,4))
示例#36
0
    def __init__(self, A, **kwargs):
        """CholeskyOperator corresponding to the sparse matrix `A`.

        CholeskyOperator(A)

        The sparse matrix `A` must be in one of the SciPy sparse
        matrix formats.
        """
        m, n = A.shape
        if m != n:
            raise ValueError("Input matrix must be square")

        self.__factor = cholesky(A)

        super(CholeskyOperator, self).__init__(n, n, matvec=self.cholesky_matvec, symmetric=True, **kwargs)
示例#37
0
def test_solve_edge_cases():
    m = real_matrix()
    f = cholesky(m)
    # sparse matrices give a sparse back:
    assert sparse.issparse(f(sparse.eye(*m.shape).tocsc()))
    # dense matrices give a dense back:
    assert not sparse.issparse(f(np.eye(*m.shape)))
    # 1d dense matrices are accepted and a 1d vector is returned (this matches
    # the behavior of np.dot):
    assert f(np.arange(m.shape[0])).shape == (m.shape[0],)
    # 2d dense matrices are also accepted:
    assert f(np.arange(m.shape[0])[:, np.newaxis]).shape == (m.shape[0], 1)
    # But not if the dimensions are wrong...:
    assert_raises(CholmodError, f, np.arange(m.shape[0] + 1)[:, np.newaxis])
    assert_raises(CholmodError, f, np.arange(m.shape[0])[np.newaxis, :])
    assert_raises(CholmodError, f, np.arange(m.shape[0])[:, np.newaxis, np.newaxis])
    # And ditto for the sparse version:
    assert_raises(CholmodError, f, sparse.eye(m.shape[0] + 1, m.shape[1]).tocsc())
示例#38
0
def getModelG(img,var,mat,cmat,rmat=None,reg=None,niter=10):
    from scikits.sparse.cholmod import cholesky
    from scipy.sparse import hstack
    import numpy
    rhs = mat.T*(img/var)

    B = fastMult(mat.T,cmat*mat)

    if rmat is not None:
        lhs = B+rmat*reg
        regs = [reg]
    else:
        niter = 0
        lhs = B
        reg = 0

    i = 0
    import time
    t = time.time()
    F = cholesky(lhs)
    print time.time()-t
    t = time.time()
    fit = F(rhs)
    print time.time()-t
    res = 0.
    for i in range(niter):
        res = fit.dot(rmat*fit)

        delta = reg*1e-3
        lhs2 = B+(reg+delta)*rmat

        T = (2./delta)*(numpy.log(F.cholesky(lhs2).L().diagonal()).sum()-numpy.log(F.L().diagonal()).sum())
        reg = abs(mat.shape[1]-T*reg)/res

        if abs(reg-regs[-1])/reg<0.005:
            break
        regs.append(reg)
        lhs = B+regs[-1]*rmat
        F = F.cholesky(lhs)
        fit = F(rhs)
        res = -0.5*res*regs[-1]
    res += -0.5*((mat*fit-img)**2/var).sum()

    return res,fit,mat*fit,rhs,(reg,i)
示例#39
0
def scipy_solver(A, M, is_hermitian):
    '''Smallest eigenvalues'''
    A, M = map(mat_to_csr, (A, M))
    
    chol = cholesky(M)
    Minv = LinearOperator(matvec=lambda x, mat=chol: mat.solve_A(x),
                          shape=M.shape)

    if is_hermitian:
        try:
            eigw = eigsh(A, k=5, M=M, sigma=None, which='SM', v0=None, ncv=None,        
                         maxiter=None, tol=1E-10, return_eigenvectors=False)
        except ArpackError as e:
            print '\tDivergence'
            eigw = e.eigenvalues
    else:
        raise NotImplementedError

    return eigw
示例#40
0
    def __init__(self, A, **kwargs):
        """CholeskyOperator corresponding to the sparse matrix `A`.

        CholeskyOperator(A)

        The sparse matrix `A` must be in one of the SciPy sparse
        matrix formats.
        """
        m, n = A.shape
        if m != n:
            raise ValueError("Input matrix must be square")

        self.__factor = cholesky(A)

        super(CholeskyOperator, self).__init__(n,
                                               n,
                                               matvec=self.cholesky_matvec,
                                               symmetric=True,
                                               **kwargs)
示例#41
0
文件: cmm.py 项目: KeeganRen/cmm
def manifold_harmonics(verts, tris, K, scaled=True, return_D=False, return_eigenvalues=False):
    Q, vertex_area = compute_mesh_laplacian(
        verts, tris, 'cotangent', 
        return_vertex_area=True, area_type='lumped_mass'
    )
    if scaled:
        D = sparse.spdiags(vertex_area, 0, len(verts), len(verts))
    else:
        D = sparse.spdiags(np.ones_like(vertex_area), 0, len(verts), len(verts))

    try:
        lambda_dense, Phi_dense = eigsh(-Q, M=D, k=K, sigma=0)
    except RuntimeError, e:
        if e.message == 'Factor is exactly singular':
            logging.warn("factor is singular, trying some regularization and cholmod")
            chol_solve = cholesky(-Q + sparse.eye(Q.shape[0]) * 1.e-9, mode='simplicial')
            OPinv = sparse.linalg.LinearOperator(Q.shape, matvec=chol_solve)
            lambda_dense, Phi_dense = eigsh(-Q, M=D, k=K, sigma=0, OPinv=OPinv)
        else:
            raise e
示例#42
0
def test_solve_edge_cases():
    m = real_matrix()
    f = cholesky(m)
    # sparse matrices give a sparse back:
    assert sparse.issparse(f(sparse.eye(*m.shape).tocsc()))
    # dense matrices give a dense back:
    assert not sparse.issparse(f(np.eye(*m.shape)))
    # 1d dense matrices are accepted and a 1d vector is returned (this matches
    # the behavior of np.dot):
    assert f(np.arange(m.shape[0])).shape == (m.shape[0], )
    # 2d dense matrices are also accepted:
    assert f(np.arange(m.shape[0])[:, np.newaxis]).shape == (m.shape[0], 1)
    # But not if the dimensions are wrong...:
    assert_raises(CholmodError, f, np.arange(m.shape[0] + 1)[:, np.newaxis])
    assert_raises(CholmodError, f, np.arange(m.shape[0])[np.newaxis, :])
    assert_raises(CholmodError, f,
                  np.arange(m.shape[0])[:, np.newaxis, np.newaxis])
    # And ditto for the sparse version:
    assert_raises(CholmodError, f,
                  sparse.eye(m.shape[0] + 1, m.shape[1]).tocsc())
示例#43
0
    def compute_mfpt_symmetric(self, Peq):
        """make the matrix symmetric by multiplying both sides of the equation by Peq
        
            sum_v Peq_u k_uv = -Peq_u
        """
        intermediates = self.nodes - self.B
        
        node_list = list(intermediates)
        n = len(node_list)
        matrix = scipy.sparse.dok_matrix((n,n))
        node2i = dict([(node,i) for i, node in enumerate(node_list)])
        
        right_side = -np.array([Peq[u] for u in node_list])
        
        for iu, u in enumerate(node_list):
            matrix[iu,iu] = -self.sum_out_rates[u] * Peq[u]
        
        for uv, rate in self.rates.iteritems():
            u, v = uv
            if u in intermediates and v in intermediates: 
                ui = node2i[u]
                vi = node2i[v]
                assert ui != vi
                matrix[ui,vi] = rate * Peq[u]
        
        node_list = node_list
        node2i = node2i
        matrix =  matrix.tocsc()
        
        t0 = time.clock()
        from scikits.sparse.cholmod import cholesky
        factor = cholesky(matrix)
        times = factor(right_side)
#        times = scikits.sparse.spsolve(matrix, right_side,
#                                            use_umfpack=True)
        print "time solving symmetric linalg", time.clock() - t0
        self.time_solve += time.clock() - t0
        self.mfpt_dict = dict(((node, time) for node, time in izip(node_list, times)))
        if np.any(times < 0):
            raise LinalgError("error the mean first passage times are not all greater than zero")
        return self.mfpt_dict
示例#44
0
def sample_v(hessian, m_factor, nore, bbeta, Y, W, inv_curr_covar, log_c1, num_v_samples, num_parallel_loop, parallel_loop_flag):
	##### b_opt is same as \theta*
	
	# First we sample b's using cholesky decomposition of hessian
	#### start of line 11
	np.random.seed()
	start_time = time.time()
	warnings.filterwarnings("ignore")
	factor = cholesky(hessian)
	D = factor.D(); D = np.reshape(D, (len(D),1));
	bbeta_propP = np.sqrt(m_factor)*factor.solve_Lt(np.random.normal(size=(W.shape[1],num_v_samples))/np.sqrt(D))
	bbeta_prop = np.repeat(bbeta.toarray(), num_v_samples, axis=1)+ factor.apply_Pt(bbeta_propP)
	bbeta_prop = sp.csr_matrix(bbeta_prop, dtype=np.float64)
	##### end of line 11
	
	##### Calculate v's in parallel given the \theta's (b_prop)
	###### start of line 12

	if parallel_loop_flag:
		range_list = []
		for i in range(num_parallel_loop):
			range_list.append(np.arange(int(i*num_v_samples/num_parallel_loop), int((i+1)*num_v_samples/num_parallel_loop)))
		bbeta_prop_list = []
		for i in range(num_parallel_loop):
			bbeta_prop_list.append(bbeta_prop[:,range_list[i]])
		result = (Parallel(n_jobs=num_parallel_loop)(delayed(calc_vm)(W, Y, bbeta_prop_list[i], bbeta.toarray(), inv_curr_covar, hessian,log_c1, m_factor, nore) for i in range(num_parallel_loop)))
		v_m = ();
		for temp in result:
			v_m = v_m+(temp,)

		v_m = np.vstack(v_m)
		v_m = v_m.ravel()
		v_m = np.squeeze(np.asarray(v_m))
	else:
		result = calc_vm(W, Y, bbeta_prop, bbeta.toarray(), inv_curr_covar, hessian,log_c1, m_factor, nore)
		v_m = np.asarray(result); 
		v_m = np.reshape(v_m, (len(v_m),))
	
	############ End of line 12
	return (v_m, bbeta_prop)
示例#45
0
def get_potentials(conductivity_laplacian, io_index_pair):
    """
    Recovers voltages based on the conductivity Laplacian and the IO array

    :param conductivity_laplacian:
    :param io_index_pair:

    :return: array of potential in each node
    """
    # TODO: technically, Cholesky is not the best solver. Change is needed, but in approximation
    # it should be good enough

    if switch_to_splu:
        io_array = build_sink_source_current_array(io_index_pair, conductivity_laplacian.shape, splu=True)
        local_conductivity_laplacian = trim_matrix(conductivity_laplacian, io_index_pair[1])
        log.info('starting splu computation')
        solver = splu(csc_matrix(local_conductivity_laplacian))
        log.info('splu computation done')
        return solver.solve(io_array.toarray())
    else:
        io_array = build_sink_source_current_array(io_index_pair, conductivity_laplacian.shape)
        solver = cholesky(csc_matrix(conductivity_laplacian), line_loss)
        return solver(io_array)
def getModel(img, var, lmat, pmat, cmat, rmat, reg, niter=10, onlyRes=False):
    from scikits.sparse.cholmod import cholesky
    import numpy

    omat = pmat * lmat
    rhs = omat.T * (img / var)

    B = omat.T * cmat * omat

    res = 0.
    regs = [reg]
    lhs = B + regs[-1] * rmat

    F = cholesky(lhs)
    fit = F(rhs)
    for i in range(niter):
        res = fit.dot(rmat * fit)

        delta = reg * 1e3
        lhs2 = B + (reg + delta) * rmat

        T = (2. / delta) * (numpy.log(F.cholesky(lhs2).L().diagonal()).sum() -
                            numpy.log(F.L().diagonal()).sum())
        reg = (omat.shape[0] - T * reg) / res
        if abs(reg - regs[-1]) / reg < 0.005:
            break
        regs.append(reg)
        lhs = B + regs[-1] * rmat
        F = F.cholesky(lhs)
        fit = F(rhs)
    print reg, regs
    res = -0.5 * res * regs[-1] + -0.5 * ((omat * fit - img)**2 / var).sum()
    if onlyRes:
        return res, reg
    model = (omat * fit)
    return res, reg, fit, model
示例#47
0
#means = {'1':1,'3':3,'2':2}
#precisions={('1','2'):12,('2','3'):23,('1','1'):11,('3','3'):33,('2','2'):22}

means = {'1':1,'3':3,'2':2}
n = len(means)
precisions={('1','2'):1,('2','3'):1,('1','1'):2,('3','3'):2,('2','2'):2}

gmrf = GMRF(means, precisions)

sig_complete = inv(gmrf.precision.todense())

def fexact(idxs):
  return np.sum(sig_complete[np.ix_(idxs, idxs)])


factor = cholesky(gmrf.precision)
D = factor.D()
D12 = np.sqrt(D)

#X = gmrf.precision.todense()
#P=np.eye(n)
#P=P[factor.P()].T
#L=factor.L().todense()
#D=np.diag(factor.D())
#X2 = P.dot(L).dot(L.T).dot(P.T)
#I = P.dot(inv(L.T)).dot(inv(D)).dot(inv(L)).dot(P.T)

def fexact2(idxs):
  z = np.zeros(n)
  z[idxs] = 1.0
  z_perm = factor.solve_P(z)
示例#48
0
	C_inv = sp.bsr_matrix((Cp_inv_arr, indices, indptr), blocksize=(point_param, point_param))

	indptr = np.arange(num_cameras+1)
	indices = np.arange(num_cameras)
	B = sp.bsr_matrix((Bp_arr, indices, indptr), blocksize=(cam_param, cam_param))

	indices = E_dict['point_id']
	u, indptr = np.unique(E_dict['cam_id'], return_index=True)
	indptr = np.hstack([indptr, len(indices)])
	E = sp.bsr_matrix((E_arr, indices, indptr), blocksize=(cam_param, point_param))

	EC_inv = E*C_inv
	S = B + EC_inv*E.T
	k = v - EC_inv*w

	factor = cholesky(S)
	y = factor(k)[:,0]
	z = C_inv * (w - E.T*y)


	camera_new = np.empty_like(cameras)
	points_new = np.empty_like(points)
	camera_new.flat = cameras.flat - y
	points_new.flat = points.flat - z

	new_error_sum = 0
	for observation_id in range(num_observations):
		o = observations[observation_id]
		cam_id = o['cam_id']
		point_id = o['point_id']
		cam = camera_new[cam_id]
示例#49
0
for number in range(1, 21):
    print 'Domain {}:'.format(number)
    B = get_matrix(
        './results/matrices_Z/domains_64_0_12.25_mass_{}.txt'.format(number))
    A = get_matrix(
        './results/matrices_Z/domains_64_0_12.25_stiff_{}.txt'.format(number))
    # To get eigenvalue about 12.25 we need to:
    #    * 2883  : integer entries in A, B
    #    / 128^2 : mesh of size 93 x 128 instead of 93/128 x 1
    # We scale again to get integer entries after subtracting  12.25 + 3/256,
    positive = 128 * 128 * 256 * A - (49 * 64 + 3) * 2883 * B
    #           128^2 * A - (12.25 + 3/256) * 2883 * B
    #           everything * 256 to get integers

    # cholesky factorization
    factor = cholesky(positive)
    diag = factor.D()
    L = factor.L_D()[0]
    P = factor.P()

    print >>text, 'Number of entries: ', len(positive.data)
    # the following should be positive
    print >>text, "Smallest diagonal element in LDL': ", diag.min()
    print >>text, 'Ratio largest/smallest :  ', diag.max() / diag.min()
    print >>text, 'Number of entries in L : ', len(L.data)
    print >>text, 'Permutation : ', P
    # save permuted positive
    posP = positive[:, P].tocsr()[P].tocsc()
    f = open('results/CHOLMOD_permuted/permuted{}.txt'.format(number), 'w')
    M = posP.tocoo()
    print >>f, zip(M.row, M.col, M.data)
示例#50
0
def als_baseline_scikits_sparse(signal_input, smoothness_param=1e3, asym_param=1e-4, print_iteration=False):
    """
    als_baseline_scikits_sparse(signal_input [,smoothness_param , asym_param]

    Compute the baseline_current of signal_input using an asymmetric least squares
    algorithm designed by P. H. C. Eilers. This implementation uses
    CHOLMOD through the scikits.sparse toolkit wrapper.

    Parameters
    ----------
    signal_input : ndarray (1D)

    smoothness_param : float, optional (default, 1e3)
        Smoothness parameter

    asym_param : float, optional (default, 1e-4)
        Assymetry parameter

    Returns
    -------
    out : ndarray
        Baseline vector

    Notes
    -----
    This is the first attempt at converting MATLAB (Mathworks, Inc)
    scripts into Python code; thus, there will be bugs, the efficiency
    will be low(-ish), and I appreciate any useful suggestions or
    bug-finds.

    References
    ----------
    [1] P. H. C. Eilers, "A perfect smoother," Anal. Chem. 75,
        3631-3636 (2003).

    [2] P. H. C. Eilers and H. F. M. Boelens, "Baseline correction with
        asymmetric least squares smoothing," Report. October 21, 2005.

    [3] C. H. Camp Jr, Y. J. Lee, and M. T. Cicerone, "Quantitative,
        Comparable Coherent Anti-Stokes Raman Scattering (CARS)
        Spectroscopy: Correcting Errors in Phase Retrieval"

    """

    signal_shape_orig = signal_input.shape
    signal_length = signal_shape_orig[-1]

    dim = _np.ndim(signal_input)
    
    assert dim <= 3, "The input signal_input needs to be 1D, 2D, or 3D"
    if dim == 1:
        num_to_detrend = 1
    elif dim == 2:
        num_to_detrend = signal_input.shape[0]
    else:
        space_shp = list(signal_shape_orig)[0:-1]
        num_to_detrend = signal_input.shape[0]*signal_input.shape[1]

    baseline_output = _np.zeros(_np.shape(signal_input))

    difference_matrix = _sparse.coo_matrix(_np.diff(_np.eye(signal_length),\
    n=ORDER, axis=0))

    # Iterate over spatial dimension (always 2D)
    for count_spectra in range(num_to_detrend):
        if dim == 1:
            signal_current = signal_input
        elif dim == 2:
            signal_current = signal_input[count_spectra,:]
        else:
            # Calculate equivalent row- and column-count
            rc, cc = _row_col_from_lin(count_spectra, space_shp)
            signal_current = signal_input[rc, cc, :]

        if count_spectra == 0:
            penalty_vector = _np.ones(signal_length)
            baseline_current = _np.zeros([signal_length])
            baseline_last = _np.zeros([signal_length])
        else: # Start with the previous spectral baseline to seed
            penalty_vector = _np.squeeze(asym_param*(signal_current >=\
            baseline_current)+(1-asym_param)*\
            (signal_current < baseline_current))

        # Iterative asymmetric least squares smoothing
        for count_iterate in range(MAX_ITER):
            penalty_matrix = _sparse.diags(penalty_vector, 0, shape=(signal_length,\
            signal_length), format='csc')

            minimazation_matrix = penalty_matrix + (smoothness_param*difference_matrix.T)*difference_matrix

            # Cholesky factorization A = LL'
            factor = _scikits_cholmod.cholesky(minimazation_matrix, beta=0, mode="auto")

            if (count_iterate > 0 or count_spectra > 0):
                baseline_last = baseline_current

            # Solve A * baseline_current = penalty vector * Signal
            baseline_current = factor.solve_A(penalty_vector[:]*signal_current)

            # Difference check b/w iterations
            if count_iterate > 0 or count_spectra > 0:
                differ = _np.abs(_np.sum(baseline_current -\
                baseline_last, axis=0))
                if differ < MIN_DIFF:
                    break
            # Apply asymmetric penalization
            penalty_vector = _np.squeeze(asym_param*(signal_current >=\
            baseline_current)+(1-asym_param)*\
            (signal_current < baseline_current))

        if dim == 1:
            baseline_output = baseline_current
        elif dim == 2:
            baseline_output[count_spectra,:] = baseline_current
        else:
            baseline_output[rc, cc, :] = baseline_current

        if print_iteration:
            if dim < 3:
                print('Finished detrending spectra {}/{}'.format(
                      count_spectra + 1, num_to_detrend))
            elif rc + 1 == space_shp[0]:
                print('Finished detrending spectra {}/{}'.format(
                      count_spectra + 1, num_to_detrend))

    return baseline_output
示例#51
0
    def create_biharmonic_solver(self, boundary_verts, clip_D=0.1):
        r"""Set up biharmonic equation with Dirichlet boundary conditions on the cortical
        mesh and precompute Cholesky factorization for solving it. The vertices listed in
        `boundary_verts` are considered part of the boundary, and will not be included in
        the factorization.

        To facilitate Cholesky decomposition (which requires a symmetric matrix), the
        squared Laplace-Beltrami operator is separated into left-hand-side (L2) and
        right-hand-side (Dinv) parts. If we write the L-B operator as the product of
        the stiffness matrix (V-W) and the inverse mass matrix (Dinv), the biharmonic
        problem is as follows (with `u` denoting non-boundary vertices)

        .. math::
            :nowrap:
            
            \begin{eqnarray}
            L^2_{u} \phi &=& -\rho_{u} \\
            \left[ D^{-1} (V-W) D^{-1} (V-W) \right]_{u} \phi &=& -\rho_{u} \\
            \left[ (V-W) D^{-1} (V-W) \right]_{u} \phi &=& -\left[D \rho\right]_{u}
            \end{eqnarray}

        Parameters
        ----------
        boundary_verts : list or ndarray of length V
            Indices of vertices that will be part of the Dirichlet boundary.

        Returns
        -------
        lhs : sparse matrix
            Left side of biharmonic problem, (V-W) D^{-1} (V-W)
        rhs : sparse matrix, dia
            Right side of biharmonic problem, D
        Dinv : sparse matrix, dia
            Inverse mass matrix, D^{-1}
        lhsfac : cholesky Factor object
            Factorized left side, solves biharmonic problem
        notboundary : ndarray, int
            Indices of non-boundary vertices
        """
        try:
            from scikits.sparse.cholmod import cholesky
            factorize = lambda x: cholesky(x).solve_A
        except ImportError:
            factorize = sparse.linalg.dsolve.factorized
            
        B, D, W, V = self.laplace_operator
        npt = len(D)

        g = np.nonzero(D > 0)[0] # Find vertices with non-zero mass
        #g = np.nonzero((L.sum(0) != 0).A.ravel())[0] # Find vertices with non-zero mass
        notboundary = np.setdiff1d(np.arange(npt)[g], boundary_verts) # find non-boundary verts
        D = np.clip(D, clip_D, D.max())

        Dinv = sparse.dia_matrix((D**-1,[0]), (npt,npt)).tocsr() # construct Dinv
        L = Dinv.dot((V-W)) # construct Laplace-Beltrami operator
        
        lhs = (V-W).dot(L) # construct left side, almost squared L-B operator
        #lhsfac = cholesky(lhs[notboundary][:,notboundary]) # factorize
        lhsfac = factorize(lhs[notboundary][:,notboundary]) # factorize
        
        return lhs, D, Dinv, lhsfac, notboundary
示例#52
0
 def __init__(self, A):
     self.n = A.shape[0]
     self.factor = factor = cholmod.cholesky(A)
     self.L = factor.L()
     self.p = np.argsort(factor.P())
示例#53
0
def master_edge_current(conductivity_laplacian,
                        index_list,
                        cancellation=True,
                        potential_dominated=True,
                        sampling=False,
                        sampling_depth=10,
                        memory_source=None,
                        memoization=None):
    """
    master method for all the required edge current calculations

    :param conductivity_laplacian:
    :param index_list:
    :param cancellation:
    :param potential_dominated:
    :param sampling:
    :param sampling_depth:
    :param memory_source:
    :param memoization:
    :return:
    """
    # TODO: remove memoization in order to reduce overhead nad mongo database load

    # generate index list in agreement with the sampling strategy
    if sampling:
        list_of_pairs = []
        for _ in repeat(None, sampling_depth):
            idx_list_c = copy(index_list)
            random.shuffle(idx_list_c)
            list_of_pairs += zip(idx_list_c[:len(idx_list_c) / 2],
                                 idx_list_c[len(idx_list_c) / 2:])
    else:
        list_of_pairs = [(i, j) for i, j in combinations(set(index_list), 2)]

    total_pairs = len(list_of_pairs)

    up_pair_2_voltage_current = {}
    current_accumulator = lil_matrix(conductivity_laplacian.shape)
    # dump(csc_matrix(conductivity_laplacian), open('debug_for_cholesky.dmp', 'w'))
    # log.exception('debug dump occured here!')
    # raw_input('press enter to continue!')
    solver = cholesky(csc_matrix(conductivity_laplacian), fudge)

    # run the main loop on the list of indexes in agreement with the memoization strategy:
    for counter, (i, j) in enumerate(list_of_pairs):

        if counter % total_pairs / 100 == 0:
            log.debug('getting pairwise flow %s out of %s', counter + 1,
                      total_pairs)

        if memory_source and tuple(sorted((i, j))) in memory_source.keys():
            potential_diff, current_upper = memory_source[tuple(sorted(
                (i, j)))]

        else:
            potential_diff, current_upper = \
                edge_current_iteration(conductivity_laplacian, (i, j),
                                       solver=solver)

        if memoization:
            up_pair_2_voltage_current[tuple(sorted((i, j)))] = \
                (potential_diff, current_upper)

        # normalize to potential, if needed
        if potential_dominated:

            if potential_diff != 0:
                current_upper = current_upper / potential_diff

            # warn if potential difference is null or close to it
            else:
                log.warning(
                    'pairwise flow. On indexes %s %s potential difference is null. %s',
                    i, j, 'Tension-normalization was aborted')

        current_accumulator += sparse_abs(current_upper)

    if cancellation:
        current_accumulator /= total_pairs

    return current_accumulator, up_pair_2_voltage_current
def inv_mat(M):
    fac = cholesky(M)
    uni = sparse.coo_matrix(
        ((np.arange(M.shape[0]), np.arange(M.shape[0])), np.ones(M.shape[0])))
    return fac(uni)
示例#55
0
def _random_correlated_image(mean, sigma, image_shape, alpha=0.3, rng=None):
    """
    Creates a random image with correlated neighbors.
    pixel covariance is sigma^2, direct neighors pixel covariance is alpha * sigma^2.

    Parameters
    ----------
    mean : the mean value of the image pixel values.
    sigma : the std dev of image pixel values.
    image_shape : tuple, shape = (3, )
    alpha : the neighbors correlation factor.
    rng : random number generator (a numpy.random.RandomState instance).
    """
    dim_x, dim_y, dim_z = image_shape
    dim_image = dim_x * dim_y * dim_z

    correlated_image = 0
    for neighbor in [(1, 0, 0), (0, 1, 0), (0, 0, 1)]:
        corr_data = []
        corr_i = []
        corr_j = []
        for i, j, k in [(0, 0, 0), neighbor]:
            d2 = 1.0 * (i * i + j * j + k * k)
            ind = np.asarray(np.mgrid[0:dim_x - i, 0:dim_y - j, 0:dim_z - k],
                             dtype=np.int)
            ind = ind.reshape((3, (dim_x - i) * (dim_y - j) * (dim_z - k)))
            corr_i.extend(
                np.ravel_multi_index(ind, (dim_x, dim_y, dim_z)).tolist())
            corr_j.extend(
                np.ravel_multi_index(ind + np.asarray([i, j, k])[:, None],
                                     (dim_x, dim_y, dim_z)).tolist())
            if i > 0 or j > 0 or k > 0:
                corr_i.extend(
                    np.ravel_multi_index(ind + np.asarray([i, j, k])[:, None],
                                         (dim_x, dim_y, dim_z)).tolist())
                corr_j.extend(
                    np.ravel_multi_index(ind, (dim_x, dim_y, dim_z)).tolist())
            if i == 0 and j == 0 and k == 0:
                corr_data.extend([3.0] * ind.shape[1])
            else:
                corr_data.extend([alpha * 3.0] * 2 * ind.shape[1])

        correlation = scisp.csc_matrix((corr_data, (corr_i, corr_j)),
                                       shape=(dim_image, dim_image))

        factor = cholesky(correlation)
        L = factor.L()
        P = factor.P()[None, :]
        P = scisp.csc_matrix((np.ones(dim_image),
                              np.vstack(
                                  (P, np.asarray(range(dim_image))[None, :]))),
                             shape=(dim_image, dim_image))

        sq_correlation = P.dot(L)
        X = rng.normal(0, 1, dim_image)
        Y = sq_correlation.dot(X)
        Y = Y.reshape((dim_x, dim_y, dim_z))
        X = X.reshape((dim_x, dim_y, dim_z))
        correlated_image += Y
    correlated_image /= 3

    return correlated_image * sigma + mean
示例#56
0
#system.params.structural_quench_params.debug = True
#system.params.structural_quench_params.iprint = 100
db = system.create_database()
bh = system.get_basinhopping(db)
bh.run(1)

m = db.minima()[0]
coords = m.coords

potential = system.get_potential()
energy, gradient, hessian = potential.getEnergyGradientHessian(coords)

dummy_vec = ts.gramm_schmidt(ts.zeroEV_cluster(coords))

shifted_hess = hessian.copy()

for i in range(6):
    shifted_hess += np.outer(dummy_vec[i], dummy_vec[i])

shifted_eval, shifted_evec = get_sorted_eig(shifted_hess)

print "First log sum: ", np.sum(np.log(shifted_eval[6:]))

sparse_hess = scipy.sparse.csc_matrix(shifted_hess)
factor = cholmod.cholesky(sparse_hess)

diagonal = np.diagonal(factor.L().todense())

logar = 2 * np.log(diagonal)
log_sum = np.sum(logar)
print "Second log sum: ", log_sum
示例#57
0
A = np.eye(nmatrix)*10
A[3,1] = 4
A[1,3] = 4
#A[5,4] = 2
#A[4,5] = 2

print A

E, VV = np.linalg.eig(A)
print E
print VV
AA = csc_matrix(A)

Ls = []

factor = cholesky(AA)
L = factor.L()

for i in range(50):
    #print i
    factor = cholesky(AA)
    L = factor.L()
    AA = L.conjugate().transpose().dot(L)
    Ls.append(L.conjugate().transpose())

print np.diag(AA.todense())
LL = np.zeros_like(Ls[0].todense())
LL = Ls[0].todense()
for L in Ls:
    LL = np.dot(L.todense(),LL)
示例#58
0
def test_cholesky_matrix_market():
    for problem in ("well1033", "illc1033", "well1850", "illc1850"):
        X = mm_matrix(problem)
        y = mm_matrix(problem + "_rhs1")
        answer = np.linalg.lstsq(X.todense(), y)[0]
        XtX = (X.T * X).tocsc()
        Xty = X.T * y
        for mode in ("auto", "simplicial", "supernodal"):
            assert np.allclose(cholesky(XtX, mode=mode)(Xty), answer)
            assert np.allclose(cholesky_AAt(X.T, mode=mode)(Xty), answer)
            assert np.allclose(cholesky(XtX, mode=mode).solve_A(Xty), answer)
            assert np.allclose(
                cholesky_AAt(X.T, mode=mode).solve_A(Xty), answer)

            f1 = analyze(XtX, mode=mode)
            f2 = f1.cholesky(XtX)
            assert np.allclose(f2(Xty), answer)
            assert_raises(CholmodError, f1, Xty)
            assert_raises(CholmodError, f1.solve_A, Xty)
            assert_raises(CholmodError, f1.solve_LDLt, Xty)
            assert_raises(CholmodError, f1.solve_LD, Xty)
            assert_raises(CholmodError, f1.solve_DLt, Xty)
            assert_raises(CholmodError, f1.solve_L, Xty)
            assert_raises(CholmodError, f1.solve_D, Xty)
            assert_raises(CholmodError, f1.apply_P, Xty)
            assert_raises(CholmodError, f1.apply_Pt, Xty)
            f1.P()
            assert_raises(CholmodError, f1.L)
            assert_raises(CholmodError, f1.LD)
            assert_raises(CholmodError, f1.L_D)
            assert_raises(CholmodError, f1.L_D)
            f1.cholesky_inplace(XtX)
            assert np.allclose(f1(Xty), answer)

            f3 = analyze_AAt(X.T, mode=mode)
            f4 = f3.cholesky(XtX)
            assert np.allclose(f4(Xty), answer)
            assert_raises(CholmodError, f3, Xty)
            f3.cholesky_AAt_inplace(X.T)
            assert np.allclose(f3(Xty), answer)

            print problem, mode
            for f in (f1, f2, f3, f4):
                pXtX = XtX.todense()[f.P()[:, np.newaxis],
                                     f.P()[np.newaxis, :]]
                assert np.allclose(np.prod(f.D()),
                                   np.linalg.det(XtX.todense()))
                assert np.allclose((f.L() * f.L().T).todense(), pXtX)
                L, D = f.L_D()
                assert np.allclose((L * D * L.T).todense(), pXtX)

                b = np.arange(XtX.shape[0])[:, np.newaxis]
                assert np.allclose(f.solve_A(b), np.dot(XtX.todense().I, b))
                assert np.allclose(f(b), np.dot(XtX.todense().I, b))
                assert np.allclose(f.solve_LDLt(b),
                                   np.dot((L * D * L.T).todense().I, b))
                assert np.allclose(f.solve_LD(b), np.dot((L * D).todense().I,
                                                         b))
                assert np.allclose(f.solve_DLt(b),
                                   np.dot((D * L.T).todense().I, b))
                assert np.allclose(f.solve_L(b), np.dot(L.todense().I, b))
                assert np.allclose(f.solve_Lt(b), np.dot(L.T.todense().I, b))
                assert np.allclose(f.solve_D(b), np.dot(D.todense().I, b))

                assert np.allclose(f.apply_P(b), b[f.P(), :])
                assert np.allclose(f.solve_P(b), b[f.P(), :])
                # Pt is the inverse of P, and argsort inverts permutation
                # vectors:
                assert np.allclose(f.apply_Pt(b), b[np.argsort(f.P()), :])
                assert np.allclose(f.solve_Pt(b), b[np.argsort(f.P()), :])