SNABSuite  0.x
Spiking Neural Architecture Benchmark Suite
utilities.hpp
Go to the documentation of this file.
1 /*
2  * SNABSuite -- Spiking Neural Architecture Benchmark Suite
3  * Copyright (C) 2016 Christoph Jenzen
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #ifndef SNABSUITE_UTIL_UTILITIES_HPP
22 #define SNABSUITE_UTIL_UTILITIES_HPP
23 
24 #include <cypress/cypress.hpp>
25 
26 #include <algorithm> // Minimal and Maximal element
27 #include <cmath>
28 #include <fstream>
29 #include <iostream>
30 #include <numeric> // std::accumulate
31 #include <string>
32 #include <vector>
33 
34 namespace SNAB {
35 
36 using cypress::Json;
37 
43 class Utilities {
44 public:
54  static std::vector<std::string> &split(const std::string &s, char delim,
55  std::vector<std::string> &elems);
62  static std::vector<std::string> split(const std::string &s, char delim);
63 
69  static void progress_callback(double p);
70 
81  template <typename T>
82  static void calculate_statistics(const std::vector<T> &data, T &min, T &max,
83  double &avg, double &std_dev)
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  }
110 
111  template <typename T>
121  static void write_vector_to_csv(T &data, std::string file_name)
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  }
136 
137  template <typename T>
147  static void write_vector2_to_csv(T &data, std::string file_name,
148  std::string first_line = "")
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  }
168 
178  static Json merge_json(const Json &a, const Json &b);
179 
189  static Json manipulate_backend_string(std::string &backend, Json &json);
190 
199  static void plot_spikes(std::string filename, std::string simulator);
200 
211  static void plot_histogram(std::string filename, std::string simulator,
212  bool normalized, int n_bins, std::string title);
213 
225  static void plot_voltages_spikes(std::string filename,
226  std::string simulator, size_t mem_col = 1,
227  size_t t_col = 0,
228  std::string spikes_file = "",
229  size_t spikes_col = 0);
230 
240  static void plot_1d_curve(std::string filename, std::string simulator,
241  size_t x_col, size_t y_col, int std_dev_vol = -1);
242 };
243 } // namespace SNAB
244 
245 #endif /* SNABSUITE_UTIL_UTILITIES_HPP */
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...
static void progress_callback(double p)
Funtion for generating a progress bar on terminal.
static void write_vector_to_csv(T &data, std::string file_name)
Writes a vector to a csv.
Definition: utilities.hpp:121
Collection of usefull Utilities not directly related to spiking networks.
Definition: utilities.hpp:43
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...
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.
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.
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...
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.
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) ...
Definition: utilities.hpp:82
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.
static void write_vector2_to_csv(T &data, std::string file_name, std::string first_line="")
Writes a 2D vector to a csv.
Definition: utilities.hpp:147