Beispiel #1
0
int main(int argc, char *argv[])
{
    int rank, p;
    struct timeval t1, t2;
    byte_t buf[MAX_SIZE];

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &p);

    printf("my rank = %d\n", rank);
    printf("Rank = %d: number of processes = %d\n", rank, p);

    assert(p >= 2);

    FILE *sendFile = fopen("send.dat", "a");
    FILE *recvFile = fopen("recv.dat", "a");
    if(sendFile == NULL || recvFile == NULL)
    {
        perror("Error: Failed to open file");
        MPI_Finalize();
        return 1;
    }

    // sync procs before collecting data
    //if(rank == 0)
    //{
    //    MPI_Send(&buf, 1, MPI_BYTE, 1, 0, MPI_COMM_WORLD); // send
    //    MPI_Barrier(MPI_COMM_WORLD); // sync send/recv
    //}
    //else
    //{
    //    MPI_Status status; // sender rank
    //    MPI_Recv(&buf, 1, MPI_BYTE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); // receive
    //    MPI_Barrier(MPI_COMM_WORLD); // sync send/recv
    //}

    for(int size = 1; size <= MAX_SIZE; size <<= 1)
    {
        int time[ITER_PER_SIZE];
        for(int i = 0; i < ITER_PER_SIZE; i++)
        {
            if(rank == 0)
            {
                int dest = rank + 1;
                gettimeofday(&t1, NULL); // t1
                MPI_Ssend(&buf, size, MPI_BYTE, dest, 0, MPI_COMM_WORLD); // send
                gettimeofday(&t2, NULL); // t2
                time[i] = (t2.tv_sec-t1.tv_sec)*1000 + (t2.tv_usec-t1.tv_usec)/1000; // t2 - t1
                MPI_Barrier(MPI_COMM_WORLD); // sync send/recv
            } 
            else
            {
                MPI_Status status; // sender rank
                gettimeofday(&t1, NULL); // t1
                MPI_Recv(&buf, size, MPI_BYTE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); // receive
                gettimeofday(&t2, NULL); // t2
                time[i] = (t2.tv_sec-t1.tv_sec)*1000 + (t2.tv_usec-t1.tv_usec)/1000; // t2 - t1
                MPI_Barrier(MPI_COMM_WORLD); // sync send/recv
            }
        }

        // record data
        if(rank == 0)
        {
            printf("%d -> %d: Send(%d) %d ms\n", rank, rank + p/2, size, mean(time, ITER_PER_SIZE)); 
            fprintf(sendFile, "%d,%d\n", size, mean(time, ITER_PER_SIZE)); 
        }
        else
        {
            printf("%d -> %d: Recv(%d) %d ms\n", rank, rank - p/2, size, mean(time, ITER_PER_SIZE));
            fprintf(recvFile, "%d,%d\n", size, mean(time, ITER_PER_SIZE));
        }
    }

    fclose(sendFile);
    fclose(recvFile);

    MPI_Finalize();
}
Beispiel #2
0
Stats::matrix_type Stats::Impl::covariance() const
{
    Stats::vector_type m = mean(); 
    return meanOuterProduct() - outer_prod(m, m); 
}
Beispiel #3
0
int ar_estimate(double *x, int N, int method) {
	int p, ordermax, logn, i;
	double wmean, aic, loglik, var, lvar, aic0;
	double *inp, *resid, *hess, *phi;

	inp = (double*)malloc(sizeof(double)* N);
	resid = (double*)malloc(sizeof(double)* N);

	logn = (int) (10.0 * log((double)N));
	ordermax = imin(N - 1, logn);

	if (method == 2) {
		ordermax = imin(ordermax, 12); // MLE
	}
	wmean = mean(x, N);
	aic0 = 0.0;

	hess = (double*)malloc(sizeof(double)* (ordermax + 1) * (ordermax + 1));
	phi = (double*)malloc(sizeof(double)* ordermax);

	for (i = 0; i < N; ++i) {
		inp[i] = x[i] - wmean;
	}

	for (i = 0; i < ordermax; ++i) {
		if (method == 0) {
			ywalg2(inp, N, i + 1, phi, &var);
		}
		else if (method == 1) {
			burgalg(inp, N-1, i + 1, phi, &var);
		}
		else if (method == 2) {
			as154(inp, N, 7, i + 1, 0, 0, phi, NULL, &wmean, &var, resid, &loglik, hess);
		}
		else {
			printf("\n The code only accepts numerical values 0,1 and 2 \n");
			printf("\n Method 0 : Yule-Walker \n");
			printf("\n Method 1 : Burg \n");
			printf("\n Method 2 : MLE \n");
		}
		lvar = log(var);
		aic = lvar + 2 * (double)(i + 1) / N;
		if (i == 0) {
			aic = aic0;
			p = 1;
		}
		else {
			if (aic < aic0) {
				aic0 = aic;
				p = i + 1;
			}
		}
	}

	printf("\n\n");
	printf("AIC Estimate : p = %d \n", p);

	free(inp);
	free(resid);
	free(hess);
	free(phi);
	return p;
}
Beispiel #4
0
int TestIndexPackDouble(int n, int stride,
                        double *avgTimeUser, double *avgTimeMPI, double *dest, const double *src)
{
    double *restrict d_dest;
    const double *restrict d_src;
    register int i, j;
    int rep, position;
    int *restrict displs = 0;
    double t1, t2, t[NTRIALS];
    MPI_Datatype indextype;

    displs = (int *) malloc(n * sizeof(int));
    for (i = 0; i < n; i++)
        displs[i] = i * stride;

    /* User code */
    if (verbose)
        printf("TestIndexPackDouble (USER): ");
    for (j = 0; j < NTRIALS; j++) {
        t1 = MPI_Wtime();
        for (rep = 0; rep < N_REPS; rep++) {
            i = n;
            d_dest = dest;
            d_src = src;
            for (i = 0; i < n; i++) {
                *d_dest++ = d_src[displs[i]];
            }
        }
        t2 = MPI_Wtime() - t1;
        t[j] = t2;
        if (verbose)
            printf("%.3f ", t[j]);
    }
    if (verbose)
        printf("[%.3f]\n", noise(t, NTRIALS));
    /* If there is too much noise, discard the test */
    if (noise(t, NTRIALS) > VARIANCE_THRESHOLD) {
        *avgTimeUser = 0;
        *avgTimeMPI = 0;
        if (verbose)
            printf("Too much noise; discarding measurement\n");
        return 0;
    }
    *avgTimeUser = mean(t, NTRIALS) / N_REPS;

    /* MPI Index code */
    MPI_Type_create_indexed_block(n, 1, displs, MPI_DOUBLE, &indextype);
    MPI_Type_commit(&indextype);

    free(displs);

    if (verbose)
        printf("TestIndexPackDouble (MPI): ");
    for (j = 0; j < NTRIALS; j++) {
        t1 = MPI_Wtime();
        for (rep = 0; rep < N_REPS; rep++) {
            position = 0;
            MPI_Pack((void *) src, 1, indextype, dest, n * sizeof(double),
                     &position, MPI_COMM_SELF);
        }
        t2 = MPI_Wtime() - t1;
        t[j] = t2;
        if (verbose)
            printf("%.3f ", t[j]);
    }
    if (verbose)
        printf("[%.3f]\n", noise(t, NTRIALS));
    /* If there is too much noise, discard the test */
    if (noise(t, NTRIALS) > VARIANCE_THRESHOLD) {
        *avgTimeUser = 0;
        *avgTimeMPI = 0;
        if (verbose)
            printf("Too much noise; discarding measurement\n");
    } else {
        *avgTimeMPI = mean(t, NTRIALS) / N_REPS;
    }
    MPI_Type_free(&indextype);

    return 0;
}
Beispiel #5
0
void SingleParticle2dx::DataStructures::Particle::calculateConsistency()
{
	size_type n = 6;
	std::vector<Eigen::VectorXf> points;
	std::vector<Particle*> neighbors = getNeighbors();
	
	for (size_type i=0; i<static_cast<size_type>(neighbors.size()); i++ )
	{
		Eigen::VectorXf new_vec(n);
		new_vec[0] = neighbors[i]->getNewOrientation().getTLTAXIS();
		new_vec[1] = neighbors[i]->getNewOrientation().getTLTANG();
		new_vec[2] = neighbors[i]->getNewOrientation().getTAXA();
		new_vec[3] = neighbors[i]->getParticleShift().getShiftX();
		new_vec[4] = neighbors[i]->getParticleShift().getShiftY();
		new_vec[5] = neighbors[i]->getSimMeasure();
		points.push_back(new_vec);
	}
	
	Eigen::VectorXf mean(n);
	
	for(size_type i=0; i<static_cast<size_type>(points.size()); i++)
	{
		mean += points[i];
	}
	mean /= static_cast<value_type>(points.size());
	
//	std::cout << ": mean = " << mean[0] << " " << mean[1] << " " << mean[2] << " " << mean[3] << " " << mean[4] << " " << mean[5] << std::endl;
	
	Eigen::MatrixXf covMat = Eigen::MatrixXf::Zero(n,n);
	
//	std::cout << mean << std::endl;
	
	for(size_type i=0; i<static_cast<size_type>(points.size()); i++)
	{
		Eigen::VectorXf diff = (points[i]-mean).conjugate();
		covMat += diff * diff.adjoint();
	}
	
//	std::cout << ":det = " << covMat.determinant() << std::endl;
	
	value_type det = covMat.determinant();
	
	if ( !std::isfinite(det) )
	{
		setConsistency(0);
		return;
	}
	
	if ( det < 0.001 )
	{
		setConsistency(0);
		return;
	}
	
	//SingleParticle2dx::Utilities::UtilityFunctions::reqularizeMatrix(covMat);
	
//	std::cout << covMat << std::endl;
	
	Eigen::VectorXf vec(n);
	vec[0] = getNewOrientation().getTLTAXIS();
	vec[1] = getNewOrientation().getTLTANG();
	vec[2] = getNewOrientation().getTAXA();
	vec[3] = getParticleShift().getShiftX();
	vec[4] = getParticleShift().getShiftY();
	vec[5] = getSimMeasure();
	
	value_type result = -0.5 * log(covMat.determinant());
	
//	#pragma omp critical (det_output)
	//{
		//std::cout << ":det = " << det << std::endl;
	//}
	
	if ( covMat.determinant() < 0.000001 )
	{
		setConsistency(0);
		return;
	}
	
//	std::cout << ":first term: " << result << std::endl;
	
	Eigen::VectorXf tmp1 = (covMat.inverse()) * (vec-mean);
	value_type tmp2 = (vec-mean).dot(tmp1);
	
	result -= 0.5 * tmp2;
	
//	std::cout << ":second term: " << -0.5 * tmp2 << std::endl;

	if ( boost::math::isnan(result) || boost::math::isnan(-result) )
	{
		std::cout << "::reset CONS realy done now" << std::endl;
		result = 0;
	}
	
	setConsistency(result);
}
Beispiel #6
0
/*! \brief Tests the **icpMean_Weighted** kernel.
 *  \details The kernel computes the means of sets of points.
 */
TEST (ICP, icpMean_Weighted)
{
    try
    {
        const unsigned int n = 1 << 14;  // 16384
        const unsigned int d = 8;
        // const unsigned int bufferInFMSize = n * sizeof (cl_float8);
        // const unsigned int bufferInWSize = n * sizeof (cl_float);
        // const unsigned int bufferInSWSize = sizeof (cl_double);
        // const unsigned int bufferOutSize = 2 * sizeof (cl_float4);

        // Setup the OpenCL environment
        clutils::CLEnv clEnv;
        clEnv.addContext (0);
        clEnv.addQueue (0, 0, CL_QUEUE_PROFILING_ENABLE);
        clEnv.addProgram (0, kernel_filename_icp);

        // Configure kernel execution parameters
        clutils::CLEnvInfo<1> info (0, 0, 0, { 0 }, 0);
        const cl_algo::ICP::ICPMeanConfig C = cl_algo::ICP::ICPMeanConfig::WEIGHTED;
        cl_algo::ICP::ICPMean<C> mean (clEnv, info);
        mean.init (n);

        // Initialize data (writes on staging buffer directly)
        std::generate (mean.hPtrInF, mean.hPtrInF + n * d, ICP::rNum_0_10000);
        std::generate (mean.hPtrInM, mean.hPtrInM + n * d, ICP::rNum_0_255);
        std::generate (mean.hPtrInW, mean.hPtrInW + n, ICP::rNum_R_0_1);
        mean.hPtrInSW[0] = std::accumulate (mean.hPtrInW, mean.hPtrInW + n, 0.0);
        // ICP::printBufferF ("Original F:", mean.hPtrInF, d, n, 3);
        // ICP::printBufferF ("Original M:", mean.hPtrInM, d, n, 3);
        // ICP::printBufferF ("Original W:", mean.hPtrInW, 1, n, 3);

        // Copy data to device
        mean.write (cl_algo::ICP::ICPMean<C>::Memory::D_IN_F);
        mean.write (cl_algo::ICP::ICPMean<C>::Memory::D_IN_M);
        mean.write (cl_algo::ICP::ICPMean<C>::Memory::D_IN_W);
        mean.write (cl_algo::ICP::ICPMean<C>::Memory::D_IN_SUM_W);
        
        mean.run ();  // Execute kernels (~ 25 us)
        
        cl_float *results = (cl_float *) mean.read ();  // Copy results to host
        // ICP::printBufferF ("Received:", results, 4, 2, 3);

        // Produce reference mean vector
        cl_float refMean[8];
        ICP::cpuICPMeanWeighted (mean.hPtrInF, mean.hPtrInM, refMean, mean.hPtrInW, n);
        // ICP::printBufferF ("Expected:", refMean, 4, 2, 3);

        // Verify mean vector
        float eps = 420000 * std::numeric_limits<float>::epsilon ();  // 0.0500679
        for (uint i = 0; i < 8; ++i)
            ASSERT_LT (std::abs (refMean[i] - results[i]), eps);

        // Profiling ===========================================================
        if (profiling)
        {
            const int nRepeat = 1;  /* Number of times to perform the tests. */

            // CPU
            clutils::CPUTimer<double, std::milli> cTimer;
            clutils::ProfilingInfo<nRepeat> pCPU ("CPU");
            for (int i = 0; i < nRepeat; ++i)
            {
                cTimer.start ();
                ICP::cpuICPMeanWeighted (mean.hPtrInF, mean.hPtrInM, refMean, mean.hPtrInW, n);
                pCPU[i] = cTimer.stop ();
            }
            
            // GPU
            clutils::GPUTimer<std::milli> gTimer (clEnv.devices[0][0]);
            clutils::ProfilingInfo<nRepeat> pGPU ("GPU");
            for (int i = 0; i < nRepeat; ++i)
                pGPU[i] = mean.run (gTimer);

            // Benchmark
            pGPU.print (pCPU, "ICPMean<WEIGHTED>");
        }

    }
    catch (const cl::Error &error)
    {
        std::cerr << error.what ()
                  << " (" << clutils::getOpenCLErrorCodeString (error.err ()) 
                  << ")"  << std::endl;
        exit (EXIT_FAILURE);
    }
}
Beispiel #7
0
// Compute mean and covariance from a CSV file of features
Mat AudioAnalyzer::CSVToCovariance(string filename)
{

	// CSV Parsing    
    Mat matrice;
    Mat result;
    int nframes = 0, nitems = 0;
    
    // Get the CSV file as a stream
    ifstream inFile (filename.c_str());
     if (inFile.is_open()) {
		 // Retrieve the number of frames of the file (=number of lines - header)
		nframes = count(istreambuf_iterator<char>(inFile), 
                 istreambuf_iterator<char>(), '\n') - 1;
		

		// Go back to the beginning of the file
		inFile.seekg(0);
		int linenum = 0;
		int itemnum = 0;
		string line;
		while (getline (inFile, line))
		{
			//cout << "\nLine #" << linenum << ":" << endl;
			istringstream linestream(line);
			string item;
			itemnum = 0;
			// forget firts two cols
			getline (linestream, item, ';');
			getline (linestream, item, ';');
			while (getline (linestream, item, ';'))
			{
				if (linenum > 0)																		// CHECK WITH NEW CONF FILE !!!!!!!!!
				{
					matrice.at<float>(linenum-1, itemnum) = (float)atof(item.c_str());
					//cout << "Item #" << itemnum << ": " << item << "-"<<matrice.at<float>(linenum-1, itemnum)<< endl;
				}
				itemnum++;
			}
			if (linenum == 0)
			{
				nitems = itemnum;
				matrice.create(nframes, nitems, CV_32F);
			} 
			linenum++;
		}  
		inFile.close();
		//cout << "matrice.cols : " << matrice.cols << " / matrice.rows " << matrice.rows << endl;
    } else  {
		cout << "Error: can't open CSV file." << endl;
    }
    
    // If the matrix has been successfully populated from the CSV file
    if (matrice.rows > 0 && matrice.cols > 0) {
		// Calculate the mean vector the covariance matrix
		Mat covar(0,0,CV_32F);
		Mat mean(0,0,CV_32F);
		calcCovarMatrix(matrice, covar, mean, CV_COVAR_ROWS | CV_COVAR_NORMAL, CV_32F);
		
		//cout << "CSVToCovariance: covariance ("<<covar.cols<< "," << covar.rows << ") computed for "<<nitems<<" features on "<<nframes<<" frames."<<endl;
		
		
		// Init the result matrix :
		// - number of rows : 1
		// - number of columns : nitems (means) + sum(n-i, i=0..n) (variance)
		result.create(1, (int)(nitems*(1 + (float)(nitems+1)/2)), CV_32F);
		int index = 0;
		// Add the mean to the matrix
	
		for(int j=0; j<mean.cols; j++) {
			result.at<float>(0,index) = mean.at<float>(0,j);
			index++;
		}
		
		for(int i=0; i<covar.rows; i++) {
			for(int j=i; j<covar.cols; j++) {
				result.at<float>(0,index) = covar.at<float>(i,j);
				index++;
			}
		}	


    
	}  else {
		cout << "Error while parsing CSV file" << endl;
	}
	
	
	return result;
}
Beispiel #8
0
		const T logSelexMeanOne(const T &x) const
		{
			T y = log(gsm::coefficients(x, this->GetSelCoeffs()));
			y -= log(mean(mfexp(y)));
			return y;
		}
