testRunner.cpp
Go to the documentation of this file.
1 
22 #include <boost/program_options.hpp>
23 #include <string>
24 #include <set>
25 #include <stack>
26 #include <queue>
27 #include <utility>
28 #include <boost/filesystem.hpp>
29 #include <sys/time.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <fstream>
33 #include <iostream>
34 
35 using namespace ISM;
36 using namespace std;
37 namespace po = boost::program_options;
38 namespace fs = boost::filesystem;
39 
40 struct TestSpec {
41  int setCount;
42  bool useClustering;
43  int objectCount;
44  double bin_size;
45  bool useId;
46  bool useType;
47 };
48 
49 struct TestResult {
50  TestSpec spec;
51  double avgRuntime;
52  double avgConfidence;
53 };
54 
55 void removeFileIfExists(const string& filename) {
56  if (fs::exists(filename)) {
57  fs::remove(filename);
58  }
59 }
60 
61 void printTest(const TestSpec& t) {
62  cerr << t.setCount << "," << t.objectCount << "," << t.bin_size << "," << t.useClustering
63  << "," << t.useType << "," << t.useId << endl;
64 }
65 
66 void printTestResult(const TestResult& r) {
67  cerr << r.spec.setCount << "," << r.spec.objectCount << "," << r.spec.bin_size << "," << r.spec.useClustering
68  << "," << r.spec.useType << "," << r.spec.useId << "," << r.avgRuntime << "," << r.avgConfidence << endl;
69 }
70 
71 void writeTestResult(const TestResult& r, ofstream& csvFile) {
72  csvFile << r.spec.setCount << "," << r.spec.objectCount << "," << r.spec.bin_size << "," << r.spec.useClustering
73  << "," << r.spec.useType << "," << r.spec.useId << "," << r.avgRuntime << "," << r.avgConfidence
74  << endl;
75 }
76 
77 int main(int argc, char** argv) {
78  po::options_description desc("Allowed options");
79  desc.add_options()("help,h", "produce help message")
80  ("database-file,d", po::value<string>()->default_value("record.sqlite"), "database file to use")
81  ("test-database-file,t", po::value<string>()->default_value("test.sqlite"), "database file write tests to")
82  ("csv-file,c", po::value<string>()->default_value("results.csv"), "csv file to write results");
83 
84  po::variables_map vm;
85  po::store(po::parse_command_line(argc, argv, desc), vm);
86  po::notify(vm);
87 
88  if (vm.count("help")) {
89  cerr << desc << "\n";
90  return 0;
91  }
92 
93  vector<int> setCounts = { 10, 50, 100, 150, 200, 250, 300, 350, 400 };
94  vector<bool> useClusterings = { true, false };
95  vector<int> objectCounts = { 1, 2, 3, 4, 5, 6 };
96  vector<double> bin_sizes = {
97  0.5, 0.4, 0.3, 0.2, 0.1,
98  0.09, 0.08, 0.07, 0.06, 0.05, 0.04, 0.03, 0.02, 0.01
99  };
100  vector<bool> useIds = { true };
101  vector<bool> useTypes = { true };
102 
103  queue<TestSpec> tests;
104  stack<TestResult> testResults;
105 
106  for (const int& setCount : setCounts) {
107  for (const bool& useClustering : useClusterings) {
108  for (const int& objectCount : objectCounts) {
109  for (const double& bin_size : bin_sizes) {
110  for (const bool& useId : useIds) {
111  for (const bool& useType : useTypes) {
112  TestSpec t = { setCount, useClustering, objectCount, bin_size, useId, useType };
113  tests.push(t);
114  }
115  }
116  }
117  }
118  }
119  }
120 
121 
122  string sourceFile = vm["database-file"].as<string>();
123  string testFile = vm["test-database-file"].as<string>();
124 
125  string csvFilename = vm["csv-file"].as<string>();
126  removeFileIfExists(csvFilename);
127  ofstream csvFile;
128  csvFile.open(csvFilename.c_str());
129  csvFile << "setCount,objectCount,bin_size,useClustering,useType,useId,runtime,confidence" << endl;
130 
131  while (!tests.empty()) {
132  removeFileIfExists(testFile);
133  TestSpec test = tests.front();
134  tests.pop();
135 
136  printTest(test);
137 
138  TableHelper source = TableHelper(sourceFile);
139  TableHelper* target = new TableHelper(testFile);
140  set<pair<string, string> > typesAndIds;
141  string patternName = source.getRecordedPatternNames()[0];
142 
143  const RecordedPatternPtr pattern = source.getRecordedPattern(patternName);
144  for (ObjectSetPtr& set : pattern->objectSets)
145  {
146  for (ObjectPtr& obj : set->objects)
147  {
148  pair<string, string> p = make_pair(obj->type, obj->observedId);
149  if (typesAndIds.find(p) == typesAndIds.end()) {
150  typesAndIds.insert(p);
151  }
152  }
153  }
154 
155  set<pair<string, string> > typesAndIdsToUse;
156  while (typesAndIdsToUse.size() < (size_t)test.objectCount) {
157  set<pair<string, string> >::iterator first = typesAndIds.begin();
158  typesAndIdsToUse.insert(*first);
159  typesAndIds.erase(first);
160  }
161 
162  cerr << "creating test database";
163  int insertedSets = 0;
164  string patternNameToUse;
165  for (ObjectSetPtr& set : pattern->objectSets)
166  {
167  ObjectSetPtr newSet(new ObjectSet());
168  for (ObjectPtr& obj : set->objects)
169  {
170  pair<string, string> p = make_pair(obj->type, obj->observedId);
171  if (typesAndIdsToUse.find(p) != typesAndIdsToUse.end()) {
172  newSet->insert(obj);
173  }
174  }
175  target->insertRecordedObjectSet(newSet, patternName);
176  insertedSets++;
177  cerr << ".";
178  cerr.flush();
179  if (insertedSets >= test.setCount) {
180  break;
181  }
182  }
183  cerr << " done" << endl;
184 
185  delete (target);
186 
187  Trainer* trainer = new Trainer(testFile);
188  if (!test.useClustering) {
189  trainer->setUseClustering(false);
190  }
191  trainer->trainPattern();
192  delete trainer;
193 
194  TestResult testResult = { test, 0, 0 };
195  target = new TableHelper(testFile);
196  cerr <<"recognizing";
197  for (int i = 0; i < insertedSets; i++) {
198  const ObjectSetPtr testSet = target->getRecordedObjectSet(i);
199 
200  for (ObjectPtr& obj : testSet->objects) {
201  if (!test.useType) {
202  obj->type = "";
203  }
204  if (!test.useId) {
205  obj->observedId = "";
206  }
207  }
208 
209  Recognizer recognizer(testFile, test.bin_size, 10, true);
210 
211  struct timeval start, end;
212 
213  gettimeofday(&start, NULL);
214 
215  vector<RecognitionResultPtr> results = recognizer.recognizePattern(testSet, 0, 1);
216 
217  gettimeofday(&end, NULL);
218 
219  long mtime, seconds, useconds;
220  seconds = end.tv_sec - start.tv_sec;
221  useconds = end.tv_usec - start.tv_usec;
222 
223  mtime = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
224 
225  testResult.avgRuntime += mtime;
226 
227  for (RecognitionResultPtr& result : results) {
228  if (result->patternName == patternName) {
229  testResult.avgConfidence += result->confidence;
230  }
231  }
232  cerr<<".";
233  cerr.flush();
234  }
235 
236  cerr<<"done"<<endl;
237  delete (target);
238 
239  testResult.avgConfidence /= (double) insertedSets;
240  testResult.avgRuntime /= (double) insertedSets;
241 
242  testResults.push(testResult);
243  printTestResult(testResult);
244  writeTestResult(testResult, csvFile);
245  }
246 
247  csvFile.close();
248 
249  return 0;
250 }
const ObjectSetPtr getRecordedObjectSet(int setId) const
void removeFileIfExists(const string &filename)
Definition: testRunner.cpp:55
bool useType
Definition: testRunner.cpp:46
void printTestResult(const TestResult &r)
Definition: testRunner.cpp:66
void setUseClustering(const bool useClustering)
Definition: Trainer.cpp:56
double avgConfidence
int insertRecordedObjectSet(const boost::shared_ptr< ObjectSet > &os, const std::string &patternName)
std::string patternName
boost::shared_ptr< RecordedPattern > RecordedPatternPtr
void trainPattern()
Definition: Trainer.cpp:60
int insertedSets
int main(int argc, char **argv)
Definition: testRunner.cpp:77
void writeTestResult(const TestResult &r, ofstream &csvFile)
Definition: testRunner.cpp:71
bool useId
Definition: testRunner.cpp:45
boost::shared_ptr< ObjectSet > ObjectSetPtr
Definition: ObjectSet.hpp:53
boost::shared_ptr< RecognitionResult > RecognitionResultPtr
const std::vector< RecognitionResultPtr > recognizePattern(const ObjectSetPtr &objectSet, const double filterThreshold=0.0, const int resultsPerPattern=-1, const std::string targetPatternName="")
Definition: Recognizer.cpp:47
std::vector< unsigned > objectCounts
std::vector< std::string > getRecordedPatternNames() const
void printTest(const TestSpec &t)
Definition: testRunner.cpp:61
this namespace contains all generally usable classes.
boost::shared_ptr< Object > ObjectPtr
Definition: Object.hpp:82
const RecordedPatternPtr getRecordedPattern(const std::string &patternName) const


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:41