Exemplo n.º 1
0
void AlexSim::writeHist(const QString filename) const
{
    long ii;
    long nbins=(long)((t-tStart)/burstDuration*10);
    if(nbins==0||photons.size()==0) qWarning()<<"writeHist: no photon records ";
    gsl_histogram * histDonor = gsl_histogram_alloc (nbins);
    gsl_histogram_set_ranges_uniform (histDonor, tStart, t); // change t to tEnd if the latter is implemented
    gsl_histogram * histAcceptor = gsl_histogram_alloc (nbins);
    gsl_histogram_set_ranges_uniform (histAcceptor, tStart, t); // change t to tEnd if the latter is implemented
    for (ii=0;ii<photons.size();ii++) {
#ifdef PHOTONMACRO
        if(isChannel(photons.at(ii),DonorEm)) gsl_histogram_increment (histDonor, photons.at(ii).time);
#else
        if(photons.at(ii).isChannel(DonorEm)) gsl_histogram_increment (histDonor, photons.at(ii).time);
#endif
        else gsl_histogram_increment (histAcceptor, photons.at(ii).time);
    }

    QFile file(filename);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) QMessageBox::warning(0, "error", file.errorString());
    QTextStream out(&file);
    out <<"# Simulation data. photon count over time. parameters are:\n";
    out <<"# rateDonor="<<rateDemDex<<"\trateAcceptor="<<rateAemAex<<"\trateBackground="<<rateBackground<<"\tburstDuration="<<burstDuration<<"\tsigma="<<sigma<<"\ttStart="<<tStart<<"\ttEnd="<<tEnd()<<"\n";
    out.setRealNumberPrecision(11);
    out <<"#  time in ms \tdonor channel \tacceptor channel\n";

    for(ii=0;ii<histDonor->n;ii++) {
        out << histDonor->range[ii]*1e3 << "\t" << gsl_histogram_get(histDonor,ii) << "\t" << gsl_histogram_get(histAcceptor,ii) << "\n";
    }
    file.close();
    gsl_histogram_free (histDonor);
    gsl_histogram_free (histAcceptor);

    qDebug()<<nbins<<"histogram entries written to file "<<filename;
}
Exemplo n.º 2
0
Arquivo: MCMC.cpp Projeto: rfok/MCMC
               //calls the Point(int) constructor, creating Point object parameters, before calling MCMC's