inline
bool
op_princomp::direct_princomp
  (
         Mat<typename T1::elem_type>&     coeff_out,
         Mat<typename T1::elem_type>&     score_out,
         Col<typename T1::elem_type>&     latent_out,
         Col<typename T1::elem_type>&     tsquared_out,
  const Base<typename T1::elem_type, T1>& X,
  const typename arma_not_cx<typename T1::elem_type>::result* junk
  )
  {
  arma_extra_debug_sigprint();
  arma_ignore(junk);
  
  typedef typename T1::elem_type eT;
  
  const unwrap_check<T1> Y( X.get_ref(), score_out );
  const Mat<eT>& in    = Y.M;

  const uword n_rows = in.n_rows;
  const uword n_cols = in.n_cols;
  
  if(n_rows > 1) // more than one sample
    {
    // subtract the mean - use score_out as temporary matrix
    score_out = in - repmat(mean(in), n_rows, 1);
 	  
    // singular value decomposition
    Mat<eT> U;
    Col<eT> s;
    
    const bool svd_ok = svd(U,s,coeff_out,score_out);
    
    if(svd_ok == false)
      {
      return false;
      }
    
    
    //U.reset();  // TODO: do we need this ?  U will get automatically deleted anyway
    
    // normalize the eigenvalues
    s /= std::sqrt( double(n_rows - 1) );
    
    // project the samples to the principals
    score_out *= coeff_out;
    
    if(n_rows <= n_cols) // number of samples is less than their dimensionality
      {
      score_out.cols(n_rows-1,n_cols-1).zeros();
      
      //Col<eT> s_tmp = zeros< Col<eT> >(n_cols);
      Col<eT> s_tmp(n_cols);
      s_tmp.zeros();
      
      s_tmp.rows(0,n_rows-2) = s.rows(0,n_rows-2);
      s = s_tmp;
          
      // compute the Hotelling's T-squared
      s_tmp.rows(0,n_rows-2) = eT(1) / s_tmp.rows(0,n_rows-2);
      
      const Mat<eT> S = score_out * diagmat(Col<eT>(s_tmp));   
      tsquared_out = sum(S%S,1); 
      }
    else
      {
      // compute the Hotelling's T-squared   
      const Mat<eT> S = score_out * diagmat(Col<eT>( eT(1) / s));
      tsquared_out = sum(S%S,1);
      }
            
    // compute the eigenvalues of the principal vectors
    latent_out = s%s;
    }
  else // 0 or 1 samples
    {
    coeff_out.eye(n_cols, n_cols);
    
    score_out.copy_size(in);
    score_out.zeros();
    
    latent_out.set_size(n_cols);
    latent_out.zeros();
    
    tsquared_out.set_size(n_rows);
    tsquared_out.zeros();
    }
  
  return true;
  }
Beispiel #10
0
		const T logSelexMeanOne(const T &x) const
		{
			T y = log(gsm::plogis95<T>(x, this->GetS50(), this->GetS95()));
			y  -= log(mean(mfexp(y)));
			return y;
		}
Beispiel #11
0
		const T logSelexMeanOne(const T &x) const
		{
			T y = log(gsm::pdubnorm<T>(x, this->GetSL(), this->GetS50(), this->GetSR()));
			y  -= log(mean(mfexp(y)));
			return y;
		}
Beispiel #12
0
		const T logSelexMeanOne(const T &x) const
		{
			T y = log(gsm::plogis(x, this->GetMean(), this->GetStd()));
			y  -= log(mean(mfexp(y)));
			return y;
		}
