neuro_toolbox
IO.hpp
Go to the documentation of this file.
1 #ifndef IO_HPP
2 #define IO_HPP
3 
4 #include "config.hpp"
5 
6 namespace NTB
7 {
8 
9  template <typename T>
10  inline std::vector<T> load_vector(std::string filename)
11  {
23  std::vector<T> numbers;
24  std::ifstream inputFile(filename);
25  if (inputFile.good())
26  {
27  T current_number = 0;
28  while (inputFile >> current_number)
29  numbers.push_back(current_number);
30 
31  inputFile.close();
32  }
33 
34  else
35  {
36  std::cout << "Error reading " << filename << "\n";
37  exit(EXIT_FAILURE);
38  }
39 
40  return numbers;
41  }
42  //-------------------------------------------------------------------------
43  inline bool file_exists(const std::string &filename)
44  {
52  struct stat buf;
53  if (stat(filename.c_str(), &buf) != -1)
54  {
55  return true;
56  }
57  return false;
58  }
59 
60  //-------------------------------------------------------------------------
61  template <typename T>
62  inline std::vector<std::vector<T>> load_matrix(
63  const std::string filename,
64  const int row,
65  const int col)
66  {
81  std::ifstream ifile(filename);
82 
83  /*to check if input file exists*/
84  if (file_exists(filename))
85  {
86  std::vector<std::vector<T>> Cij(row, std::vector<T>(col));
87 
88  for (int i = 0; i < row; i++)
89  {
90  for (int j = 0; j < col; j++)
91  {
92  ifile >> Cij[i][j];
93  }
94  }
95  ifile.close();
96  return Cij;
97  }
98  else
99  {
100  std::cerr << "\n file : " << filename << " not found \n";
101  exit(2);
102  }
103  }
104 
105  //-------------------------------------------------------------------------
106  template <typename T>
107  std::vector<std::vector<T>> adjmat_to_adjlist(
108  const std::vector<std::vector<T>> &A,
109  const double threshold = 1e-8,
110  std::string in_degree = "row")
111  {
123  int row = A.size();
124  int col = A[0].size();
125  std::vector<std::vector<int>> adjlist;
126 
127  if (in_degree == "row")
128  {
129  adjlist.resize(row);
130 
131  for (int i = 0; i < row; i++)
132  {
133  for (int j = 0; j < col; j++)
134  {
135  if (std::abs(A[i][j]) > 1.e-8)
136  adjlist[i].push_back(j);
137  }
138  }
139  }
140  else
141  {
142  adjlist.resize(col);
143 
144  for (int i = 0; i < col; i++)
145  {
146  for (int j = 0; j < row; j++)
147  {
148  if (std::abs(A[i][j]) > 1.e-8)
149  adjlist[i].push_back(j);
150  }
151  }
152  }
153 
154  return adjlist;
155  }
156 
157  //-------------------------------------------------------------------------
158  template <typename T>
159  void write_matrix_to_file(const std::vector<std::vector<T>> &A,
160  const std::string filename)
161  {
169  int row = A.size();
170  int col = A[0].size();
171 
172  std::ofstream ofile;
173  ofile.open(filename);
174  if (ofile.is_open())
175  {
176  for (int i = 0; i < row; i++)
177  {
178  for (int j = 0; j < col; j++)
179  {
180  ofile << A[i][j] << " ";
181  }
182  ofile << "\n";
183  }
184  ofile.close();
185  }
186  else
187  std::cout << "Error opening file to write data. \n";
188  }
189 
190  //-------------------------------------------------------------------------
191  template <typename T>
192  void write_vector_to_file(const std::vector<T> &v,
193  const std::string filename)
194  {
204  size_t n = v.size();
205  std::ofstream ofile;
206  ofile.open(filename);
207  if (ofile.is_open())
208  {
209  for (size_t i = 0; i < n; ++i)
210  ofile << v[i] << "\n";
211  ofile.close();
212  }
213  else
214  {
215  std::cout << "Error opening file to write data. \n";
216  exit(EXIT_FAILURE);
217  }
218  }
219  //-------------------------------------------------------------------------
220 
221  void spikes_to_file(const std::string filename,
222  const std::vector<std::vector<double>> &spikes)
223  {
224 
225  FILE *spkfile;
226  spkfile = fopen(filename.c_str(), "w");
227 
228  if (file_exists(filename))
229  {
230  for (size_t i = 0; i < spikes.size(); i++)
231  {
232  for (size_t j = 0; j < spikes[i].size(); j++)
233  {
234  fprintf(spkfile, "%18.6f", spikes[i][j]);
235  }
236  fprintf(spkfile, "\n");
237  }
238 
239  fclose(spkfile);
240  }
241  else
242  {
243  printf("Could not open file properly in spike_to_file!\n");
244  exit(EXIT_FAILURE);
245  }
246  }
247 
248 } // namespace NTB
249 
250 #endif // !IO_HPP
void write_matrix_to_file(const std::vector< std::vector< T >> &A, const std::string filename)
Definition: IO.hpp:159
void spikes_to_file(const std::string filename, const std::vector< std::vector< double >> &spikes)
Definition: IO.hpp:221
void write_vector_to_file(const std::vector< T > &v, const std::string filename)
Definition: IO.hpp:192
Definition: IO.hpp:6
bool file_exists(const std::string &filename)
Definition: IO.hpp:43
std::vector< T > load_vector(std::string filename)
Definition: IO.hpp:10
std::vector< std::vector< T > > load_matrix(const std::string filename, const int row, const int col)
Definition: IO.hpp:62
std::vector< std::vector< T > > adjmat_to_adjlist(const std::vector< std::vector< T >> &A, const double threshold=1e-8, std::string in_degree="row")
Definition: IO.hpp:107