MCMC::MCMC() : parameters(DIMENSION), proposal_mean(DIMENSION,0.0), proposal_var(DIMENSION,PROPOSAL_VAR) {   
    rng = gsl_rng_alloc(gsl_rng_ranlxs0); //assigning a RNG to an MCMC object
    
	simulationLength = 5000000;
    accepts = 0;
    rejects = 0;
    
    bin_num = 50;
    range_min = 0;
    range_max = 5.0;
    thin_factor = 25;

    h = gsl_histogram_alloc(bin_num);
	gsl_histogram_set_ranges_uniform(h, range_min, range_max);

    bin_num_X = 50;
    bin_num_Y = 50;
    X_min = 0;
    X_max = 2;
    Y_min = 0;
    Y_max = 2;
    
    h2d	= gsl_histogram2d_alloc (bin_num_X, bin_num_Y);
    gsl_histogram2d_set_ranges_uniform (h2d, X_min, X_max, Y_min, Y_max);

}
Exemplo n.º 3
0
//Overloaded Constructors
population::population(int N_in, int L_in, double r_in, int seed_in, gsl_rng* rng_in) { //Neutral Evolution!
    try {
        if (r_in < 0 || r_in > 1 || N_in < 0 || L_in <0) {throw "Bad inputs. Check arguments.";}
        else {
            N = N_in;
            L = L_in;
            r = r_in;
            gen_pop = 0;
            neutral = true;
            avgFit = 0;
            selective_weight = vector<double>(N); //All zeros
            genetic_weight = vector< vector<int> >(N,vector<int>(L));
            fixations = vector< vector<fixation_event> > (N);
            seed = seed_in ? seed_in:get_random_seed();
            rng = rng_in;
            gsl_rng_set(rng,seed);
            blockHist = gsl_histogram_alloc(L);
            gsl_histogram_set_ranges_uniform(blockHist,1,L+1);
            for(int i=0;i<N;i++) {
                pop.push_back(genome(i,L,rng));
            }
            updateBlockSizes();
        }
    }
    catch(const char* Message) {cout << "Error: " << Message << "\n";}
}
Exemplo n.º 4
0
void AlexSim::simTestLognormal()
{
    int n=10000; // number of samples
    int nbins=100; // number of bins for the histogram

    QFile file("./AlexSimTestLognormal.txt");

    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) QMessageBox::information(0, "error", file.errorString());
    QTextStream out(&file);
    out <<"# Test lognormal distribution. Parameters are: burstDuration="<<burstDuration<<"\tvariance="<<burstDurationVar<<"\n";
    out <<"# burst duration in ms\tfrequency\n";
    out.setRealNumberPrecision(11);

    gsl_histogram * hist = gsl_histogram_alloc (nbins);
    gsl_histogram_set_ranges_uniform (hist, 0, 10*burstDuration);
    for (int ii=0;ii<n;ii++) {
        gsl_histogram_increment (hist,gsl_ran_lognormal(r,mu,sigma));
    }

    for(unsigned int ii=0;ii<hist->n;ii++) {
        out << hist->range[ii]*1e3 << "\t" << gsl_histogram_get(hist,ii) << "\n";
    }
    file.close();
    gsl_histogram_free (hist);
}
Exemplo n.º 5
0
bool myHistogram::Convert(vector<double> &x)
{
	gsl_histogram *r;
	size_t n=x.size();
	if(n<=2) return false;
	double *res;
	res=new double[n];
	size_t i;
	for(i=0;i<n;i++) res[i]=x[i];
	double std=gsl_stats_sd(res,1,n);
	double bin=3.49*std/pow(n*1.0,1.0/3);//Scott's ruler
	if(bin<=0) 
	{
		delete []res;
		return false;
	}
	double a=gsl_stats_min(res,1,n);
	double b=gsl_stats_max(res,1,n);
	int num=(int)((b-a)/bin);
	r=gsl_histogram_alloc(num);
	gsl_histogram_set_ranges_uniform(r,a,b);
	for(i=0;i<n;i++)
	{
		gsl_histogram_increment(r,res[i]);
	}	
	Convert(r,n);	
	gsl_histogram_free(r);
	delete []res;	
	return true;
}
Exemplo n.º 6
0
population::population(int N_in, int L_in, double r_in, int seed_in, gsl_rng* rng_in, vector< vector<double> >& fit) {
    try {
        if (fit.size() != N_in || fit[0].size() != L_in) {throw "Incorrect Fitness Landscape dimensions!";}
        else if (r_in < 0 || r_in > 1 || N_in < 0 || L_in <0) {throw "Bad inputs. Check arguments.";}
        else {
            N = N_in;
            L = L_in;
            r = r_in;
            gen_pop = 0;
            neutral = false;
            avgFit = 0;
            selective_weight = vector<double>(N);
            genetic_weight = vector< vector<int> >(N,vector<int>(L));
            fixations = vector< vector<fixation_event> > (N);
            seed = seed_in ? seed_in : get_random_seed();
            rng = rng_in;
            gsl_rng_set(rng,seed);
            blockHist = gsl_histogram_alloc(L);
            gsl_histogram_set_ranges_uniform(blockHist,1,L+1); //Questionable range choice. As it stands now, the bins are partitioned as follows {[1,2),[2,3)......[L,L+1)}. Thus if all have length L, then average will come out to be (L+.5). Will have to rescale.
            map<int,double> genome_fit;
            for(int i=0;i<N;i++) {
                genome_fit = convert_vector_toMap(fit[i]);
                if (!genome_fit.empty()) {pop.push_back(genome(i, L, rng, genome_fit));}
                else {pop.push_back(genome(i,L,rng));}
                avgFit += pop[i].get_fit();
            }
            avgFit/=N;
            update_selection_weights();
            updateBlockSizes();
        }
    }
    catch (const char* Message) {
        cout << "Error:" << Message << "\n";
    }
}
Exemplo n.º 7
0
gsl_histogram * calc_hist(const gsl_vector * v, int nbins) {
	double max;
	double min;
	unsigned int i;
	double binwidth;
	double sum = 0;
	double val;
	gsl_histogram * h;

	gsl_vector_minmax(v, &min, &max);
	binwidth = (max - min) / nbins;
	dump_d("min", min);
	dump_d("max", max);

	debug("allocating the histogram");
	h = gsl_histogram_alloc(v->size);
	debug("setting range");
	require(gsl_histogram_set_ranges_uniform (h, min, max));

	/* with out the following, the max element doesn't fall in the last bin */
	h->range[h->n] += 1;

	debug("summing up");
	for (i = 0; i < v->size; i++) {
		val = gsl_vector_get(v, i);
		sum += val;
		require(gsl_histogram_increment (h, val));
	}
	debug("scaling");
	/* double gsl_histogram_sum (const gsl_histogram * h) */
	require(gsl_histogram_scale (h, 1/sum));
	debug("done");
	return h;
}
Exemplo n.º 8
0
int main(void) {
	struct data ntuple_row;

	gsl_ntuple *ntuple
	= gsl_ntuple_open ("test.dat", &ntuple_row,
	sizeof (ntuple_row));
	double lower = 1.5;

	gsl_ntuple_select_fn S;
	gsl_ntuple_value_fn V;

	gsl_histogram *h = gsl_histogram_alloc (100);
	gsl_histogram_set_ranges_uniform(h, 0.0, 10.0);

	S.function = &sel_func;
	S.params = &lower;

	V.function = &val_func;
	V.params = 0;

	gsl_ntuple_project (h, ntuple, &V, &S);

	gsl_histogram_fprintf (stdout, h, "%f", "%f");
	gsl_histogram_free (h);
	gsl_ntuple_close (ntuple);

	return 0;
}
Exemplo n.º 9
0
void QwtHistogram::loadDataFromMatrix() {
  if (!d_matrix)
    return;

  int size = d_matrix->numRows() * d_matrix->numCols();
  const double *data = d_matrix->matrixModel()->dataVector();

  int n;
  gsl_histogram *h;
  if (d_autoBin) {
    double min, max;
    d_matrix->range(&min, &max);
    d_begin = floor(min);
    d_end = ceil(max);
    d_bin_size = 1.0;

    n = static_cast<int>(floor((d_end - d_begin) / d_bin_size));
    if (!n)
      return;

    h = gsl_histogram_alloc(n);
    if (!h)
      return;
    gsl_histogram_set_ranges_uniform(h, floor(min), ceil(max));
  } else {
    n = static_cast<int>((d_end - d_begin) / d_bin_size + 1);
    if (!n)
      return;

    h = gsl_histogram_alloc(n);
    if (!h)
      return;

    double *range = new double[n + 2];
    for (int i = 0; i <= n + 1; i++)
      range[i] = d_begin + i * d_bin_size;

    gsl_histogram_set_ranges(h, range, n + 1);
    delete[] range;
  }

  for (int i = 0; i < size; i++)
    gsl_histogram_increment(h, data[i]);

  QVarLengthArray<double> X(n), Y(n); // stores ranges (x) and bins (y)
  for (int i = 0; i < n; i++) {
    Y[i] = gsl_histogram_get(h, i);
    double lower, upper;
    gsl_histogram_get_range(h, i, &lower, &upper);
    X[i] = lower;
  }
  setData(X.data(), Y.data(), n);

  d_mean = gsl_histogram_mean(h);
  d_standard_deviation = gsl_histogram_sigma(h);
  d_min = gsl_histogram_min_val(h);
  d_max = gsl_histogram_max_val(h);

  gsl_histogram_free(h);
}
Exemplo n.º 10
0
/* Histogram a PETSC vector 
 *
 * x is the vector
 * nbins -- number of bins
 * xmin, xmax -- histogram xmin, xmax -- assume uniform bins
 * hh -- output vector -- assumed to be defined.
 */
