Exemple #1
0
void Annotator::update_color(MeshBundle<DefaultMesh>::Ptr m,const arma::uvec& label)
{
    assert(m->mesh_.n_vertices()==label.size());
    uint32_t* c = (uint32_t*)m->custom_color_.vertex_colors();
    #pragma omp parallel for
    for(int i=0;i<label.size();++i)
    {
        assert( l2c_.find(int(label(i))) != l2c_.end() );
        c[i] = l2c_[ int(label(i)) ];
    }
}
Exemple #2
0
	/**
         * Add a random local matrix and rhs spanning over given rows and columns.
         * 
         */
	void add(arma::uvec rows, arma::uvec cols) {
          
            arma::mat loc_mat=arma::randu<arma::mat>(rows.size(), cols.size());
            arma::vec loc_rhs=arma::randu<arma::vec>(rows.size());
            // apply to full system
            full_matrix_.submat(rows, cols)+=loc_mat;
            full_rhs_.elem(rows)+=loc_rhs;
            // apply to fixture system
            arma::vec row_sol=dirichlet_values_.elem(rows);
            arma::vec col_sol=dirichlet_values_.elem(cols);
            
            
            auto i_rows=arma::conv_to<std::vector<int> >::from(
                          arma::conv_to<arma::ivec>::from(rows)%dirichlet_.elem(rows));
            auto i_cols=arma::conv_to<std::vector<int> >::from(
                          arma::conv_to<arma::ivec>::from(cols)%dirichlet_.elem(cols));
            
            //cout << "i_rows\n" << arma::ivec(i_rows);
            //cout << "i_cols\n" << arma::ivec(i_cols);
            this->set_values(i_rows, i_cols, loc_mat, loc_rhs, row_sol, col_sol);            
            
            
            // check consistency
            double eps=4*arma::datum::eps;
            // zero dirichlet rows and cols
            //cout << "Dirich rows:\n" << dirichlet_rows_;
            //cout << "matrix_:\n" << matrix_;
            EXPECT_TRUE( arma::norm(matrix_.submat(dirichlet_rows_, non_dirichlet_rows_), "inf") < eps);
            
            EXPECT_TRUE( arma::norm(matrix_.submat(non_dirichlet_rows_, dirichlet_rows_), "inf") < eps);
            
            auto dirich_submat = matrix_.submat(dirichlet_rows_, dirichlet_rows_);
            EXPECT_TRUE( arma::norm( dirich_submat - arma::diagmat(dirich_submat), "inf") < eps );
            
            /*
            // full check
            arma::mat reduced_matrix_=full_matrix_;
            auto dirich_cols=reduced_matrix_.submat(arma::span::all, dirichlet_rows_);
            arma::vec reduced_rhs_=full_rhs_ - dirich_cols *dirichlet_values_;
            dirich_cols.zeros();
            
            reduced_matrix_.submat(dirichlet_rows_, arma::span::all).zeros();
            reduced_matrix_.submat(dirichlet_rows_, dirichlet_rows_) = dirich_submat;
            reduced_rhs_.subvec(dirichlet_rows_)=dirich_submat*dirichlet_values_;
            
            EXPECT_TRUE( arma::all( abs(matrix_ - reduced_matrix_)<eps ) );
            EXPECT_TRUE( arma::all( abs(rhs_ - reduced_rhs_)<eps ) );
            */
	}
