Example #1
0
// [[Rcpp::export]]
void doubleBM(const S4& BM, XPtr<BigMatrix> xpMat2) {

  XPtr<BigMatrix> xpMat = BM.slot("address");
  int n = xpMat->nrow();
  int m = xpMat->ncol();
  RawSubMatAcc macc(*xpMat, seq_len(n)-1, seq_len(m)-1, BM.slot("code"));

  MatrixAccessor<unsigned char> macc2(*xpMat2);

  int i, j, j2;
  double tmp;

  for (j = j2 = 0; j < m; j++, j2 += 2) {
    for (i = 0; i < n; i++) {
      tmp = macc(i, j);
      if (tmp == 0) {
        macc2[j2][i] = macc2[j2+1][i] = 0;
      } else if (tmp == 1) {
        macc2[j2][i] = 0;
        macc2[j2+1][i] = 1;
      } else if (tmp == 2) {
        macc2[j2][i] = macc2[j2+1][i] = 1;
      } else {
        throw Rcpp::exception("Your big.matrix should have only Os, 1s or 2s");
      }
    }
  }
}
Example #2
0
// [[Rcpp::export]]
void fillMat(SEXP xptr_bed, Environment BM) {

  XPtr<FBM> xpBM = BM["address"];
  size_t n = xpBM->nrow();
  size_t m = xpBM->ncol();
  SubBMAcc<unsigned char> macc(xpBM, seq_len(n) - 1, seq_len(m) - 1);

  XPtr<bed> xpBed(xptr_bed);
  bedAcc bedacc(xpBed, seq_len(m));

  for (size_t j = 0; j < m; j++) {
    for (size_t i = 0; i < n; i++) {
      macc(i, j) = bedacc(i, j);
    }
  }
}
Example #3
0
File: ransac.c Project: jguinet/s2p
// RANSAC
//
// Given a list of data points, find the parameters of a model that fits to
// those points.  Several models are tried, and the model with the highest
// number of inliers is kept.
//
// A basic idea of this kind of ransac is that a maximum allowed error is fixed
// by hand, and then the inliers of a model are defined as the data points
// which fit the model up to the allowed error.  The RANSAC algorithm randomly
// tries several models and keeps the one with the largest number of inliers.
int ransac(
		// output
		//int *out_ninliers, // number of inliers
		bool *out_mask,    // array mask identifying the inliers
		float *out_model,  // model parameters

		// input data
		float *data,       // array of input data

		// input context
		int datadim,       // dimension of each data point
		int n,             // number of data points
		int modeldim,      // number of model parameters
		ransac_error_evaluation_function *mev,
		ransac_model_generating_function *mgen,
		int nfit,          // data points needed to produce a model

		// input parameters
		int ntrials,       // number of models to try
		int min_inliers,   // minimum allowed number of inliers
		float max_error,   // maximum allowed error

		// decoration
		ransac_model_accepting_function *macc,
		void *usr
		)
{
	fprintf(stderr, "running RANSAC over %d datapoints of dimension %d\n",
			n, datadim);
	fprintf(stderr, "will try to find a model of size %d from %d points\n",
		       	modeldim, nfit);
	fprintf(stderr, "we will make %d trials and keep the best with e<%g\n",
			ntrials, max_error);
	fprintf(stderr, "a model must have more than %d inliers\n",
			min_inliers);


	mt_init((unsigned long int) 0);  // fix seed for the Mersenne Twister PRNG
	int best_ninliers = 0;
	float best_model[modeldim];
	bool *best_mask = xmalloc(n * sizeof*best_mask);
	bool *tmp_mask = xmalloc(n * sizeof*best_mask);

	for (int i = 0; i < ntrials; i++)
	{
		int indices[nfit];
		fill_random_indices(indices, nfit, 0, n);

		float x[nfit*datadim];
		for (int j = 0; j < nfit; j++)
		for (int k = 0; k < datadim; k++)
			x[datadim*j + k] = data[datadim*indices[j] + k];

		float model[modeldim*MAX_MODELS];
		int nm = mgen(model, x, usr);
		if (!nm)
			continue;
		if (macc && !macc(model, usr))
			continue;

		// generally, nm=1
		for (int j = 0; j < nm; j++)
		{
			float *modelj = model + j*modeldim;
			int n_inliers = ransac_trial(tmp_mask, data, modelj,
					max_error, datadim, n, mev, usr);

			if (n_inliers > best_ninliers)
			{
				best_ninliers = n_inliers;
				for(int k = 0; k < modeldim; k++)
					best_model[k] = modelj[k];
				for(int k = 0; k < n; k++)
					best_mask[k] = tmp_mask[k];
			}
		}
	}

	fprintf(stderr, "RANSAC found this best model:");
	for (int i = 0; i < modeldim; i++)
		fprintf(stderr, " %g", best_model[i]);
	fprintf(stderr, "\n");
	if (0) {
		FILE *f = xfopen("/tmp/ramo.txt", "w");
		for (int i = 0; i < modeldim; i++)
			fprintf(f,"%lf%c",best_model[i],i==modeldim-1?'\n':' ');
		xfclose(f);
	}
	//fprintf(stderr, "errors of outliers:");
	//for (int i = 0; i < n; i++)
	//	if (!best_mask[i]) {
	//		float e = mev(best_model, data+i*datadim, usr);
	//		fprintf(stderr, " %g", e);
	//	}
	//fprintf(stderr, "\n");
	//fprintf(stderr, "errors of inliers:");
	//for (int i = 0; i < n; i++)
	//	if (best_mask[i]) {
	//		float e = mev(best_model, data+i*datadim, usr);
	//		fprintf(stderr, " %g", e);
	//	}
	//fprintf(stderr, "\n");
	//fprintf(stderr, "errors of data points:\n");
	//for (int i = 0; i < n; i++) {
	//	float e = mev(best_model, data+i*datadim, usr);
	//	fprintf(stderr, "\t%g\t%s\n", e, best_mask[i]?"GOOD":"bad");
	//}

	int return_value = 0;
	if (best_ninliers >= min_inliers)
	{
		return_value =  best_ninliers;
	} else
		return_value = 0;

	for (int j = 0; j < modeldim; j++)
		if (!isfinite(best_model[j]))
			fail("model_%d not finite", j);

	if (out_model)
		for(int j = 0; j < modeldim; j++)
			out_model[j] = best_model[j];
	if (out_mask)
		for(int j = 0; j < n; j++)
			out_mask[j] = best_mask[j];

	free(best_mask);
	free(tmp_mask);

	return return_value;
}