pf
utils.h
1 #ifndef UTILIS_H
2 #define UTILIS_H
3 
4 #include <Eigen/Dense>
5 #include <fstream>
6 #include <array>
7 #include <iostream> // std::cerr
8 #include <fstream> // std::ofstream
9 #include <string> // string
10 #include <vector> // vector
11 
12 
13 namespace utils{
14 
15  // return string that has str plus the current date/time, format is YYYY-MM-DD.HH:mm:ss
16  std::string genStringWithTime(const std::string& str);
17 
18 
30  template<size_t dim, typename float_t>
31  void logParams(const Eigen::Matrix<float_t,dim,1> &vec, std::ofstream &ofs);
32 
33 
40  template<size_t dim, typename float_t>
41  void logParams(const Eigen::Matrix<float_t,dim,1> &vec, const std::string &outfile);//std::ofstream &ofs);
42 
43 
50  template<size_t size, typename float_t>
51  void logParams(const std::array<float_t, size> &arr, const std::string &outfile);//std::ofstream &ofs);
52 
53 
60  template<size_t nc, typename float_t>
61  std::vector<Eigen::Matrix<float_t,nc,1> > readInData(const std::string& fileLoc);
62 
63 
64 template<size_t dim, typename float_t>
65 void logParams(const Eigen::Matrix<float_t,dim,1> &vec, std::ofstream &ofs)
66 {
67  // make sure open and doesn't close
68  if(ofs.is_open()){
69 
70  // write stuff
71  for(size_t i = 0; i < dim; ++i){
72  if( i == 0){
73  ofs << vec(i,0);
74  } else {
75  ofs << "," << vec(i,0);
76  }
77  }
78  ofs << "\n";
79 
80 
81  }else{
82  std::cerr << "tried to write to a closed ofstream! " << "\n";
83  }
84 }
85 
86 
87 template<size_t dim, typename float_t>
88 void logParams(const Eigen::Matrix<float_t,dim,1> &vec, const std::string &outfile)
89 {
90 
91  // open the file in append mode
92  std::ofstream f(outfile, std::ios::app);
93 
94  // make sure open
95  if(f.is_open()){
96 
97  // write stuff
98  for(size_t i = 0; i < dim; ++i){
99  if( i == 0){
100  f << vec(i,0);
101  } else {
102  f << "," << vec(i,0);
103  }
104  }
105  f << "\n";
106 
107  f.close();
108 
109  }else{
110  std::cerr << "logParams() failed to open file at: " << outfile << "\n";
111  }
112 }
113 
114 
115 template<size_t size, typename float_t>
116 void logParams(const std::array<float_t, size> &arr, const std::string &outfile)
117 {
122  // open the file in append mode
123  std::ofstream f(outfile, std::ios::app);
124 
125  // make sure file open
126  if(f.is_open()){
127 
128  // write
129  for(size_t i = 0; i < size; ++i){
130  if( i == 0){
131  f << arr[i];
132  } else {
133  f << "," << arr[i];
134  }
135  }
136  f << "\n";
137  }else{
138  std::cerr << "logParams() failed to open file at: " << outfile << "\n";
139  }
140 }
141 
142 
148 template<size_t nc, typename float_t>
149 std::vector<Eigen::Matrix<float_t,nc,1> > readInData(const std::string& fileLoc)
150 {
151  // returning this. gotta build it up
152  std::vector<Eigen::Matrix<float_t,nc,1> > data;
153 
154  // start reading
155  std::string line;
156  std::ifstream ifs(fileLoc);
157  std::string one_number;
158  if(!ifs.is_open()){ // check if we can open inFile
159  std::cerr << "readInData() failed to read data from: " << fileLoc << "\n";
160  }
161 
162  // didn't fail...keep going
163  while ( std::getline(ifs, line) ){ // get a whole row as a string
164 
165  std::vector<float_t> data_row;
166  try{
167  // get a single element on a row
168  std::istringstream buff(line);
169 
170  // make one number between commas
171  while(std::getline(buff, one_number, ',')){
172  data_row.push_back(std::stod(one_number));
173  }
174 
175  } catch (const std::invalid_argument& ia){
176  std::cerr << "Invalid Argument: " << ia.what() << "\n";
177  continue;
178  }
179 
180  // now append this Vec to your collection
181  Eigen::Map<Eigen::Matrix<float_t,nc,1> > drw(&data_row[0], nc);
182  data.push_back(drw);
183  }
184 
185  return data;
186 
187 }
188 
189 } // namespace utils
190 
191 
192 #endif // UTILIS_H
Definition: utils.h:13