Exemple #3
0
void SVMConfiguration::setSparseData(
    arma::uvec rowptr,
    arma::uvec colind,
    arma::vec values,
    size_t nrow,
    size_t ncol,
    bool one_indexed
) {
    // rowind and colptr are one-indexed -- we are sad
    if (one_indexed) {
        for (size_t i=0; i < rowptr.size(); ++i) {
            rowptr[i] -= 1;
        }
        for (size_t i=0; i < colind.size(); ++i) {
            colind[i] -= 1;
        }
    }

    // THIS IS TRICKY:
    // we are using fact that CSR format for A is CSC format for A^T
    this->sparse_data = arma::sp_mat(colind, rowptr, values, ncol, nrow);
}
void InPatchGraphCut::applyToFrame(const arma::uvec& gc_label,const arma::uvec& label_indices)
{
    MeshBundle<DefaultMesh>& mesh = *meshes_[current_frame_];
    arma::uvec& label = labels_[current_frame_];
    arma::uvec patch_label;
    current_patch_graph_->sv2pix(gc_label,patch_label);
    if(patch_label.size()!=label_indices.size())std::logic_error("gc_label.size()!=label_indices.size()");
    arma::uvec new_patch_indices = arma::find( patch_label==0 );
    arma::uvec new_patch_value = label(label_indices);
    new_patch_value(new_patch_indices).fill(0);
    label(label_indices) = new_patch_value;
    mesh.custom_color_.fromlabel(label);
}
/* Metropolis-Hastings updates of mu 
* Updates are implemented simulateaneously for all biological genes
*/
arma::mat muUpdateNoSpikes(
    arma::vec const& mu0, 
    arma::vec const& prop_var, 
    arma::mat const& Counts,  
    arma::vec const& invdelta, 
    arma::vec const& nu, 
    arma::vec const& sum_bycell_all, 
    double const& s2_mu,
    int const& q0,
    int const& n,
    arma::vec & mu1,
    arma::vec & u,
    arma::vec & ind,
    double const& Constrain,
    int const& RefGene,
    arma::uvec const& ConstrainGene,
    arma::uvec const& NotConstrainGene,
    int const& ConstrainType)
{
  using arma::span;
  
  int nConstrainGene = ConstrainGene.size();
  int nNotConstrainGene = NotConstrainGene.size();
  
  // PROPOSAL STEP    
  mu1 = exp(arma::randn(q0) % sqrt(prop_var) + log(mu0));
  u = arma::randu(q0);
  
  // INITIALIZE MU
  double aux; double iAux;
  double sumAux = sum(log(mu0.elem(ConstrainGene))) - log(mu0(RefGene));
  
  // ACCEPT/REJECT STEP
  
  // Step 1: Computing the likelihood contribution of the acceptance rate 
  // Calculated in the same way for all genes, 
  // but the reference one (no need to be sequential)
  arma::vec log_aux = (log(mu1) - log(mu0)) % sum_bycell_all;
  for (int i=0; i < q0; i++) {
    if(i != RefGene) {
      for (int j=0; j < n; j++) {
        log_aux(i) -= ( Counts(i,j) + invdelta(i) ) * 
          log( ( nu(j)*mu1(i) + invdelta(i) ) / 
          ( nu(j)*mu0(i) + invdelta(i) ));
      }
    }
  }
  
  // Step 2: Computing prior component of the acceptance rate 
  
  // Step 2.1: For genes that are under the constrain (excluding the reference one)
  for (int i=0; i < nConstrainGene; i++) {
    iAux = ConstrainGene(i);
    if(iAux != RefGene) {
      aux = 0.5 * (ConstrainGene.size() * Constrain - (sumAux - log(mu0(iAux))));
      log_aux(iAux) -= (0.5 * 2 /s2_mu) * (pow(log(mu1(iAux)) - aux,2)); 
      log_aux(iAux) += (0.5 * 2 /s2_mu) * (pow(log(mu0(iAux)) - aux,2));
      // ACCEPT REJECT
      if((log(u(iAux)) < log_aux(iAux)) & (mu1(iAux) > 1e-3)) {
        ind(iAux) = 1; sumAux += log(mu1(iAux)) - log(mu0(iAux)); 
      }
      else{ind(iAux) = 0; mu1(iAux) = mu0(iAux); }      
    }
  }
  
  // Step 2.2: For the reference gene 
  ind(RefGene) = 1;
  mu1(RefGene) = exp(ConstrainGene.size() * Constrain - sumAux);
  
  // Step 2.3: For genes that are *not* under the constrain
  // Only relevant for a trimmed constrain
  if(ConstrainType == 2) {
    for (int i=0; i < nNotConstrainGene; i++) {
      iAux = NotConstrainGene(i);
      log_aux(iAux) -= (0.5/s2_mu) * (pow(log(mu1(iAux)),2) - pow(log(mu0(iAux)),2));
      // ACCEPT REJECT
      if((log(u(iAux)) < log_aux(iAux)) & (mu1(iAux) > 1e-3)) { ind(iAux) = 1; }
      else{ind(iAux) = 0; mu1(iAux) = mu0(iAux);}
    }
  }
  // OUTPUT
  return join_rows(mu1, ind);
}