void VecHist(const Vec& x, int nbins, double xmin, double xmax, vector<double>& hh) {
  gsl_histogram *h1; 
  double *_x, x1;
  PetscInt lo, hi;
  vector<double> tmp(nbins);

  // Set up the histogram struct
  h1 = gsl_histogram_alloc(nbins);
  gsl_histogram_set_ranges_uniform(h1, xmin, xmax);

  // Get the array
  VecGetOwnershipRange(x, &lo, &hi);
  hi -= lo;
  VecGetArray(x, &_x);
  for (PetscInt ii=0; ii < hi; ++ii) {
    x1 = _x[ii];
    if (x1 < xmin) x1 = xmin;
    if (x1 >= xmax) x1 = xmax - 1.e-10;
    gsl_histogram_increment(h1, x1);
  }
  VecRestoreArray(x, &_x);
  
  // Fill the temporary output vector
  for (int ii =0; ii<nbins; ++ii) 
    tmp[ii] = gsl_histogram_get(h1, ii);

  // MPI Allreduce
  MPI_Allreduce(&tmp[0], &hh[0], nbins, MPI_DOUBLE, MPI_SUM, PETSC_COMM_WORLD); 

  // Clean up
  gsl_histogram_free(h1);
}
Exemplo n.º 11
0
/* ==== */
static gsl_histogram *
ini_histogram_uniform(const int n,
		      const double min,
		      const double max)
{
  gsl_histogram *a = gsl_histogram_alloc(n);
  gsl_histogram_set_ranges_uniform(a,min,max);
  return a;
}
Exemplo n.º 12
0
CAMLprim value ml_gsl_histogram_set_ranges_uniform(value vh, 
						   value xmin, value xmax)
{
  gsl_histogram h;
  histo_of_val(&h, vh);
  gsl_histogram_set_ranges_uniform(&h, Double_val(xmin), 
				   Double_val(xmax));
  return Val_unit;
}
Exemplo n.º 13
0
gsl_histogram *AlexData::burstDurationDistribution(size_t nbins)
{
    gsl_histogram *hist =  gsl_histogram_alloc(nbins);
    gsl_histogram_set_ranges_uniform(hist,0,burstSearchParamsGlobal.MaxDuration*1e3);

    for(int i=0;i<burstVectorDual.size();++i)
        gsl_histogram_increment(hist, burstVectorDual.at(i).duration*1e3);

    return hist;
}
Exemplo n.º 14
0
gsl_histogram * create_hist(int nbins, double min, double max) {
	gsl_histogram * h;

	h = gsl_histogram_alloc(nbins);
	gsl_histogram_set_ranges_uniform(h, min, max);

	/* with out the following, the max element doesn't fall in the last bin */
	h->range[h->n] += (max - min) / 10000;
	return h;
}
Exemplo n.º 15
0
gsl_histogram *AlexData::burstRateDistribution(size_t nbins)
{
    gsl_histogram *hist =  gsl_histogram_alloc(nbins);
    gsl_histogram_set_ranges_uniform(hist,burstSearchParamsGlobal.MinBurstrate*1e-3,burstSearchParamsGlobal.MaxBurstrate*1e-3);

    for(int i=0;i<burstVectorDual.size();++i)
        gsl_histogram_increment(hist, burstVectorDual.at(i).burstrate()*1e-3);

    return hist;
}
Exemplo n.º 16
0
void QwtHistogram::initData(double *Y, int size)
{
	if(size<2 || (size == 2 && Y[0] == Y[1]))
	{//non valid histogram data
		double x[2], y[2];
		for (int i = 0; i<2; i++ ){
			y[i] = 0;
			x[i] = 0;
		}
		setData(x, y, 2);
		return;
	}

	int n = 10;//default value
	QVarLengthArray<double> x(n), y(n);//double x[n], y[n]; //store ranges (x) and bins (y)
	gsl_histogram * h = gsl_histogram_alloc (n);
	if (!h)
		return;

	gsl_vector *v;
	v = gsl_vector_alloc (size);
	for (int i = 0; i<size; i++ )
		gsl_vector_set (v, i, Y[i]);

	double min, max;
	gsl_vector_minmax (v, &min, &max);
	gsl_vector_free (v);

	d_begin = floor(min);
	d_end = ceil(max);

	gsl_histogram_set_ranges_uniform (h, floor(min), ceil(max));

	for (int i = 0; i<size; i++ )
		gsl_histogram_increment (h, Y[i]);

	for (int i = 0; i<n; i++ ){
		y[i] = gsl_histogram_get (h, i);
		double lower, upper;
		gsl_histogram_get_range (h, i, &lower, &upper);
		x[i] = lower;
	}

	setData(x.data(), y.data(), n);//setData(x, y, n);

	d_bin_size = (d_end - d_begin)/(double)n;
	d_autoBin = true;
    d_mean = gsl_histogram_mean(h);
	d_standard_deviation = gsl_histogram_sigma(h);
	d_min = gsl_histogram_min_val(h);
	d_max = gsl_histogram_max_val(h);

	gsl_histogram_free (h);
}
Exemplo n.º 17
0
int
main (int argc, char **argv)
{
  double a = 0.0, b = 1.0;
  size_t n = 10;

  if (argc != 3 && argc !=4)
    {
      printf ("Usage: gsl-histogram xmin xmax [n]\n");
      printf (
"Computes a histogram of the data on stdin using n bins from xmin to xmax.\n"
"If n is unspecified then bins of integer width are used.\n");
      exit (0);
    }

  a = atof (argv[1]);
  b = atof (argv[2]);

  if (argc == 4) 
    {
      n = atoi (argv[3]);
    }
  else
    {
      n = (int)(b - a) ;
    }

  {
    double x;
    gsl_histogram *h = gsl_histogram_alloc (n);

    gsl_histogram_set_ranges_uniform (h, a, b);

    while (fscanf(stdin, "%lg", &x) == 1)
      {
        gsl_histogram_increment(h, x);
      }

#ifdef DISPLAY_STATS
    {
      double mean = gsl_histogram_mean (h);
      double sigma = gsl_histogram_sigma (h);
      fprintf (stdout, "# mean = %g\n", mean);
      fprintf (stdout, "# sigma = %g\n", sigma);
    }
#endif

    gsl_histogram_fprintf (stdout, h, "%g", "%g");

    gsl_histogram_free (h);
  }

  return 0;
}
Exemplo n.º 18
0
gsl_histogram *AlexData::burstSizeDistribution(size_t nbins, double maxBurstSize)
{
    if(maxBurstSize==0) maxBurstSize = burstSearchParamsGlobal.MaxBurstrate*burstSearchParamsGlobal.MaxDuration;
    gsl_histogram *hist =  gsl_histogram_alloc(nbins);
    gsl_histogram_set_ranges_uniform(hist,burstSearchParamsGlobal.MinBurstSize,maxBurstSize);

    for(int i=0;i<burstVectorDual.size();++i)
        gsl_histogram_increment(hist, burstVectorDual.at(i).numberOfPhotons);

    return hist;
}
Exemplo n.º 19
0
gsl_histogram* population::get_cumulant_fit_hist(int l, double sigma) {
    //Initialize histogram.
    gsl_histogram* fit_hist = gsl_histogram_alloc(1000); //Think more about bin size.
    gsl_histogram_set_ranges_uniform(fit_hist, -10*l*sigma, 10*l*sigma);
    for (int n=0; n<N; n++) { //Iterate over the entire population.
        for (int i=0; i<L-l; i+l/2) { //Center Locus @ i+l/2
            gsl_histogram_increment(fit_hist,pop[n].sub_cumulant_fit(i,l)); //Histogram delta_f values. 
        }   
    }
    return fit_hist;
}
Exemplo n.º 20
0
void DistToPA(gsl_histogram * h, double mag, double sigma)
{

double m1 = 0.5*mag + 0.5;
double s1 = 0.25*sigma;
double n1 = m1*(1. - m1)/s1 - 1.; 
double a1 = m1*n1;
double b1 = (1. - m1)*n1;

    for (int pa = 1; pa < 8; pa++) {
        char line[80];
        double x;
        double data[6000];
        int size = 0;
        int nbins = h->n;
        gsl_histogram * hpa = gsl_histogram_alloc( nbins);
        gsl_histogram_set_ranges_uniform( hpa, 0.0, 1.0);

        char fname[50];
        sprintf(fname,"/home/jonatas/mzanlz/mZ_pa%d.dat",pa);
        FILE * fp;
        fp = fopen(fname,"rt");
        while(fgets(line,80,fp) != NULL){
            x = atof(line); 
            size++;
            data[size] = x;
            gsl_histogram_increment( hpa, x);
        }
        double m2 = 0.5*gsl_stats_mean( data, 1, size ) + 0.5;
        double s2 = 0.25*gsl_stats_variance( data, 1, size );
        double n2 = m2*(1. - m2)/s2 - 1.; 
        double a2 = m2*n2;
        double b2 = (1. - m2)*n2;


        NormalizaGSLHistograma( hpa );
        //char hname[100];
        //sprintf(hname,"pa%d",pa);
        //PrintGSLHistogram( hpa, hname );        

        gsl_histogram_sub( hpa, h);
        double D = 0;
        for (size_t i = 0; i < nbins; i++) {
            D += gsl_histogram_get( hpa , i )*gsl_histogram_get( hpa , i );
        }
        // printf("%g %g ", sqrt(D), KLbetas(a1,b1,a2,b2));

        fclose(fp);
        gsl_histogram_free( hpa );
    }

}
Exemplo n.º 21
0
Arquivo: i2c.c Projeto: useche/btstats
static gsl_histogram *new_hist()
{
	gsl_histogram *h;

	/* allocate bins and set ranges uniformally */
	h = gsl_histogram_alloc(N_BINS);
	gsl_histogram_set_ranges_uniform(h,0,N_BINS*BINS_SEP);

	/* last bin should hold everything greater
	 * than (N_BINS-1)*BINS_SEP */
	h->range[h->n] = DBL_MAX;

	return h;
}
Exemplo n.º 22
0
size_t histogram (size_t N, double *data, size_t nbins, double bin_min, double bin_max, gsl_histogram **hist) {
  size_t i;

  /* prepare the gsl_histogram structure */
  *hist = gsl_histogram_alloc (nbins);
  gsl_histogram_set_ranges_uniform (*hist, bin_min, bin_max);

  /* populate the bins in the histogram */
  for (i=0; i<N; i++)
    gsl_histogram_increment (*hist, data [i]);

  /* success */
  return MYLIB_SUCCESS;
}
Exemplo n.º 23
0
bool myHistogram::Convert(double *res,int n)
{
	gsl_histogram *r;
	if(n<=2) return false;
	double std=gsl_stats_sd(res,1,n);
	double bin=3.49*std/pow(n*1.0,1.0/3);//Scott's ruler
	if(bin<=0) return false;	
	double a=gsl_stats_min(res,1,n);
	double b=gsl_stats_max(res,1,n);
	int num=(int)((b-a)/bin);
	r=gsl_histogram_alloc(num);
	gsl_histogram_set_ranges_uniform(r,a,b);
	for(int i=0;i<n;i++)
	{
		gsl_histogram_increment(r,res[i]);
	}	
	Convert(r,n);	
	gsl_histogram_free(r);	
	return true;
}
Exemplo n.º 24
0
bool HistList::AddHist(double *res,int n,double m,double sd,CString raw)
{
	gsl_histogram *r;
	if(n<=0) return false;
	double std=gsl_stats_sd(res,1,n);
	double bin=3.49*std/pow(n*1.0,1.0/3);//Scott's ruler
	if(bin<=0) return false;	
	double a=gsl_stats_min(res,1,n);
	double b=gsl_stats_max(res,1,n);
	int num=(int)((b-a)/bin);
	r=gsl_histogram_alloc(num);
	gsl_histogram_set_ranges_uniform(r,a,b);
	for(int i=0;i<n;i++)
	{
		gsl_histogram_increment(r,res[i]);
	}
	bool bResult=AddHist(r,m,sd,raw);
	gsl_histogram_free(r);
	return bResult;
}
Exemplo n.º 25
0
/** @short Markov Chain Monte Carlo 
		@param rng the GSL random number generator 
		@param p the vector of parameters being searched over 
		@param x the vector of observations */
