<commit_before>//
//  pfilter.cpp
//  EpiGenMCMC
//
//  Created by Lucy Li on 05/05/2016.
//  Copyright (c) 2016 Lucy Li, Imperial College London. All rights reserved.
//

#include "pfilter.h"
#include <iostream>

namespace EpiGenPfilter {
    double pfilter(Model & sim_model, Parameter & model_params, MCMCoptions & options, Particle &particles, Trajectory & output_traj, TimeSeriesData &epi_data, TreeData &tree_data, MultiTreeData &multitree_data) {
        double loglik = 0.0;
        int num_groups = options.num_groups;
        int num_particles = options.particles;
        int init_seed = options.seed;
        int total_dt = options.total_dt;
        double sim_dt = options.sim_dt;
        int total_steps = ceil((double)total_dt/(double)options.pfilter_every);
        int add_dt = 0;
        double ESS_threshold = options.pfilter_threshold*(double)num_particles;
        Likelihood likelihood_calc;
//        std::vector <Parameter> values;// (options.num_threads, model_params);
//        for (int i=0; i!=options.num_threads; ++i) values.push_back(model_params);
//        for (int i=0; i!=model_params.get_total_params(); ++i) values.push_back(model_params.get(i));
        std::vector <std::vector<double> > values(options.num_threads, std::vector<double>(model_params.get_total_params(), 0.0));
        for (int i=0; i!=options.num_threads; ++i) {
            for (int j=0; j!=model_params.get_total_params(); ++j) {
                values[i][j] = model_params.get(j);
            }
        }
//        printf("Size of values = %d\n",values.size());
        double reporting_rate = 1.0;
        if (model_params.param_exists("reporting")) {
            reporting_rate = model_params.get("reporting");
        }
        std::vector <std::string> param_names = model_params.get_names_vector();
        std::vector <std::vector<std::string> > param_names_threads (options.num_threads);
        if (model_params.param_exists("time_before_data")) {
            add_dt = model_params.get("time_before_data");
        }
        if (options.save_traj) {
            if (add_dt > 0) {
                particles.start_particle_tracing(add_dt+total_dt, num_groups);
            }
            else if (add_dt < 0) {
                particles.start_particle_tracing(add_dt+total_dt, num_groups);
                total_steps = ceil((double)(total_dt+add_dt)/(double)options.pfilter_every);
            }
            else {
                particles.start_particle_tracing(total_dt, num_groups);
            }
        }
        std::vector <Model> models;
        for (int i=0; i<options.num_threads; ++i) {
            models.push_back(sim_model);
        }
        std::vector <int> add_dt_threads (options.num_threads, add_dt);
        std::vector <int> start_dt_threads (options.num_threads, 0);
        std::vector <int> end_dt_threads (options.num_threads, add_dt);
        std::vector <double> dt_threads (options.num_threads, sim_dt);
        std::vector <int> total_dt_threads(options.num_threads, total_dt);
        std::vector <double> reporting_rate_threads(options.num_threads, reporting_rate);
        std::vector <int> num_groups_threads(options.num_threads, num_groups);
        // Simulate model and calculate likelihood assuming no observed data
        if (model_params.param_exists("time_before_data")) {
            if (add_dt > 0) {
                omp_set_num_threads(options.num_threads);
//                std::vector <Trajectory *> curr_trajs;
//                for (int i=0; i!=num_particles; ++i) {
//                    curr_trajs.push_back(particles.get_traj(i));
//                }
#pragma omp parallel for shared(particles, values)
                for (int i=0; i<num_particles; i++) {
                    int tn = omp_get_thread_num();
                    gsl_rng* r = gsl_rng_alloc( gsl_rng_mt19937 );
                    gsl_rng_set( r, omp_get_thread_num() + i );
                    // Adjust length of trajectory
                    particles.get_traj(i)->resize(add_dt, num_groups);
                    models[tn].simulate(values[tn], param_names_threads[tn], particles.get_traj(i), 0, add_dt_threads[tn], dt_threads[tn], total_dt_threads[tn], r);
                    if (options.which_likelihood<2) {
                        double w = likelihood_calc.binomial_lik(reporting_rate_threads[tn], particles.get_traj(i)->get_total_traj(), add_dt_threads[tn]+total_dt_threads[tn], 0, add_dt_threads[tn], num_groups_threads[tn], false);
                        particles.set_weight(w, i, false);
                    }
                    if (options.save_traj) {
                        particles.save_traj_to_matrix(i, 0, add_dt);
                        particles.save_ancestry(i, 0, add_dt);
                    }
                }
            }
        }
        init_seed += num_particles;
        int t=0;
        int start_dt;
        int end_dt;
        for (t=0; t!=total_steps; ++t) {
//            std::vector<double> we(options.particles, 0.0), wg(options.particles, 0.0);
            start_dt = t*options.pfilter_every;
            end_dt = std::min(total_dt, (t+1)*options.pfilter_every);
            std::fill(start_dt_threads.begin(), start_dt_threads.end(), start_dt);
            std::fill(end_dt_threads.begin(), end_dt_threads.end(), end_dt);
            omp_set_num_threads(options.num_threads);
#pragma omp parallel for shared (particles, values)
            for (int i=0; i<num_particles; i++) {
                int tn = omp_get_thread_num();
                gsl_rng* r = gsl_rng_alloc( gsl_rng_mt19937 );
                gsl_rng_set( r, omp_get_thread_num() + i );
                // Adjust length of trajectory
                //                    if (tn==0) std::cout << i << ' ' << std::endl;
                particles.get_traj(i)->resize(end_dt-start_dt, options.num_groups);
                models[tn].simulate(values[tn], param_names_threads[tn], particles.get_traj(i), start_dt_threads[tn], end_dt_threads[tn], dt_threads[tn], total_dt_threads[tn], r);
                double w = 1.0;
                double temp = 0.0;
                if (options.which_likelihood<2) {
                    double A = particles.get_traj(i)->get_total_traj();
                    temp = likelihood_calc.binomial_lik(reporting_rate_threads[tn], A, epi_data.get_data_ptr(0), add_dt_threads[tn]+total_dt_threads[tn], start_dt_threads[tn], end_dt_threads[tn], add_dt_threads[tn], num_groups_threads[tn], false);
                    w *= temp;
                    //                        we[i] = log(temp);
                }
                if (options.which_likelihood != 1) {
                    temp = likelihood_calc.coalescent_lik(particles.get_traj(i)->get_traj_ptr(1, 0), particles.get_traj(i)->get_traj_ptr(2, 0),
                                                          tree_data.get_binomial_ptr(0), tree_data.get_interval_ptr(0), tree_data.get_ends_ptr(0),
                                                          start_dt_threads[tn], end_dt_threads[tn], add_dt_threads[tn], false);
                    w *= temp;
                    //                        wg[i] = log(temp);
                }
                particles.set_weight(w, i, true);
                if (options.save_traj) {
                    particles.save_traj_to_matrix(i, start_dt_threads[tn]+add_dt_threads[tn], end_dt_threads[tn]+add_dt_threads[tn]);
                    particles.save_ancestry(i, start_dt_threads[tn]+add_dt_threads[tn], end_dt_threads[tn]+add_dt_threads[tn]);
                }
            }
//            std::cout << "Epi Weight: " << std::accumulate(we.begin(), we.end(), 0.0) << " Gen Weight: " << std::accumulate(wg.begin(), wg.end(), 0.0) << " Total: " << particles.get_total_weight() << std::endl;
            double curr_ESS = particles.get_ESS();
            if (curr_ESS < ESS_threshold) {
                double total_weight = particles.get_total_weight();
                if (total_weight == 0.0) {
                    loglik += -0.1*std::numeric_limits<double>::max();
//                    std::cout << std::accumulate(epi_data.get_data_ptr(0)+start_dt, epi_data.get_data_ptr(0)+end_dt, 0.0) << " : " << particles.get_traj(0)->get_traj(0) << std::endl;
                    std::cout << "stop time: " << end_dt << std::endl;
                    break;
                } else {
                    loglik += log(total_weight) - log(num_particles);
                }
                particles.resample(options.rng[0]);
            }
            else {
                particles.reset_parents();
            }
        }
        if (options.save_traj) {
            output_traj.resize((total_dt+add_dt), num_groups);
            //if (loglik > -0.1*std::numeric_limits<double>::max()) {
                particles.retrace_traj(output_traj, options.rng[0]);
            //}
        }
        for (int i=0; i!=num_particles; ++i) {
            particles.get_traj(i)->reset();
        }
        std::vector < std::vector<double> >().swap(values);
        return (loglik);
    }
}
<commit_msg>use the correct trajectory pointer<commit_after>//
//  pfilter.cpp
//  EpiGenMCMC
//
//  Created by Lucy Li on 05/05/2016.
//  Copyright (c) 2016 Lucy Li, Imperial College London. All rights reserved.
//

#include "pfilter.h"
#include <iostream>

