示例#1
0
void	edit_based_on_flags(t_mod *data)
{
	if (data->precision == 1 && data->procent == 0)
	{
		if (data->specifier != 's' && data->specifier != 'c')
		{
			stock_precision(data);
			compute_precision(data);
		}
		else
			edit_strings_precision(data);
	}
	if (data->hash_mod == 1)
		case_hash(data);
	if (data->width == 1)
		stock_width(data);
	if (data->zero_mod == 1)
		case_zero(data);
	if (data->plus_mod == 1)
		case_plus(data);
	if (data->dot_mod == 1)
		case_dot(data);
	compute_width(data);
	if (data->space_mod == 1 && data->plus_mod == 0)
		case_space(data);
	edit_wide_flags(data);
	edit_wildcard(data);
}
示例#2
0
int main(int argc, char* argv[])
{
	try {

		flann::Matrix<float> query;
		flann::Matrix<int> match;

		flann::load_from_file(query, "sift100K.h5","query");
		flann::load_from_file(match, "sift100K.h5","match");
		//	flann::load_from_file(gt_dists, "sift100K.h5","dists");

		flann::mpi::Client<float, float> index("localhost","9999");

		int nn = 1;
		flann::Matrix<int> indices(new int[query.rows*nn], query.rows, nn);
		flann::Matrix<float> dists(new float[query.rows*nn], query.rows, nn);

		start_timer("Performing search...\n");
		index.knnSearch(query, indices, dists, nn, flann::SearchParams(64));
		printf("Search done (%g seconds)\n", stop_timer());

		printf("Checking results\n");
		float precision = compute_precision(match, indices);
		printf("Precision is: %g\n", precision);

	}
	catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 0;
}
示例#3
0
DESIGN_mrf::DESIGN_mrf(datamatrix & dm,datamatrix & iv,
                       GENERAL_OPTIONS * o,DISTR * dp,FC_linear * fcl,
                       const MAP::map & m,vector<ST::string> & op,
                       vector<ST::string> & vn)
                      : DESIGN(o,dp,fcl)
  {

  if (errors==false)
    {

    read_options(op,vn);

    discrete = true;

    ma = m;
    type = Mrf;

    nrpar = ma.get_nrregions();
    consecutive=true;

    Zout = datamatrix(nrpar,1,1);
    index_Zout = statmatrix<int>(Zout.rows(),1);
    index_Zout.indexinit();
    }

  if (errors==false)
    init_data(dm,iv);

  if (errors==false)
    {
    compute_penalty();

    XWX = envmatdouble(0,nrpar);
    XWres = datamatrix(nrpar,1);
    Wsum = datamatrix(nrpar,1,1);

    compute_precision(1.0);

    compute_basisNull();

    identity=true;
    }

/*
  ofstream out("c:\\bayesx\\trunk\\testh\\results\\dm.raw");
  dm.prettyPrint(out);

  ofstream out2("c:\\bayesx\\trunk\\testh\\results\\iv.raw");
  iv.prettyPrint(out2);
*/

  }
int main(int argc, char** argv)
{
	boost::mpi::environment env(argc, argv);
	boost::mpi::communicator world;

    int nn = 1;

//    flann::Matrix<float> dataset;
    flann::Matrix<float> query;
    flann::Matrix<int> match;

    IF_RANK0 start_timer("Loading data...\n");
    flann::load_from_file(query, "sift100K.h5","query");
    flann::load_from_file(match, "sift100K.h5","match");
    flann::Matrix<int> indices(new int[query.rows*nn], query.rows, nn);
    flann::Matrix<float> dists(new float[query.rows*nn], query.rows, nn);

    // construct an randomized kd-tree index using 4 kd-trees
    flann::mpi::Index<flann::L2<float> > index("sift100K.h5", "dataset", flann::KDTreeIndexParams(4));
    IF_RANK0 printf("Loading data done (%g seconds)\n", stop_timer());

    IF_RANK0 printf("Index size: (%d,%d)\n", index.size(), index.veclen());

    start_timer("Building index...\n");
    index.buildIndex();                                                                                               
	printf("Building index done (%g seconds)\n", stop_timer());

    // do a knn search, using 128 checks
	IF_RANK0 start_timer("Performing search...\n");
    index.knnSearch(query, indices, dists, nn, flann::SearchParams(128));
    IF_RANK0 printf("Search done (%g seconds)\n", stop_timer());

    IF_RANK0 {
    	printf("Indices size: (%d,%d)\n", indices.rows, indices.cols);
    	printf("Checking results\n");
    	float precision = compute_precision(match, indices);
    	printf("Precision is: %g\n", precision);
    }
    delete[] query.data;
    delete[] indices.data;
    delete[] dists.data;
    delete[] match.data;
    
    return 0;
}
示例#5
0
文件: print.cpp 项目: frenchjl/stan
/**
 * The Stan print function.
 *
 * @param argc Number of arguments
 * @param argv Arguments
 * 
 * @return 0 for success, 
 *         non-zero otherwise
 */