int runMe(int argc, char *argv[])
{

    ArgProcessor args(argc, argv);
    if (args.isArgSet("--help") ||
        (!(args.isArgSet("--reads") && args.isArgSet("--kmers")))) {
        cerr << usage(args) << endl << endl;
        exit(1);
    }
    string reads_fasta_file = args.getStringVal("--reads");
    string kmers_fasta_file = args.getStringVal("--kmers");
    bool is_DS = (!args.isArgSet("--SS"));
    if (args.isArgSet("--kmer_size")) {
        KMER_SIZE = args.getIntVal("--kmer_size");
        if (KMER_SIZE < 20) {
            cerr << "Error, min kmer size is 20";
            exit(2);
        }
    }
    if (args.isArgSet("--monitor")) {
        IRKE_COMMON::MONITOR = args.getIntVal("--monitor");
    }
    if (args.isArgSet("--num_threads")) {
        int num_threads = args.getIntVal("--num_threads");
        if (num_threads < MAX_THREADS) {
            omp_set_num_threads(num_threads);
        }
        else {
            // set to max
            omp_set_num_threads(MAX_THREADS);
        }
    }

    if (omp_get_max_threads() > MAX_THREADS) {
        omp_set_num_threads(MAX_THREADS);
    }
    KmerCounter kcounter(KMER_SIZE, is_DS);
    populate_kmer_counter(kcounter, kmers_fasta_file);
    Fasta_reader fasta_reader(reads_fasta_file);
    bool write_coverage_info = args.isArgSet("--capture_coverage_info");

    int start_time = time(NULL);

#pragma omp parallel
    while (true) {

        if (!fasta_reader.hasNext())
            break;

        int myTid = omp_get_thread_num();

        Fasta_entry fe = fasta_reader.getNext();
        string sequence = fe.get_sequence();
        if (sequence == "")
            continue;

        string header = fe.get_header();
        vector<unsigned int> kmer_coverage = compute_kmer_coverage(sequence, kcounter);
        unsigned int median_cov = median_coverage(kmer_coverage);
        float mean_cov = mean(kmer_coverage);
        float stdev = stDev(kmer_coverage);
        float pct_stdev_of_avg = stdev / mean_cov * 100;
        stringstream stats_text;

        stats_text << median_cov << "\t"
            << mean_cov << "\t"
            << stdev << "\t"
            << pct_stdev_of_avg << "\t"
            << fe.get_accession();

        stats_text << "\tthread:" << myTid;

        if (write_coverage_info) {
            // add the coverage info
            stats_text << "\t";
            for (size_t i = 0; i < kmer_coverage.size(); i++) {
                stats_text << kmer_coverage[i];
                if (i != kmer_coverage.size() - 1) {
                    stats_text << ",";
                }
            }
        }
        stats_text << endl;

#pragma omp critical
        {
            cout << stats_text.str();
        }

        if (mean_cov < 0) {
            cerr << "ERROR, cannot have negative coverage!!" << endl;
            exit(1);
        }

    }

    int end_time = time(NULL);

    cerr << "STATS_GENERATION_TIME: " << (end_time - start_time) << " seconds." << endl;

    return (0);
}
int main(int argc, char *argv[])
{
	int i, j, k;
	int nh=0;              /*  counter for the total number of hours  */
	int nhd=0;             /*  counter for the number of hours of the actual day  */
	int month=0, day=0, jday=0, jday_hoy=0, last_day=0, last_month=0, last_jday=0;
	int status=6;             /*  indicates EOF of the input weather data file  */
	int fatal=0;              /*  indicates soon exit due to fatal error        */
	int azimuth_class=0;
	int *daylight_status;     /*  0=night hour, 1=sunrise/sunset hour, 2=innerday hour  */

	double time, centrum_time, *times;
	double irrad_glo = 0.0, irrad_beam_nor, irrad_beam_hor, irrad_dif;     /* in W/m² */
	double *irrads_glo, *irrads_beam_nor, *irrads_dif, *indices_glo, *indices_beam, sr_ss_indices_glo[3];
	double *irrads_glo_st, *irrads_glo_clear_st, *irrads_beam_nor_st, *irrads_dif_st, *indices_glo_st;
	double time_t, time_k, mean_glo_st, mean_beam_st, mean_dif_st, sum_beam_nor, sum_beam_hor, sum_dif;
	double sunrise_localtime, sunset_localtime;
	double solar_elevation, solar_azimuth, eccentricity_correction;
	double punk;              /*  indicates nice sound  */
	double previous_ligoh = 0, actual_ligoh;
	/*  ligoh = last index_glo of an hour: introduced to minimize discontinuities between subsequent hours  */

	
	
	if (argc == 1) {
		char *progname = fixargv0(argv[0]);
		fprintf(stdout, "%s: fatal error -  header file missing\n", progname);
		fprintf(stdout, "start program with:  %s  <header file>\n ", progname);
		exit(1);
	}

	if (!strcmp(argv[1], "-version")) {
		puts(VersionID);
		exit(0);
	}

	header=argv[1];
	read_in_genshortterm_header();

	



	/*printf("input_weather_data=%s\n",input_weather_data);
	  printf("input_weather_data_shortterm=%s\n",input_weather_data_shortterm);
	  printf("shortterm_timestep=%d\n",shortterm_timestep);
	  printf("input_units_genshortterm=%d\n",input_units_genshortterm);
	  printf("output_units_genshortterm=%d\n",output_units_genshortterm);
	  printf("solar_time=%d\n",solar_time);
	  printf("latitude=%f \nlongitude=%f \ntime_zone=%f\n",latitude,longitude,time_zone);
	  printf("site_elevation=%f\n",site_elevation);
	  printf("horizon_in=%d horizon_data_in=%s\n",horizon_in,horizon_data_in);
	  printf("horizon_out=%d horizon_data_out=%s\n",horizon_out,horizon_data_out);
	  printf("linke_estimation=%d\n",linke_estimation);*/

	HOURLY_DATA = open_input(input_weather_data);
	/* added by C. Reinhart                   */
	/* test whether input file has a header   */
	/* in case the is a header, it is skipped */
	fscanf(HOURLY_DATA,"%s", keyword);
  	if( !strcmp(keyword,"place") ){
		rewind(HOURLY_DATA);
		fgets(header_line_1,300,HOURLY_DATA);
		fgets(header_line_2,300,HOURLY_DATA);
		fgets(header_line_3,300,HOURLY_DATA);
		fgets(header_line_4,300,HOURLY_DATA);
		fgets(header_line_5,300,HOURLY_DATA);
		fgets(header_line_6,300,HOURLY_DATA);
		// get time step of input file
		fscanf(HOURLY_DATA,"%d %d %lf", &month, &day, &centrum_time);fscanf(HOURLY_DATA,"%*[^\n]");fscanf(HOURLY_DATA,"%*[\n\r]");
		fscanf(HOURLY_DATA,"%d %d %lf", &month, &day, &time);fscanf(HOURLY_DATA,"%*[^\n]");fscanf(HOURLY_DATA,"%*[\n\r]");
		test_input_time_step=(int)(60.0*fabs(time-centrum_time));
		//printf("The time step of the input file (%s) is %d minutes\n",input_weather_data, test_input_time_step);
		if(test_input_time_step != shortterm_timestep && test_input_time_step != 60 ){
		 	printf("wea_data_file does not have a 60 minute time step interval! %d\n",test_input_time_step);
		}
		rewind(HOURLY_DATA);
		fgets(header_line_1,300,HOURLY_DATA);
		fgets(header_line_2,300,HOURLY_DATA);
		fgets(header_line_3,300,HOURLY_DATA);
		fgets(header_line_4,300,HOURLY_DATA);
		fgets(header_line_5,300,HOURLY_DATA);
		fgets(header_line_6,300,HOURLY_DATA);
	}else{	// input file has no header
		rewind(HOURLY_DATA);
		// get time step of input file
		fscanf(HOURLY_DATA,"%d %d %lf", &month, &day, &centrum_time);fscanf(HOURLY_DATA,"%*[^\n]");fscanf(HOURLY_DATA,"%*[\n\r]");
		fscanf(HOURLY_DATA,"%d %d %lf", &month, &day, &time);fscanf(HOURLY_DATA,"%*[^\n]");fscanf(HOURLY_DATA,"%*[\n\r]");
		test_input_time_step=(int)(60.0*fabs(time-centrum_time));
		fprintf(stderr,"The time step of the inpt file (%s) is %d minutes\n",input_weather_data, test_input_time_step);
		if(test_input_time_step != shortterm_timestep && test_input_time_step != 60 ){
		 	fprintf(stderr,"wea_data_file does not have a 60 minute time step interval! %d\n",test_input_time_step);
		}
		rewind(HOURLY_DATA);
		sprintf(header_line_1,"place %s\n",input_weather_data);
		sprintf(header_line_2,"latitude %f\n",latitude);
		sprintf(header_line_3,"longitude %f\n",longitude);
		sprintf(header_line_4,"time_zone %f\n",time_zone);
		sprintf(header_line_5,"site_elevation %f\n",site_elevation);
		sprintf(header_line_6,"weather_data_file_units %d\n",output_units_genshortterm);

	}


	SHORT_TERM_DATA = open_output(input_weather_data_shortterm);
    //print file header
  	fprintf(SHORT_TERM_DATA,"%s", header_line_1);
  	fprintf(SHORT_TERM_DATA,"%s", header_line_2);
  	fprintf(SHORT_TERM_DATA,"%s", header_line_3);
  	fprintf(SHORT_TERM_DATA,"%s", header_line_4);
  	fprintf(SHORT_TERM_DATA,"%s", header_line_5);
  	fprintf(SHORT_TERM_DATA,"%s", header_line_6);


	if ((times = malloc(24 * sizeof(double))) == NULL) goto memerr;
	if ((irrads_glo = malloc(24 * sizeof(double))) == NULL) goto memerr;
	if ((irrads_beam_nor = malloc(24 * sizeof(double))) == NULL) goto memerr;
	if ((irrads_dif = malloc(24 * sizeof(double))) == NULL) goto memerr;
	if ((indices_glo = malloc(24 * sizeof(double))) == NULL) goto memerr;
	if ((indices_beam = malloc(24 * sizeof(double))) == NULL) goto memerr;
	if ((daylight_status = malloc(24 * sizeof(int))) == NULL) goto memerr;

	if ((irrads_glo_st = malloc(sph*sizeof(double))) == NULL) goto memerr;
	if ((irrads_glo_clear_st = malloc(sph*sizeof(double))) == NULL) goto memerr;
	if ((irrads_beam_nor_st = malloc(sph*sizeof(double))) == NULL) goto memerr;
	if ((irrads_dif_st = malloc(sph*sizeof(double))) == NULL) goto memerr;
	if ((indices_glo_st = malloc(sph*sizeof(double))) == NULL) goto memerr;


	if ( shortterm_timestep == test_input_time_step )      /*  no generation of shortterm data, but conversion of direct-hor to direct-norm irradiance  */
		{
			//fprintf(stderr,"ds_shortterm: message: input time step equals output time step.\n");
			while ( status > 0 )                       /*  as long as EOF is not reached  */
				{
					if ( input_units_genshortterm == 1 )
					{
						status = fscanf(HOURLY_DATA,"%d %d %lf %lf %lf", &month, &day, &time, &irrad_beam_nor, &irrad_dif);
						if(irrad_beam_nor<0|| irrad_dif<0)
							{
								status=-1;
								printf("FATAL ERROR: Negative direct or diffuse irradiance at month: %d, day: %d, time %.1f\n",month,day,time);
								printf("Generating cliamte file stopped.\n");
							}
					}
					if ( input_units_genshortterm == 2 )
					{
						status = fscanf(HOURLY_DATA,"%d %d %lf %lf %lf", &month, &day, &time, &irrad_beam_hor, &irrad_dif);
						if(irrad_beam_hor<0|| irrad_dif<0)
							{
								status=-1;
								printf("FATAL ERROR: Negative direct or diffuse irradiance at month: %d, day: %d, time %.1f\n",month,day,time);
								printf("Generating cliamte file stopped.\n");
							}
					}
					if ( status <= 0 )  goto end;

					if ( input_units_genshortterm == 2 )                             /*  calculate irrad_beam_nor  */
						{
							if ( irrad_beam_hor > 0 )
								{
									jday = jdate(month, day);
									sunrise_sunset_localtime ( latitude, longitude, time_zone, jday, &sunrise_localtime, &sunset_localtime );
									centrum_time=time;
									if ( fabs(time-sunrise_localtime) <= 0.5 )  centrum_time=sunrise_localtime+(time+0.5-sunrise_localtime)/2.0;
									if ( fabs(time-sunset_localtime) <= 0.5 )  centrum_time=time-0.5+(sunset_localtime-(time-0.5))/2.0;
									solar_elev_azi_ecc ( latitude, longitude, time_zone, jday, centrum_time, solar_time, &solar_elevation, &solar_azimuth, &eccentricity_correction);
									irrad_beam_nor = irrad_beam_hor / sin(radians(solar_elevation));
									if ( irrad_beam_nor < 0 )  irrad_beam_nor=0;
								}
							else irrad_beam_nor=0;
						}
					//}

					fprintf(SHORT_TERM_DATA,"%d %d %.3f %.0f %.0f\n", month, day, time, irrad_beam_nor, irrad_dif);
				}
		}


	else                                 /*  generation of 1-min short-term data according to modified Skartveit & Olseth  */
		{                                    /*  by Oliver Walkenhorst, November 2000                                          */

			if ( test_input_time_step < 60 )create60minTempFile();
			if ( horizon_in )
				{
					printf("reads in input horizon data ... \n");
					read_horizon_azimuth_data ( horizon_data_in, &horizon_azimuth_in[0] );
				}
			else  for ( i=0 ; i<36 ; i++ )  horizon_azimuth_in[i]=0;

			if ( horizon_out )
				{
					printf("reads in output horizon data ... \n");
					read_horizon_azimuth_data ( horizon_data_out, &horizon_azimuth_out[0] );
				}
			else  for ( i=0 ; i<36 ; i++ )  horizon_azimuth_out[i]=0;

			if ( linke_estimation )
				{
					//printf("\nEstimating monthly Linke Turbidities from hourly direct irradiances ... ");
					estimate_linke_factor_from_hourly_direct_irradiances();
					//for (i=0;i<12;i++)  printf("%.1f ",linke_turbidity_factor_am2[i]);
					//printf(" ");
				}

			rewind(HOURLY_DATA);
			/* added by C. Reinhart                   */
			/* test whether input file has a header   */
			/* in case the is a header, it is skipped */
			fscanf(HOURLY_DATA,"%s", keyword);
			if( !strcmp(keyword,"place") )
				{
					fscanf(HOURLY_DATA,"%*[^\n]");fscanf(HOURLY_DATA,"%*[\n\r]");
					fscanf(HOURLY_DATA,"%*[^\n]");fscanf(HOURLY_DATA,"%*[\n\r]");
					fscanf(HOURLY_DATA,"%*[^\n]");fscanf(HOURLY_DATA,"%*[\n\r]");
					fscanf(HOURLY_DATA,"%*[^\n]");fscanf(HOURLY_DATA,"%*[\n\r]");
					fscanf(HOURLY_DATA,"%*[^\n]");fscanf(HOURLY_DATA,"%*[\n\r]");
					fscanf(HOURLY_DATA,"%*[^\n]");fscanf(HOURLY_DATA,"%*[\n\r]");
				}else{
					rewind(HOURLY_DATA);
				}



			while ( status > 0 )               /*  read data from the input weather file as long as EOF is not reached  */
				{
					if ( input_units_genshortterm == 1 )
						status = fscanf(HOURLY_DATA,"%d %d %lf %lf %lf", &month, &day, &time, &irrad_beam_nor, &irrad_dif);
					if ( input_units_genshortterm == 2 )
						status = fscanf(HOURLY_DATA,"%d %d %lf %lf %lf", &month, &day, &time, &irrad_beam_hor, &irrad_dif);
					if ( status <= 0 )  goto process_last_day;

					nh++;

					jday = jdate(month, day);

					if ( input_units_genshortterm == 1 )         /*  calculation of the global irradiance for the actual hour  */
						{
							if ( irrad_beam_nor > 0 )
								{
									sunrise_sunset_localtime ( latitude, longitude, time_zone, jday, &sunrise_localtime, &sunset_localtime );
									centrum_time=time;
									if ( fabs(time-sunrise_localtime) <= 0.5 )  centrum_time=sunrise_localtime+(time+0.5-sunrise_localtime)/2.0;
									if ( fabs(time-sunset_localtime) <= 0.5 )  centrum_time=time-0.5+(sunset_localtime-(time-0.5))/2.0;
									solar_elev_azi_ecc ( latitude, longitude, time_zone, jday, centrum_time, solar_time, &solar_elevation, &solar_azimuth, &eccentricity_correction);
									irrad_beam_hor = irrad_beam_nor * sin(radians(solar_elevation));
									if ( irrad_beam_hor < 0 )  irrad_beam_hor=0;
								}
							else irrad_beam_hor=0;

							irrad_glo=irrad_beam_hor+irrad_dif;
						}

					if ( input_units_genshortterm == 2 )         /*  calculation of the global irradiance for the actual hour  */
						{
							if ( irrad_beam_hor > 0 )
								{
									sunrise_sunset_localtime ( latitude, longitude, time_zone, jday, &sunrise_localtime, &sunset_localtime );
									centrum_time=time;
									if ( fabs(time-sunrise_localtime) <= 0.5 )  centrum_time=sunrise_localtime+(time+0.5-sunrise_localtime)/2.0;
									if ( fabs(time-sunset_localtime) <= 0.5 )  centrum_time=time-0.5+(sunset_localtime-(time-0.5))/2.0;
									solar_elev_azi_ecc ( latitude, longitude, time_zone, jday, centrum_time, solar_time, &solar_elevation, &solar_azimuth, &eccentricity_correction);
									irrad_beam_nor = irrad_beam_hor / sin(radians(solar_elevation));
									if ( irrad_beam_nor < 0 )  irrad_beam_nor=0;
								}
							else irrad_beam_nor=0;

							irrad_glo=irrad_beam_hor+irrad_dif;
						}


					/*  check irradiances and correct numbers if necessary  */
					if ( irrad_glo < 0 )
						{
							sprintf(errmsg, "irrad_glo=%f at month: %d day: %d time: %.3f has been replaced with 0", irrad_glo, month, day, time);
							error(WARNING, errmsg);
							irrad_glo=0.0;
						}
					if (irrad_glo > SOLAR_CONSTANT_E)
						{
							sprintf(errmsg, "irrad_glo=%f at month: %d day: %d time: %.3f has been replaced with %f\n", irrad_glo, month, day, time, SOLAR_CONSTANT_E);
							error(WARNING, errmsg);
							irrad_glo = SOLAR_CONSTANT_E;
						}

					if ( irrad_beam_nor < 0 )
						{
							sprintf(errmsg, "irrad_beam_nor=%e at month: %d day: %d time: %.3f has been replaced with %f\n", irrad_beam_nor, month, day, time, 0.0);
							error(WARNING, errmsg);
							irrad_beam_nor = 0.0;
						}
					if (irrad_beam_nor > SOLAR_CONSTANT_E)
						{
							sprintf(errmsg, "irrad_beam_nor=%f at month: %d day: %d time: %.3f has been replaced with %f\n", irrad_beam_nor, month, day, time, SOLAR_CONSTANT_E);
							error(WARNING, errmsg);
							irrad_beam_nor = SOLAR_CONSTANT_E;
						}

					if ( irrad_dif < 0 )
						{
							sprintf(errmsg, "irrad_dif=%f at month: %d day: %d time: %.3f has been replaced with %f\n", irrad_beam_nor, month, day, time, 0.0);
							error(WARNING, errmsg);
							irrad_dif = 0.0;
						}
					if (irrad_dif > SOLAR_CONSTANT_E)
						{
							sprintf(errmsg, "irrad_dif=%f at month: %d day: %d time: %.3f has been replaced with %f\n", irrad_beam_nor, month, day, time, SOLAR_CONSTANT_E);
							error(WARNING, errmsg);
							irrad_dif = SOLAR_CONSTANT_E;
						}

					if ( fatal == 1 )  exit(1);


					if ( last_jday == jday || nh == 1 )      /*  store the hourly irradiances of the actual day  */
						{
							times[nhd]=time;
							irrads_glo[nhd] = irrad_glo;
							irrads_beam_nor[nhd] = irrad_beam_nor;
							irrads_dif[nhd] = irrad_dif;
							nhd++;
						}

					else
						{
						process_last_day:                                     /*  process the last day  */
							{
								for ( i=0 ; i<nhd ; i++ )              /*  determine the daylight status of each hour  */
									{
										if ( i == 0 || i == nhd-1 )
											{
												if ( irrads_glo[i] < 0.001 )  daylight_status[i]=0;
												if ( irrads_glo[i] >= 0.001 )  daylight_status[i]=1;
											}
										if ( i > 0 && i<nhd-1 )
											{
												if ( irrads_glo[i-1] < 0.001 && irrads_glo[i] < 0.001 )  daylight_status[i]=0;
												if ( irrads_glo[i] < 0.001 && irrads_glo[i+1] < 0.001 )  daylight_status[i]=0;
												if ( irrads_glo[i-1] >= 0.001 && irrads_glo[i] < 0.001 && irrads_glo[i+1] >= 0.001 )
												{
													irrads_glo[i]=0.5*(irrads_glo[i-1]+irrads_glo[i+1]);
													sprintf(errmsg, "at %d %d %.3f global irradiance = 0 in between two hours with\n non-vanishing global irradiance: check your data and try again", last_month, last_day, times[i]);
													error(WARNING, errmsg);
												}

												if ( irrads_glo[i-1] < 0.001 && irrads_glo[i] >= 0.001 && irrads_glo[i+1] < 0.001 )
												{
													irrads_glo[i]=0.5*(irrads_glo[i-1]+irrads_glo[i+1]);
													sprintf(errmsg, "month=%d day=%d contains only one hour with non-vanishing global irradiance: remove this day from your input data file and try again", last_month, last_day);
													error(WARNING, errmsg);
												}
												if ( irrads_glo[i-1] < 0.001 && irrads_glo[i] >= 0.001 && irrads_glo[i+1] >= 0.001 )  daylight_status[i]=1;
												if ( irrads_glo[i-1] >= 0.001 && irrads_glo[i] >= 0.001 && irrads_glo[i+1] < 0.001 )  daylight_status[i]=1;
												if ( irrads_glo[i-1] >= 0.001 && irrads_glo[i] >= 0.001 && irrads_glo[i+1] >= 0.001 )  daylight_status[i]=2;
											}

										if ( daylight_status[i] > 0 )        /*  calculate the clearness indices  */
											{
												glo_and_beam_indices_hour ( latitude, longitude, time_zone, last_jday, times[i], solar_time, irrads_glo[i], irrads_beam_nor[i], &indices_glo[i], &indices_beam[i] );
												if ( i < nhd-1 && times[i+1]-times[i] > 1.5 )
												{
													sprintf(errmsg, "at %d %d %.3f the time difference to the subsequent hour is greater than 1.5: check your data and try again (innerday time differences should equal 1)", last_month, last_day, times[i]);
													error(USER, errmsg);
												}
											}
									}

								for ( i=0 ; i<nhd ; i++ )                /*  process each hour  */
									{
										if ( daylight_status[i] == 0 )         /*  print zeros for hours without global irradiance  */
											{
												for ( j=1 ; j<=(60/shortterm_timestep) ; j++ )
													{
														time_t = times[i] - 0.5 + ( j - 0.5 ) / (60/shortterm_timestep);
														if ( output_units_genshortterm == 1 )
															fprintf ( SHORT_TERM_DATA,"%d %d %.3f %.0f %.0f\n", last_month, last_day, time_t, 0.0, 0.0 );
														if ( output_units_genshortterm == 2 )
															{
																jday_hoy = jdate(last_month, last_day);
																solar_elev_azi_ecc (latitude, longitude, time_zone, jday_hoy, time_t, solar_time, &solar_elevation, &solar_azimuth, &eccentricity_correction);

																fprintf ( SHORT_TERM_DATA,"%d %d %.3f %.0f %.0f\n", last_month, last_day, time_t, 0.0, 0.0 );
															}
													}
											}

										else                                  /*  generate short-term irradiances for daylight hours  */
											{
												irrads_clear_st ( latitude, longitude, time_zone, last_jday, times[i], solar_time, sph, irrads_glo_clear_st);

												if ( daylight_status[i] == 1 && times[i] < 12 )             /*  sunrise hour  */
													{
														sr_ss_indices_glo[0] = indices_glo[i+1];
														sr_ss_indices_glo[1] = indices_glo[i];
														sr_ss_indices_glo[2] = indices_glo[i+1];
														skartveit ( sr_ss_indices_glo, indices_beam[i], sph, previous_ligoh, indices_glo_st, &actual_ligoh );
													}

												if ( daylight_status[i] == 1 && times[i] >= 12 )            /*  sunset hour  */
													{
														sr_ss_indices_glo[0] = indices_glo[i-1];
														sr_ss_indices_glo[1] = indices_glo[i];
														sr_ss_indices_glo[2] = indices_glo[i-1];
														skartveit ( sr_ss_indices_glo, indices_beam[i], sph, previous_ligoh, indices_glo_st, &actual_ligoh );
													}

												if ( daylight_status[i] == 2 )                             /*  innerday hours  */
													{
														if ( irrads_glo[i] <= 0.001 )
															{
																irrads_glo[i]=0.5*(irrads_glo[i-1]+irrads_glo[i+1]);
																sprintf(errmsg, "at month=%d day=%d time=%.3f should be non-vanishing global irradiance: check your input file and try again", last_month, last_day, times[i]);
																error(USER, errmsg);
															}
														else  skartveit ( &indices_glo[i-1], indices_beam[i], sph, previous_ligoh, indices_glo_st, &actual_ligoh );
													}

												previous_ligoh = actual_ligoh;

												for ( j=1 ; j<=sph ; j++ )  irrads_glo_st[j-1] = indices_glo_st[j-1] * irrads_glo_clear_st[j-1];

												mean_glo_st = mean ( sph, &irrads_glo_st[0] );

												if ( mean_glo_st > 0 )                /*  global renormalization to the given hourly mean value  */
													for ( j=1 ; j<=sph ; j++ )
														irrads_glo_st[j-1] = irrads_glo[i] / mean_glo_st * irrads_glo_st[j-1];

												for (j = 1; j <= sph; j++)  if (irrads_glo_st[j - 1] > SOLAR_CONSTANT_E)  irrads_glo_st[j - 1] = SOLAR_CONSTANT_E;

												for ( j=1 ; j<=sph ; j++ )        /*  Reindl diffuse fraction estimation  */
													{
														solar_elev_azi_ecc (latitude, longitude, time_zone, last_jday, times[i]-0.5+(j-0.5)/sph, solar_time, &solar_elevation, &solar_azimuth, &eccentricity_correction);
														irrads_dif_st[j-1]= diffuse_fraction(irrads_glo_st[j-1],solar_elevation,eccentricity_correction)*irrads_glo_st[j-1];

														if ( solar_azimuth < 0 )  azimuth_class = ((int)solar_azimuth)/10 + 17;
														else   azimuth_class = ((int)solar_azimuth)/10 + 18;

														if ( solar_elevation > horizon_azimuth_out[azimuth_class] )
															irrads_beam_nor_st[j - 1] = (irrads_glo_st[j - 1] - irrads_dif_st[j - 1]) / sin(radians(solar_elevation));
														else
															{
																irrads_beam_nor_st[j-1]=0;
																irrads_dif_st[j-1]=irrads_glo_st[j-1];
															}

														if (irrads_beam_nor_st[j - 1] > SOLAR_CONSTANT_E)  irrads_beam_nor_st[j - 1] = SOLAR_CONSTANT_E;
													}

												mean_beam_st = mean ( sph, &irrads_beam_nor_st[0] );
												mean_dif_st = mean ( sph, &irrads_dif_st[0] );

												if ( mean_beam_st > 0 )        /*  beam renormalization to the given hourly mean value  */
													for ( j=1 ; j<=sph ; j++ )
														irrads_beam_nor_st[j-1] = irrads_beam_nor[i] / mean_beam_st * irrads_beam_nor_st[j-1];


												if ( daylight_status[i] == 1 ) { /*  Tito  */
													k=0;
													for ( j=1 ; j<=sph ; j++ ){
														if( irrads_dif_st[j-1]>0.01){ k++;}
													}
													if( (k+30) < 60 )
														irrads_dif[i]*=(k*1.0/(k+30.0));
												}

												if ( mean_dif_st > 0 )         /*  diffuse renormalization to the given hourly mean value  */
													for ( j=1 ; j<=sph ; j++ )
														irrads_dif_st[j-1] = irrads_dif[i] / mean_dif_st * irrads_dif_st[j-1];

												for ( j=1 ; j<=sph ; j++ )
													if (irrads_beam_nor_st[j - 1] > SOLAR_CONSTANT_E)  irrads_beam_nor_st[j - 1] = SOLAR_CONSTANT_E;

												for ( j=1 ; j<=(60/shortterm_timestep) ; j++ )
													{
														time_t = times[i] - 0.5 + ( j - 0.5 ) / (60/shortterm_timestep);
														if ( shortterm_timestep == 1 )
															{
																if ( output_units_genshortterm == 1 )
																	fprintf ( SHORT_TERM_DATA,"%d %d %.3f %.0f %.0f\n", last_month, last_day, time_t, irrads_beam_nor_st[j-1], irrads_dif_st[j-1] );
																if ( output_units_genshortterm == 2 )
																	{
																		solar_elev_azi_ecc (latitude, longitude, time_zone, last_jday, time_t, solar_time,&solar_elevation, &solar_azimuth, &eccentricity_correction);
																		/*if ( solar_elevation < 0 )  solar_elevation=0;*/
																		punk = solar_elevation;
																		if ( solar_elevation < 0 )  punk=0;
																		fprintf(SHORT_TERM_DATA, "%.3f %.0f %.0f %.3f %.3f\n", (last_jday - 1) * 24 + time_t, irrads_beam_nor_st[j - 1] * sin(radians(punk)), irrads_dif_st[j - 1], solar_elevation, solar_azimuth);
																	}
															}
														else
															{
																if ( output_units_genshortterm == 1 )
																	{
																		sum_beam_nor=0;
																		sum_dif=0;
																		for ( k=(j-1)*shortterm_timestep ; k<j*shortterm_timestep ; k++ )
																			{
																				sum_beam_nor+=irrads_beam_nor_st[k];
																				sum_dif+=irrads_dif_st[k];
																			}
																		fprintf ( SHORT_TERM_DATA,"%d %d %.3f %.0f %.0f\n",last_month, last_day, time_t, sum_beam_nor/shortterm_timestep, sum_dif/shortterm_timestep );
																	}

																if ( output_units_genshortterm == 2 )
																	{
																		sum_beam_hor=0;
																		sum_dif=0;
																		for ( k=(j-1)*shortterm_timestep ; k<j*shortterm_timestep ; k++ )
																			{
																				time_k = times[i] - 0.5 + ( k + 0.5 ) / 60;
																				solar_elev_azi_ecc (latitude, longitude, time_zone, last_jday, time_k, solar_time,&solar_elevation, &solar_azimuth, &eccentricity_correction);
																				if ( solar_elevation < 0 )  solar_elevation=0;
																				sum_beam_hor += irrads_beam_nor_st[k] * sin(radians(solar_elevation));
																				sum_dif+=irrads_dif_st[k];
																			}
																		fprintf ( SHORT_TERM_DATA,"%.3f %.0f %.0f\n", (last_jday-1)*24+time_t, sum_beam_hor/shortterm_timestep, sum_dif/shortterm_timestep );
																	}
															}
													}
											}
									}

								if ( status <= 0 )  goto end;

								times[0]=time;
								irrads_glo[0] = irrad_glo;
								irrads_beam_nor[0] = irrad_beam_nor;
								irrads_dif[0] = irrad_dif;
								nhd=1;
							}
						}

					last_day=day;
					last_month=month;
					last_jday=jday;
				}
		}

 end:
	{
		close_file(HOURLY_DATA);
		close_file(SHORT_TERM_DATA);
	}
	if (day!=31 || month !=12)
	{
		printf("WARNING - Incomplete input climate file (%s)! The file ends on month %d and day %d.\n",input_weather_data,month,day);
		printf("Please review the output file before proceedingto Step3 using SITE>>OPEN CLIMATE FILE IN TEXT EDITOR.\n");
	} else
	{
			printf("DS_SHORTTERM - A climate file with a time step of %d minutes has been generated under %s.\n\n",time_step,input_weather_data_shortterm);

	}
	return 0;
memerr:
	error(SYSTEM, "out of memory in function main");
}
Beispiel #15
0
void ModalityConvex::update(Image& image, PatchSet* patchSet, Rect bounds)
{

  Ptr<PatchSet> patches = reliablePatchesFilter.empty() ? patchSet : patchSet->filter(*reliablePatchesFilter);

  if (patches->size() < 3)
    {
      //flush();
      return;
    }

  Mat temp = image.get_float_mask();

  temp.setTo(0);

  if (history.empty())
    {
      history.create(image.height(), image.width(), CV_32F);
      history.setTo(0);
    }

  Point2f * points = new Point2f[patches->size()];
  Point2f * hull = NULL;

  Point2f offset = Point2f(image.width()/2, image.height() /2) - patchSet->mean_position();

  Point2f mean(0, 0);

  for (int i = 0; i < patches->size(); i++)
    {
      points[i] = patches->get_position(i) + offset;
    }

  int size = convex_hull(points, patches->size(), &hull);

  for (int i = 0; i < size; i++)
    {
      mean.x += hull[i].x;
      mean.y += hull[i].y;
    }

  mean.x /= size;
  mean.y /= size;

  Point * hulli = new Point[size];
  Point * hullei = new Point[size];

  for (int i = 0; i < size; i++)
    {
      hulli[i].x = (int) hull[i].x;
      hulli[i].y = (int) hull[i].y;
    }

  expand(hull, size, mean, margin);

  for (int i = 0; i < size; i++)
    {
      hullei[i].x = (int) hull[i].x;
      hullei[i].y = (int) hull[i].y;
    }

  fillConvexPoly(temp, hullei, size, Scalar(1 - margin_diminish));
  fillConvexPoly(temp, hulli, size, Scalar(1));

  delete [] points;
  free(hull);
  delete [] hulli;
  delete [] hullei;

  history = history * persistence + temp * (1.0f - persistence);

  debugCanvas->draw(history);
  debugCanvas->push();

}
inline
bool
op_princomp::direct_princomp
  (
         Mat< std::complex<typename T1::pod_type> >&     coeff_out,
         Mat< std::complex<typename T1::pod_type> >&     score_out,
  const Base< std::complex<typename T1::pod_type>, T1 >& X,
  const typename arma_cx_only<typename T1::elem_type>::result* junk
  )
  {
  arma_extra_debug_sigprint();
  arma_ignore(junk);
  
  typedef typename T1::pod_type     T;
  typedef          std::complex<T> eT;
  
  const unwrap_check<T1> Y( X.get_ref(), score_out );
  const Mat<eT>& in    = Y.M;
  
  const uword n_rows = in.n_rows;
  const uword n_cols = in.n_cols;
  
  if(n_rows > 1) // more than one sample
    {
    // subtract the mean - use score_out as temporary matrix
    score_out = in - repmat(mean(in), n_rows, 1);
 	  
    // singular value decomposition
    Mat<eT> U;
    Col< T> s;
    
    const bool svd_ok = svd(U,s,coeff_out,score_out);
    
    if(svd_ok == false)
      {
      return false;
      }
    
    // U.reset();
    
    // normalize the eigenvalues
    s /= std::sqrt( double(n_rows - 1) );

    // project the samples to the principals
    score_out *= coeff_out;

    if(n_rows <= n_cols) // number of samples is less than their dimensionality
      {
      score_out.cols(n_rows-1,n_cols-1).zeros();
      }

    }
  else // 0 or 1 samples
    {
    coeff_out.eye(n_cols, n_cols);
    
    score_out.copy_size(in);
    score_out.zeros();
    }
  
  return true;
  }
