SNABSuite  0.x
Spiking Neural Architecture Benchmark Suite
Static Public Member Functions | List of all members
SNAB::Utilities Class Reference

Collection of usefull Utilities not directly related to spiking networks. More...

#include <utilities.hpp>

Static Public Member Functions

static std::vector< std::string > & split (const std::string &s, char delim, std::vector< std::string > &elems)
 Splits a string s into parts devided by delim and stores the result in elems and returns it. More...
 
static std::vector< std::string > split (const std::string &s, char delim)
 The same as Utilities::split, but only returning the vector. More...
 
static void progress_callback (double p)
 Funtion for generating a progress bar on terminal. More...
 
template<typename T >
static void calculate_statistics (const std::vector< T > &data, T &min, T &max, double &avg, double &std_dev)
 Calculating statistics of a vector, using an estimator for std_dev (sample covariance) More...
 
template<typename T >
static void write_vector_to_csv (T &data, std::string file_name)
 Writes a vector to a csv. More...
 
template<typename T >
static void write_vector2_to_csv (T &data, std::string file_name, std::string first_line="")
 Writes a 2D vector to a csv. More...
 
static Json merge_json (const Json &a, const Json &b)
 Merge two json objects. Note: Values already included in a will be overwritten! See github for source. More...
 
static Json manipulate_backend_string (std::string &backend, Json &json)
 Merge the backend strings with a provided json object. Note: options already included in backend will not be overwritten! More...
 
static void plot_spikes (std::string filename, std::string simulator)
 Given a filename of a csv with list of spikes this will produce a raster plot. More...
 
static void plot_histogram (std::string filename, std::string simulator, bool normalized, int n_bins, std::string title)
 Plotting a histogram a one dimensional data in a csv. More...
 
static void plot_voltages_spikes (std::string filename, std::string simulator, size_t mem_col=1, size_t t_col=0, std::string spikes_file="", size_t spikes_col=0)
 Plotting membrane voltage and (optional) plotting vertical lines for spikes on top. More...
 
static void plot_1d_curve (std::string filename, std::string simulator, size_t x_col, size_t y_col, int std_dev_vol=-1)
 Plotting a curve with optional standard deviation. More...
 

Detailed Description

Collection of usefull Utilities not directly related to spiking networks.

Definition at line 43 of file utilities.hpp.

Member Function Documentation

template<typename T >
static void SNAB::Utilities::calculate_statistics ( const std::vector< T > &  data,
T &  min,
T &  max,
double &  avg,
double &  std_dev 
)
inlinestatic

Calculating statistics of a vector, using an estimator for std_dev (sample covariance)

Parameters
datacontains sample data
minobject for minimal value
maxobject for maximal value
avgobject for sample average
std_devobject for smaple covariance

Definition at line 82 of file utilities.hpp.

84  {
85  if (data.size() > 1) {
86  // Calculate statistics
87  max = *std::max_element(data.begin(), data.end());
88  min = *std::min_element(data.begin(), data.end());
89  avg = double(std::accumulate(data.begin(), data.end(), 0.0)) /
90  double(data.size());
91  std_dev = 0.0;
92  std::for_each(data.begin(), data.end(), [&](const T val) {
93  std_dev += (val - avg) * (val - avg);
94  });
95  std_dev = std::sqrt(double(std_dev) / double(data.size() - 1));
96  }
97  else if (data.size() == 1) {
98  min = data[0];
99  max = data[0];
100  avg = data[0];
101  std_dev = 0;
102  }
103  else {
104  min = 0;
105  max = 0;
106  avg = 0;
107  std_dev = 0;
108  }
109  }
static Json SNAB::Utilities::manipulate_backend_string ( std::string &  backend,
Json &  json 
)
static

Merge the backend strings with a provided json object. Note: options already included in backend will not be overwritten!

Parameters
backendstring containig "[backend]" (+ "={setup options}"), will contain combined setup after execution
jsonObject containig additional options for backend
Returns
: Combined json object
static Json SNAB::Utilities::merge_json ( const Json &  a,
const Json &  b 
)
static

Merge two json objects. Note: Values already included in a will be overwritten! See github for source.