int main(int argc, const char* argv[]) {
  
  if (argc == 1) {
    print_usage();
    return 0;
  }
  
  // Parse any arguments specifying filenames
  std::ifstream ifstream;
  std::vector<std::string> filenames;
  
  for (int i = 1; i < argc; i++) {
    
    if (std::string(argv[i]).find("--autocorr=") != std::string::npos)
      continue;
    
    if (std::string(argv[i]).find("--sig_figs=") != std::string::npos)
      continue;
    
    if (std::string("--help") == std::string(argv[i])) {
      print_usage();
      return 0;
    }
    
    ifstream.open(argv[i]);
    
    if (ifstream.good()) {
      filenames.push_back(argv[i]);
      ifstream.close();
    } else {
      std::cout << "File " << argv[i] << " not found" << std::endl;
    }
    
  }
  
  if (!filenames.size()) {
    std::cout << "No valid input files, exiting." << std::endl;
    return 0;
  }
  
  // Parse specified files
  Eigen::VectorXd warmup_times(filenames.size());
  Eigen::VectorXd sampling_times(filenames.size());
  
  Eigen::VectorXi thin(filenames.size());
  
  ifstream.open(filenames[0].c_str());
  
  stan::io::stan_csv stan_csv = stan::io::stan_csv_reader::parse(ifstream);
  warmup_times(0) = stan_csv.timing.warmup;
  sampling_times(0) = stan_csv.timing.sampling;
  
  stan::mcmc::chains<> chains(stan_csv);
  ifstream.close();
  
  thin(0) = stan_csv.metadata.thin;
  
  for (std::vector<std::string>::size_type chain = 1; 
       chain < filenames.size(); chain++) {
    ifstream.open(filenames[chain].c_str());
    stan_csv = stan::io::stan_csv_reader::parse(ifstream);
    chains.add(stan_csv);
    ifstream.close();
    thin(chain) = stan_csv.metadata.thin;
    
    warmup_times(chain) = stan_csv.timing.warmup;
    sampling_times(chain) = stan_csv.timing.sampling;
    
  }
  
  double total_warmup_time = warmup_times.sum();
  double total_sampling_time = sampling_times.sum();

  // Compute largest variable name length
  const int skip = 0;
  std::string model_name = stan_csv.metadata.model;
  size_t max_name_length = 0;
  for (int i = skip; i < chains.num_params(); i++) 
    if (chains.param_name(i).length() > max_name_length)
      max_name_length = chains.param_name(i).length();
  for (int i = 0; i < 2; i++) 
    if (chains.param_name(i).length() > max_name_length)
      max_name_length = chains.param_name(i).length();


  // Prepare values
  int n = 9;
  
  Eigen::MatrixXd values(chains.num_params(), n);
  values.setZero();
  Eigen::VectorXd probs(3);
  probs << 0.05, 0.5, 0.95;
  
  for (int i = 0; i < chains.num_params(); i++) {
    double sd = chains.sd(i);
    double n_eff = chains.effective_sample_size(i);
    values(i,0) = chains.mean(i);
    values(i,1) = sd / sqrt(n_eff);
    values(i,2) = sd;
    Eigen::VectorXd quantiles = chains.quantiles(i,probs);
    for (int j = 0; j < 3; j++)
      values(i,3+j) = quantiles(j);
    values(i,6) = n_eff;
    values(i,7) = n_eff / total_sampling_time;
    values(i,8) = chains.split_potential_scale_reduction(i);
  }
  
  // Prepare header
  Eigen::Matrix<std::string, Eigen::Dynamic, 1> headers(n);
  headers << 
    "Mean", "MCSE", "StdDev",
    "5%", "50%", "95%", 
    "N_Eff", "N_Eff/s", "R_hat";
  
  // Set sig figs
  Eigen::VectorXi column_sig_figs(n);
  
  int sig_figs = 2;
  
  for (int k = 1; k < argc; k++)
    if (std::string(argv[k]).find("--sig_figs=") != std::string::npos)
      sig_figs = atoi(std::string(argv[k]).substr(11).c_str());
  
  // Compute column widths
  Eigen::VectorXi column_widths(n);
  Eigen::Matrix<std::ios_base::fmtflags, Eigen::Dynamic, 1> formats(n);
  column_widths = calculate_column_widths(values, headers, sig_figs, formats);
  
  // Initial output
  std::cout << "Inference for Stan model: " << model_name << std::endl
            << chains.num_chains() << " chains: each with iter=(" << chains.num_kept_samples(0);
  for (int chain = 1; chain < chains.num_chains(); chain++)
    std::cout << "," << chains.num_kept_samples(chain);
  std::cout << ")";
  
  // Timing output
  std::cout << "; warmup=(" << chains.warmup(0);
  for (int chain = 1; chain < chains.num_chains(); chain++)
    std::cout << "," << chains.warmup(chain);
  std::cout << ")";
                          
  std::cout << "; thin=(" << thin(0);
  
  for (int chain = 1; chain < chains.num_chains(); chain++)
    std::cout << "," << thin(chain);
  std::cout << ")";
                          
  std::cout << "; " << chains.num_samples() << " iterations saved."
            << std::endl << std::endl;

  std::string warmup_unit = "seconds";
  
  if (total_warmup_time / 3600 > 1) {
    total_warmup_time /= 3600;
    warmup_unit = "hours";
  } else if (total_warmup_time / 60 > 1) {
    total_warmup_time /= 60;
    warmup_unit = "minutes";
  }
  
  std::cout << "Warmup took ("
            << std::fixed
            << std::setprecision(compute_precision(warmup_times(0), sig_figs, false))
            << warmup_times(0);
  for (int chain = 1; chain < chains.num_chains(); chain++)
    std::cout << ", " << std::fixed
              << std::setprecision(compute_precision(warmup_times(chain), sig_figs, false))
              << warmup_times(chain);
  std::cout << ") seconds, ";
  std::cout << std::fixed
            << std::setprecision(compute_precision(total_warmup_time, sig_figs, false))
            << total_warmup_time << " " << warmup_unit << " total" << std::endl;

  std::string sampling_unit = "seconds";
  
  if (total_sampling_time / 3600 > 1) {
    total_sampling_time /= 3600;
    sampling_unit = "hours";
  } else if (total_sampling_time / 60 > 1) {
    total_sampling_time /= 60;
    sampling_unit = "minutes";
  }

  std::cout << "Sampling took ("
            << std::fixed
            << std::setprecision(compute_precision(sampling_times(0), sig_figs, false))
            << sampling_times(0);
  for (int chain = 1; chain < chains.num_chains(); chain++)
    std::cout << ", " << std::fixed
              << std::setprecision(compute_precision(sampling_times(chain), sig_figs, false))
              << sampling_times(chain);
  std::cout << ") seconds, ";
  std::cout << std::fixed
            << std::setprecision(compute_precision(total_sampling_time, sig_figs, false))
            << total_sampling_time << " " << sampling_unit << " total" << std::endl;
  std::cout << std::endl;

  // Header output
  std::cout << std::setw(max_name_length + 1) << "";
  for (int i = 0; i < n; i++) {
    std::cout << std::setw(column_widths(i)) << headers(i);
  }
  std::cout << std::endl;
  
  // Value output
  for (int i = skip; i < chains.num_params(); i++) {
    if (!is_matrix(chains.param_name(i))) {
      std::cout << std::setw(max_name_length + 1) << std::left << chains.param_name(i);
      std::cout << std::right;
      for (int j = 0; j < n; j++) {
        std::cout.setf(formats(j), std::ios::floatfield);
        std::cout << std::setprecision(
                                       compute_precision(values(i,j), sig_figs, formats(j) == std::ios_base::scientific))
                  << std::setw(column_widths(j)) << values(i, j);
      }
      std::cout << std::endl;
    } else {
      std::vector<int> dims = dimensions(chains, i);
      std::vector<int> index(dims.size(), 1);
      int max = 1;
      for (int j = 0; j < dims.size(); j++)
        max *= dims[j];
      
      for (int k = 0; k < max; k++) {
        int param_index = i + matrix_index(index, dims);
        std::cout << std::setw(max_name_length + 1) << std::left 
                  << chains.param_name(param_index);
        std::cout << std::right;
        for (int j = 0; j < n; j++) {
          std::cout.setf(formats(j), std::ios::floatfield);
          std::cout 
            << std::setprecision(compute_precision(values(param_index,j), 
                                                   sig_figs, 
                                                   formats(j) == std::ios_base::scientific))
            << std::setw(column_widths(j)) << values(param_index, j);
        }
        std::cout << std::endl;
        next_index(index, dims);
      }
      i += max-1;
    }
  }

  /// Footer output
  std::cout << std::endl;
  std::cout << "Samples were drawn using " << stan_csv.metadata.algorithm
            << " with " << stan_csv.metadata.engine << "." << std::endl
            << "For each parameter, N_Eff is a crude measure of effective sample size," << std::endl
            << "and R_hat is the potential scale reduction factor on split chains (at " << std::endl
            << "convergence, R_hat=1)." << std::endl
            << std::endl;
  
  // Print autocorrelation, if desired
  for (int k = 1; k < argc; k++) {
    
    if (std::string(argv[k]).find("--autocorr=") != std::string::npos) {
      
      const int c = atoi(std::string(argv[k]).substr(11).c_str());
      
      if (c < 0 || c >= chains.num_chains()) {
        std::cout << "Bad chain index " << c << ", aborting autocorrelation display." << std::endl;
        break;
      }
      
      Eigen::MatrixXd autocorr(chains.num_params(), chains.num_samples(c));
      
      for (int i = 0; i < chains.num_params(); i++) {
        autocorr.row(i) = chains.autocorrelation(c, i);
      }
      
      // Format and print header
      std::cout << "Displaying the autocorrelations for chain " << c << ":" << std::endl;
      std::cout << std::endl;
      
      const int n_autocorr = autocorr.row(0).size();
      
      int lag_width = 1;
      int number = n_autocorr; 
      while ( number != 0) { number /= 10; lag_width++; }

      std::cout << std::setw(lag_width > 4 ? lag_width : 4) << "Lag";
      for (int i = 0; i < chains.num_params(); ++i) {
        std::cout << std::setw(max_name_length + 1) << std::right << chains.param_name(i);
      }
      std::cout << std::endl;

      // Print body  
      for (int n = 0; n < n_autocorr; ++n) {
        std::cout << std::setw(lag_width) << std::right << n;
        for (int i = 0; i < chains.num_params(); ++i) {
          std::cout << std::setw(max_name_length + 1) << std::right << autocorr(i, n);
        }
        std::cout << std::endl;
      }
  
    }
        
  }
      
  return 0;
        
}
示例#6
0
void print_histogram(FILE *fout, HISTOGRAM *h, int rez, int indent)
{
  int numb; /* The number of bins that have data. */
  int minb=0; /* The lowest index of a bin with data. */
  int maxb=0; /* The highest index of a bin with data. */
  register int cm, cs;
  int prec = 6; /* the field width used to print the bin size  */
  int proc = 6; /* the field width used to print the frequency */
  int single;
  register int ib;

#ifdef DEBUG
  printf("h->cnt = %d\n",h->cnt);
  printf("h->filled = %d\n",h->filled);
#endif

  if (h->cnt == 0)
    { return ;}

#ifdef DEBUG3
  {
    int is;
    for (is = 0; is < MIN((h->cnt),(h->nsample)); is++) {
      DataType *data;
      fprintf(fout," %d : %d ",is,h->thescore[is]);
      if(h->datasize) {
#ifdef USE_CUSTOM_INDEXING
	data = (h->indexdata)(h->sample_data,is);
#else
	assert(is >= 0 && is < h->nbucket);
	data = getData(h->sample_data,h->datasize,is);
#endif
	(h->printdata)(fout,data);
      }
      fprintf(fout,"\n");
    }
  }
#endif

  if (! h->filled)
    { fill(h);}

  numb = 0;
  for (ib = 0; ib < h->nbucket; ib++) {
    int icnt;
    icnt   =  h->bucket_cnt[ib];
    if(icnt > 0)
      { if (numb == 0) minb = ib;
        numb += 1;
        maxb = ib;
#ifdef DEBUG3
	fprintf(fout," %d : %d ",ib, icnt);
	if(h->datasize) {
	  DataType *data;
#if 0
	  data = (h->indexdata)(h->bucket_data,ib);
#else
	  assert(ib >= 0 && ib < h->nbucket);
	  data = getData(h->bucket_data,h->datasize,ib);
#endif
	  (h->printdata)(fout,data);
	}
	fprintf(fout,"\n");
#endif
      }
  }
#ifdef DEBUG
  h->bucket_width = (h->hgh-h->low-1)/(h->nbucket) + 1;
  printf("numb,minb,maxb = %d,%d,%d\n",numb,minb,maxb);
  printf("h->sync,h->low,h->bucket_width = %d,%d,%d\n",
	 h->sync,h->low,h->bucket_width);
#endif
  cm = compute_cm(h, rez, numb);
  cs = (h->sync - h->low)/h->bucket_width;
  //  printf("1* cm,cs = %d,%d\n",cm,cs);
  if (cs > 0) {
    cs = cs%cm - cm;
  } else {
    cs = - ( (-cs)%cm );
  }
  //  printf("2* cm,cs = %d,%d\n",cm,cs);
  cs += ((h->nbucket - cs)/cm+1)*cm;
  //  printf("3* cm,cs = %d,%d\n",cm,cs);

#ifdef DEBUG
  printf("cm,cs = %d,%d\n",cm,cs);
#endif

#ifdef NEVER
  prec = compute_precision(h,minb,maxb);
  proc = compute_proc(h,cs,cm);
#endif

  single = (cm*h->bucket_width == 1);
  single = FALSE;
  if (single)
    { if (prec >= indent) indent = prec+1; }
  else
    { if (2*prec + 3 >= indent) indent = 2*prec+4; }

  if(h->datasize) {
    assert(cm == 1);
  }
  for (ib = cs; ib+cm >= 1; ib -= cm) {
    int j;
    int sum_of_cnt = 0;
    int min_score, max_score;

#ifdef NEXT
    (h->cleardata)(h->temp_data);

    min_score = (h->low) + ib*(h->bucket_width);
    max_score = (h->low) + (ib+cm)*(h->bucket_width) - 1;
#endif

    min_score = h->bucket_min[ib];
    max_score = h->bucket_max[ib];
    min_score = MAX(min_score,h->min);
    max_score = MIN(max_score,h->max);
    for (j = 0; j < cm; j++) {
      if( (ib+j >= 0) && (ib+j < h->nbucket) )
	{
	  sum_of_cnt += h->bucket_cnt[ib+j];
	  min_score = MIN(min_score,h->bucket_min[ib+j]);
	  max_score = MAX(max_score,h->bucket_max[ib+j]);
	  if(h->datasize) {
	    DataType *data;
#ifdef USE_CUSTOM_INDEXING
	    data = (h->indexdata)(h->bucket_data,ib+j);
#else
	    assert((ib+j) >= 0 && (ib+j) < h->nbucket);
	    data = getData(h->bucket_data,h->datasize,ib+j);
#endif
	    //	    fprintf(stderr,"* j = %d ib + j = %d data = 0x%x min %d max %d\n", j,ib + j,
	    //    data, min_score, max_score);
	    if( j == 0 ) {
#ifdef USE_CUSTOM_INDEXING
	      (h->setdata)(h->temp_data,0,data);
#else
	      memcpy(h->temp_data, data, h->datasize);
	      //setData(h->temp_data, h->datasize, 0, data);
#endif
	    } else {
	      (h->aggregate)(h->temp_data,0,data);
	    }
	  }
	}
      if(h->datasize) {
	if( ib == cs ) {
#ifdef USE_CUSTOM_INDEXING
	  (h->setdata)(h->scan_data,0,h->temp_data);
#else
	  memcpy(h->scan_data, h->temp_data, h->datasize);
	  // setData(h->scan_data, h->datasize, 0, h->temp_data);
#endif
	} else {
	  (h->aggregate)(h->scan_data,0,h->temp_data);
	}
      }

    if (sum_of_cnt > 0)
      {
	//	fprintf(stderr,"* single %d min %d max %d sum_of_cnt = %d\n",
	//single, min_score, max_score, sum_of_cnt);
	if(single || (min_score == max_score)) {
	  fprintf(fout, "%*s%*d: %*d ",indent-prec,
		  "",prec,min_score,proc,sum_of_cnt);
	  if(h->datasize) {
	    (h->printdata)(fout,h->temp_data,h->scan_data,h->aggr_data);
	  }
	  fprintf(fout,"\n");
	} else {
	  fprintf(fout,"%*s%*d - %*d: %*d ",
		  indent-2*prec-3,"",
		  prec,min_score,prec,max_score,proc,sum_of_cnt);
	  if(h->datasize) {
	    (h->printdata)(fout,h->temp_data,h->scan_data,h->aggr_data);
	  }
	  fprintf(fout,"\n");
	}
      }
  }
  }
}
示例#7
0
double Real_timer::precision() const {
    // computes precision upon first call
    // returns -1.0 if timer system call fails.
    static double prec = compute_precision();
    return prec;
}