namespace EpiGenPfilter {
    double pfilter(Model & sim_model, Parameter & model_params, MCMCoptions & options, Particle &particles, Trajectory & output_traj, TimeSeriesData &epi_data, TreeData &tree_data, MultiTreeData &multitree_data) {
        double loglik = 0.0;
        int num_groups = options.num_groups;
        int num_particles = options.particles;
        int init_seed = options.seed;
        int total_dt = options.total_dt;
        double sim_dt = options.sim_dt;
        int total_steps = ceil((double)total_dt/(double)options.pfilter_every);
        int add_dt = 0;
        double ESS_threshold = options.pfilter_threshold*(double)num_particles;
        Likelihood likelihood_calc;
//        std::vector <Parameter> values;// (options.num_threads, model_params);
//        for (int i=0; i!=options.num_threads; ++i) values.push_back(model_params);
//        for (int i=0; i!=model_params.get_total_params(); ++i) values.push_back(model_params.get(i));
        std::vector <std::vector<double> > values(options.num_threads, std::vector<double>(model_params.get_total_params(), 0.0));
        for (int i=0; i!=options.num_threads; ++i) {
            for (int j=0; j!=model_params.get_total_params(); ++j) {
                values[i][j] = model_params.get(j);
            }
        }
//        printf("Size of values = %d\n",values.size());
        double reporting_rate = 1.0;
        if (model_params.param_exists("reporting")) {
            reporting_rate = model_params.get("reporting");
        }
        std::vector <std::string> param_names = model_params.get_names_vector();
        std::vector <std::vector<std::string> > param_names_threads (options.num_threads);
        if (model_params.param_exists("time_before_data")) {
            add_dt = model_params.get("time_before_data");
        }
        if (options.save_traj) {
            if (add_dt > 0) {
                particles.start_particle_tracing(add_dt+total_dt, num_groups);
            }
            else if (add_dt < 0) {
                particles.start_particle_tracing(add_dt+total_dt, num_groups);
                total_steps = ceil((double)(total_dt+add_dt)/(double)options.pfilter_every);
            }
            else {
                particles.start_particle_tracing(total_dt, num_groups);
            }
        }
        std::vector <Model> models;
        for (int i=0; i<options.num_threads; ++i) {
            models.push_back(sim_model);
        }
        std::vector <int> add_dt_threads (options.num_threads, add_dt);
        std::vector <int> start_dt_threads (options.num_threads, 0);
        std::vector <int> end_dt_threads (options.num_threads, add_dt);
        std::vector <double> dt_threads (options.num_threads, sim_dt);
        std::vector <int> total_dt_threads(options.num_threads, total_dt);
        std::vector <double> reporting_rate_threads(options.num_threads, reporting_rate);
        std::vector <int> num_groups_threads(options.num_threads, num_groups);
        // Simulate model and calculate likelihood assuming no observed data
        if (model_params.param_exists("time_before_data")) {
            if (add_dt > 0) {
                omp_set_num_threads(options.num_threads);
//                std::vector <Trajectory *> curr_trajs;
//                for (int i=0; i!=num_particles; ++i) {
//                    curr_trajs.push_back(particles.get_traj(i));
//                }
#pragma omp parallel for shared(particles, values)
                for (int i=0; i<num_particles; i++) {
                    int tn = omp_get_thread_num();
                    gsl_rng* r = gsl_rng_alloc( gsl_rng_mt19937 );
                    gsl_rng_set( r, omp_get_thread_num() + i );
                    // Adjust length of trajectory
                    particles.get_traj(i)->resize(add_dt, num_groups);
                    models[tn].simulate(values[tn], param_names_threads[tn], particles.get_traj(i), 0, add_dt_threads[tn], dt_threads[tn], total_dt_threads[tn], r);
                    if (options.which_likelihood<2) {
                        double w = likelihood_calc.binomial_lik(reporting_rate_threads[tn], particles.get_traj(i)->get_total_traj(), add_dt_threads[tn]+total_dt_threads[tn], 0, add_dt_threads[tn], num_groups_threads[tn], false);
                        particles.set_weight(w, i, false);
                    }
                    if (options.save_traj) {
                        particles.save_traj_to_matrix(i, 0, add_dt);
                        particles.save_ancestry(i, 0, add_dt);
                    }
                }
            }
        }
        init_seed += num_particles;
        int t=0;
        int start_dt;
        int end_dt;
        for (t=0; t!=total_steps; ++t) {
//            std::vector<double> we(options.particles, 0.0), wg(options.particles, 0.0);
            start_dt = t*options.pfilter_every;
            end_dt = std::min(total_dt, (t+1)*options.pfilter_every);
            std::fill(start_dt_threads.begin(), start_dt_threads.end(), start_dt);
            std::fill(end_dt_threads.begin(), end_dt_threads.end(), end_dt);
            omp_set_num_threads(options.num_threads);
#pragma omp parallel for shared (particles, values)
            for (int i=0; i<num_particles; i++) {
                int tn = omp_get_thread_num();
                gsl_rng* r = gsl_rng_alloc( gsl_rng_mt19937 );
                gsl_rng_set( r, omp_get_thread_num() + i );
                // Adjust length of trajectory
                //                    if (tn==0) std::cout << i << ' ' << std::endl;
                particles.get_traj(i)->resize(end_dt-start_dt, options.num_groups);
                models[tn].simulate(values[tn], param_names_threads[tn], particles.get_traj(i), start_dt_threads[tn], end_dt_threads[tn], dt_threads[tn], total_dt_threads[tn], r);
                double w = 1.0;
                double temp = 0.0;
                if (options.which_likelihood<2) {
                    double A = particles.get_traj(i)->get_total_traj();
                    temp = likelihood_calc.binomial_lik(reporting_rate_threads[tn], A, epi_data.get_data_ptr(0), add_dt_threads[tn]+total_dt_threads[tn], start_dt_threads[tn], end_dt_threads[tn], add_dt_threads[tn], num_groups_threads[tn], false);
                    w *= temp;
                    //                        we[i] = log(temp);
                }
                if (options.which_likelihood != 1) {
                    temp = likelihood_calc.coalescent_lik(particles.get_traj(i)->get_traj_ptr(0, 0), particles.get_traj(i)->get_traj_ptr(1, 0),
                                                          tree_data.get_binomial_ptr(0), tree_data.get_interval_ptr(0), tree_data.get_ends_ptr(0),
                                                          start_dt_threads[tn], end_dt_threads[tn], add_dt_threads[tn], false);
                    w *= temp;
                    //                        wg[i] = log(temp);
                }
                particles.set_weight(w, i, true);
                if (options.save_traj) {
                    particles.save_traj_to_matrix(i, start_dt_threads[tn]+add_dt_threads[tn], end_dt_threads[tn]+add_dt_threads[tn]);
                    particles.save_ancestry(i, start_dt_threads[tn]+add_dt_threads[tn], end_dt_threads[tn]+add_dt_threads[tn]);
                }
            }
//            std::cout << "Epi Weight: " << std::accumulate(we.begin(), we.end(), 0.0) << " Gen Weight: " << std::accumulate(wg.begin(), wg.end(), 0.0) << " Total: " << particles.get_total_weight() << std::endl;
            double curr_ESS = particles.get_ESS();
            if (curr_ESS < ESS_threshold) {
                double total_weight = particles.get_total_weight();
                if (total_weight == 0.0) {
                    loglik += -0.1*std::numeric_limits<double>::max();
//                    std::cout << std::accumulate(epi_data.get_data_ptr(0)+start_dt, epi_data.get_data_ptr(0)+end_dt, 0.0) << " : " << particles.get_traj(0)->get_traj(0) << std::endl;
                    std::cout << "stop time: " << end_dt << std::endl;
                    break;
                } else {
                    loglik += log(total_weight) - log(num_particles);
                }
                particles.resample(options.rng[0]);
            }
            else {
                particles.reset_parents();
            }
        }
        if (options.save_traj) {
            output_traj.resize((total_dt+add_dt), num_groups);
            //if (loglik > -0.1*std::numeric_limits<double>::max()) {
                particles.retrace_traj(output_traj, options.rng[0]);
            //}
        }
        for (int i=0; i!=num_particles; ++i) {
            particles.get_traj(i)->reset();
        }
        std::vector < std::vector<double> >().swap(values);
        return (loglik);
    }
}
<|endoftext|>


<commit_before>#include "mmd/pmx/mesh.hpp"
#include "../stream.hpp"

namespace mmd {
    namespace pmx {

        void Vertex::load(Fs *fs, Header *header) {
            Stream *stream = (Stream *)fs;
            position = stream->readVec3();
            normal = stream->readVec3();
            uv = stream->readVec2();
            for (int i = 0; i < header->additional; ++i) {
                addition[i] = stream->readVec4();
            }
            boneType = stream->readByte();
            switch (boneType) {
                case 0:
                BDEF.bone[0] = stream->readIndex(header->bone);
                break;

                case 1:
                BDEF.bone[0] = stream->readIndex(header->bone);
                BDEF.bone[1] = stream->readIndex(header->bone);
                BDEF.weight[0] = stream->readFloat();
                BDEF.weight[1] = 1.0f - BDEF.weight[0];
                break;

                case 2:
                case 4:
                for (int i = 0; i < 4; ++i) {
                    BDEF.bone[i] = stream->readIndex(header->bone);
                }
                for (int i = 0; i < 4; ++i) {
                    BDEF.weight[i] = stream->readFloat();
                }
                break;

                case 3:
                SDEF.bone[0] = stream->readIndex(header->bone);
                SDEF.bone[1] = stream->readIndex(header->bone);
                SDEF.weight[0] = stream->readFloat();
                SDEF.weight[1] = 1.0f - SDEF.weight[0];
                SDEF.C = stream->readVec3();
                SDEF.R0 = stream->readVec3();
                SDEF.R1 = stream->readVec3();
                break;

                default:
                FAIL << "Invalid bone type!";
            }
            edge = stream->readFloat();
        }

        void Surface::load(Fs *fs, Header *header) {
            Stream *stream = (Stream *)fs;
            A = stream->readUIndex(header->vertex);
            B = stream->readUIndex(header->vertex);
            C = stream->readUIndex(header->vertex);
        }

        void Mesh::load(Fs *fs, Header *header) {
            LOG << "load Mesh";
            Stream *stream = (Stream *)fs;
            int n = stream->readInt();
            vertex.resize(n);
            for (int i = 0; i < n; ++i) {
                vertex[i].load(fs, header);
            }
            n = stream->readInt();
            surface.resize(n);
            for (int i = 0; i < n; ++i) {
                surface[i].load(fs, header);
            }

            LOG << "---------------------";
            LOG << "Mesh infomation";
            LOG << "---------------------";
            LOG << "vertices: " << vertex.size();
            LOG << "surfaces: " << surface.size();
        }

    } /* pmx */
} /* mmd */
<commit_msg>solve a bug<commit_after>#include "mmd/pmx/mesh.hpp"
#include "../stream.hpp"

namespace mmd {
    namespace pmx {

        void Vertex::load(Fs *fs, Header *header) {
            Stream *stream = (Stream *)fs;
            position = stream->readVec3();
            normal = stream->readVec3();
            uv = stream->readVec2();
            for (int i = 0; i < header->additional; ++i) {
                addition[i] = stream->readVec4();
            }
            boneType = stream->readByte();
            switch (boneType) {
                case 0:
                BDEF.bone[0] = stream->readIndex(header->bone);
                break;

                case 1:
                BDEF.bone[0] = stream->readIndex(header->bone);
                BDEF.bone[1] = stream->readIndex(header->bone);
                BDEF.weight[0] = stream->readFloat();
                BDEF.weight[1] = 1.0f - BDEF.weight[0];
                break;

                case 2:
                case 4:
                for (int i = 0; i < 4; ++i) {
                    BDEF.bone[i] = stream->readIndex(header->bone);
                }
                for (int i = 0; i < 4; ++i) {
                    BDEF.weight[i] = stream->readFloat();
                }
                break;

                case 3:
                SDEF.bone[0] = stream->readIndex(header->bone);
                SDEF.bone[1] = stream->readIndex(header->bone);
                SDEF.weight[0] = stream->readFloat();
                SDEF.weight[1] = 1.0f - SDEF.weight[0];
                SDEF.C = stream->readVec3();
                SDEF.R0 = stream->readVec3();
                SDEF.R1 = stream->readVec3();
                break;

                default:
                FAIL << "Invalid bone type!";
            }
            edge = stream->readFloat();
        }