Beispiel #17
0
int
main(int argc, char **argv)
{
    double starttime, endtime;
    int ntasks, myrank;
    char name[128];                      
    int namelen;
    MPI_Status status;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
    MPI_Comm_size(MPI_COMM_WORLD, &ntasks);

    MPI_Get_processor_name(name,&namelen);

    /* Get a few OpenMP parameters.                                               */
    int O_P  = omp_get_num_procs();          /* get number of OpenMP processors       */
    int O_T  = omp_get_num_threads();        /* get number of OpenMP threads          */
    int O_ID = omp_get_thread_num();         /* get OpenMP thread ID                  */
    //printf("name:%s   M_ID:%d  O_ID:%d  O_P:%d  O_T:%d\n", name,myrank,O_ID,O_P,O_T);

    FILE *f;
    char line[LINE_SIZE];
    int numlines = 0;

    exprinfo *exprannot = NULL;
    char **glines = NULL;

    f = fopen("gene_list.txt", "r");
    while(fgets(line, LINE_SIZE, f)) {
        glines = (char**)realloc(glines, sizeof(char*)*(numlines+1));
        glines[numlines] = strdup(line);

        char *pch = strtok (line,",");
        char * gene = pch;

        pch = strtok (NULL, ",");
        int chr = atoi(trimwhitespace(pch));

        exprannot = (exprinfo*)realloc(exprannot,sizeof(exprinfo)*(numlines+1));

        exprannot[numlines].gene      = strdup(gene);
        exprannot[numlines].chr       = chr;

        if (!exprannot) printf("not allcoated\n");
        numlines++;
    }
    fclose(f);
    f = fopen("probe_id_mapping.txt", "r");
    numlines = 0;

    free(glines[0]);
    free(glines);

    probeinfo *records = NULL;
    char **lines = NULL;

    while(fgets(line, LINE_SIZE, f)) {              
        lines = (char**)realloc(lines, sizeof(char*)*(numlines+1));
        lines[numlines] = strdup(line);

        char *pch = strtok (line,",");
        int probeid = atoi(pch);

        pch = strtok (NULL, ",");
        char * gene = pch;

        pch = strtok (NULL, ",");
        int chr = atoi(trimwhitespace(pch));

        records = (probeinfo*)realloc(records,sizeof(probeinfo)*(numlines+1));

        records[numlines].probeid   = probeid;
        records[numlines].chr       = chr;
        records[numlines].gene      = strdup(gene);
        if (!records) printf("not allcoated\n");
        numlines++;

    }
    free(lines[0]);
    free(lines);

    fclose(f);


    int NUM_GENES = numlines;
    unsigned long x_nr, x_nc, y_nr, y_nc;

    double **X = h5_read("x.h5", 1, "/X",         &x_nr, &x_nc);
    double **Y = h5_read("filtered_probes.h5", 1, "/FilteredProbes", &y_nr, &y_nc);

    //printf("loaded X, num rows = %d, num cols = %d\n", x_nr, x_nc);
    //printf("loaded Y, num rows = %d, num cols = %d\n", y_nr, y_nc);

    unsigned long total_mem = (x_nr * y_nc);
    double **RHO   = create2dArray(x_nr, y_nc);
    
    int gene, probe, tid, work_completed;
    work_completed = 0;

    
    int BLOCK_SIZE = NUM_ROWS/ntasks;
    int offset = myrank*BLOCK_SIZE;
    int STOP_IDX = offset+BLOCK_SIZE;
    if (NUM_ROWS - STOP_IDX < BLOCK_SIZE)
        STOP_IDX = NUM_ROWS;

   // printf("offset = %d, for rank %d, with ntasks = %d, and BLOCK_SIZE = %d, STOP_IDX = %d\n", 
    //            offset, myrank, ntasks, BLOCK_SIZE, STOP_IDX);

    int num_sig_found = 0; 
    starttime = MPI_Wtime();

    #pragma omp parallel \
     for shared(X, Y, RHO, BLOCK_SIZE, offset, work_completed) \
     private(probe,gene, tid)
    for (gene = offset; gene < STOP_IDX; gene++) {
       for (probe = 0; probe < NUM_COLS; probe++) {
           double *x = getrowvec(X, gene, BUFFER_SIZE);
           double *y = getcolvec(Y, probe, BUFFER_SIZE);

           double avgx = mean(x, BUFFER_SIZE);
           double * xcentered = sub(x, avgx, BUFFER_SIZE);
                    
           double avgy = mean(y, BUFFER_SIZE);
           double * ycentered = sub(y, avgy, BUFFER_SIZE);
           
           double * prod_result = prod(xcentered, ycentered, BUFFER_SIZE);
           double sum_prod = sum(prod_result, BUFFER_SIZE);

           double stdX = stddev(x, avgx, BUFFER_SIZE);
           double stdY = stddev(y, avgy, BUFFER_SIZE);
           double rho  = sum_prod/((BUFFER_SIZE-1)*(stdX*stdY));

           RHO[gene][probe] = rho;
           if (work_completed % 10000 == 0) {
               tid = omp_get_thread_num();
               printf("rank = %d, work = %d, result[%d,%d] from %d = %f\n", myrank, work_completed,
                    gene, probe, tid, rho);
           }
           work_completed++;
           free(x);
           free(y);
           free(xcentered);
           free(ycentered);
           free(prod_result);

       }
    }
    //printf("********* %d FINISHED **********\n", myrank);

    //f = fopen("significant.txt", "a");
    #pragma omp parallel for shared(RHO,exprannot, records)
    for (int i = 0; i < NUM_ROWS; i++) {
        for (int j = 0; j < NUM_COLS; j++) {
            double zscore = ztest(RHO[i][j],BUFFER_SIZE);
            /*
            if (zscore > 5.0) {

                fprintf(f, "%d,%d,%f,%f,%d,%s,%d,%s\n", 
                        i, j, zscore, RHO[i][j], records[j].chr, records[j].gene, exprannot[i].chr,exprannot[i].gene);
                if (i*j%1000 == 0)
                    printf("%d,%d,%f,%f,%d,%s,%d,%s\n", 
                        i, j, zscore, RHO[i][j], records[j].chr, records[j].gene, exprannot[i].chr,exprannot[i].gene);
            }
            */
        }
    }
    //fclose(f);
    endtime   = MPI_Wtime();
    free(X[0]);
    free(X);
    free(Y[0]);
    free(Y);
    printf("rank %d - elapse time - %f\n",myrank, endtime-starttime);
    //for (int i = 0; i < NUM_ROWS; i++) {
        //printf("%s,%d\n", exprannot[i].gene, exprannot[i].chr);
    //}
    //printf("rank %d FINISHED\n",myrank);
    //h5_write(RHO, NUM_ROWS, NUM_COLS, "rho_omp.h5", "/rho");
    free(RHO[0]);
    free(exprannot);
    free(records);
    free(RHO);

  MPI_Finalize();
  return 0;
}
Beispiel #18
0
void matchTemplate( const Mat& _img, const Mat& _templ, Mat& result, int method )
{
    CV_Assert( CV_TM_SQDIFF <= method && method <= CV_TM_CCOEFF_NORMED );
    
    int numType = method == CV_TM_CCORR || method == CV_TM_CCORR_NORMED ? 0 :
                  method == CV_TM_CCOEFF || method == CV_TM_CCOEFF_NORMED ? 1 : 2;
    bool isNormed = method == CV_TM_CCORR_NORMED ||
                    method == CV_TM_SQDIFF_NORMED ||
                    method == CV_TM_CCOEFF_NORMED;

    Mat img = _img, templ = _templ;
    if( img.rows < templ.rows || img.cols < templ.cols )
        std::swap(img, templ);
    
    CV_Assert( (img.depth() == CV_8U || img.depth() == CV_32F) &&
               img.type() == templ.type() );

    int cn = img.channels();
    crossCorr( img, templ, result,
               Size(img.cols - templ.cols + 1, img.rows - templ.rows + 1),
               CV_32F, Point(0,0), 0, 0);

    if( method == CV_TM_CCORR )
        return;

    double invArea = 1./((double)templ.rows * templ.cols);

    Mat sum, sqsum;
    Scalar templMean, templSdv;
    double *q0 = 0, *q1 = 0, *q2 = 0, *q3 = 0;
    double templNorm = 0, templSum2 = 0;
    
    if( method == CV_TM_CCOEFF )
    {
        integral(img, sum, CV_64F);
        templMean = mean(templ);
    }
    else
    {
        integral(img, sum, sqsum, CV_64F);
        meanStdDev( templ, templMean, templSdv );

        templNorm = CV_SQR(templSdv[0]) + CV_SQR(templSdv[1]) +
                    CV_SQR(templSdv[2]) + CV_SQR(templSdv[3]);

        if( templNorm < DBL_EPSILON && method == CV_TM_CCOEFF_NORMED )
        {
            result = Scalar::all(1);
            return;
        }
        
        templSum2 = templNorm +
                     CV_SQR(templMean[0]) + CV_SQR(templMean[1]) +
                     CV_SQR(templMean[2]) + CV_SQR(templMean[3]);

        if( numType != 1 )
        {
            templMean = Scalar::all(0);
            templNorm = templSum2;
        }
        
        templSum2 /= invArea;
        templNorm = sqrt(templNorm);
        templNorm /= sqrt(invArea); // care of accuracy here

        q0 = (double*)sqsum.data;
        q1 = q0 + templ.cols*cn;
        q2 = (double*)(sqsum.data + templ.rows*sqsum.step);
        q3 = q2 + templ.cols*cn;
    }

    double* p0 = (double*)sum.data;
    double* p1 = p0 + templ.cols*cn;
    double* p2 = (double*)(sum.data + templ.rows*sum.step);
    double* p3 = p2 + templ.cols*cn;

    int sumstep = sum.data ? (int)(sum.step / sizeof(double)) : 0;
    int sqstep = sqsum.data ? (int)(sqsum.step / sizeof(double)) : 0;

    int i, j, k;
    
    for( i = 0; i < result.rows; i++ )
    {
        float* rrow = (float*)(result.data + i*result.step);
        int idx = i * sumstep;
        int idx2 = i * sqstep;

        for( j = 0; j < result.cols; j++, idx += cn, idx2 += cn )
        {
            double num = rrow[j], t;
            double wndMean2 = 0, wndSum2 = 0;
            
            if( numType == 1 )
            {
                for( k = 0; k < cn; k++ )
                {
                    t = p0[idx+k] - p1[idx+k] - p2[idx+k] + p3[idx+k];
                    wndMean2 += CV_SQR(t);
                    num -= t*templMean[k];
                }

                wndMean2 *= invArea;
            }

            if( isNormed || numType == 2 )
            {
                for( k = 0; k < cn; k++ )
                {
                    t = q0[idx2+k] - q1[idx2+k] - q2[idx2+k] + q3[idx2+k];
                    wndSum2 += t;
                }

                if( numType == 2 )
                    num = wndSum2 - 2*num + templSum2;
            }

            if( isNormed )
            {
                t = sqrt(MAX(wndSum2 - wndMean2,0))*templNorm;
                if( fabs(num) < t )
                    num /= t;
                else if( fabs(num) < t*1.125 )
                    num = num > 0 ? 1 : -1;
                else
                    num = method != CV_TM_SQDIFF_NORMED ? 0 : 1;
            }

            rrow[j] = (float)num;
        }
    }
}
Beispiel #19
0
 result_type result(Args const &args) const
 {
     extractor<MeanFeature> mean;
     result_type tmp = mean(args);
     return accumulators::moment<2>(args) - tmp * tmp;
 }
