MachineIntelligenceCore:Algorithms
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DataCollector.hpp
Go to the documentation of this file.
1 
23 #ifndef SRC_UTILS_DATACOLLECTOR_HPP_
24 #define SRC_UTILS_DATACOLLECTOR_HPP_
25 
26 #include <vector>
27 #include <string>
28 #include <map>
29 #include <limits>
30 
31 #include <fstream> // std::ofstream
32 
33 #include<logger/Log.hpp>
34 
35 #include <types/Color.hpp>
36 #include <types/MatrixTypes.hpp>
37 
38 namespace mic {
39 namespace utils {
40 
45 template <typename DATA_TYPE>
46 struct DataContainer {
50  std::vector<DATA_TYPE> data;
51 
55  bool auto_scale;
56 
60  DATA_TYPE min_value;
61 
65  DATA_TYPE max_value;
66 
71 
75  float line_width;
76 };
77 
83 template<class DATA_TYPE>
84 using DataContainerPtr = typename std::shared_ptr < DataContainer < DATA_TYPE> >;
85 
92 template<class LABEL_TYPE, class DATA_TYPE>
93 using DataContainers = typename std::map<LABEL_TYPE, DataContainerPtr <DATA_TYPE> >;
94 
95 
96 
103 template<class LABEL_TYPE, class DATA_TYPE>
104 using DataContainerIt = typename std::map<LABEL_TYPE, DataContainerPtr <DATA_TYPE> >::iterator;
105 
106 
113 template <class LABEL_TYPE, class DATA_TYPE>
115 public:
120 
124  virtual ~DataCollector() { };
125 
134  void createContainer(LABEL_TYPE label_, DATA_TYPE min_, DATA_TYPE max_, mic::types::color_rgba color_ = mic::types::color_rgba(255, 255, 255, 180), float line_width_ = 1.0f) {
135  LOG(LTRACE)<< "DataCollector::createDataContainer";
136 
138  // Set min-max parameters.
139  tmp->auto_scale = false;
140  tmp->min_value = min_;
141  tmp->max_value = max_;
142  tmp->line_width = line_width_;
143  tmp->color = color_;
144  // Add container.
145  containers.insert( std::make_pair (label_, tmp) );
146  }
147 
154  void createContainer(LABEL_TYPE label_, mic::types::color_rgba color_ = mic::types::color_rgba(255, 255, 255, 180), float line_width_ = 1.0f) {
155  LOG(LTRACE)<< "DataCollector::createDataContainer";
156 
158  // Set min-max parameters.
159  tmp->auto_scale = true;
160  tmp->min_value = std::numeric_limits<DATA_TYPE>::max();
161  tmp->max_value = std::numeric_limits<DATA_TYPE>::min();
162  tmp->line_width = line_width_;
163  tmp->color = color_;
164  // Add data container.
165  containers.insert( std::make_pair (label_, tmp) );
166  }
167 
168 
174  void addDataToContainer(LABEL_TYPE label_, DATA_TYPE value_){
175  LOG(LTRACE)<< "DataCollector::addDataToContainer";
176 
177  // Try to find the label in registry.
179 
180  if (it != containers.end()) {
181  // Add new value to data.
182  (it->second)->data.push_back(value_);
183  // Check autoscale.
184  if ((it->second)->auto_scale) {
185  (it->second)->min_value = (value_ < (it->second)->min_value) ? value_ : (it->second)->min_value;
186  (it->second)->max_value = (value_ > (it->second)->max_value) ? value_ : (it->second)->max_value;
187  }
188  } else {
189  LOG(LERROR) << "There is no container with label: " << label_;
190  }
191  }
192 
199  LOG(LTRACE)<< "DataCollector::getContainerData";
200 
201  // Try to find the label in registry.
203 
204  if (it == containers.end()) {
205  LOG(LERROR) << "There is no container with label: " << label_;
206  // Return empty container.
208  tmp->auto_scale = true;
209  tmp->min_value = std::numeric_limits<DATA_TYPE>::max();
210  tmp->max_value = std::numeric_limits<DATA_TYPE>::min();
211  return tmp;
212  } else
213  return it->second;
214  }
215 
221  LOG(LTRACE)<< "DataCollector::getContainer";
222  return containers;
223  }
224 
225 
230  void exportDataToCsv(std::string filename_ = "data.csv"){
231  LOG(LTRACE)<< "DataCollector::exportDataToCsv";
232  // Open output filestream.
233  std::ofstream output(filename_);
234 
235  // Iterate through containers.
236  for(DataContainerIt<LABEL_TYPE, DATA_TYPE> it = containers.begin(); it != containers.end(); it++) {
237  // Export header.
238  output << it->first << ", min ,";
239  output << (it->second)->min_value << ", max, ";
240  output << (it->second)->max_value << std::endl;
241 
242  // Export data.
243  if ((it->second)->data.size() > 1)
244  std::copy((it->second)->data.begin(), (it->second)->data.end() - 1, std::ostream_iterator<DATA_TYPE>(output, ", "));
245  if ((it->second)->data.size())
246  output << (it->second)->data.back() << std::endl;
247  }//: for
248  }
249 
257  static void exportVectorToCsv(std::string filename_, std::string label_, std::vector<DATA_TYPE> data_, bool append_ = false){
258  LOG(LTRACE)<< "DataCollector::exportVectorToCsv";
259  // Open output filestream.
260  std::ofstream output;
261  if (!append_)
262  output.open(filename_);
263  else
264  output.open(filename_, std::ofstream::out | std::ofstream::app);
265 
266  // Export header
267  output << label_ << std::endl;
268 
269  // Export data.
270  if (data_.size() > 1)
271  std::copy(data_.begin(), data_.end() - 1, std::ostream_iterator<DATA_TYPE>(output, ", "));
272  if (data_.size())
273  output << data_.back() << std::endl;
274  }
275 
283  static void exportValueToCsv(std::string filename_, std::string label_, DATA_TYPE value_, bool append_ = false){
284  LOG(LTRACE)<< "DataCollector::exportValueToCsv";
285  // Open output filestream.
286  std::ofstream output;
287  if (!append_)
288  output.open(filename_);
289  else
290  output.open(filename_, std::ofstream::out | std::ofstream::app);
291 
292  // Export data.
293  output << label_ << ", " << value_ << std::endl;
294  }
295 
296 
303  static void exportCommentToCsv(std::string filename_, std::string exportCommentToCsv, bool append_ = false){
304  LOG(LTRACE)<< "DataCollector::exportCommentToCsv";
305  // Open output filestream.
306  std::ofstream output;
307  if (!append_)
308  output.open(filename_);
309  else
310  output.open(filename_, std::ofstream::out | std::ofstream::app);
311 
312  // Export data.
313  output << exportCommentToCsv << std::endl;
314  }
315 
316 
324  static void exportMatrixToCsv(std::string filename_, std::string label_, std::shared_ptr< mic::types::Matrix<DATA_TYPE> > matrix_, bool append_ = false){
325  LOG(LTRACE)<< "DataCollector::exportVectorToCsv";
326  // Open output filestream.
327  std::ofstream output;
328  if (!append_)
329  output.open(filename_);
330  else
331  output.open(filename_, std::ofstream::out | std::ofstream::app);
332 
333  // Export header.
334  output << label_ << std::endl;
335 
336  // Export matrix.
337  for (size_t y = 0; y < (size_t)matrix_->rows(); y++) {
338  for (size_t x = 0; x < (size_t)matrix_->cols(); x++) {
339  output << matrix_(y,x) << ", ";
340  }//: for
341  }//: for
342  output << std::endl;
343  }
344 
345 
346 
354  static void exportMatricesToCsv(std::string filename_, std::string label_, std::vector< mic::types::MatrixPtr<DATA_TYPE> > data_, bool append_ = false){
355  LOG(LTRACE)<< "DataCollector::exportVectorToCsv";
356  // Open output filestream.
357  std::ofstream output;
358  if (!append_)
359  output.open(filename_);
360  else
361  output.open(filename_, std::ofstream::out | std::ofstream::app);
362 
363  // Export header.
364  output << label_ << std::endl;
365 
366  for (size_t i=0; i<data_.size(); i++) {
367  mic::types::MatrixPtr<DATA_TYPE> matrix_ptr = data_[i];
368 
369  //output << *matrix;
370 
371  //DATA_TYPE* data_ptr = matrix->data();
372  // Export matrix.
373  for (size_t y = 0; y < (size_t)matrix_ptr->rows(); y++) {
374  for (size_t x = 0; x < (size_t)matrix_ptr->cols(); x++) {
375  output << (*matrix_ptr)(y,x) << ", ";
376  }//: for
377  }//: for
378  //output << data_ptr[matrix->rows()*matrix->cols() -1] << std::endl;
379  output << std::endl;
380  }//: for
381  }
382 
383 
384 protected:
389 
390 };
391 
392 
399 template<class LABEL_TYPE, class DATA_TYPE>
400 using DataCollectorPtr = typename std::shared_ptr<mic::utils::DataCollector<LABEL_TYPE, DATA_TYPE > >;
401 
402 
403 
404 } /* namespace utils */
405 } /* namespace mic */
406 
407 #endif /* SRC_UTILS_DATACOLLECTOR_HPP_ */
DataContainers< LABEL_TYPE, DATA_TYPE > getContainers()
typename std::shared_ptr< DataContainer< DATA_TYPE > > DataContainerPtr
Type representing a pointer to data container.
mic::types::color_rgba color
typename std::shared_ptr< mic::utils::DataCollector< LABEL_TYPE, DATA_TYPE > > DataCollectorPtr
A pointer to data collector.
void exportDataToCsv(std::string filename_="data.csv")
Contains definition of basic matrix datatypes derived from Eigen.
void createContainer(LABEL_TYPE label_, DATA_TYPE min_, DATA_TYPE max_, mic::types::color_rgba color_=mic::types::color_rgba(255, 255, 255, 180), float line_width_=1.0f)
DataContainerPtr< DATA_TYPE > getDataFromContainer(LABEL_TYPE label_)
typename std::map< LABEL_TYPE, DataContainerPtr< DATA_TYPE > > DataContainers
Type representing a set (map) of containers.
Class responsible for collection of data during experiments end exporting the results to files...
static void exportMatrixToCsv(std::string filename_, std::string label_, std::shared_ptr< mic::types::Matrix< DATA_TYPE > > matrix_, bool append_=false)
DataContainers< LABEL_TYPE, DATA_TYPE > containers
static void exportValueToCsv(std::string filename_, std::string label_, DATA_TYPE value_, bool append_=false)
typename std::shared_ptr< mic::types::Matrix< T > > MatrixPtr
Typedef for a shared pointer to template-typed dynamic matrices.
Definition: Matrix.hpp:479
typename std::map< LABEL_TYPE, DataContainerPtr< DATA_TYPE > >::iterator DataContainerIt
Type representing a data container iterator.
static void exportCommentToCsv(std::string filename_, std::string exportCommentToCsv, bool append_=false)
Class for storing single a pixel of four channel (RGBA) image.
Definition: Color.hpp:55
std::vector< DATA_TYPE > data
void createContainer(LABEL_TYPE label_, mic::types::color_rgba color_=mic::types::color_rgba(255, 255, 255, 180), float line_width_=1.0f)
static void exportMatricesToCsv(std::string filename_, std::string label_, std::vector< mic::types::MatrixPtr< DATA_TYPE > > data_, bool append_=false)
Template-typed Matrix of dynamic size. Uses OpenBLAS if found by CMAKE - overloaded, specializations of * operator for types: float, double.
Definition: Matrix.hpp:64
void addDataToContainer(LABEL_TYPE label_, DATA_TYPE value_)
static void exportVectorToCsv(std::string filename_, std::string label_, std::vector< DATA_TYPE > data_, bool append_=false)