        void Surface::load(Fs *fs, Header *header) {
            Stream *stream = (Stream *)fs;
            A = stream->readUIndex(header->vertex);
            B = stream->readUIndex(header->vertex);
            C = stream->readUIndex(header->vertex);
        }

        void Mesh::load(Fs *fs, Header *header) {
            LOG << "load Mesh";
            Stream *stream = (Stream *)fs;
            int n = stream->readInt();
            vertex.resize(n);
            for (int i = 0; i < n; ++i) {
                vertex[i].load(fs, header);
            }
            n = stream->readInt() / 3;
            surface.resize(n);
            for (int i = 0; i < n; ++i) {
                surface[i].load(fs, header);
            }

            LOG << "---------------------";
            LOG << "Mesh infomation";
            LOG << "---------------------";
            LOG << "vertices: " << vertex.size();
            LOG << "surfaces: " << surface.size();
        }

    } /* pmx */
} /* mmd */
<|endoftext|>


<commit_before>/*
 * Simka2ComputeKmerSpectrum.hpp
 *
 *  Created on: 4 nov. 2016
 *      Author: gbenoit
 */

#include <gatb/gatb_core.hpp>
//#include "../core/SimkaUtils.hpp"
#include "../simka2/Simka2Utils.hpp"
#include "SimkaDistanceMatrixBinary.hpp"
//#include "../core/SimkaUtils.hpp"
//#include "Simka2Database.hpp"
//#include "../minikc/MiniKC.hpp"
//#include <gatb/kmer/impl/RepartitionAlgorithm.hpp>
//#include <gatb/kmer/impl/ConfigurationAlgorithm.hpp>
//#include "SimkaAlgorithm.hpp"
//#include "SimkaAlgorithm.hpp"

/*********************************************************************
* ** SimkaAlgorithm
*********************************************************************/
class SimkaDistanceExporterAlgorithm : public Algorithm
{

public:

	string _dirBinaryMatrices;
	string _inputFilenameIds;
	string _outputDir;

	vector<string> _ids;
	vector<string> _wantedIds;
	vector<size_t> _wantedIdsIndex;
	map<string, size_t> _idToIndex;
	size_t _matrixSize;

	SimkaDistanceExporterAlgorithm(IProperties* options):
		Algorithm("simkaDistanceExporter", -1, options)
	{
		_inputFilenameIds = "";
	}

	void execute(){
		parseArgs();
		createWantedIds();
		createIdsIndex();
		writeMatrices();
	}

	void parseArgs(){

		_dirBinaryMatrices =  getInput()->getStr(STR_URI_INPUT);
		_outputDir =  getInput()->getStr(STR_URI_OUTPUT);

		if(getInput()->get(STR_SIMKA2_INPUT_IDS)){
			_inputFilenameIds =  getInput()->getStr(STR_SIMKA2_INPUT_IDS);
		}

		if(!System::file().doesExist(_outputDir)){
			System::file().mkdir(_outputDir, -1);
		}

	}

	void createWantedIds(){

		SimkaDistanceMatrixBinary::loadMatrixIds(_dirBinaryMatrices, _ids);

		if(_inputFilenameIds.empty()){
			_wantedIds = vector<string>(_ids);
		}
		else{
			string line;
			ifstream inputFile(_inputFilenameIds.c_str());

			while(getline(inputFile, line)){

				line.erase(std::remove(line.begin(),line.end(),' '),line.end());
				if(line == "") continue;

				_wantedIds.push_back(line);
			}
		}

		_matrixSize = _wantedIds.size();
		cout << "matrix size: " << _matrixSize << endl;
	}

	void createIdsIndex(){

		for(size_t i=0; i<_ids.size(); i++){
			_idToIndex[_ids[i]] = i;
		}

		_wantedIdsIndex.resize(_matrixSize);
		for(size_t i=0; i<_matrixSize; i++){
			_wantedIdsIndex[i] = _idToIndex[_wantedIds[i]];
		}
	}

	void writeMatrices(){
		vector<string> matrixFilenames = System::file().listdir(_dirBinaryMatrices);

		for(size_t i=0; i<matrixFilenames.size(); i++){
			string matrixFilename = _dirBinaryMatrices + "/" + matrixFilenames[i];
			if(matrixFilename.find("mat_") == string::npos) continue;

			string distanceName = matrixFilenames[i];
			string ext = ".bin";
			std::string::size_type it = distanceName.find(ext);
			distanceName.erase(it, ext.length());

			cout << distanceName << endl;
			writeMatrixASCII(distanceName, matrixFilename);
		}

	}

	void writeMatrixASCII(const string& distanceName, const string& binaryMatrixFilename){

		vector<float> rowData(_matrixSize, 0);
		ifstream binaryMatrixFile(binaryMatrixFilename.c_str(), ios::binary);
		string filename = _outputDir + "/" + distanceName + ".csv";
		gzFile out = gzopen((filename + ".gz").c_str(),"wb");

		string str = "";

		for(size_t i=0; i<_matrixSize; i++){
			str += ";" + _wantedIds[i];
		}
		str += '\n';
		gzwrite(out, str.c_str(), str.size());

		for(size_t i=0; i<_matrixSize; i++){

			str = "";
			str += _wantedIds[i] + ";";

			size_t rowIndex = _wantedIdsIndex[i];
			SimkaDistanceMatrixBinary::loadRow(rowIndex, binaryMatrixFile, rowData);

			for(size_t j=0; j<_matrixSize; j++){

				str += Stringify::format("%f", rowData[_wantedIdsIndex[j]]) + ";";

			}

			str.erase(str.size()-1);
			str += '\n';

			gzwrite(out, str.c_str(), str.size());
		}

		gzclose(out);
		binaryMatrixFile.close();
	}

	/*
	void outputMatrixToAscii(){
		string filename = outputDir + "/" + outputFilename + _outputFilenameSuffix + ".csv";
		//IFile* file = gatb::core::system::impl::System::file().newFile(filename, "wb");
		Bag<string>* bag = new BagGzFile<string>(outputDir + "/" + outputFilename + _outputFilenameSuffix + ".csv.gz");
		//Bag<string>* cachedBag = new BagCache<string>(bag, 10000);

		gzFile out = gzopen((filename + ".gz").c_str(),"wb");
		//char buf[BUFSIZ] = { 0 };

		//char buffer[200];
		string str;

		for(size_t i=0; i<matrix.size(); i++){
			str += ";" + bankNames[i];
			//str += ";" + datasetInfos[i]._name;
		}
		str += '\n';
		gzwrite(out, str.c_str(), str.size());
		//cachedBag->insert(str);
		//file->fwrite(str.c_str(), str.size(), 1);

		str = "";

		for(size_t i=0; i<matrix.size(); i++){

			str = "";
			str += bankNames[i] + ";";
			//str += datasetInfos[i]._name + ";";
			for(size_t j=0; j<matrix.size(); j++){

				//snprintf(buffer,200,"%.2f", matrix[i][j]);
				//snprintf(buffer,200,"%f", matrix[i][j]);
				str += Stringify::format("%f", matrix[i][j]) + ";";

				//str += to_string(matrix[i][j]) + ";";
			}

			//matrixNormalizedStr.erase(matrixNormalizedStr.end()-1);
			str.erase(str.size()-1);
			//str.pop_back(); //remove ; at the end of the line
			str += '\n';

			//cout << str << endl;
			//file->fwrite(str.c_str(), str.size(), 1);
			gzwrite(out, str.c_str(), str.size());
			//cachedBag->insert(str);
		}

		//gatb::core::system::IFile* file = gatb::core::system::impl::System::file().newFile(outputDir + "/" + outputFilename + _outputFilenameSuffix + ".csv", "wb");

		//file->flush();
		//delete file;
		gzclose(out);
		//delete cachedBag;

	}*/

};

class SimkaDistanceExporter: public Tool{
public:

	string _execFilename;

	SimkaDistanceExporter(string execFilename): Tool ("Simka-DistanceMatrixWriter"){
		_execFilename = execFilename;

	    IOptionsParser* parser = getParser();//new OptionsParser ("Simka2 - Compute Kmer Spectrum");

	    parser->push_front (new OptionOneParam (STR_URI_INPUT, "path to a dir of binary matrices", true));
	    parser->push_front (new OptionOneParam (STR_SIMKA2_INPUT_IDS, "filename of ids in the result matrix (one id per line). Do not used this option to used all ids.", false));
	    parser->push_front (new OptionOneParam (STR_URI_OUTPUT, "output dir for distance matrices", true));

	    //parser->push_front (new OptionOneParam (STR_URI_OUTPUT, "output directory for merged kmer spectrum", true));
	   // parser->push_front (new OptionOneParam (STR_SIMKA2_DATABASE_DIR, "dir path to a simka database", true));
	    //parser->push_front (new OptionOneParam (STR_SIMKA2_NB_PARTITION, "number of partitions", true));
	    //parser->push_front (new OptionOneParam (STR_SIMKA2_PARTITION_ID, "number of the partition", true));
	    //parser->push_front (new OptionOneParam (STR_SIMKA2_DISTANCE_MATRIX_DIR, "input filename of k-mer spectrums for which distances has to be computed", true));

	   // IOptionsParser* kmerParser = new OptionsParser ("kmer");
	    //kmerParser->push_back (new OptionOneParam (STR_KMER_SIZE, "size of a kmer", true));

	    //parser->getParser(STR_NB_CORES)->setVisible(false);
		//parser->push_back(kmerParser);

	}

	void execute ()
	{
		IProperties* input = getInput();
		//Parameter params(*this, getInput());
		//Parameter params(input, _execFilename);

		//size_t kmerSize = getInput()->getInt (STR_KMER_SIZE);

	    //Integer::apply<Functor,Parameter> (kmerSize, params);

		SimkaDistanceExporterAlgorithm* algo = new SimkaDistanceExporterAlgorithm(input);
		algo->execute();
		delete algo;
	}

};

int main (int argc, char* argv[])
{
    try
    {
    	SimkaDistanceExporter(string(argv[0])).run (argc, argv);
    }
    catch (Exception& e)
    {
        std::cout << "EXCEPTION: " << e.getMessage() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

<commit_msg>fix bug in distance exporter<commit_after>/*
 * Simka2ComputeKmerSpectrum.hpp
 *
 *  Created on: 4 nov. 2016
 *      Author: gbenoit
 */

#include <gatb/gatb_core.hpp>
//#include "../core/SimkaUtils.hpp"
#include "../simka2/Simka2Utils.hpp"
#include "SimkaDistanceMatrixBinary.hpp"
//#include "../core/SimkaUtils.hpp"
//#include "Simka2Database.hpp"
//#include "../minikc/MiniKC.hpp"
//#include <gatb/kmer/impl/RepartitionAlgorithm.hpp>
//#include <gatb/kmer/impl/ConfigurationAlgorithm.hpp>
//#include "SimkaAlgorithm.hpp"
//#include "SimkaAlgorithm.hpp"

/*********************************************************************
* ** SimkaAlgorithm
*********************************************************************/
class SimkaDistanceExporterAlgorithm : public Algorithm
{

public:

	string _dirBinaryMatrices;
	string _inputFilenameIds;
	string _outputDir;

	vector<string> _ids;
	vector<string> _wantedIds;
	vector<size_t> _wantedIdsIndex;
	map<string, size_t> _idToIndex;
	size_t _inputMatrixSize;
	size_t _outputMatrixSize;

	SimkaDistanceExporterAlgorithm(IProperties* options):
		Algorithm("simkaDistanceExporter", -1, options)
	{
		_inputFilenameIds = "";
	}

	void execute(){
		parseArgs();
		createWantedIds();
		createIdsIndex();
		writeMatrices();
	}

	void parseArgs(){

		_dirBinaryMatrices =  getInput()->getStr(STR_URI_INPUT);
		_outputDir =  getInput()->getStr(STR_URI_OUTPUT);

		if(getInput()->get(STR_SIMKA2_INPUT_IDS)){
			_inputFilenameIds =  getInput()->getStr(STR_SIMKA2_INPUT_IDS);
		}

		if(!System::file().doesExist(_outputDir)){
			System::file().mkdir(_outputDir, -1);
		}

	}

	void createWantedIds(){

		SimkaDistanceMatrixBinary::loadMatrixIds(_dirBinaryMatrices, _ids);

		if(_inputFilenameIds.empty()){
			_wantedIds = vector<string>(_ids);
		}
		else{
			string line;
			ifstream inputFile(_inputFilenameIds.c_str());

			while(getline(inputFile, line)){

				line.erase(std::remove(line.begin(),line.end(),' '),line.end());
				if(line == "") continue;

				_wantedIds.push_back(line);
			}
		}

		_inputMatrixSize = _ids.size();
		_outputMatrixSize = _wantedIds.size();
		cout << "original matrix size: " << _inputMatrixSize << endl;
		cout << "output matrix size: " << _outputMatrixSize << endl;
	}

	void createIdsIndex(){

		for(size_t i=0; i<_ids.size(); i++){
			_idToIndex[_ids[i]] = i;
		}

		_wantedIdsIndex.resize(_outputMatrixSize);
		for(size_t i=0; i<_outputMatrixSize; i++){
			_wantedIdsIndex[i] = _idToIndex[_wantedIds[i]];
		}
	}

	void writeMatrices(){
		vector<string> matrixFilenames = System::file().listdir(_dirBinaryMatrices);

		for(size_t i=0; i<matrixFilenames.size(); i++){
			string matrixFilename = _dirBinaryMatrices + "/" + matrixFilenames[i];
			if(matrixFilename.find("mat_") == string::npos) continue;

			string distanceName = matrixFilenames[i];
			string ext = ".bin";
			std::string::size_type it = distanceName.find(ext);
			distanceName.erase(it, ext.length());

			cout << distanceName << endl;
			writeMatrixASCII(distanceName, matrixFilename);
		}

	}

	void writeMatrixASCII(const string& distanceName, const string& binaryMatrixFilename){

		vector<float> rowData(_inputMatrixSize, 0);
		ifstream binaryMatrixFile(binaryMatrixFilename.c_str(), ios::binary);
		string filename = _outputDir + "/" + distanceName + ".csv";
		gzFile out = gzopen((filename + ".gz").c_str(),"wb");

		string str = "";

		for(size_t i=0; i<_outputMatrixSize; i++){
			str += ";" + _wantedIds[i];
		}
		str += '\n';
		gzwrite(out, str.c_str(), str.size());

		for(size_t i=0; i<_outputMatrixSize; i++){

			str = "";
			str += _wantedIds[i] + ";";

			size_t rowIndex = _wantedIdsIndex[i];
			SimkaDistanceMatrixBinary::loadRow(rowIndex, binaryMatrixFile, rowData);

			for(size_t j=0; j<_outputMatrixSize; j++){

				str += Stringify::format("%f", rowData[_wantedIdsIndex[j]]) + ";";

			}

			str.erase(str.size()-1);
			str += '\n';

			gzwrite(out, str.c_str(), str.size());
		}

		gzclose(out);
		binaryMatrixFile.close();
	}

	/*
	void outputMatrixToAscii(){
		string filename = outputDir + "/" + outputFilename + _outputFilenameSuffix + ".csv";
		//IFile* file = gatb::core::system::impl::System::file().newFile(filename, "wb");
		Bag<string>* bag = new BagGzFile<string>(outputDir + "/" + outputFilename + _outputFilenameSuffix + ".csv.gz");
		//Bag<string>* cachedBag = new BagCache<string>(bag, 10000);

		gzFile out = gzopen((filename + ".gz").c_str(),"wb");
		//char buf[BUFSIZ] = { 0 };

		//char buffer[200];
		string str;

		for(size_t i=0; i<matrix.size(); i++){
			str += ";" + bankNames[i];
			//str += ";" + datasetInfos[i]._name;
		}
		str += '\n';
		gzwrite(out, str.c_str(), str.size());
		//cachedBag->insert(str);
		//file->fwrite(str.c_str(), str.size(), 1);

		str = "";

		for(size_t i=0; i<matrix.size(); i++){

			str = "";
			str += bankNames[i] + ";";
			//str += datasetInfos[i]._name + ";";
			for(size_t j=0; j<matrix.size(); j++){

				//snprintf(buffer,200,"%.2f", matrix[i][j]);
				//snprintf(buffer,200,"%f", matrix[i][j]);
				str += Stringify::format("%f", matrix[i][j]) + ";";

				//str += to_string(matrix[i][j]) + ";";
			}

			//matrixNormalizedStr.erase(matrixNormalizedStr.end()-1);
			str.erase(str.size()-1);
			//str.pop_back(); //remove ; at the end of the line
			str += '\n';

			//cout << str << endl;
			//file->fwrite(str.c_str(), str.size(), 1);
			gzwrite(out, str.c_str(), str.size());
			//cachedBag->insert(str);
		}

		//gatb::core::system::IFile* file = gatb::core::system::impl::System::file().newFile(outputDir + "/" + outputFilename + _outputFilenameSuffix + ".csv", "wb");

		//file->flush();
		//delete file;
		gzclose(out);
		//delete cachedBag;

	}*/

};

class SimkaDistanceExporter: public Tool{
public:

	string _execFilename;

	SimkaDistanceExporter(string execFilename): Tool ("Simka-DistanceMatrixWriter"){
		_execFilename = execFilename;

	    IOptionsParser* parser = getParser();//new OptionsParser ("Simka2 - Compute Kmer Spectrum");

	    parser->push_front (new OptionOneParam (STR_URI_INPUT, "path to a dir of binary matrices", true));
	    parser->push_front (new OptionOneParam (STR_SIMKA2_INPUT_IDS, "filename of ids in the result matrix (one id per line). Do not used this option to used all ids.", false));
	    parser->push_front (new OptionOneParam (STR_URI_OUTPUT, "output dir for distance matrices", true));

	    //parser->push_front (new OptionOneParam (STR_URI_OUTPUT, "output directory for merged kmer spectrum", true));
	   // parser->push_front (new OptionOneParam (STR_SIMKA2_DATABASE_DIR, "dir path to a simka database", true));
	    //parser->push_front (new OptionOneParam (STR_SIMKA2_NB_PARTITION, "number of partitions", true));
	    //parser->push_front (new OptionOneParam (STR_SIMKA2_PARTITION_ID, "number of the partition", true));
	    //parser->push_front (new OptionOneParam (STR_SIMKA2_DISTANCE_MATRIX_DIR, "input filename of k-mer spectrums for which distances has to be computed", true));

	   // IOptionsParser* kmerParser = new OptionsParser ("kmer");
	    //kmerParser->push_back (new OptionOneParam (STR_KMER_SIZE, "size of a kmer", true));

	    //parser->getParser(STR_NB_CORES)->setVisible(false);
		//parser->push_back(kmerParser);

	}

	void execute ()
	{
		IProperties* input = getInput();
		//Parameter params(*this, getInput());
		//Parameter params(input, _execFilename);

		//size_t kmerSize = getInput()->getInt (STR_KMER_SIZE);

	    //Integer::apply<Functor,Parameter> (kmerSize, params);

		SimkaDistanceExporterAlgorithm* algo = new SimkaDistanceExporterAlgorithm(input);
		algo->execute();
		delete algo;
	}

};

int main (int argc, char* argv[])
{
    try
    {
    	SimkaDistanceExporter(string(argv[0])).run (argc, argv);
    }
    catch (Exception& e)
    {
        std::cout << "EXCEPTION: " << e.getMessage() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

<|endoftext|>


<commit_before>/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#ifndef HAVE_LIBVLC
# error This file requires libvlc
#endif

#include "Directory.h"
#include "File.h"
#include "utils/Filename.h"
#include "utils/VLCInstance.h"

#include "compat/ConditionVariable.h"
#include "compat/Mutex.h"

#include <vlcpp/vlc.hpp>

namespace medialibrary
{
namespace fs
{

NetworkDirectory::NetworkDirectory( const std::string& mrl, fs::IFileSystemFactory& fsFactory )
    : CommonDirectory( fsFactory )
    , m_mrl( utils::file::toFolderPath( mrl ) )
{
}

const std::string& NetworkDirectory::mrl() const
{
    return m_mrl;
}

void NetworkDirectory::read() const
{
    VLC::Media media( VLCInstance::get(), m_mrl, VLC::Media::FromLocation );
    assert( media.parsedStatus() != VLC::Media::ParsedStatus::Done );

    compat::Mutex mutex;
    compat::ConditionVariable cond;
    VLC::Media::ParsedStatus res = VLC::Media::ParsedStatus::Skipped;

    auto eventHandler = media.eventManager().onParsedChanged(
        [&mutex, &cond, &res]( VLC::Media::ParsedStatus status) {
            std::lock_guard<compat::Mutex> lock( mutex );
            res = status;
            cond.notify_all();
        });

    std::unique_lock<compat::Mutex> lock( mutex );
    media.parseWithOptions( VLC::Media::ParseFlags::Network |
                            VLC::Media::ParseFlags::Local, -1 );
    bool timeout = cond.wait_for( lock, std::chrono::seconds{ 5 }, [&res]() {
        return res != VLC::Media::ParsedStatus::Skipped;
    });
    eventHandler->unregister();
    if ( timeout == true )
        throw std::system_error( ETIMEDOUT, std::generic_category(),
                                 "Failed to browse network directory: Network is too slow" );
    if ( res == VLC::Media::ParsedStatus::Failed )
        throw std::system_error( EIO, std::generic_category(),
                                 "Failed to browse network directory: Unknown error" );
    auto subItems = media.subitems();
    for ( auto i = 0; i < subItems->count(); ++i )
    {
        auto m = subItems->itemAtIndex( i );
        if ( m->type() == VLC::Media::Type::Directory )
            m_dirs.push_back( std::make_shared<fs::NetworkDirectory>( m->mrl(), m_fsFactory ) );
        else
            m_files.push_back( std::make_shared<fs::NetworkFile>( m->mrl() ) );
    }
}

}
}
<commit_msg>fs: Network: Directory: Fix inverted logic<commit_after>/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#ifndef HAVE_LIBVLC
# error This file requires libvlc
#endif

#include "Directory.h"
#include "File.h"
#include "utils/Filename.h"
#include "utils/VLCInstance.h"

#include "compat/ConditionVariable.h"
#include "compat/Mutex.h"

#include <vlcpp/vlc.hpp>

namespace medialibrary
{
namespace fs
{

NetworkDirectory::NetworkDirectory( const std::string& mrl, fs::IFileSystemFactory& fsFactory )
    : CommonDirectory( fsFactory )
    , m_mrl( utils::file::toFolderPath( mrl ) )
{
}

const std::string& NetworkDirectory::mrl() const
{
    return m_mrl;
}

void NetworkDirectory::read() const
{
    VLC::Media media( VLCInstance::get(), m_mrl, VLC::Media::FromLocation );
    assert( media.parsedStatus() != VLC::Media::ParsedStatus::Done );

    compat::Mutex mutex;
    compat::ConditionVariable cond;
    VLC::Media::ParsedStatus res = VLC::Media::ParsedStatus::Skipped;

    auto eventHandler = media.eventManager().onParsedChanged(
        [&mutex, &cond, &res]( VLC::Media::ParsedStatus status) {
            std::lock_guard<compat::Mutex> lock( mutex );
            res = status;
            cond.notify_all();
        });

    std::unique_lock<compat::Mutex> lock( mutex );
    media.parseWithOptions( VLC::Media::ParseFlags::Network |
                            VLC::Media::ParseFlags::Local, -1 );
    bool success = cond.wait_for( lock, std::chrono::seconds{ 5 }, [&res]() {
        return res != VLC::Media::ParsedStatus::Skipped;
    });
    eventHandler->unregister();
    if ( success == false )
        throw std::system_error( ETIMEDOUT, std::generic_category(),
                                 "Failed to browse network directory: Network is too slow" );
    if ( res == VLC::Media::ParsedStatus::Failed )
        throw std::system_error( EIO, std::generic_category(),
                                 "Failed to browse network directory: Unknown error" );
    auto subItems = media.subitems();
    for ( auto i = 0; i < subItems->count(); ++i )
    {
        auto m = subItems->itemAtIndex( i );
        if ( m->type() == VLC::Media::Type::Directory )
            m_dirs.push_back( std::make_shared<fs::NetworkDirectory>( m->mrl(), m_fsFactory ) );
        else
            m_files.push_back( std::make_shared<fs::NetworkFile>( m->mrl() ) );
    }
}

}
}
<|endoftext|>


<commit_before>/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
*                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/

#include <sofa/component/topology/DynamicSparseGridTopologyContainer.h>
#include <sofa/core/ObjectFactory.h>

#include <sofa/component/container/VoxelGridLoader.h>

namespace sofa
{
namespace component
{
namespace topology
{

using namespace std;
using namespace sofa::defaulttype;

SOFA_DECL_CLASS ( DynamicSparseGridTopologyContainer );
int DynamicSparseGridTopologyContainerClass = core::RegisterObject ( "Hexahedron set topology container" )
        .add< DynamicSparseGridTopologyContainer >()
        ;

DynamicSparseGridTopologyContainer::DynamicSparseGridTopologyContainer()
    : HexahedronSetTopologyContainer()
    , resolution ( initData ( &resolution, Vec3i ( 0, 0, 0 ), "resolution", "voxel grid resolution" ) )
{
}

DynamicSparseGridTopologyContainer::DynamicSparseGridTopologyContainer ( const sofa::helper::vector< Hexahedron > &hexahedra )
    : HexahedronSetTopologyContainer ( hexahedra )
    , resolution ( initData ( &resolution, Vec3i ( 0, 0, 0 ), "resolution", "voxel grid resolution" ) )
{
}

void DynamicSparseGridTopologyContainer::loadFromMeshLoader ( sofa::component::MeshLoader* loader )
{
    HexahedronSetTopologyContainer::loadFromMeshLoader( loader);
    sofa::component::VoxelGridLoader* voxelGridLoader = dynamic_cast< sofa::component::VoxelGridLoader*> ( loader );
    if ( !voxelGridLoader )
    {
        cerr << "DynamicSparseGridTopologyContainer::loadFromMeshLoader(): The loader used is not a VoxelGridLoader ! You must use it for this topology." << endl;
        exit(0);
    }

    // Init regular/topo mapping
    helper::vector<BaseMeshTopology::HexaID>& iirg = *(idxInRegularGrid.beginEdit());
    voxelGridLoader->getIndicesInRegularGrid( iirg);
    for( unsigned int i = 0; i < iirg.size(); i++)
    {
        idInRegularGrid2IndexInTopo.insert( make_pair( iirg[i], i ));
    }
    idxInRegularGrid.endEdit();

    // Init values
    int dataSize = voxelGridLoader->getDataSize();
    unsigned char* data = voxelGridLoader->getData();

    // init values in regular grid. (dense).
    valuesIndexedInRegularGrid.resize( dataSize);
    for( int i = 0; i < dataSize; i++)
        valuesIndexedInRegularGrid[i] = data[i];

    // init values in topo. (pas dense).
    helper::vector<unsigned char>& viit = *(valuesIndexedInTopology.beginEdit());
    viit.resize( iirg.size());
    for( int i = 0; i < iirg.size(); i++)
    {
        viit[i] = data[iirg[i]];
    }
    valuesIndexedInTopology.endEdit();

    // init resolution & voxelSize.
    Vec3i& res = *resolution.beginEdit();
    voxelGridLoader->getResolution ( res );
    resolution.endEdit();

    voxelGridLoader->getVoxelSize ( voxelSize );
}

} // namespace topology

} // namespace component

} // namespace sofa

<commit_msg>r4096/sofa-dev : FIX: compilation warnings<commit_after>/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
*                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/

#include <sofa/component/topology/DynamicSparseGridTopologyContainer.h>
#include <sofa/core/ObjectFactory.h>

#include <sofa/component/container/VoxelGridLoader.h>

namespace sofa
{
namespace component
{
namespace topology
{

using namespace std;
using namespace sofa::defaulttype;

SOFA_DECL_CLASS ( DynamicSparseGridTopologyContainer );
int DynamicSparseGridTopologyContainerClass = core::RegisterObject ( "Hexahedron set topology container" )
        .add< DynamicSparseGridTopologyContainer >()
        ;

DynamicSparseGridTopologyContainer::DynamicSparseGridTopologyContainer()
    : HexahedronSetTopologyContainer()
    , resolution ( initData ( &resolution, Vec3i ( 0, 0, 0 ), "resolution", "voxel grid resolution" ) )
{
}

DynamicSparseGridTopologyContainer::DynamicSparseGridTopologyContainer ( const sofa::helper::vector< Hexahedron > &hexahedra )
    : HexahedronSetTopologyContainer ( hexahedra )
    , resolution ( initData ( &resolution, Vec3i ( 0, 0, 0 ), "resolution", "voxel grid resolution" ) )
{
}

void DynamicSparseGridTopologyContainer::loadFromMeshLoader ( sofa::component::MeshLoader* loader )
{
    HexahedronSetTopologyContainer::loadFromMeshLoader( loader);
    sofa::component::VoxelGridLoader* voxelGridLoader = dynamic_cast< sofa::component::VoxelGridLoader*> ( loader );
    if ( !voxelGridLoader )
    {
        cerr << "DynamicSparseGridTopologyContainer::loadFromMeshLoader(): The loader used is not a VoxelGridLoader ! You must use it for this topology." << endl;
        exit(0);
    }

    // Init regular/topo mapping
    helper::vector<BaseMeshTopology::HexaID>& iirg = *(idxInRegularGrid.beginEdit());
    voxelGridLoader->getIndicesInRegularGrid( iirg);
    for( unsigned int i = 0; i < iirg.size(); i++)
    {
        idInRegularGrid2IndexInTopo.insert( make_pair( iirg[i], i ));
    }
    idxInRegularGrid.endEdit();

    // Init values
    int dataSize = voxelGridLoader->getDataSize();
    unsigned char* data = voxelGridLoader->getData();

    // init values in regular grid. (dense).
    valuesIndexedInRegularGrid.resize( dataSize);
    for( int i = 0; i < dataSize; i++)
        valuesIndexedInRegularGrid[i] = data[i];

    // init values in topo. (pas dense).
    helper::vector<unsigned char>& viit = *(valuesIndexedInTopology.beginEdit());
    viit.resize( iirg.size());
    for(unsigned int i = 0; i < iirg.size(); i++)
    {
        viit[i] = data[iirg[i]];
    }
    valuesIndexedInTopology.endEdit();

    // init resolution & voxelSize.
    Vec3i& res = *resolution.beginEdit();
    voxelGridLoader->getResolution ( res );
    resolution.endEdit();

    voxelGridLoader->getVoxelSize ( voxelSize );
}

} // namespace topology

} // namespace component

} // namespace sofa

<|endoftext|>


<commit_before>// Reverse order of vector<int>

#include "../../std_lib_facilities.h"

int main() {
  return 0;
}<commit_msg>Reversing order of vector<commit_after>// Reverse order of vector<int>

#include "../../std_lib_facilities.h"
#include <exception> // std::exception

void reverseOrder(vector<int> t, vector<int> &res) {
  while (!t.empty())
  {
    res.push_back(t.back());
    t.pop_back();
  }
}

void print(const string &outputLabel, const vector<int> &vec)
{
  cout << outputLabel << " ";
  for (auto i : vec)
  {
    cout << i << " ";
  }
  cout << endl;
}

int main() {
  try {
    vector<int> normal = {1,2,3,4,5,6,7,8,9};
    vector<int> reversed;

    reverseOrder(normal, reversed);

    cout << "We'll try to reverse order of vector ";
    print(">> ", normal);
    print("res >> ", reversed);
    print("normal >> ", normal);
  } catch (std::exception& e) {
    cerr << e.what() << endl;
    return 1;
  }
  return 0;
}<|endoftext|>


<commit_before>// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/test_kit.h>

#include <vespa/searchlib/attribute/attributefactory.h>
#include <vespa/searchlib/fef/test/ftlib.h>
#include <vespa/searchlib/features/max_reduce_prod_join_feature.h>
#include <vespa/searchlib/attribute/attribute.h>

using search::feature_t;
using namespace search::fef;
using namespace search::fef::test;
using namespace search::features;
using search::AttributeFactory;
using search::IntegerAttribute;
using CollectionType = FieldInfo::CollectionType;

typedef search::attribute::Config AVC;
typedef search::attribute::BasicType AVBT;
typedef search::attribute::CollectionType AVCT;
typedef search::AttributeVector::SP AttributePtr;
typedef FtTestApp FTA;

struct SetupFixture
{
    InternalMaxReduceProdJoinBlueprint blueprint;
    IndexEnvironment indexEnv;
    SetupFixture(const vespalib::string &attr)
        : blueprint(),
          indexEnv()
    {
        FieldInfo attr_info(FieldType::ATTRIBUTE, CollectionType::ARRAY, attr, 0);
        indexEnv.getFields().push_back(attr_info);
    }
};

TEST_F("require that blueprint can be created", SetupFixture("attribute(foo)"))
{
    EXPECT_TRUE(FTA::assertCreateInstance(f.blueprint, "internalMaxReduceProdJoin"));
}

TEST_F("require that setup fails if source spec is invalid", SetupFixture("attribute(foo)"))
{
    FTA::FT_SETUP_FAIL(f.blueprint, f.indexEnv, StringList().add("source(foo)"));
}

TEST_F("require that setup fails if attribute does not exist", SetupFixture("attribute(foo)"))
{
    FTA::FT_SETUP_FAIL(f.blueprint, f.indexEnv, StringList().add("attribute(bar)").add("query(baz)"));
}

TEST_F("require that setup succeeds with attribute and query parameters", SetupFixture("attribute(foo)"))
{
    FTA::FT_SETUP_OK(f.blueprint, f.indexEnv,
                    StringList().add("attribute(foo)").add("query(bar)"),
                    StringList(),
                    StringList().add("scalar"));
}

struct ExecFixture
{
    BlueprintFactory factory;
    FtFeatureTest test;
    vespalib::string feature;
    ExecFixture(const vespalib::string &f)
            : factory(),
              test(factory, f),
              feature(f)
    {
        factory.addPrototype(std::make_shared<InternalMaxReduceProdJoinBlueprint>());
        setupAttributeVectors();
        setupQueryEnvironment();
        ASSERT_TRUE(test.setup());
    }

    void setupAttributeVectors() {
        vespalib::string attrIntArray = "attribute(intarray)";
        vespalib::string attrLongArray = "attribute(longarray)";

        test.getIndexEnv().getBuilder().addField(FieldType::ATTRIBUTE, CollectionType::ARRAY, attrLongArray);
        test.getIndexEnv().getBuilder().addField(FieldType::ATTRIBUTE, CollectionType::ARRAY, attrIntArray);

        std::vector<AttributePtr> attrs;
        attrs.push_back(AttributeFactory::createAttribute(attrLongArray, AVC(AVBT::INT64,  AVCT::ARRAY)));
        attrs.push_back(AttributeFactory::createAttribute(attrIntArray, AVC(AVBT::INT32,  AVCT::ARRAY)));
        for (const auto &attr : attrs) {
            attr->addReservedDoc();
            attr->addDocs(1);
            test.getIndexEnv().getAttributeMap().add(attr);
        }

        IntegerAttribute *longArray = static_cast<IntegerAttribute *>(attrs[0].get());
        longArray->append(1, 1111, 0);
        longArray->append(1, 2222, 0);
        longArray->append(1, 78, 0);

        IntegerAttribute *intArray = static_cast<IntegerAttribute *>(attrs[1].get());
        intArray->append(1, 78, 0);
        intArray->append(1, 1111, 0);

        for (const auto &attr : attrs) {
            attr->commit();
        }
    }

    void setupQueryEnvironment() {
        test.getQueryEnv().getProperties().add("query(wset)", "{1111:1234, 2222:2245}");
        test.getQueryEnv().getProperties().add("query(wsetnomatch)", "{1:1000, 2:2000}");
        test.getQueryEnv().getProperties().add("query(array)", "[1111,2222]");
    }

    bool evaluatesTo(feature_t expected) {
        return test.execute(expected);
    }

};

TEST_F("require that executor returns correct result for long array",
       ExecFixture("internalMaxReduceProdJoin(attribute(longarray),query(wset))"))
{
    EXPECT_TRUE(f.evaluatesTo(2245));
}

TEST_F("require that executor returns correct result for int array",
       ExecFixture("internalMaxReduceProdJoin(attribute(intarray),query(wset))"))
{
    EXPECT_TRUE(f.evaluatesTo(1234));
}

TEST_F("require that executor returns 0 if no items match",
       ExecFixture("internalMaxReduceProdJoin(attribute(longarray),query(wsetnomatch))"))
{
    EXPECT_TRUE(f.evaluatesTo(0.0));
}

TEST_F("require that executor return 0 if query is not a weighted set",
       ExecFixture("internalMaxReduceProdJoin(attribute(longarray),query(array))"))
{
    EXPECT_TRUE(f.evaluatesTo(0.0));
}

TEST_MAIN() { TEST_RUN_ALL(); }
<commit_msg>Remove unnecessary feature name in test fixture<commit_after>// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/test_kit.h>

#include <vespa/searchlib/attribute/attributefactory.h>
#include <vespa/searchlib/fef/test/ftlib.h>
#include <vespa/searchlib/features/max_reduce_prod_join_feature.h>
#include <vespa/searchlib/attribute/attribute.h>

using search::feature_t;
using namespace search::fef;
using namespace search::fef::test;
using namespace search::features;
using search::AttributeFactory;
using search::IntegerAttribute;
using CollectionType = FieldInfo::CollectionType;

typedef search::attribute::Config AVC;
typedef search::attribute::BasicType AVBT;
typedef search::attribute::CollectionType AVCT;
typedef search::AttributeVector::SP AttributePtr;
typedef FtTestApp FTA;

struct SetupFixture
{
    InternalMaxReduceProdJoinBlueprint blueprint;
    IndexEnvironment indexEnv;
    SetupFixture(const vespalib::string &attr)
        : blueprint(),
          indexEnv()
    {
        FieldInfo attr_info(FieldType::ATTRIBUTE, CollectionType::ARRAY, attr, 0);
        indexEnv.getFields().push_back(attr_info);
    }
};

TEST_F("require that blueprint can be created", SetupFixture("attribute(foo)"))
{
    EXPECT_TRUE(FTA::assertCreateInstance(f.blueprint, "internalMaxReduceProdJoin"));
}

TEST_F("require that setup fails if source spec is invalid", SetupFixture("attribute(foo)"))
{
    FTA::FT_SETUP_FAIL(f.blueprint, f.indexEnv, StringList().add("source(foo)"));
}

TEST_F("require that setup fails if attribute does not exist", SetupFixture("attribute(foo)"))
{
    FTA::FT_SETUP_FAIL(f.blueprint, f.indexEnv, StringList().add("attribute(bar)").add("query(baz)"));
}

TEST_F("require that setup succeeds with attribute and query parameters", SetupFixture("attribute(foo)"))
{
    FTA::FT_SETUP_OK(f.blueprint, f.indexEnv,
                    StringList().add("attribute(foo)").add("query(bar)"),
                    StringList(),
                    StringList().add("scalar"));
}

struct ExecFixture
{
    BlueprintFactory factory;
    FtFeatureTest test;
    ExecFixture(const vespalib::string &feature)
            : factory(),
              test(factory, feature)
    {
        factory.addPrototype(std::make_shared<InternalMaxReduceProdJoinBlueprint>());
        setupAttributeVectors();
        setupQueryEnvironment();
        ASSERT_TRUE(test.setup());
    }

    void setupAttributeVectors() {
        vespalib::string attrIntArray = "attribute(intarray)";
        vespalib::string attrLongArray = "attribute(longarray)";

        test.getIndexEnv().getBuilder().addField(FieldType::ATTRIBUTE, CollectionType::ARRAY, attrLongArray);
        test.getIndexEnv().getBuilder().addField(FieldType::ATTRIBUTE, CollectionType::ARRAY, attrIntArray);

        std::vector<AttributePtr> attrs;
        attrs.push_back(AttributeFactory::createAttribute(attrLongArray, AVC(AVBT::INT64,  AVCT::ARRAY)));
        attrs.push_back(AttributeFactory::createAttribute(attrIntArray, AVC(AVBT::INT32,  AVCT::ARRAY)));
        for (const auto &attr : attrs) {
            attr->addReservedDoc();
            attr->addDocs(1);
            test.getIndexEnv().getAttributeMap().add(attr);
        }

        IntegerAttribute *longArray = static_cast<IntegerAttribute *>(attrs[0].get());
        longArray->append(1, 1111, 0);
        longArray->append(1, 2222, 0);
        longArray->append(1, 78, 0);

        IntegerAttribute *intArray = static_cast<IntegerAttribute *>(attrs[1].get());
        intArray->append(1, 78, 0);
        intArray->append(1, 1111, 0);

        for (const auto &attr : attrs) {
            attr->commit();
        }
    }

    void setupQueryEnvironment() {
        test.getQueryEnv().getProperties().add("query(wset)", "{1111:1234, 2222:2245}");
        test.getQueryEnv().getProperties().add("query(wsetnomatch)", "{1:1000, 2:2000}");
        test.getQueryEnv().getProperties().add("query(array)", "[1111,2222]");
    }

    bool evaluatesTo(feature_t expected) {
        return test.execute(expected);
    }

};

TEST_F("require that executor returns correct result for long array",
       ExecFixture("internalMaxReduceProdJoin(attribute(longarray),query(wset))"))
{
    EXPECT_TRUE(f.evaluatesTo(2245));
}

TEST_F("require that executor returns correct result for int array",
       ExecFixture("internalMaxReduceProdJoin(attribute(intarray),query(wset))"))
{
    EXPECT_TRUE(f.evaluatesTo(1234));
}

TEST_F("require that executor returns 0 if no items match",
       ExecFixture("internalMaxReduceProdJoin(attribute(longarray),query(wsetnomatch))"))
{
    EXPECT_TRUE(f.evaluatesTo(0.0));
}

TEST_F("require that executor return 0 if query is not a weighted set",
       ExecFixture("internalMaxReduceProdJoin(attribute(longarray),query(array))"))
{
    EXPECT_TRUE(f.evaluatesTo(0.0));
}

TEST_MAIN() { TEST_RUN_ALL(); }
<|endoftext|>


<commit_before>#include <fstream>

#include "gtest.h"

#include <common_types.hpp>
#include <cell.hpp>
#include <fvm_multicell.hpp>
#include <util/rangeutil.hpp>

#include "../test_util.hpp"
#include "../test_common_cells.hpp"

TEST(fvm_multi, cable)
{
    using namespace nest::mc;

    nest::mc::cell cell=make_cell_ball_and_3sticks();

    using fvm_cell = fvm::fvm_multicell<double, cell_lid_type>;

    std::vector<fvm_cell::target_handle> targets;
    std::vector<fvm_cell::detector_handle> detectors;
    std::vector<fvm_cell::probe_handle> probes;

    fvm_cell fvcell;
    fvcell.initialize(util::singleton_view(cell), detectors, targets, probes);

    auto& J = fvcell.jacobian();

    // 1 (soma) + 3 (dendritic segments) × 4 compartments
    EXPECT_EQ(cell.num_compartments(), 13u);

    // assert that the matrix has one row for each compartment
    EXPECT_EQ(J.size(), cell.num_compartments());

    fvcell.setup_matrix(0.02);

    // assert that the number of cv areas is the same as the matrix size
    // i.e. both should equal the number of compartments
    EXPECT_EQ(fvcell.cv_areas().size(), J.size());
}

TEST(fvm_multi, init)
{
    using namespace nest::mc;

    nest::mc::cell cell = make_cell_ball_and_stick();

    const auto m = cell.model();
    EXPECT_EQ(m.tree.num_segments(), 2u);

    auto& soma_hh = cell.soma()->mechanism("hh");

    soma_hh.set("gnabar", 0.12);
    soma_hh.set("gkbar", 0.036);
    soma_hh.set("gl", 0.0003);
    soma_hh.set("el", -54.3);

    // check that parameter values were set correctly
    EXPECT_EQ(cell.soma()->mechanism("hh").get("gnabar").value, 0.12);
    EXPECT_EQ(cell.soma()->mechanism("hh").get("gkbar").value, 0.036);
    EXPECT_EQ(cell.soma()->mechanism("hh").get("gl").value, 0.0003);
    EXPECT_EQ(cell.soma()->mechanism("hh").get("el").value, -54.3);

    cell.segment(1)->set_compartments(10);

    using fvm_cell = fvm::fvm_multicell<double, cell_lid_type>;
    std::vector<fvm_cell::target_handle> targets;
    std::vector<fvm_cell::detector_handle> detectors;
    std::vector<fvm_cell::probe_handle> probes;

    fvm_cell fvcell;
    fvcell.initialize(util::singleton_view(cell), detectors, targets, probes);

    auto& J = fvcell.jacobian();
    EXPECT_EQ(J.size(), 11u);

    fvcell.setup_matrix(0.01);
}

TEST(fvm_multi, multi_init)
{
    using namespace nest::mc;

    nest::mc::cell cells[] = {
        make_cell_ball_and_stick(),
        make_cell_ball_and_3sticks()
    };

    EXPECT_EQ(cells[0].num_segments(), 2u);
    EXPECT_EQ(cells[0].segment(1)->num_compartments(), 4u);
    EXPECT_EQ(cells[1].num_segments(), 4u);
    EXPECT_EQ(cells[1].segment(1)->num_compartments(), 4u);
    EXPECT_EQ(cells[1].segment(2)->num_compartments(), 4u);
    EXPECT_EQ(cells[1].segment(3)->num_compartments(), 4u);

    cells[0].add_synapse({1, 0.4}, parameter_list("expsyn"));
    cells[0].add_synapse({1, 0.4}, parameter_list("expsyn"));
    cells[1].add_synapse({2, 0.4}, parameter_list("exp2syn"));
    cells[1].add_synapse({3, 0.4}, parameter_list("expsyn"));

    cells[1].add_detector({0, 0}, 3.3);

    using fvm_cell = fvm::fvm_multicell<double, cell_lid_type>;
    std::vector<fvm_cell::target_handle> targets(4);
    std::vector<fvm_cell::detector_handle> detectors(1);
    std::vector<fvm_cell::probe_handle> probes;

    fvm_cell fvcell;
    fvcell.initialize(cells, detectors, targets, probes);

    auto& J = fvcell.jacobian();
    EXPECT_EQ(J.size(), 5u+13u);

    // check indices in instantiated mechanisms
    for (const auto& mech: fvcell.mechanisms()) {
        if (mech->name()=="hh") {
            // HH on somas of two cells, with group compartment indices
            // 0 and 5.
            ASSERT_EQ(mech->node_index().size(), 2u);
            EXPECT_EQ(mech->node_index()[0], 0u);
            EXPECT_EQ(mech->node_index()[1], 5u);
        }
        if (mech->name()=="expsyn") {
            // Three expsyn synapses, two in second compartment
            // of dendrite segment of first cell, one in second compartment
            // of last segment of second cell.
            ASSERT_EQ(mech->node_index().size(), 3u);
            EXPECT_EQ(mech->node_index()[0], 2u);
            EXPECT_EQ(mech->node_index()[1], 2u);
            EXPECT_EQ(mech->node_index()[2], 15u);
        }
        if (mech->name()=="exp2syn") {
            // One exp2syn synapse, in second compartment
            // of penultimate segment of second cell.
            ASSERT_EQ(mech->node_index().size(), 1u);
            EXPECT_EQ(mech->node_index()[0], 11u);
        }
    }

    fvcell.setup_matrix(0.01);
}

// test that stimuli are added correctly
TEST(fvm_multi, stimulus)
{
    using namespace nest::mc;
    using util::singleton_view;

    // the default ball and stick has one stimulus at the terminal end of the dendrite
    auto cell = make_cell_ball_and_stick();

    // ... so add a second at the soma to make things more interesting
    cell.add_stimulus({0,0.5}, {1., 2., 0.1});

    // now we have two stims :
    //
    //           |stim0 |stim1
    // -----------------------
    // delay     |   5  |    1
    // duration  |  80  |    2
    // amplitude | 0.3  |  0.1
    // compmnt   |   4  |    0

    using fvm_cell = fvm::fvm_multicell<double, cell_lid_type>;
    std::vector<fvm_cell::target_handle> targets;
    std::vector<fvm_cell::detector_handle> detectors;
    std::vector<fvm_cell::probe_handle> probes;

    fvm_cell fvcell;
    fvcell.initialize(singleton_view(cell), detectors, targets, probes);

    auto& stim = fvcell.stimuli();
    EXPECT_EQ(stim.size(), 2u);

    EXPECT_EQ(stim[0].first, 4u);
    EXPECT_EQ(stim[1].first, 0u);

    EXPECT_EQ(stim[0].second.delay(), 5.);
    EXPECT_EQ(stim[1].second.delay(), 1.);
    EXPECT_EQ(stim[0].second.duration(), 80.);
    EXPECT_EQ(stim[1].second.duration(),  2.);
    EXPECT_EQ(stim[0].second.amplitude(), 0.3);
    EXPECT_EQ(stim[1].second.amplitude(), 0.1);
}

// test that mechanism indexes are computed correctly
TEST(fvm_multi, mechanism_indexes)
{
    using namespace nest::mc;

    // create a cell with 4 sements:
    // a soma with a branching dendrite
    // - hh on soma and first branch of dendrite (segs 0 and 2)
    // - pas on main dendrite and second branch (segs 1 and 3)
    //
    //              /
    //             pas
    //            /
    // hh---pas--.
    //            \.
    //             hh
    //              \.

    cell c;
    auto soma = c.add_soma(12.6157/2.0);
    soma->add_mechanism(hh_parameters());

    // add dendrite of length 200 um and diameter 1 um with passive channel
    c.add_cable(0, segmentKind::dendrite, 0.5, 0.5, 100);
    c.add_cable(1, segmentKind::dendrite, 0.5, 0.5, 100);
    c.add_cable(1, segmentKind::dendrite, 0.5, 0.5, 100);

    auto& segs = c.segments();
    segs[1]->add_mechanism(pas_parameters());
    segs[2]->add_mechanism(hh_parameters());
    segs[3]->add_mechanism(pas_parameters());

    for (auto& seg: segs) {
        if (seg->is_dendrite()) {
            seg->mechanism("membrane").set("r_L", 100);
            seg->set_compartments(4);
        }
    }

    // generate the lowered fvm cell
    using fvm_cell = fvm::fvm_multicell<double, cell_lid_type>;
    std::vector<fvm_cell::target_handle> targets;
    std::vector<fvm_cell::detector_handle> detectors;
    std::vector<fvm_cell::probe_handle> probes;

    fvm_cell fvcell;
    fvcell.initialize(util::singleton_view(c), detectors, targets, probes);

    // make vectors with the expected CV indexes for each mechanism
    std::vector<unsigned> hh_index  = {0u, 4u, 5u, 6u, 7u, 8u};
    std::vector<unsigned> pas_index = {0u, 1u, 2u, 3u, 4u, 9u, 10u, 11u, 12u};
    // iterate over mechanisms and test whether they were assigned to the correct CVs
    // TODO : this fails because we do not handle CVs at branching points (including soma) correctly
    for(auto& mech : fvcell.mechanisms()) {
        auto const& ni = mech->node_index();
        if(mech->name()=="hh") {
            EXPECT_EQ(ni.size(), hh_index.size());
            // only check index by index if size of index arrays match
            if(ni.size()==hh_index.size()) {
                for(auto i : util::make_span(0u, ni.size())) EXPECT_EQ(ni[i], hh_index[i]);
            }
        }
        else if(mech->name()=="pas") {
            EXPECT_EQ(ni.size(), pas_index.size());
            // only check index by index if size of index arrays match
            if(ni.size()==pas_index.size()) {
                for(auto i : util::make_span(0u, ni.size())) EXPECT_EQ(ni[i], pas_index[i]);
            }
        }
    }
}
<commit_msg>address review comments for PR #22<commit_after>#include <fstream>

#include "gtest.h"

#include <common_types.hpp>
#include <cell.hpp>
#include <fvm_multicell.hpp>
#include <util/rangeutil.hpp>

#include "../test_util.hpp"
#include "../test_common_cells.hpp"

TEST(fvm_multi, cable)
{
    using namespace nest::mc;

    nest::mc::cell cell=make_cell_ball_and_3sticks();

    using fvm_cell = fvm::fvm_multicell<double, cell_lid_type>;

    std::vector<fvm_cell::target_handle> targets;
    std::vector<fvm_cell::detector_handle> detectors;
    std::vector<fvm_cell::probe_handle> probes;

    fvm_cell fvcell;
    fvcell.initialize(util::singleton_view(cell), detectors, targets, probes);

    auto& J = fvcell.jacobian();

    // 1 (soma) + 3 (dendritic segments) × 4 compartments
    EXPECT_EQ(cell.num_compartments(), 13u);

    // assert that the matrix has one row for each compartment
    EXPECT_EQ(J.size(), cell.num_compartments());

    fvcell.setup_matrix(0.02);

    // assert that the number of cv areas is the same as the matrix size
    // i.e. both should equal the number of compartments
    EXPECT_EQ(fvcell.cv_areas().size(), J.size());
}

TEST(fvm_multi, init)
{
    using namespace nest::mc;

    nest::mc::cell cell = make_cell_ball_and_stick();

    const auto m = cell.model();
    EXPECT_EQ(m.tree.num_segments(), 2u);

    auto& soma_hh = cell.soma()->mechanism("hh");

    soma_hh.set("gnabar", 0.12);
    soma_hh.set("gkbar", 0.036);
    soma_hh.set("gl", 0.0003);
    soma_hh.set("el", -54.3);

    // check that parameter values were set correctly
    EXPECT_EQ(cell.soma()->mechanism("hh").get("gnabar").value, 0.12);
    EXPECT_EQ(cell.soma()->mechanism("hh").get("gkbar").value, 0.036);
    EXPECT_EQ(cell.soma()->mechanism("hh").get("gl").value, 0.0003);
    EXPECT_EQ(cell.soma()->mechanism("hh").get("el").value, -54.3);

    cell.segment(1)->set_compartments(10);

    using fvm_cell = fvm::fvm_multicell<double, cell_lid_type>;
    std::vector<fvm_cell::target_handle> targets;
    std::vector<fvm_cell::detector_handle> detectors;
    std::vector<fvm_cell::probe_handle> probes;

    fvm_cell fvcell;
    fvcell.initialize(util::singleton_view(cell), detectors, targets, probes);

    auto& J = fvcell.jacobian();
    EXPECT_EQ(J.size(), 11u);

    fvcell.setup_matrix(0.01);
}

TEST(fvm_multi, multi_init)
{
    using namespace nest::mc;

    nest::mc::cell cells[] = {
        make_cell_ball_and_stick(),
        make_cell_ball_and_3sticks()
    };

    EXPECT_EQ(cells[0].num_segments(), 2u);
    EXPECT_EQ(cells[0].segment(1)->num_compartments(), 4u);
    EXPECT_EQ(cells[1].num_segments(), 4u);
    EXPECT_EQ(cells[1].segment(1)->num_compartments(), 4u);
    EXPECT_EQ(cells[1].segment(2)->num_compartments(), 4u);
    EXPECT_EQ(cells[1].segment(3)->num_compartments(), 4u);

    cells[0].add_synapse({1, 0.4}, parameter_list("expsyn"));
    cells[0].add_synapse({1, 0.4}, parameter_list("expsyn"));
    cells[1].add_synapse({2, 0.4}, parameter_list("exp2syn"));
    cells[1].add_synapse({3, 0.4}, parameter_list("expsyn"));

    cells[1].add_detector({0, 0}, 3.3);

    using fvm_cell = fvm::fvm_multicell<double, cell_lid_type>;
    std::vector<fvm_cell::target_handle> targets(4);
    std::vector<fvm_cell::detector_handle> detectors(1);
    std::vector<fvm_cell::probe_handle> probes;

    fvm_cell fvcell;
    fvcell.initialize(cells, detectors, targets, probes);

    auto& J = fvcell.jacobian();
    EXPECT_EQ(J.size(), 5u+13u);

    // check indices in instantiated mechanisms
    for (const auto& mech: fvcell.mechanisms()) {
        if (mech->name()=="hh") {
            // HH on somas of two cells, with group compartment indices
            // 0 and 5.
            ASSERT_EQ(mech->node_index().size(), 2u);
            EXPECT_EQ(mech->node_index()[0], 0u);
            EXPECT_EQ(mech->node_index()[1], 5u);
        }
        if (mech->name()=="expsyn") {
            // Three expsyn synapses, two in second compartment
            // of dendrite segment of first cell, one in second compartment
            // of last segment of second cell.
            ASSERT_EQ(mech->node_index().size(), 3u);
            EXPECT_EQ(mech->node_index()[0], 2u);
            EXPECT_EQ(mech->node_index()[1], 2u);
            EXPECT_EQ(mech->node_index()[2], 15u);
        }
        if (mech->name()=="exp2syn") {
            // One exp2syn synapse, in second compartment
            // of penultimate segment of second cell.
            ASSERT_EQ(mech->node_index().size(), 1u);
            EXPECT_EQ(mech->node_index()[0], 11u);
        }
    }

    fvcell.setup_matrix(0.01);
}

// test that stimuli are added correctly
TEST(fvm_multi, stimulus)
{
    using namespace nest::mc;
    using util::singleton_view;

    // the default ball and stick has one stimulus at the terminal end of the dendrite
    auto cell = make_cell_ball_and_stick();

    // ... so add a second at the soma to make things more interesting
    cell.add_stimulus({0,0.5}, {1., 2., 0.1});

    // now we have two stims :
    //
    //           |stim0 |stim1
    // -----------------------
    // delay     |   5  |    1
    // duration  |  80  |    2
    // amplitude | 0.3  |  0.1
    // compmnt   |   4  |    0

    using fvm_cell = fvm::fvm_multicell<double, cell_lid_type>;
    std::vector<fvm_cell::target_handle> targets;
    std::vector<fvm_cell::detector_handle> detectors;
    std::vector<fvm_cell::probe_handle> probes;

    fvm_cell fvcell;
    fvcell.initialize(singleton_view(cell), detectors, targets, probes);

    auto& stim = fvcell.stimuli();
    EXPECT_EQ(stim.size(), 2u);

    EXPECT_EQ(stim[0].first, 4u);
    EXPECT_EQ(stim[1].first, 0u);

    EXPECT_EQ(stim[0].second.delay(), 5.);
    EXPECT_EQ(stim[1].second.delay(), 1.);
    EXPECT_EQ(stim[0].second.duration(), 80.);
    EXPECT_EQ(stim[1].second.duration(),  2.);
    EXPECT_EQ(stim[0].second.amplitude(), 0.3);
    EXPECT_EQ(stim[1].second.amplitude(), 0.1);
}

// test that mechanism indexes are computed correctly
TEST(fvm_multi, mechanism_indexes)
{
    using namespace nest::mc;

    // create a cell with 4 sements:
    // a soma with a branching dendrite
    // - hh on soma and first branch of dendrite (segs 0 and 2)
    // - pas on main dendrite and second branch (segs 1 and 3)
    //
    //              /
    //             pas
    //            /
    // hh---pas--.
    //            \.
    //             hh
    //              \.

    cell c;
    auto soma = c.add_soma(12.6157/2.0);
    soma->add_mechanism(hh_parameters());

    // add dendrite of length 200 um and diameter 1 um with passive channel
    c.add_cable(0, segmentKind::dendrite, 0.5, 0.5, 100);
    c.add_cable(1, segmentKind::dendrite, 0.5, 0.5, 100);
    c.add_cable(1, segmentKind::dendrite, 0.5, 0.5, 100);

    auto& segs = c.segments();
    segs[1]->add_mechanism(pas_parameters());
    segs[2]->add_mechanism(hh_parameters());
    segs[3]->add_mechanism(pas_parameters());

    for (auto& seg: segs) {
        if (seg->is_dendrite()) {
            seg->mechanism("membrane").set("r_L", 100);
            seg->set_compartments(4);
        }
    }

    // generate the lowered fvm cell
    using fvm_cell = fvm::fvm_multicell<double, cell_lid_type>;
    std::vector<fvm_cell::target_handle> targets;
    std::vector<fvm_cell::detector_handle> detectors;
    std::vector<fvm_cell::probe_handle> probes;

    fvm_cell fvcell;
    fvcell.initialize(util::singleton_view(c), detectors, targets, probes);

    // make vectors with the expected CV indexes for each mechanism
    std::vector<unsigned> hh_index  = {0u, 4u, 5u, 6u, 7u, 8u};
    std::vector<unsigned> pas_index = {0u, 1u, 2u, 3u, 4u, 9u, 10u, 11u, 12u};
    // iterate over mechanisms and test whether they were assigned to the correct CVs
    // TODO : this fails because we do not handle CVs at branching points (including soma) correctly
    for(auto& mech : fvcell.mechanisms()) {
        auto const& n = mech->node_index();
        std::vector<unsigned> ni(n.begin(), n.end());
        if(mech->name()=="hh") {
            EXPECT_EQ(ni, hh_index);
        }
        else if(mech->name()=="pas") {
            EXPECT_EQ(ni, pas_index);
        }
    }
}
<|endoftext|>