Beispiel #20
0
int main(int argc, char ** argv) {

	// Keep track of the start time of the program
  long long program_start_time = get_time();
	
	// Let the user specify the number of frames to process
	int num_frames = 1;
	
	if (argc !=3){
		fprintf(stderr, "usage: %s <num of frames> <input file>", argv[0]);
		exit(1);
	}
	
	if (argc > 1){
		num_frames = atoi(argv[1]);
		}

	// Open video file
	char *video_file_name;
	video_file_name = argv[3];
	
	avi_t *cell_file = AVI_open_input_file(video_file_name, 1);
	if (cell_file == NULL)	{
		AVI_print_error("Error with AVI_open_input_file");
		return -1;
	}
	
	int i, j, *crow, *ccol, pair_counter = 0, x_result_len = 0, Iter = 20, ns = 4, k_count = 0, n;
	MAT *cellx, *celly, *A;
	double *GICOV_spots, *t, *G, *x_result, *y_result, *V, *QAX_CENTERS, *QAY_CENTERS;
	double threshold = 1.8, radius = 10.0, delta = 3.0, dt = 0.01, b = 5.0;
	
	// Extract a cropped version of the first frame from the video file
	MAT *image_chopped = get_frame(cell_file, 0, 1, 0);
	printf("Detecting cells in frame 0\n");
	
	// Get gradient matrices in x and y directions
	MAT *grad_x = gradient_x(image_chopped);
	MAT *grad_y = gradient_y(image_chopped);
	
	m_free(image_chopped);
	
	// Get GICOV matrices corresponding to image gradients
	long long GICOV_start_time = get_time();
	MAT *gicov = ellipsematching(grad_x, grad_y);
	
	// Square GICOV values
	MAT *max_gicov = m_get(gicov->m, gicov->n);
	for (i = 0; i < gicov->m; i++) {
		for (j = 0; j < gicov->n; j++) {
			double val = m_get_val(gicov, i, j);
			m_set_val(max_gicov, i, j, val * val);
		}
	}
	
	long long GICOV_end_time = get_time();
	
	// Dilate the GICOV matrix
	long long dilate_start_time = get_time();
	MAT *strel = structuring_element(12);
	MAT *img_dilated = dilate_f(max_gicov, strel);
	long long dilate_end_time = get_time();
	
	// Find possible matches for cell centers based on GICOV and record the rows/columns in which they are found
	pair_counter = 0;
	crow = (int *) malloc(max_gicov->m * max_gicov->n * sizeof(int));
	ccol = (int *) malloc(max_gicov->m * max_gicov->n * sizeof(int));
	for (i = 0; i < max_gicov->m; i++) {
		for (j = 0; j < max_gicov->n; j++) {
			if (!(m_get_val(max_gicov,i,j) == 0.0) && (m_get_val(img_dilated,i,j) == m_get_val(max_gicov,i,j))) {
				crow[pair_counter] = i;
				ccol[pair_counter] = j;
				pair_counter++;
			}
		}
	}
	
	GICOV_spots = (double *) malloc(sizeof(double)*pair_counter);
	for (i = 0; i < pair_counter; i++)
		GICOV_spots[i] = m_get_val(gicov, crow[i], ccol[i]);
	
	G = (double *) calloc(pair_counter, sizeof(double));
	x_result = (double *) calloc(pair_counter, sizeof(double));
	y_result = (double *) calloc(pair_counter, sizeof(double));
	
	x_result_len = 0;
	for (i = 0; i < pair_counter; i++) {
		if ((crow[i] > 29) && (crow[i] < BOTTOM - TOP + 39)) {
			x_result[x_result_len] = ccol[i];
			y_result[x_result_len] = crow[i] - 40;
			G[x_result_len] = GICOV_spots[i];
			x_result_len++;
		}
	}
	
	// Make an array t which holds each "time step" for the possible cells
	t = (double *) malloc(sizeof(double) * 36);
	for (i = 0; i < 36; i++) {
		t[i] = (double)i * 2.0 * PI / 36.0;
	}
	
	// Store cell boundaries (as simple circles) for all cells
	cellx = m_get(x_result_len, 36);
	celly = m_get(x_result_len, 36);
	for(i = 0; i < x_result_len; i++) {
		for(j = 0; j < 36; j++) {
			m_set_val(cellx, i, j, x_result[i] + radius * cos(t[j]));
			m_set_val(celly, i, j, y_result[i] + radius * sin(t[j]));
		}
	}
	
	A = TMatrix(9,4);

	
	V = (double *) malloc(sizeof(double) * pair_counter);
	QAX_CENTERS = (double * )malloc(sizeof(double) * pair_counter);
	QAY_CENTERS = (double *) malloc(sizeof(double) * pair_counter);
	memset(V, 0, sizeof(double) * pair_counter);
	memset(QAX_CENTERS, 0, sizeof(double) * pair_counter);
	memset(QAY_CENTERS, 0, sizeof(double) * pair_counter);

	// For all possible results, find the ones that are feasibly leukocytes and store their centers
	k_count = 0;
	for (n = 0; n < x_result_len; n++) {
		if ((G[n] < -1 * threshold) || G[n] > threshold) {
			MAT * x, *y;
			VEC * x_row, * y_row;
			x = m_get(1, 36);
			y = m_get(1, 36);

			x_row = v_get(36);
			y_row = v_get(36);

			// Get current values of possible cells from cellx/celly matrices
			x_row = get_row(cellx, n, x_row);
			y_row = get_row(celly, n, y_row);
			uniformseg(x_row, y_row, x, y);

			// Make sure that the possible leukocytes are not too close to the edge of the frame
			if ((m_min(x) > b) && (m_min(y) > b) && (m_max(x) < cell_file->width - b) && (m_max(y) < cell_file->height - b)) {
				MAT * Cx, * Cy, *Cy_temp, * Ix1, * Iy1;
				VEC  *Xs, *Ys, *W, *Nx, *Ny, *X, *Y;
				Cx = m_get(1, 36);
				Cy = m_get(1, 36);
				Cx = mmtr_mlt(A, x, Cx);
				Cy = mmtr_mlt(A, y, Cy);
				
				Cy_temp = m_get(Cy->m, Cy->n);
				
				for (i = 0; i < 9; i++)
					m_set_val(Cy, i, 0, m_get_val(Cy, i, 0) + 40.0);
					
				// Iteratively refine the snake/spline
				for (i = 0; i < Iter; i++) {
					int typeofcell;
					
					if(G[n] > 0.0) typeofcell = 0;
					else typeofcell = 1;
					
					splineenergyform01(Cx, Cy, grad_x, grad_y, ns, delta, 2.0 * dt, typeofcell);
				}
				
				X = getsampling(Cx, ns);
				for (i = 0; i < Cy->m; i++)
					m_set_val(Cy_temp, i, 0, m_get_val(Cy, i, 0) - 40.0);
				Y = getsampling(Cy_temp, ns);
				
				Ix1 = linear_interp2(grad_x, X, Y);
				Iy1 = linear_interp2(grad_x, X, Y);
				Xs = getfdriv(Cx, ns);
				Ys = getfdriv(Cy, ns);
				
				Nx = v_get(Ys->dim);
				for (i = 0; i < Ys->dim; i++)
					v_set_val(Nx, i, v_get_val(Ys, i) / sqrt(v_get_val(Xs, i)*v_get_val(Xs, i) + v_get_val(Ys, i)*v_get_val(Ys, i)));
					
				Ny = v_get(Xs->dim);
				for (i = 0; i < Xs->dim; i++)
					v_set_val(Ny, i, -1.0 * v_get_val(Xs, i) / sqrt(v_get_val(Xs, i)*v_get_val(Xs, i) + v_get_val(Ys, i)*v_get_val(Ys, i)));
					
				W = v_get(Nx->dim);
				for (i = 0; i < Nx->dim; i++)
					v_set_val(W, i, m_get_val(Ix1, 0, i) * v_get_val(Nx, i) + m_get_val(Iy1, 0, i) * v_get_val(Ny, i));
					
				V[n] = mean(W) / std_dev(W);
				
				//get means of X and Y values for all "snaxels" of the spline contour, thus finding the cell centers
				QAX_CENTERS[k_count] = mean(X);
				QAY_CENTERS[k_count] = mean(Y) + TOP;
				
				k_count++;
				
				// Free memory
				v_free(W);
				v_free(Ny);
				v_free(Nx);
				v_free(Ys);
				v_free(Xs);
				m_free(Iy1);
				m_free(Ix1);
				v_free(Y);
				v_free(X);
				m_free(Cy_temp);
				m_free(Cy);
				m_free(Cx);				
			}
			
			// Free memory
			v_free(y_row);
			v_free(x_row);
			m_free(y);
			m_free(x);
		}
	}

	// Free memory
	free(V);
	free(ccol);
	free(crow);
	free(GICOV_spots);
	free(t);
	free(G);
	free(x_result);
	free(y_result);
	m_free(A);
	m_free(celly);
	m_free(cellx);
	m_free(img_dilated);
	m_free(max_gicov);
	m_free(gicov);
	m_free(grad_y);
	m_free(grad_x);
	
	// Report the total number of cells detected
	printf("Cells detected: %d\n\n", k_count);
	
	// Report the breakdown of the detection runtime
	printf("Detection runtime\n");
	printf("-----------------\n");
	printf("GICOV computation: %.5f seconds\n", ((float) (GICOV_end_time - GICOV_start_time)) / (1000*1000));
	printf("   GICOV dilation: %.5f seconds\n", ((float) (dilate_end_time - dilate_start_time)) / (1000*1000));
	printf("            Total: %.5f seconds\n", ((float) (get_time() - program_start_time)) / (1000*1000));
	
	// Now that the cells have been detected in the first frame,
	//  track the ellipses through subsequent frames
	if (num_frames > 1) printf("\nTracking cells across %d frames\n", num_frames);
	else                printf("\nTracking cells across 1 frame\n");
	long long tracking_start_time = get_time();
	int num_snaxels = 20;
	ellipsetrack(cell_file, QAX_CENTERS, QAY_CENTERS, k_count, radius, num_snaxels, num_frames);
	printf("           Total: %.5f seconds\n", ((float) (get_time() - tracking_start_time)) / (float) (1000*1000*num_frames));
	
	// Report total program execution time
    printf("\nTotal application run time: %.5f seconds\n", ((float) (get_time() - program_start_time)) / (1000*1000));

	return 0;
}
Beispiel #21
0
 /**
  * \return a Gaussian sample of the type \c Variate determined by mapping a
  * noise sample into the Gaussian sample space
  *
  * \param sample    Noise Sample
  *
  * \throws see square_root()
  */
 Variate map_standard_normal(const StandardVariate& sample) const override
 {
     return mean() + square_root() * sample;
 }