Parameters
atarget json, keys will b overwritten
bsource json, will be copied into a
Returns
combined json object
static void SNAB::Utilities::plot_1d_curve ( std::string  filename,
std::string  simulator,
size_t  x_col,
size_t  y_col,
int  std_dev_vol = -1 
)
static

Plotting a curve with optional standard deviation.

Parameters
filenamefile containing data
simulatorthe (unshortened) backend/simulator string
x_colcolumn of x values
y_colcolumn of y values
std_dev_volcolumn of standard deviation values
static void SNAB::Utilities::plot_histogram ( std::string  filename,
std::string  simulator,
bool  normalized,
int  n_bins,
std::string  title 
)
static

Plotting a histogram a one dimensional data in a csv.

Parameters
filenameFile and path to the csv
simulatorsimulator string, options will be stripped insider
normalizedflag whether histogram should be normalized
n_binsnumber of bins for the histogram, negative values use 'auto' in pyplot
titletitle to be plotted on top of histogram
static void SNAB::Utilities::plot_spikes ( std::string  filename,
std::string  simulator 
)
static

Given a filename of a csv with list of spikes this will produce a raster plot.

Parameters
filenamename and path to a csv
simulatorsimulator string, simulator options will be stripped inside
static void SNAB::Utilities::plot_voltages_spikes ( std::string  filename,
std::string  simulator,
size_t  mem_col = 1,
size_t  t_col = 0,
std::string  spikes_file = "",
size_t  spikes_col = 0 
)
static

Plotting membrane voltage and (optional) plotting vertical lines for spikes on top.

Parameters
filenamename of the file containing membrane voltage, csv style
simulatorthe (unshortened) backend/simulator string
mem_colcolumn containing the membrane potential (default = 1)
t_colcolumn containing the time values (default = 0)
spikes_file(optional) file containing spikes of the same neuron
spikes_colcolumn of spike data in spikes_file
static void SNAB::Utilities::progress_callback ( double  p)
static

Funtion for generating a progress bar on terminal.

Parameters
pvalue between 0 and 1 representing current progress
static std::vector<std::string>& SNAB::Utilities::split ( const std::string &  s,
char  delim,
std::vector< std::string > &  elems 
)
static

Splits a string s into parts devided by delim and stores the result in elems and returns it.

Parameters
sstring to be splitted
delimchar which seperated the strings
elemscontainer in which result is appended at the end
Returns
the new elems
static std::vector<std::string> SNAB::Utilities::split ( const std::string &  s,
char  delim 
)
static

The same as Utilities::split, but only returning the vector.

Parameters
sstring to be splitted
delimchar which seperated the strings
template<typename T >
static void SNAB::Utilities::write_vector2_to_csv ( T &  data,
std::string  file_name,
std::string  first_line = "" 
)
inlinestatic

Writes a 2D vector to a csv.

Parameters
TType of data e.g. vector of a vector of ints, iterator musst support '<<' for output
dataReference to the data
file_namestring containing filename, make sure folders already exists

Definition at line 147 of file utilities.hpp.

149  {
150  std::ofstream file;
151  file.open(file_name, std::ofstream::out);
152  if (file.good()) {
153  if (first_line != "") {
154  file << first_line << std::endl;
155  }
156  for (auto i : data) {
157  for (auto j : i) {
158  file << j << ", ";
159  }
160  file << std::endl;
161  }
162  }
163  else {
164  std::cerr << "Could not open file " << file_name
165  << "for writing csv!" << std::endl;
166  }
167  }
template<typename T >
static void SNAB::Utilities::write_vector_to_csv ( T &  data,
std::string  file_name 
)
inlinestatic

Writes a vector to a csv.

Parameters
TType of data e.g. vector of ints, iterator musst support '<<' for output
dataReference to the data
file_namestring containing filename, make sure folders already exists

Definition at line 121 of file utilities.hpp.

122  {
123  std::ofstream file;
124  file.open(file_name, std::ofstream::out);
125  if (file.good()) {
126  for (auto i : data) {
127  file << i << std::endl;
128  }
129  // file << std::endl;
130  }
131  else {
132  std::cerr << "Could not open file " << file_name
133  << "for writing csv!" << std::endl;
134  }
135  }

The documentation for this class was generated from the following file: