Skip to content

Commit

Permalink
Remove text-based IO in c++ simulator
Browse files Browse the repository at this point in the history
Former-commit-id: 492eea1
  • Loading branch information
yigu8115 committed Mar 31, 2017
1 parent 0a8b24b commit 8ac7c31
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 1,056 deletions.
50 changes: 4 additions & 46 deletions cpp_sources/ChemSyn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

#include "ChemSyn.h"

ChemSyn::ChemSyn(const double dt_input, const int step_tot_input, const char delim_input, const char indicator_input){
ChemSyn::ChemSyn(const double dt_input, const int step_tot_input){

dt = dt_input;
step_tot = step_tot_input;
delim = delim_input;
indicator = indicator_input;

// Default parameters
V_ex = 0.0; // Excitatory reversal, 0.0
Expand Down Expand Up @@ -508,6 +506,7 @@ void ChemSyn::sample_data(const int step_current){


void ChemSyn::set_para(string para_str){
const char delim = ',';
if (!para_str.empty()){
istringstream para(para_str);
string para_name, para_value_str;
Expand All @@ -532,9 +531,10 @@ void ChemSyn::set_para(string para_str){


string ChemSyn::dump_para(){
const char delim = ',';

stringstream dump;


dump << "pop_ind_pre" << delim << pop_ind_pre << delim << endl;
dump << "pop_ind_post" << delim << pop_ind_post << delim << endl;
dump << "syn_type" << delim << syn_type << delim << endl;
Expand Down Expand Up @@ -589,44 +589,6 @@ void ChemSyn::start_cov_record(const int time_start, const int time_end){
}


void ChemSyn::output_results(ofstream& output_file){
// SYND001 # synapse parameters
// count number of variables
stringstream dump_count;
string para_str = dump_para();
dump_count << para_str;
string str_temp;
int var_number = 0;
while(getline(dump_count,str_temp)){++var_number;} // count number of variables
output_file << indicator << " SYND001" << endl;
output_file << var_number << delim << endl;
output_file << para_str;


// SYND002 # sampled synapse data
if (!sample.neurons.empty()){
output_file << indicator << " SYND002" << endl;
output_file << pop_ind_pre << delim << pop_ind_post << delim << syn_type << delim << sample.neurons.size() << delim << endl;
write2file(output_file, sample.data); // 2D matrix
}


// SYND003 # currents mean and std
if (stats.record){
output_file << indicator << " SYND003" << endl;
output_file << pop_ind_pre << delim << pop_ind_post << delim << syn_type << delim << endl;
write2file(output_file, stats.I_mean);
write2file(output_file, stats.I_std);
}

// tmp data
if (tmp_data.size() != 0){
output_file << indicator << " SYND004" << endl;
output_file << pop_ind_pre << delim << pop_ind_post << delim << syn_type << delim << tmp_data.size() << delim << endl;
write2file(output_file, tmp_data);
}
}

void ChemSyn::add_JH_Learning(vector<NeuroPop*> &NeuronPopArray,int isteps, double iscaleE, double iscaleI,double lrate_E, double lrateall_E,double lrate_I, double lrateall_I,int intau, double innoise,int type_pre,int type_post){

jh_learn_syn.on=true; //indicates this learning is to be used
Expand Down Expand Up @@ -960,9 +922,6 @@ void ChemSyn::record_stats(int step_current){
}
}


#ifdef HDF5

void ChemSyn::import_restart(H5File& file, int syn_ind){

string str;
Expand Down Expand Up @@ -1297,4 +1256,3 @@ void ChemSyn::output_results(H5File& file, int syn_ind){
}
}

#endif
7 changes: 2 additions & 5 deletions cpp_sources/ChemSyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ChemSyn
{
public:
ChemSyn(); /// default constructor
ChemSyn(const double dt, const int step_tot, const char delim, const char indicator); /// parameterised constructor
ChemSyn(const double dt, const int step_tot); /// parameterised constructor

void init(const int syn_type, const int pop_ind_pre, const int pop_ind_post, const int N_pre, const int N_post, const vector<int> &C_i, const vector<int> &C_j, const vector<double> &K_ij, const vector<double> &D_ij); /// initialise chemical synapses by reading already prepared connections

Expand Down Expand Up @@ -51,18 +51,15 @@ class ChemSyn
void old_pre_spikes_K_JH_Learn(vector<NeuroPop*> &NeuronPopArray);
void record_V_post_JH_Learn(vector<NeuroPop*> &NeuronPopArray);

#ifdef HDF5
void import_restart(H5File& file, int syn_ind);
void export_restart(Group& Group, int syn_ind);
void output_results(H5File& file_HDF5, int syn_ind);
#endif

const int & get_syn_type(); /// get synapse type
const int & get_pop_ind_pre(); /// get index of pre-synaptic population
const int & get_pop_ind_post(); /// get index of post-synaptic population
private:

char delim; /// delimiter for input and output files, usually comma
char indicator; /// indicator for protocols, usually a single greater than operator
void init(); /// parameter-dependent initialisation

string dump_para(); // dump all the parameter values used
Expand Down
5 changes: 0 additions & 5 deletions cpp_sources/MyIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ void write2file(ofstream& output_file, const vector<double>& v){
}




#ifdef HDF5

/*--------------------------------------- HDF5 --------------------------------------------*/
void write_scalar_HDF5(Group & group, unsigned int s, const string & v_name){
vector<unsigned int> v_tmp;
Expand Down Expand Up @@ -485,4 +481,3 @@ template int read_scalar_HDF5<int>(const H5File & file, const string & name);
template unsigned int read_scalar_HDF5<unsigned int>(const H5File & file, const string & name);


#endif
72 changes: 35 additions & 37 deletions cpp_sources/MyIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,47 @@
#include <iostream> // cout/cin, ofstream: Stream class to write on files, ifstream : Stream class to read from files, istringstream is for input, ostringstream for output
#include <fstream> // fstream : Stream class to both read and write from / to files

#ifdef HDF5
#include <H5Cpp.h>
#include <hdf5_hl.h>
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
#include <H5Cpp.h>
#include <hdf5_hl.h>
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif

using namespace std;

const char delim = ','; /// Bad practice: this is hard-coded here!

#ifdef HDF5
void write_vector_HDF5(Group & group, const vector<bool> & v, const string & v_name);
void write_vector_HDF5(Group & group, const vector<unsigned int> & v, const string & v_name);
void write_vector_HDF5(Group & group, const vector<int> & v, const string & v_name);
void write_vector_HDF5(Group & group, const vector<double> & v, const string & v_name);
void append_vector_to_matrix_HDF5(DataSet & dataset_tmp, const vector<double> & v, const int colNum);
void append_vector_to_matrix_HDF5(DataSet & dataset_tmp, const vector<int> & v, const int colNum);
void write_matrix_HDF5(Group & group, const vector< vector<double> > & m, const string & m_name);
void write_matrix_HDF5(Group & group, const vector< vector<int> > & m, const string & m_name);
void write_matrix_HDF5(Group & group, const vector< vector<bool> > & m, const string & m_name);
void write_vector_HDF5(Group & group, const vector<bool> & v, const string & v_name);
void write_vector_HDF5(Group & group, const vector<unsigned int> & v, const string & v_name);
void write_vector_HDF5(Group & group, const vector<int> & v, const string & v_name);
void write_vector_HDF5(Group & group, const vector<double> & v, const string & v_name);
void append_vector_to_matrix_HDF5(DataSet & dataset_tmp, const vector<double> & v, const int colNum);
void append_vector_to_matrix_HDF5(DataSet & dataset_tmp, const vector<int> & v, const int colNum);
void write_matrix_HDF5(Group & group, const vector< vector<double> > & m, const string & m_name);
void write_matrix_HDF5(Group & group, const vector< vector<int> > & m, const string & m_name);
void write_matrix_HDF5(Group & group, const vector< vector<bool> > & m, const string & m_name);

void write_string_HDF5(Group & group, const string & s, const string & s_name);
void write_scalar_HDF5(Group & group, unsigned int s, const string & v_name);
void write_scalar_HDF5(Group & group, int s, const string & v_name);
void write_scalar_HDF5(Group & group, double s, const string & v_name);
void write_string_HDF5(Group & group, const string & s, const string & s_name);
void write_scalar_HDF5(Group & group, unsigned int s, const string & v_name);
void write_scalar_HDF5(Group & group, int s, const string & v_name);
void write_scalar_HDF5(Group & group, double s, const string & v_name);

void read_string_HDF5(const H5File & file, const string & s_name, string & s);
void read_matrix_HDF5(const H5File & file, const string & name, vector< vector<int> > & m_tmp);
void read_matrix_HDF5(const H5File & file, const string & name, vector< vector<bool> > & m_tmp);
void read_matrix_HDF5(const H5File & file, const string & name, vector< vector<double> > & m_tmp);
void read_vector_HDF5(const H5File & file, const string & name, vector<unsigned int> & v_tmp);
void read_vector_HDF5(const H5File & file, const string & name, vector<int> & v_tmp);
void read_vector_HDF5(const H5File & file, const string & name, vector<bool> & v_tmp);
void read_vector_HDF5(const H5File & file, const string & name, vector<double> & v_tmp);
bool dataset_exist_HDF5(const H5File & file, const string & name);
bool group_exist_HDF5(const H5File & file, const string & name);
bool group_exist_HDF5(const string & filename, const string & name);
template < typename Type > Type read_scalar_HDF5(const H5File & file, const string & name);
#endif
void write2file(ofstream& output_file, const vector< vector<int> >& v); /// write integer matrix to output file
void write2file(ofstream& output_file, const vector< vector<double> >& v); /// write double matrix to output file
void write2file(ofstream& output_file, const vector<int>& v); /// write integer vector to output file
void write2file(ofstream& output_file, const vector<double>& v); /// write double vector to output file
void read_string_HDF5(const H5File & file, const string & s_name, string & s);
void read_matrix_HDF5(const H5File & file, const string & name, vector< vector<int> > & m_tmp);
void read_matrix_HDF5(const H5File & file, const string & name, vector< vector<bool> > & m_tmp);
void read_matrix_HDF5(const H5File & file, const string & name, vector< vector<double> > & m_tmp);
void read_vector_HDF5(const H5File & file, const string & name, vector<unsigned int> & v_tmp);
void read_vector_HDF5(const H5File & file, const string & name, vector<int> & v_tmp);
void read_vector_HDF5(const H5File & file, const string & name, vector<bool> & v_tmp);
void read_vector_HDF5(const H5File & file, const string & name, vector<double> & v_tmp);
bool dataset_exist_HDF5(const H5File & file, const string & name);
bool group_exist_HDF5(const H5File & file, const string & name);
bool group_exist_HDF5(const string & filename, const string & name);
template < typename Type > Type read_scalar_HDF5(const H5File & file, const string & name);

void write2file(ofstream& output_file, const vector< vector<int> >& v); /// write integer matrix to output file
void write2file(ofstream& output_file, const vector< vector<double> >& v); /// write double matrix to output file
void write2file(ofstream& output_file, const vector<int>& v); /// write integer vector to output file
void write2file(ofstream& output_file, const vector<double>& v); /// write double vector to output file

#endif
35 changes: 3 additions & 32 deletions cpp_sources/NeuroNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

using namespace std;

NeuroNet::NeuroNet(vector<int> N_array_input, double dt_input, int step_tot_input, char delim_input, char indicator_input){
NeuroNet::NeuroNet(vector<int> N_array_input, double dt_input, int step_tot_input){

N_array = N_array_input;
dt = dt_input;
step_tot = step_tot_input;
delim = delim_input;
indicator = indicator_input;
Num_pop = N_array.size();
runaway_killed = false;
step_killed = -1;
Expand Down Expand Up @@ -88,35 +86,11 @@ void NeuroNet::update(int step_current){

}



void NeuroNet::output_results(ofstream& output_file){

// write data
// cout << "Outputting results into text file..." << endl;

// KILL002 # step at which runaway activity is killed
output_file << indicator << " KILL002" << endl;
output_file << step_killed << delim << endl;

// dump population data
for (int i = 0; i < Num_pop; i++){
NeuroPopArray[i]->output_results(output_file);
}

// dump synapse data
for (unsigned int i = 0; i < ChemSynArray.size(); i++){
ChemSynArray[i]->output_results(output_file);
}

}

#ifdef HDF5
void NeuroNet::import_restart(H5File & file,string out_filename){

Num_pop=read_scalar_HDF5<int>(file,string("/Net/Num_pop"));
for (unsigned int ind = 0; ind < N_array.size(); ++ind){
NeuroPopArray.push_back(new NeuroPop(ind, N_array[ind], dt, step_tot, delim, indicator));
NeuroPopArray.push_back(new NeuroPop(ind, N_array[ind], dt, step_tot));
cout << "\t Initialising neuron pop " << ind+1 << "..." << endl;

NeuroPopArray[ind]->import_restart(file,ind,out_filename);
Expand All @@ -126,7 +100,7 @@ void NeuroNet::import_restart(H5File & file,string out_filename){
int n_syns=read_scalar_HDF5<int>(file,syn_str+"n_syns");
for (int ind = 0; ind < n_syns; ++ind){

ChemSynArray.push_back(new ChemSyn(dt, step_tot, delim, indicator));
ChemSynArray.push_back(new ChemSyn(dt, step_tot));
// network.ChemSynArray.back()->init(type, i_pre, j_post, N_array[i_pre], N_array[j_post], I, J, K, D);

ChemSynArray[ind]->import_restart(file,ind);
Expand Down Expand Up @@ -178,6 +152,3 @@ void NeuroNet::output_results(H5File & file_HDF5){
}

}

#endif

10 changes: 1 addition & 9 deletions cpp_sources/NeuroNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace std;
class NeuroNet{
public:
NeuroNet();
NeuroNet(vector<int> N_array, double dt, int step_tot, char delim, char indicator); // parameterised constructor for heterogeneous coupling
NeuroNet(vector<int> N_array, double dt, int step_tot); // parameterised constructor for heterogeneous coupling

vector<int> N_array; // number of neurons in each pupolation
double dt; // (ms)
Expand All @@ -24,18 +24,10 @@ class NeuroNet{
int step_killed; // initialised as -1

void update(int step_current); // update the network to current time step, use "virtual" if want override by derived class


char delim;
char indicator;
void output_results(ofstream& output_file);


#ifdef HDF5
void import_restart(H5File & file, string out_filename);
void export_restart(H5File& file_HDF5, int restart_no);
void output_results(H5File& file_HDF5);
#endif



Expand Down
Loading

0 comments on commit 8ac7c31

Please sign in to comment.