Beispiel #22
0
 float variance () const {
   // VX = E(X^2) - EX ^ 2
   float m = mean ();
   return square_sample_sum / sample_count - m * m;
 }
Beispiel #23
0
int TestVecPackDouble(int n, int stride,
                      double *avgTimeUser, double *avgTimeMPI, double *dest, const double *src)
{
    double *restrict d_dest;
    const double *restrict d_src;
    register int i, j;
    int rep, position;
    double t1, t2, t[NTRIALS];
    MPI_Datatype vectype;

    /* User code */
    if (verbose)
        printf("TestVecPackDouble (USER): ");
    for (j = 0; j < NTRIALS; j++) {
        t1 = MPI_Wtime();
        for (rep = 0; rep < N_REPS; rep++) {
            i = n;
            d_dest = dest;
            d_src = src;
            while (i--) {
                *d_dest++ = *d_src;
                d_src += stride;
            }
        }
        t2 = MPI_Wtime() - t1;
        t[j] = t2;
        if (verbose)
            printf("%.3f ", t[j]);
    }
    if (verbose)
        printf("[%.3f]\n", noise(t, NTRIALS));
    /* If there is too much noise, discard the test */
    if (noise(t, NTRIALS) > VARIANCE_THRESHOLD) {
        *avgTimeUser = 0;
        *avgTimeMPI = 0;
        if (verbose)
            printf("Too much noise; discarding measurement\n");
        return 0;
    }
    *avgTimeUser = mean(t, NTRIALS) / N_REPS;

    /* MPI Vector code */
    MPI_Type_vector(n, 1, stride, MPI_DOUBLE, &vectype);
    MPI_Type_commit(&vectype);

    if (verbose)
        printf("TestVecPackDouble (MPI): ");
    for (j = 0; j < NTRIALS; j++) {
        t1 = MPI_Wtime();
        for (rep = 0; rep < N_REPS; rep++) {
            position = 0;
            MPI_Pack((void *) src, 1, vectype, dest, n * sizeof(double), &position, MPI_COMM_SELF);
        }
        t2 = MPI_Wtime() - t1;
        t[j] = t2;
        if (verbose)
            printf("%.3f ", t[j]);
    }
    if (verbose)
        printf("[%.3f]\n", noise(t, NTRIALS));
    /* If there is too much noise, discard the test */
    if (noise(t, NTRIALS) > VARIANCE_THRESHOLD) {
        *avgTimeUser = 0;
        *avgTimeMPI = 0;
        if (verbose)
            printf("Too much noise; discarding measurement\n");
    } else {
        *avgTimeMPI = mean(t, NTRIALS) / N_REPS;
    }

    MPI_Type_free(&vectype);

    return 0;
}
Beispiel #24
0
 void UpdateUcb (Player pl, float explore_coeff) {
   ucb = pl.SubjectiveScore (mean());
   ucb += explore_coeff / sqrt (update_count());
 }
