SkePU  1.2
 All Classes Namespaces Files Functions Variables Enumerations Friends Macros Groups Pages
data_collector.h
Go to the documentation of this file.
1 
5 #ifndef DATA_COLLECTOR_H
6 #define DATA_COLLECTOR_H
7 
8 #include <string>
9 #include <set>
10 #include <utility>
11 #include <fstream>
12 #include <iomanip>
13 
14 #include <iostream>
15 
16 namespace skepu
17 {
18 
19 enum DataExportFormat
20 {
21  GNUPLOT,
22  PREDICTION_FILE
23 };
24 
38 template <typename Tx, typename Ty>
40 {
41 
42 public:
43  DataCollector2D(const std::string& _dataSetName, const std::string& _axisNameX, const std::string& _axisNameY);
44 
45  void addData(Tx x, Ty y);
46  void clear();
47  void writeDataToFile(const std::string& filename = "", DataExportFormat format = GNUPLOT);
48 
49  void appendDataToFile(const std::string& filename, bool firstTime, DataExportFormat format = GNUPLOT);
50 
51 private:
52  std::string dataSetName;
53  std::pair<std::string, std::string> axisNames;
54  std::multiset< std::pair<Tx, Ty> > dataSet;
55 
56  void writeGnuPlotFile(const std::string& filename);
57 
58  void appendGnuPlotFile(const std::string& filename, bool firstTime);
59 
60 };
61 
69 template <typename Tx, typename Ty>
70 DataCollector2D<Tx, Ty>::DataCollector2D(const std::string& _dataSetName, const std::string& _axisNameX, const std::string& _axisNameY)
71 {
72  dataSetName = _dataSetName;
73  axisNames.first = _axisNameX;
74  axisNames.second = _axisNameY;
75 }
76 
83 template <typename Tx, typename Ty>
85 {
86  dataSet.insert(std::make_pair(x, y));
87 }
88 
92 template <typename Tx, typename Ty>
94 {
95  dataSet.clear();
96 }
97 
104 template <typename Tx, typename Ty>
105 void DataCollector2D<Tx, Ty>::writeDataToFile(const std::string& filename, DataExportFormat format)
106 {
107  std::string _filename;
108 
109  if(filename.empty())
110  {
111  _filename = dataSetName + ".dat";
112  }
113  else
114  {
115  _filename = filename;
116  }
117 
118  //Call right exporter
119  if(format == GNUPLOT)
120  {
121  writeGnuPlotFile(_filename);
122  }
123 }
124 
125 
126 
133 template <typename Tx, typename Ty>
134 void DataCollector2D<Tx, Ty>::appendDataToFile(const std::string& filename, bool firstTime, DataExportFormat format)
135 {
136  std::string _filename;
137 
138  if(filename.empty())
139  {
140  _filename = dataSetName + ".dat";
141  }
142  else
143  {
144  _filename = filename;
145  }
146 
147  //Call right exporter
148  if(format == GNUPLOT)
149  {
150  appendGnuPlotFile(_filename, firstTime);
151  }
152 }
153 
159 template <typename Tx, typename Ty>
160 void DataCollector2D<Tx, Ty>::writeGnuPlotFile(const std::string& filename)
161 {
162  std::ofstream file(filename.c_str());
163  int tabLength = axisNames.first.length()+20;
164  file<<std::left;
165  file<<std::fixed <<std::setprecision(5);
166  if(file.is_open())
167  {
168  //First add name and axis names as comments
169  file<<"# " <<dataSetName <<"\n";
170  file<<"# " <<std::setw(tabLength) <<axisNames.first <<std::setw(tabLength) <<axisNames.second <<"\n";
171 
172  //Add data in two columns
173  for(typename std::multiset< std::pair<Tx, Ty> >::iterator it = dataSet.begin(); it != dataSet.end(); ++it)
174  {
175  file<<" " <<std::setw(tabLength) <<it->first <<std::setw(tabLength) <<it->second <<"\n";
176  }
177 
178  file.close();
179  }
180 }
181 
182 
183 
189 template <typename Tx, typename Ty>
190 void DataCollector2D<Tx, Ty>::appendGnuPlotFile(const std::string& filename, bool firstTime)
191 {
192  std::ofstream file(filename.c_str(), std::ios::app);
193  int tabLength = axisNames.first.length()+20;
194  file<<std::left;
195  file<<std::fixed <<std::setprecision(5);
196  if(file.is_open())
197  {
198  if(firstTime)
199  {
200  //First add name and axis names as comments
201  file<<"# " <<dataSetName <<"\n";
202  file<<"# " <<std::setw(tabLength) <<axisNames.first <<std::setw(tabLength) <<axisNames.second <<"\n";
203  }
204 
205  //Add data in two columns
206  for(typename std::multiset< std::pair<Tx, Ty> >::iterator it = dataSet.begin(); it != dataSet.end(); ++it)
207  {
208  file<<" " <<std::setw(tabLength) <<it->first <<std::setw(tabLength) <<it->second <<"\n";
209  }
210 
211  file.close();
212  }
213 }
214 
215 
216 
217 }
218 
219 #endif
220 
221 
A class that can be used to collect 2D data.
Definition: data_collector.h:39
void appendDataToFile(const std::string &filename, bool firstTime, DataExportFormat format=GNUPLOT)
Definition: data_collector.h:134
void writeDataToFile(const std::string &filename="", DataExportFormat format=GNUPLOT)
Definition: data_collector.h:105
void addData(Tx x, Ty y)
Definition: data_collector.h:84
DataCollector2D(const std::string &_dataSetName, const std::string &_axisNameX, const std::string &_axisNameY)
Definition: data_collector.h:70
void clear()
Definition: data_collector.h:93