void mcmc(const gsl_rng *rng, gsl_vector *p, const gsl_vector *x)
{
	gsl_vector * p_test = gsl_vector_alloc(p->size);
	size_t i;
	
	gsl_histogram * hist = gsl_histogram_alloc(nbins);
	gsl_histogram_set_ranges_uniform(hist, min, max);

	for(i=0; i < chain_length; i++)
	{
		gsl_vector_memcpy(p_test,p);
		propose(rng, p_test);
		double lnLikelihoodRatio = ln_lik_fn(p_test, x) - ln_lik_fn(p, x);
		if (take_step(rng, lnLikelihoodRatio))
			p = p_test;
		gsl_histogram_increment(hist, p->data[1]);
	}
	gsl_vector_free(p_test);
	gsl_histogram_fprintf(stdout, hist, "%g", "%g");
	gsl_histogram_free(hist);
}
Exemplo n.º 26
0
QVector<QPointF> DistanceToAtom::histogram(int bins) {
    QVector<QPointF> histogramVector;

    if(!m_isValid) {
        qFatal("DistanceToAtom is not valid. Run compute() first.");
        exit(1);
    }
    float minValue = 1e90;
    float maxValue = 0;

    for(const float &val : m_values) {
        if(val >= 0) {
            minValue = std::min(minValue, (float)sqrt(val));
            maxValue = std::max(maxValue, (float)sqrt(val));
        }
    }
    gsl_histogram *hist = gsl_histogram_alloc (bins);
    gsl_histogram_set_ranges_uniform (hist, minValue, maxValue);
    for(const float &value : m_values) {
        if(value >= 0) {
            gsl_histogram_increment (hist, sqrt(value));
        }
    }

    histogramVector.resize(bins);
    for(int i=0; i<bins; i++) {
        double upper, lower;
        gsl_histogram_get_range(hist, i, &lower, &upper);
        float middle = 0.5*(upper+lower);
        histogramVector[i].setX(middle);
        histogramVector[i].setY(gsl_histogram_get(hist,i));
    }

    gsl_histogram_free (hist);

    return histogramVector;
}
Exemplo n.º 27
0
void compute_global_autocorrelations(struct State *S)
/* Compute population rate autocorrelation */
{
        struct Network *ntw = &S->ntw;
        int n_neurons_sample = 100;
        const size_t n_bins = 201;
        const double max_lag = 100; /* maximal lag in ms */
        double bin_width = 2 * max_lag / (double) n_bins; /* 2*max_lag */
        char filename[100];
        gsl_histogram *h = gsl_histogram_alloc(n_bins);
        gsl_histogram_set_ranges_uniform(h, -max_lag, max_lag);
        FILE *f;

        struct Dynamic_Array poptrain;
        size_t num_spikes_total = 0;
        for (int i = 0; i < n_neurons_sample; i++)
                num_spikes_total += ntw->cell[i].spike_train.n;
        poptrain.data = emalloc(num_spikes_total * sizeof(double));
        poptrain.n = 0;
        poptrain.size = num_spikes_total;
           
        report("Computing global (population rate) autocorrelation...\n");
        fill_population_spike_train(S, &poptrain, n_neurons_sample);

        gsl_sort(poptrain.data, 1, num_spikes_total);
        sprintf(filename, "global_autocorrelation_%s", S->sim.suffix);

        double t_sp;
        f = fopen(filename, "w");
        size_t l = 0;
        size_t r = 0;
        for (size_t j = 0; j < poptrain.n; j++) {
                t_sp = poptrain.data[j];
                /* look for left index */
                while (l < poptrain.n && poptrain.data[l] < t_sp - max_lag)
                        l++;
                /* look for right index */
                while (r < poptrain.n && poptrain.data[r] < t_sp + max_lag)
                        r++;
                /* And feed the histogram with the relevant spikes  */
                for (size_t k = l; k < r; k++) 
                        gsl_histogram_increment(h, t_sp - poptrain.data[k]);
        }
        /* gsl_histogram_fprintf(f, h, "%g", "%g"); */
        /* correct for boundary effects and substract mean */
        double w;
        double T = S->sim.total_time - S->sim.offset;
        /* double nu = num_spikes_total / (T * (double) n_neurons_sample); */
        double lower, upper, ac;
        int status;
        for (size_t j = 0; j < n_bins; j++) {
                w = T - fabs( (n_bins - 1.0) / 2.0 - j ) * bin_width;
                ac = gsl_histogram_get(h, j) / (w * n_neurons_sample);
                ac -= (pow(num_spikes_total / (T * n_neurons_sample), 2) * bin_width);
                /* ac /= pow(nu * bin_width, 2); [> Normalization <] */
                status = gsl_histogram_get_range(h, j, &lower, &upper);
                if (status==0) {
                        fprintf(f, "% 9.4f % 9.4f % 9.6f\n", lower, upper, ac);
                } else {
                        report("Somehting wrong here.\n");
                        exit(2);
                }
        }
        fclose(f);
        free(poptrain.data);
        gsl_histogram_free(h);
}
Exemplo n.º 28
0
void compute_average_autocorrelations(struct State *S)
/* Compute spike-time autocorrelation */
{
        struct Network *ntw = &S->ntw;
        struct Dynamic_Array *nrn_train;
        int n_neurons_sample = 1000;
        size_t num_spikes_total = 0;
        const size_t n_bins = 201;
        const double max_lag = 50; /* in ms */
        double bin_width = 2 * max_lag / (double) n_bins; 
        char filename[100];
        gsl_histogram *h = gsl_histogram_alloc(n_bins);
        gsl_histogram_set_ranges_uniform(h, -max_lag, max_lag);
        FILE *f;

        report("Computing average autocorrelation...\n");
        sprintf(filename, "autocorrelation_%s", S->sim.suffix);

        double t_sp;
        size_t l, r;
        f = fopen(filename, "w");
        for (int i = 0; i < n_neurons_sample; i++) { 
                l = 0;
                r = 0;
                nrn_train = &ntw->cell[i].spike_train;
                num_spikes_total += nrn_train->n;
                for (size_t j = 0; j < nrn_train->n; j++) {
                        t_sp = nrn_train->data[j];
                        /* look for left index */
                        while (l <  nrn_train->n && nrn_train->data[l] < t_sp - max_lag)
                                l++;
                        /* look for right index */
                        while (r < nrn_train->n && nrn_train->data[r] < t_sp + max_lag)
                                r++;
                        /* And feed the histogram with the relevant spikes  */
                        for (size_t k = l; k < r; k++) 
                                gsl_histogram_increment(h, t_sp - nrn_train->data[k]);
                }
        }
        /* gsl_histogram_fprintf(f, h, "%g", "%g"); */
        /* correct for boundary effects and substract mean */
        double w;
        double T = S->sim.total_time - S->sim.offset;
        /* double nu = num_spikes_total / (T * (double) n_neurons_sample); */
        double lower, upper, ac;
        int status;
        for (size_t j = 0; j < n_bins; j++) {
                w = T - fabs( (n_bins - 1.0) / 2.0 - j ) * bin_width;
                ac = gsl_histogram_get(h, j) / (w * n_neurons_sample);
                ac -= (pow(num_spikes_total / (T * n_neurons_sample), 2) * bin_width);
                /* ac /= pow(nu * bin_width, 2); [> Normalization <] */
                status = gsl_histogram_get_range(h, j, &lower, &upper);
                if (status==0) {
                        fprintf(f, "% 9.4f % 9.4f % 9.7f\n", lower, upper, ac);
                } else {
                        report("Something wrong here.\n");
                        exit(2);
                }
        }
        fclose(f);
        gsl_histogram_free(h);
}
Exemplo n.º 29
0
void BurstSearchWidget::on_pushButtonBurstView_clicked()
{
    qDebug("on_pushButtonBurstView_clicked");
    if(ad->isEmpty()) return;
    JKQtPlotter* plot = new JKQtPlotter(true,NULL);
    getParameters();

    double binwidth=5e-6, alternationPeriod=ad->getExcitationPeriodDonor();
    int nbins=(int)(alternationPeriod/binwidth+1);
    gsl_histogram * histCh1 = gsl_histogram_alloc (nbins);
    gsl_histogram * histCh2 = gsl_histogram_alloc (nbins);
    gsl_histogram_set_ranges_uniform (histCh1, 0, alternationPeriod*1.1);
    gsl_histogram_set_ranges_uniform (histCh2, 0, alternationPeriod*1.1);
    int last=0;
    int lastExc=0;
    while(ad->photons.at(last).time<ad->markerTimeDexc.at(lastExc)) ++last;
    uint ch1=ui->comboBoxChannel1->channel();
    uint ch2=ui->comboBoxChannel2->channel();
    while((lastExc<1e4)&&(lastExc<ad->markerTimeDexc.size()+1)) { // average first 10000 periods

        while ((last<ad->photons.size()) && ad->photons[last].time<ad->markerTimeDexc.at(lastExc+1)) {
//            qDebug()<<QString::number(ad->photons[last].flags,2)+"\t"<<ch1<<ch2<<"\t"<<ad->photons[last].isPhotonFlag(ch1)<<ad->photons[last].isPhotonFlag(ch2);
#ifdef PHOTONMACRO
            if(isPhotonFlag(ad->photons[last],ch1)) gsl_histogram_increment (histCh1, ad->photons[last].time-ad->markerTimeDexc[lastExc]);
            if(isPhotonFlag(ad->photons[last],ch2)) gsl_histogram_increment (histCh2, ad->photons[last].time-ad->markerTimeDexc[lastExc]);
#else
            if(ad->photons[last].isPhotonFlag(ch1)) gsl_histogram_increment (histCh1, ad->photons[last].time-ad->markerTimeDexc[lastExc]);
            if(ad->photons[last].isPhotonFlag(ch2)) gsl_histogram_increment (histCh2, ad->photons[last].time-ad->markerTimeDexc[lastExc]);
#endif
            last++;
        }
        // end of excitation period
        lastExc++;
    }
    JKQTPdatastore* ds=plot->get_plotter()->getDatastore();
    ds->clear();
    plot->get_plotter()->clearGraphs(true);
    plot->get_plotter()->setGrid(false);

    qDebug("plotHist DA");
    unsigned long long nrows=(unsigned long long)histCh1->n;
    for(size_t i=0;i<histCh1->n;++i) histCh1->range[i] *= 1e6;
    plot->get_xAxis()->set_axisLabel("time in us");
    plot->get_yAxis()->set_axisLabel("countrate in Hz");
    gsl_histogram_scale(histCh1,(1.0)/(lastExc*binwidth)); // convert bins to countrate in Hz (counts averaged over lastDex excitation periods in bin of width binwidth)
    gsl_histogram_scale(histCh2,(1.0)/(lastExc*binwidth));

    size_t xColumn=ds->addCopiedColumn(histCh1->range, nrows, "time"); // adds column (copies values!) and returns column ID
    QVector<size_t> yColumns;
    yColumns.push_back(ds->addCopiedColumn(histCh1->bin, nrows, "Channel 1"));
    yColumns.push_back(ds->addCopiedColumn(histCh2->bin, nrows, "Channel 2"));
    QList<QColor> colors;
    colors.append(QColor(COUNTRATE_COLOR_CH1));
    colors.append(QColor(COUNTRATE_COLOR_CH2));
    gsl_histogram_free(histCh1);
    gsl_histogram_free(histCh2);

    // plot
    double width=0.5;
    double s=-0.5+width/2.0;
    for (int i=0; i<yColumns.size(); ++i) {
        JKQTPbarHorizontalGraph* g=new JKQTPbarHorizontalGraph(plot->get_plotter());
        g->set_xColumn(xColumn);
        g->set_yColumn(yColumns[i]);
        g->set_shift(s);
        g->set_width(width);
        g->set_style(Qt::NoPen);
        g->set_fillColor(colors.at(i));
        s=s+width;
        plot->addGraph(g);
    }
    plot->get_plotter()->set_keyPosition(JKQTPkeyInsideTopRight); // set legend position
    plot->get_plotter()->set_showKey(true);
    plot->zoomToFit();

    plot->show();

    // old:
//    if(ad->burstVectorDual.isEmpty())
//        runBurstSearch();
//    analysisFRET(ad->burstVectorDual,ad->FRETparams);

//    BurstView *burstView= new BurstView(this, ad);
//    burstView->exec();
}
Exemplo n.º 30
0
Arquivo: rng.c Projeto: Noughmad/Sola
void test_normal()
{
  gen g_uni[] = {builtin, devurandom, kalkulator, mersenne, 
#if N <= (1 << 12)
  randorg,
#endif
  0};
  test t[] = {chisq, kolsmir, 0};
  task* prob = task_new(0, g_uni, t, 0 );
  
  gsl_histogram* h = gsl_histogram_alloc(B);
  gsl_histogram_set_ranges_uniform(h, 0, 1);
  gsl_histogram_shift(h, AVG);
  prob->density = h;
  
  gsl_histogram2d* hd = gsl_histogram2d_alloc(B2, B2);
  gsl_histogram2d_set_ranges_uniform(hd, 0, 1, 0, 1);
  gsl_histogram2d_shift(hd, AVG2/2);
  prob->density2d = hd;
  
  prob->min = 0;
  prob->max = 1;
  task_perform(prob);
  task_free(prob);
  
  gen g_gauss[] = {gauss_bm, gauss_zig, gauss_ratio, 0};
  prob = task_new(2, g_gauss, t, 0);
  prob->max = 3;
  prob->min = -prob->max;
  
  h = gsl_histogram_alloc(B);
  gsl_histogram_set_ranges_uniform(h, prob->min, prob->max);
  int j;
  for (j=0; j<B; ++j)
  {
    double min, max;
    gsl_histogram_get_range(h, j, &min, &max);
    double x = (max + min)/2;
    
    gsl_histogram_accumulate(h, x, N*gsl_ran_ugaussian_pdf(x) / B * (prob->max - prob->min));
  }
  
  hd = gsl_histogram2d_alloc(B2, B2);
  gsl_histogram2d_set_ranges_uniform(hd, prob->min, prob->max, prob->min, prob->max);
  int k;
  for (j=0; j < B2; ++j)
    for (k=0; k<B2; ++k)
    {
      double min, max;
      gsl_histogram2d_get_xrange(hd, j, &min, &max);
      double x = (max + min)/2;
      
      gsl_histogram2d_get_yrange(hd, k, &min, &max);
      double y = (max + min)/2;
      
      gsl_histogram2d_accumulate(hd, x, y, gsl_ran_ugaussian_pdf(x) * gsl_ran_ugaussian_pdf(y));
    }
    
  gsl_histogram2d_scale( hd, N/2/gsl_histogram2d_sum(hd) );
  prob->density = h;
  prob->density2d = hd;
  task_perform(prob);
  task_free(prob);
}