Beispiel #25
0
/* Function Definitions */
void features_btaX(const emxArray_real_T *tap, double ft[2])
{
  int i12;
  emxArray_real_T *t;
  int loop_ub;
  double tapdt;
  emxArray_real_T *tapx;
  emxArray_real_T *dx;
  emxArray_boolean_T *i;
  emxArray_int32_T *r12;
  double tapiqr;

  /*  Computes basic tapping test features. */
  /*  Inputs: */
  /*   tap - tapping data vector: tap(:,1) - time points, */
  /*         tap(:,2:3) - X,Y touch screen coordinates */
  /*  */
  /*  (CC BY-SA 3.0) Max Little, 2014 */
  /*  Output feature vector */
  for (i12 = 0; i12 < 2; i12++) {
    ft[i12] = rtNaN;
  }

  /*  Ignore zero-length inputs */
  if (tap->size[0] == 0) {
  } else {
    b_emxInit_real_T(&t, 1);

    /*  Calculate relative time */
    loop_ub = tap->size[0];
    tapdt = tap->data[0];
    i12 = t->size[0];
    t->size[0] = loop_ub;
    emxEnsureCapacity((emxArray__common *)t, i12, (int)sizeof(double));
    for (i12 = 0; i12 < loop_ub; i12++) {
      t->data[i12] = tap->data[i12] - tapdt;
    }

    b_emxInit_real_T(&tapx, 1);

    /*  X,Y offset */
    loop_ub = tap->size[0];
    i12 = tapx->size[0];
    tapx->size[0] = loop_ub;
    emxEnsureCapacity((emxArray__common *)tapx, i12, (int)sizeof(double));
    for (i12 = 0; i12 < loop_ub; i12++) {
      tapx->data[i12] = tap->data[i12 + tap->size[0]];
    }

    tapdt = mean(tapx);
    i12 = tapx->size[0];
    emxEnsureCapacity((emxArray__common *)tapx, i12, (int)sizeof(double));
    loop_ub = tapx->size[0];
    for (i12 = 0; i12 < loop_ub; i12++) {
      tapx->data[i12] -= tapdt;
    }

    b_emxInit_real_T(&dx, 1);
    emxInit_boolean_T(&i, 1);

    /*  Find left/right finger 'depress' events */
    diff(tapx, dx);
    b_abs(dx, tapx);
    i12 = i->size[0];
    i->size[0] = tapx->size[0];
    emxEnsureCapacity((emxArray__common *)i, i12, (int)sizeof(boolean_T));
    loop_ub = tapx->size[0];
    for (i12 = 0; i12 < loop_ub; i12++) {
      i->data[i12] = (tapx->data[i12] > 20.0);
    }

    emxInit_int32_T(&r12, 1);
    eml_li_find(i, r12);
    i12 = dx->size[0];
    dx->size[0] = r12->size[0];
    emxEnsureCapacity((emxArray__common *)dx, i12, (int)sizeof(double));
    loop_ub = r12->size[0];
    emxFree_boolean_T(&i);
    for (i12 = 0; i12 < loop_ub; i12++) {
      dx->data[i12] = t->data[r12->data[i12] - 1];
    }

    emxFree_int32_T(&r12);
    emxFree_real_T(&t);

    /*  Find depress event intervals */
    diff(dx, tapx);

    /*  Median and spread of tapping rate */
    emxFree_real_T(&dx);
    if (tapx->size[0] == 0) {
      tapdt = rtNaN;
    } else {
      tapdt = vectormedian(tapx);
    }

    tapiqr = iqr(tapx);

    /*  Output tapping test feature vector */
    ft[0] = tapdt;
    ft[1] = tapiqr;
    emxFree_real_T(&tapx);
  }
}
Beispiel #26
0
int main(int argc, char *argv[])
{
    // mpi book keeping -----------
    int iproc, i, iter;
    int nproc;
    int number_amount;
    int membershipKey;
    int rank1,rank2,newSize;
    int rank;
    int numdim;
    int pivot;
    char host[255], message[55];
    MPI_Status status;
    
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    
    gethostname(host,253);
    switch(nproc) {
        case 32: numdim = 5;break;
        case 16: numdim = 4;break;
        case 8: numdim = 3;break;
        case 4: numdim = 2;break;
        case 2: numdim = 1;break;
        case 1: numdim = 0;break;
    }
    // -------
    
    // each process has an array of VECSIZE double: ain[VECSIZE]
     int ain[VECSIZE], *buf, *c = (int *) malloc(VECSIZE * sizeof(int));
    int size = VECSIZE;
    int alower[2*VECSIZE], aupper[2*VECSIZE];

    int myrank, root = 0;
    
    /* Intializes random number generator */
    srand(rank+5);
    double start = When();
// Distribute the items evenly to all of the nodes.
        // fill with random numbers
        for(i = 0; i < VECSIZE; i++) {
            ain[i] = (rand()%1000)+1;
            //          printf("init proc %d [%d]=%d\n",myrank,i,ain[i]);
        }
        memcpy(c,ain,sizeof(ain));
        qsort(c, size, sizeof(int), cmpfunc); // Each node sorts the items it has using quicksort.
    
    
    MPI_Comm comm1 = MPI_COMM_WORLD, comm2, intercomm;
    rank2 = rank ;
    int lowerSize = 0 ,upperSize = 0;
    int *sub_avgs = NULL;
    for(int curdim = 0; curdim < numdim; curdim++ ) {

        membershipKey = rank2 % 2; // creates two groups o and 1.
        MPI_Comm_split(comm1, membershipKey, rank2, &comm2);  // Break up the cube into two subcubes:
        MPI_Comm_rank(comm2,&rank2);
        if ( mediansPivot  ){
        //    printf("meadians \n");
            if (rank == 0 ){
                sub_avgs =(int *) malloc(sizeof( int) * nproc);

            }
                pivot = median(size,c);
       //         printf("before gather pivot = %ld\n",pivot);
                MPI_Gather(&pivot, 1, MPI_INT, sub_avgs, 1, MPI_INT, 0, MPI_COMM_WORLD);
                if ( rank == 0){
        //            for(int i=0; i<nproc; i++)
        //                printf("[%d]=%ld ",i,sub_avgs[i]);
        //            printf("Gathered\n");
                    pivot = median(nproc,sub_avgs);
                    free(sub_avgs);
                }
        }
        else if ( rank2 == 0 && (medianPivot || meanPivot || randomPivot)){// Node 0 broadcasts its median key K to the rest of the cube.
            if (meanPivot  ){
     //       printf("mean \n");

                pivot = mean(size,c);
            }
            else if (medianPivot ){
     //       printf("meadian \n");
                pivot = median(size,c);
            }
            else if (randomPivot ){
     //       printf("randomPivot \n");
            int randompiv = rand()%size ;
     //       printf("randomindex %d \n",randompiv );
                pivot = c[randompiv];
      //      printf("Pivot %d \n", pivot);
            }
        }
        MPI_Bcast(&pivot,1,MPI_INT, 0, comm2);
        lowerSize = 0;
        upperSize = 0;
        for(i = 0; i < size; i++) {// Each node separates its items into two arrays : 
            if (c[i] <= pivot){//        keys <= K and 
                alower[lowerSize] = c[i];
          //  printf("lower [%d]=%d\n",i,alower[lowerSize]);
                lowerSize ++;
            }
            else{//        keys > K
                aupper[upperSize] = c[i];
          //  printf("upper [%d]=%d\n",i,aupper[upperSize]);
                upperSize ++;
            }
        }
      //      printf("lowerSize %d\n",lowerSize);
      //      printf("upperSize %d\n",upperSize);


        if (membershipKey == 0 ){ // lower world (left)
        MPI_Intercomm_create(comm2, 0, comm1, 1, 99, &intercomm);
//    Each node in the lower subcube sends its items whose keys are > K to its adjacent node in the upper subcube
            MPI_Send(aupper, upperSize, MPI_INT, rank2, 0, intercomm ); 
         //   printf("upperSize %ld ",upperSize);

         //   printf("worldrank %d localrank %d sending upper\n ",rank, rank2);
        //    for(i = 0; i < (upperSize); i++)
         //       printf("[%d] = %ld ", i,aupper[i]);
         //   printf("to otherrank %d \n ", rank2);
              
              MPI_Probe(rank2, 0, intercomm, &status);
              MPI_Get_count(&status, MPI_INT, &number_amount);
              buf = (int*)malloc(sizeof(int) * number_amount);
              MPI_Recv(buf, number_amount, MPI_INT, rank2, 0, intercomm, &status);
              free(buf);
              //Each node now merges together the group it just received 
            //    with the one it kept so that its items are one again sorted.
            free(c);
            c = ARRAY_CONCAT(int, alower, lowerSize, buf,number_amount);
          //  printf("worldrank %d localrank %d gotsize %d\n",rank, rank2, number_amount);
            size = number_amount+lowerSize;

     /*          for(i = 0; i < (number_amount); i++)
                printf("[%d]=%ld ",i,buf[i]);
            printf("\n ");
            for(i = 0; i < size; i++)
                printf("[%d]=%ld ",i,c[i]);

            printf("\n "); */
        }else{
Beispiel #27
0
void model_estimate(double *x, int N, int d, int pmax, int h) {
	int m, i, t, pq, j;
	double wmean, sos, var, lvar, aic, sc, hq, aic0, sc0, hq0;
	double *inp, *phim, *a;
	int paic, qaic, psc, qsc, phq, qhq;
	/*
	x - Input Time Series of length N
	d - Number of time the series has to be differenced (d = 0 when there are no trends)
	pmax - Maximum AR and MA order pmax = max(p,q)
	h - Order of AR model to be fitted to obtain residuals
	0 <= pmax <= h
	Typically pmax = 3,4 and h = 8. Increase value of h if you want to fit high order ARMA process
	*/

	inp = (double*)malloc(sizeof(double)* (N - d));

	if (d > 0) {
		N = diff(x, N, d, inp); // No need to demean x
	}

	m = h;

	phim = (double*)malloc(sizeof(double)* m);
	a = (double*)malloc(sizeof(double)* N);

	wmean = mean(x, N);

	for (i = 0; i < N; ++i) {
		inp[i] = x[i] - wmean;
	}

	// Estimate AR(m) coefficients

	ywalg(inp, N, m, phim);

	for (i = 0; i < N; ++i) {
		a[i] = 0.0;
	}

	for (t = m; t < N; ++t) {
		a[t] = inp[t];
		for (i = 0; i < m; ++i) {
			a[t] -= phim[i] * inp[t - i - 1];
		}
	}
	aic0 = sc0 = hq0 = 0.0;
	paic = qaic = psc = qsc = phq = qhq = 0;

	for (i = 0; i <= pmax; ++i) {
		for (j = 0; j <= pmax; ++j) {
			pq = i + j;
			if (pq >= 1) {
				hrstep2(inp, N, m, i, j, pq, a, &sos, &var);
				lvar = log(var);
				aic = lvar + 2 * (double)pq / N;
				sc = lvar + log((double)N) * (double)pq / N;
				hq = lvar + 2 * log(log((double)N)) * (double)pq / N;
				if (i == 0 && j == 1) {
					aic0 = aic;
					sc0 = sc;
					hq0 = hq;
					paic = psc = phq = 0;
					qaic = qsc = qhq = 1;
				}
				else {
					if (aic < aic0) {
						aic0 = aic;
						paic = i;
						qaic = j;
					}
					if (sc < sc0) {
						sc0 = sc;
						psc = i;
						qsc = j;
					}
					if (hq < hq0) {
						hq0 = hq;
						phq = i;
						qhq = j;
					}
				}
				printf("\n p = %d q = %d AIC = %g SC = %g HQ = %g \n", i, j, aic, sc, hq);
			}
		}
	}

	printf("\n\n");
	printf("AIC Estimate : p = %d q = %d \n", paic, qaic);
	printf("SC Estimate  : p = %d q = %d \n", psc, qsc);
	printf("HQ Estimate  : p = %d q = %d \n", phq, qhq);

	free(inp);
	free(phim);
	free(a);
}
SOrientedBoundingBox *
SOrientedBoundingBox::buildOBB(std::vector<SPoint3> &vertices)
{
#if defined(HAVE_MESH)

  int num_vertices = vertices.size();
  // First organize the data

  std::set<SPoint3> unique;
  unique.insert(vertices.begin(), vertices.end());

  num_vertices = unique.size();
  fullMatrix<double> data(3, num_vertices);

  fullVector<double> mean(3);
  fullVector<double> vmins(3);
  fullVector<double> vmaxs(3);

  mean.setAll(0);
  vmins.setAll(DBL_MAX);
  vmaxs.setAll(-DBL_MAX);

  size_t idx = 0;
  for(std::set<SPoint3>::iterator uIter = unique.begin(); uIter != unique.end();
      ++uIter) {
    const SPoint3 &pp = *uIter;
    for(int d = 0; d < 3; d++) {
      data(d, idx) = pp[d];
      vmins(d) = std::min(vmins(d), pp[d]);
      vmaxs(d) = std::max(vmaxs(d), pp[d]);
      mean(d) += pp[d];
    }
    idx++;
  }

  for(int i = 0; i < 3; i++) { mean(i) /= num_vertices; }

  // Get the deviation from the mean
  fullMatrix<double> B(3, num_vertices);
  for(int i = 0; i < 3; i++) {
    for(int j = 0; j < num_vertices; j++) { B(i, j) = data(i, j) - mean(i); }
  }

  // Compute the covariance matrix
  fullMatrix<double> covariance(3, 3);
  B.mult(B.transpose(), covariance);
  covariance.scale(1. / (num_vertices - 1));
  /*
  Msg::Debug("Covariance matrix");
  Msg::Debug("%f %f %f", covariance(0,0),covariance(0,1),covariance(0,2) );
  Msg::Debug("%f %f %f", covariance(1,0),covariance(1,1),covariance(1,2) );
  Msg::Debug("%f %f %f", covariance(2,0),covariance(2,1),covariance(2,2) );
  */
  for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 3; j++) {
      if(std::abs(covariance(i, j)) < 10e-16) covariance(i, j) = 0;
    }
  }

  fullMatrix<double> left_eigv(3, 3);
  fullMatrix<double> right_eigv(3, 3);
  fullVector<double> real_eig(3);
  fullVector<double> img_eig(3);
  covariance.eig(real_eig, img_eig, left_eigv, right_eigv, true);

  // Now, project the data in the new basis.
  fullMatrix<double> projected(3, num_vertices);
  left_eigv.transpose().mult(data, projected);
  // Get the size of the box in the new direction
  fullVector<double> mins(3);
  fullVector<double> maxs(3);
  for(int i = 0; i < 3; i++) {
    mins(i) = DBL_MAX;
    maxs(i) = -DBL_MAX;
    for(int j = 0; j < num_vertices; j++) {
      maxs(i) = std::max(maxs(i), projected(i, j));
      mins(i) = std::min(mins(i), projected(i, j));
    }
  }

  // double means[3];
  double sizes[3];

  // Note:  the size is computed in the box's coordinates!
  for(int i = 0; i < 3; i++) {
    sizes[i] = maxs(i) - mins(i);
    // means[i] = (maxs(i) - mins(i)) / 2.;
  }

  if(sizes[0] == 0 && sizes[1] == 0) {
    // Entity is a straight line...
    SVector3 center;
    SVector3 Axis1;
    SVector3 Axis2;
    SVector3 Axis3;

    Axis1[0] = left_eigv(0, 0);
    Axis1[1] = left_eigv(1, 0);
    Axis1[2] = left_eigv(2, 0);
    Axis2[0] = left_eigv(0, 1);
    Axis2[1] = left_eigv(1, 1);
    Axis2[2] = left_eigv(2, 1);
    Axis3[0] = left_eigv(0, 2);
    Axis3[1] = left_eigv(1, 2);
    Axis3[2] = left_eigv(2, 2);

    center[0] = (vmaxs(0) + vmins(0)) / 2.0;
    center[1] = (vmaxs(1) + vmins(1)) / 2.0;
    center[2] = (vmaxs(2) + vmins(2)) / 2.0;

    return new SOrientedBoundingBox(center, sizes[0], sizes[1], sizes[2], Axis1,
                                    Axis2, Axis3);
  }

  // We take the smallest component, then project the data on the plane defined
  // by the other twos

  int smallest_comp = 0;
  if(sizes[0] <= sizes[1] && sizes[0] <= sizes[2])
    smallest_comp = 0;
  else if(sizes[1] <= sizes[0] && sizes[1] <= sizes[2])
    smallest_comp = 1;
  else if(sizes[2] <= sizes[0] && sizes[2] <= sizes[1])
    smallest_comp = 2;

  // The projection has been done circa line 161.
  // We just ignore the coordinate corresponding to smallest_comp.
  std::vector<SPoint2 *> points;
  for(int i = 0; i < num_vertices; i++) {
    SPoint2 *p = new SPoint2(projected(smallest_comp == 0 ? 1 : 0, i),
                             projected(smallest_comp == 2 ? 1 : 2, i));
    bool keep = true;
    for(std::vector<SPoint2 *>::iterator point = points.begin();
        point != points.end(); point++) {
      if(std::abs((*p)[0] - (**point)[0]) < 10e-10 &&
         std::abs((*p)[1] - (**point)[1]) < 10e-10) {
        keep = false;
        break;
      }
    }
    if(keep) { points.push_back(p); }
    else {
      delete p;
    }
  }

  // Find the convex hull from a delaunay triangulation of the points
  DocRecord record(points.size());
  record.numPoints = points.size();
  srand((unsigned)time(0));
  for(std::size_t i = 0; i < points.size(); i++) {
    record.points[i].where.h =
      points[i]->x() + (10e-6) * sizes[smallest_comp == 0 ? 1 : 0] *
                         (-0.5 + ((double)rand()) / RAND_MAX);
    record.points[i].where.v =
      points[i]->y() + (10e-6) * sizes[smallest_comp == 2 ? 1 : 0] *
                         (-0.5 + ((double)rand()) / RAND_MAX);
    record.points[i].adjacent = NULL;
  }

  try {
    record.MakeMeshWithPoints();
  } catch(const char *err) {
    Msg::Error("%s", err);
  }

  std::vector<Segment> convex_hull;
  for(int i = 0; i < record.numTriangles; i++) {
    Segment segs[3];
    segs[0].from = record.triangles[i].a;
    segs[0].to = record.triangles[i].b;
    segs[1].from = record.triangles[i].b;
    segs[1].to = record.triangles[i].c;
    segs[2].from = record.triangles[i].c;
    segs[2].to = record.triangles[i].a;

    for(int j = 0; j < 3; j++) {
      bool okay = true;
      for(std::vector<Segment>::iterator seg = convex_hull.begin();
          seg != convex_hull.end(); seg++) {
        if(((*seg).from == segs[j].from && (*seg).from == segs[j].to)
           // FIXME:
           // || ((*seg).from == segs[j].to && (*seg).from == segs[j].from)
        ) {
          convex_hull.erase(seg);
          okay = false;
          break;
        }
      }
      if(okay) { convex_hull.push_back(segs[j]); }
    }
  }

  // Now, examinate all the directions given by the edges of the convex hull
  // to find the one that lets us build the least-area bounding rectangle for
  // then points.
  fullVector<double> axis(2);
  axis(0) = 1;
  axis(1) = 0;
  fullVector<double> axis2(2);
  axis2(0) = 0;
  axis2(1) = 1;
  SOrientedBoundingRectangle least_rectangle;
  least_rectangle.center[0] = 0.0;
  least_rectangle.center[1] = 0.0;
  least_rectangle.size[0] = -1.0;
  least_rectangle.size[1] = 1.0;

  fullVector<double> segment(2);
  fullMatrix<double> rotation(2, 2);

  for(std::vector<Segment>::iterator seg = convex_hull.begin();
      seg != convex_hull.end(); seg++) {
    // segment(0) = record.points[(*seg).from].where.h -
    // record.points[(*seg).to].where.h;  segment(1) =
    // record.points[(*seg).from].where.v - record.points[(*seg).to].where.v;
    segment(0) = points[(*seg).from]->x() - points[(*seg).to]->x();
    segment(1) = points[(*seg).from]->y() - points[(*seg).to]->y();
    segment.scale(1.0 / segment.norm());

    double cosine = axis(0) * segment(0) + segment(1) * axis(1);
    double sine = axis(1) * segment(0) - segment(1) * axis(0);
    // double sine = axis(0)*segment(1) - segment(0)*axis(1);

    rotation(0, 0) = cosine;
    rotation(0, 1) = sine;
    rotation(1, 0) = -sine;
    rotation(1, 1) = cosine;

    // TODO C++11 std::numeric_limits<double>
    double max_x = -DBL_MAX;
    double min_x = DBL_MAX;
    double max_y = -DBL_MAX;
    double min_y = DBL_MAX;

    for(int i = 0; i < record.numPoints; i++) {
      fullVector<double> pnt(2);
      // pnt(0) = record.points[i].where.h;
      // pnt(1) = record.points[i].where.v;
      pnt(0) = points[i]->x();
      pnt(1) = points[i]->y();

      fullVector<double> rot_pnt(2);
      rotation.mult(pnt, rot_pnt);

      if(rot_pnt(0) < min_x) min_x = rot_pnt(0);
      if(rot_pnt(0) > max_x) max_x = rot_pnt(0);
      if(rot_pnt(1) < min_y) min_y = rot_pnt(1);
      if(rot_pnt(1) > max_y) max_y = rot_pnt(1);
    }

    /**/
    fullVector<double> center_rot(2);
    fullVector<double> center_before_rot(2);
    center_before_rot(0) = (max_x + min_x) / 2.0;
    center_before_rot(1) = (max_y + min_y) / 2.0;
    fullMatrix<double> rotation_inv(2, 2);

    rotation_inv(0, 0) = cosine;
    rotation_inv(0, 1) = -sine;
    rotation_inv(1, 0) = sine;
    rotation_inv(1, 1) = cosine;

    rotation_inv.mult(center_before_rot, center_rot);

    fullVector<double> axis_rot1(2);
    fullVector<double> axis_rot2(2);

    rotation_inv.mult(axis, axis_rot1);
    rotation_inv.mult(axis2, axis_rot2);

    if((least_rectangle.area() == -1) ||
       (max_x - min_x) * (max_y - min_y) < least_rectangle.area()) {
      least_rectangle.size[0] = max_x - min_x;
      least_rectangle.size[1] = max_y - min_y;
      least_rectangle.center[0] = (max_x + min_x) / 2.0;
      least_rectangle.center[1] = (max_y + min_y) / 2.0;
      least_rectangle.center[0] = center_rot(0);
      least_rectangle.center[1] = center_rot(1);
      least_rectangle.axisX[0] = axis_rot1(0);
      least_rectangle.axisX[1] = axis_rot1(1);
      //      least_rectangle.axisX[0] = segment(0);
      //      least_rectangle.axisX[1] = segment(1);
      least_rectangle.axisY[0] = axis_rot2(0);
      least_rectangle.axisY[1] = axis_rot2(1);
    }
  }
  // TODO C++11 std::numeric_limits<double>::min() / max()
  double min_pca = DBL_MAX;
  double max_pca = -DBL_MAX;
  for(int i = 0; i < num_vertices; i++) {
    min_pca = std::min(min_pca, projected(smallest_comp, i));
    max_pca = std::max(max_pca, projected(smallest_comp, i));
  }
  double center_pca = (max_pca + min_pca) / 2.0;
  double size_pca = (max_pca - min_pca);

  double raw_data[3][5];
  raw_data[0][0] = size_pca;
  raw_data[1][0] = least_rectangle.size[0];
  raw_data[2][0] = least_rectangle.size[1];

  raw_data[0][1] = center_pca;
  raw_data[1][1] = least_rectangle.center[0];
  raw_data[2][1] = least_rectangle.center[1];

  for(int i = 0; i < 3; i++) {
    raw_data[0][2 + i] = left_eigv(i, smallest_comp);
    raw_data[1][2 + i] =
      least_rectangle.axisX[0] * left_eigv(i, smallest_comp == 0 ? 1 : 0) +
      least_rectangle.axisX[1] * left_eigv(i, smallest_comp == 2 ? 1 : 2);
    raw_data[2][2 + i] =
      least_rectangle.axisY[0] * left_eigv(i, smallest_comp == 0 ? 1 : 0) +
      least_rectangle.axisY[1] * left_eigv(i, smallest_comp == 2 ? 1 : 2);
  }
  // Msg::Info("Test 1 : %f
  // %f",least_rectangle.center[0],least_rectangle.center[1]);
  // Msg::Info("Test 2 : %f
  // %f",least_rectangle.axisY[0],least_rectangle.axisY[1]);

  int tri[3];

  if(size_pca > least_rectangle.size[0]) {
    // P > R0
    if(size_pca > least_rectangle.size[1]) {
      // P > R1
      tri[0] = 0;
      if(least_rectangle.size[0] > least_rectangle.size[1]) {
        // R0 > R1
        tri[1] = 1;
        tri[2] = 2;
      }
      else {
        // R1 > R0
        tri[1] = 2;
        tri[2] = 1;
      }
    }
    else {
      // P < R1
      tri[0] = 2;
      tri[1] = 0;
      tri[2] = 1;
    }
  }
  else { // P < R0
    if(size_pca < least_rectangle.size[1]) {
      // P < R1
      tri[2] = 0;
      if(least_rectangle.size[0] > least_rectangle.size[1]) {
        tri[0] = 1;
        tri[1] = 2;
      }
      else {
        tri[0] = 2;
        tri[1] = 1;
      }
    }
    else {
      tri[0] = 1;
      tri[1] = 0;
      tri[2] = 2;
    }
  }

  SVector3 size;
  SVector3 center;
  SVector3 Axis1;
  SVector3 Axis2;
  SVector3 Axis3;

  for(int i = 0; i < 3; i++) {
    size[i] = raw_data[tri[i]][0];
    center[i] = raw_data[tri[i]][1];
    Axis1[i] = raw_data[tri[0]][2 + i];
    Axis2[i] = raw_data[tri[1]][2 + i];
    Axis3[i] = raw_data[tri[2]][2 + i];
  }

  SVector3 aux1;
  SVector3 aux2;
  SVector3 aux3;
  for(int i = 0; i < 3; i++) {
    aux1(i) = left_eigv(i, smallest_comp);
    aux2(i) = left_eigv(i, smallest_comp == 0 ? 1 : 0);
    aux3(i) = left_eigv(i, smallest_comp == 2 ? 1 : 2);
  }
  center = aux1 * center_pca + aux2 * least_rectangle.center[0] +
           aux3 * least_rectangle.center[1];
  // center[1] = -center[1];

  /*
  Msg::Info("Box center : %f %f %f",center[0],center[1],center[2]);
  Msg::Info("Box size : %f %f %f",size[0],size[1],size[2]);
  Msg::Info("Box axis 1 : %f %f %f",Axis1[0],Axis1[1],Axis1[2]);
  Msg::Info("Box axis 2 : %f %f %f",Axis2[0],Axis2[1],Axis2[2]);
  Msg::Info("Box axis 3 : %f %f %f",Axis3[0],Axis3[1],Axis3[2]);

  Msg::Info("Volume : %f", size[0]*size[1]*size[2]);
  */

  return new SOrientedBoundingBox(center, size[0], size[1], size[2], Axis1,
                                  Axis2, Axis3);
#else
  Msg::Error("SOrientedBoundingBox requires mesh module");
  return 0;
#endif
}
Beispiel #29
0
ReturnFlag SIM::run_()
{
	GAPopulation<CodeVInt, GAIndividual<CodeVInt>> subPopul(m_popsize);
	GAIndividual<CodeVInt> ia, ib;
	int i, n;
	double p;
	int flag, flag1;
	double bestlen = DBL_MAX;
	vector<int> arr(m_numDim);

	dynamic_cast<TermMean*>(m_term.get())->initialize(mean());
	while (!ifTerminating())
	{

		/*if(Global::msp_global->mp_problem->getEvaluations()/m_saveFre<m_num){
		mean<<diffEdges()<<" "<<Global::msp_global->mp_problem->getEvaluations()<<endl;
		}*/

#ifdef OFEC_DEMON
		for (i = 0; i<this->getPopSize(); i++)
			updateBestArchive(this->m_pop[i]->self());
		vector<Algorithm*> vp;
		vp.push_back(this);
		msp_buffer->updateBuffer_(&vp);
#endif
		//cout<<Global::msp_global->mp_problem->getEvaluations()<<endl;

		n = 0;
		while (n<m_popsize)
		{
			flag = 0;
			flag1 = 0;
			selection(ia, ib);
			p = Global::msp_global->mp_uniformAlg->Next();
			if (p <= m_PC)
			{
				*subPopul.getPop()[n++] = crossover(ia, ib);
				flag = 1;
			}
			if (flag == 0)
			{
				if (n<m_popsize - 1)
				{
					*subPopul.getPop()[n++] = ia;
					*subPopul.getPop()[n++] = ib;
				}
				else
				{
					*subPopul.getPop()[n++] = ia;
					flag1 = 1;
				}
			}
			p = Global::msp_global->mp_uniformAlg->Next();
			if (p <= m_PM)
			{
				if (flag == 1 || flag1 == 1) mutation(*subPopul.getPop()[n - 1]);
				else
				{
					mutation(*subPopul.getPop()[n - 2]);
					mutation(*subPopul.getPop()[n - 1]);
				}
			}
		}
		for (i = 0; i<m_popsize; i++)
			subPopul.getPop()[i]->evaluate();
		*(static_cast<GAPopulation<CodeVInt, GAIndividual<CodeVInt>>*>(this)) = subPopul;

#ifdef OFEC_CONSOLE
		OptimalEdgeInfo::getOptimalEdgeInfo()->recordEdgeInfo<GAIndividual<CodeVInt>>(Global::msp_global.get(), Solution<CodeVInt>::getBestSolutionSoFar(), m_pop, m_num, m_popsize, m_saveFre);
#endif
		m_iter++;
	}

#ifdef OFEC_CONSOLE
	OptimalEdgeInfo::getOptimalEdgeInfo()->recordEdgeInfo<GAIndividual<CodeVInt>>(Global::msp_global.get(), Solution<CodeVInt>::getBestSolutionSoFar(), m_pop, m_num, m_popsize, m_saveFre, false);
	OptimalEdgeInfo::getOptimalEdgeInfo()->recordLastInfo(Global::msp_global->m_runId, Global::msp_global->mp_problem->getEvaluations());
#endif
	return Return_Terminate;
}
Beispiel #30
0
/*PARSE RADAR DATA - traverse the two data array's and
 * use the start_array to determine
 * the start of a pulse.  Keep track of the pulse,
 * and log the pulse data based on the
 * pulse count into a 2 dimensional array, response_parsed.
 * NOTE: intensity_time must be of size NUM_TRIGGERS*SAMPLES_PER_PULSE*/
void process_radar_data(char* intensity_time,
                      rdata_t* trigger, rdata_t* response, int buf_size){

   int count = 0;
   int i, j;
   rdata_t average, max;

   assert(intensity_time != NULL);
   assert(trigger != NULL);
   assert(response != NULL);

   /*for size of radar data to be correct, init_processing must be
    *called before this function                                */
   rdata_t response_parsed[NUM_TRIGGERS][size_of_sendarray];

   /*create a simplied edge trigger based off the transmit signal*/
   memset(start, 0, buf_size);
   find_trigger_start(trigger, start, buf_size);

#ifdef PRINT_TRIGGERING
   for (i = 0; i < buf_size; i++) {
     printf("%d: %d %f %f\n", i, start[i], trigger[i], response[i]);
   }
#endif

   for(i = 13; i < buf_size-SAMPLES_PER_PULSE; i++){

     /*find the trigger and if found, load data into the 2-d array,
       while keeping track of the time of the pulse*/
     if (start[i] == 1 && none(start,i-11,i-1) == 0){
       rdata_t_cpy(response_parsed[count], &response[i], SAMPLES_PER_PULSE);
       count = count + 1;
     }

     /*only record for a preset amount of triggers*/
     if (count == NUM_TRIGGERS)
       break;

   }

#ifdef PRINT_PARSED

   for (i = 0; i < NUM_TRIGGERS; i++){
     for (j = 0; j < SAMPLES_PER_PULSE/10; j++) {
       printf("%d ", (int)floorf(response_parsed[i][j]));
     }
     printf("\n");
   }
#endif

   /*subtract the avarage value of each sub array!*/
   for(i = 0; i < NUM_TRIGGERS; i++){
       average = mean(response_parsed[i], 0, SAMPLES_PER_PULSE-1);
       rdata_t_addi(response_parsed[i], -1.0*average, SAMPLES_PER_PULSE);
   }


#ifdef PRINT_AVERAGED

   for (i = 0; i < NUM_TRIGGERS; i++){
     for (j = 0; j < SAMPLES_PER_PULSE/10; j++) {
       printf("%d ", (int)floorf(response_parsed[i][j]));
     }
     printf("\n");
   }
#endif



   /*create 2-pulse cancelor*/
   for (i = 1; i < NUM_TRIGGERS; i++)
       sub_array(response_parsed[i], response_parsed[i-1], SAMPLES_PER_PULSE);


#ifdef PRINT_CANCELOR

   for (i = 0; i < NUM_TRIGGERS; i++){
     for (j = 0; j < SAMPLES_PER_PULSE/10; j++) {
       printf("%d ", (int)floorf(response_parsed[i][j]));
     }
     printf("\n");
   }
#endif


   /*ifft and convert to intensity values*/
   for(i = 0; i < NUM_TRIGGERS; i++){
       ifft(ifft_array,response_parsed[i]);
       rdata_t_cpy(response_parsed[i], ifft_array, size_of_sendarray);
       dbv(response_parsed[i], size_of_sendarray);
   }

#ifdef PRINT_IFFT
   for (i = 0; i < NUM_TRIGGERS; i++){
     for (j = 0; j < SAMPLES_PER_PULSE/10; j++) {
       printf("%d ", (int)floor(response_parsed[i][j]));
     }
     printf("\n");
   }
#endif


   max = find_max(&response_parsed[0][0], NUM_TRIGGERS*size_of_sendarray);

   rdata_t_addi(&response_parsed[0][0], -1.0*max, NUM_TRIGGERS*size_of_sendarray);

   intensify(intensity_time, &(response_parsed[0][0]), size_of_sendarray*NUM_TRIGGERS);


#ifdef PRINT_FINAL
   for (i = 0; i < NUM_TRIGGERS; i++){
     for (j = 0; j < SAMPLES_PER_PULSE/10; j++) {
         printf("%d ", (unsigned int)(unsigned char)floor(intensity_time[NUM_TRIGGERS*i+j]));
     }
     printf("\n");
   }
#endif



}