validator.cpp
Go to the documentation of this file.
1 
21 #include <boost/program_options.hpp>
22 #include <string>
23 #include <set>
24 #include <stack>
25 #include <utility>
26 
27 using namespace ISM;
28 using namespace std;
29 namespace po = boost::program_options;
30 
31 bool detectGeneric = false;
32 int onlyN = -1;
33 int maxRuns = -1;
34 int runs = 0;
35 
37  if (maxRuns >= 0 && runs >= maxRuns) {
38  return;
39  }
40  cout << "validate pattern " << pattern->name << endl;
41  int idx = 0;
42  int setCount = 0;
43  int objectCount = 0;
44  int identifySum = 0;
45  double confidenceSum = 0;
46  double thresholdConfidence = 0.8;
47  for (ObjectSetPtr& os : pattern->objectSets) {
48  if (onlyN >= 0 && onlyN != idx) {
49  idx++;
50  continue;
51  }
52 
53  if (maxRuns >= 0 && runs >= maxRuns) {
54  break;
55  }
56 
57  set<pair<string, string> > mappedTypes;
58 
59  if (detectGeneric)
60  {
61  for (ObjectPtr& obj : os->objects) {
62  mappedTypes.insert(make_pair(obj->type, obj->observedId));
63  objectCount++;
64  obj->type = "";
65  obj->observedId = "";
66  }
67  }
68  setCount++;
69  runs++;
70 
71  vector<RecognitionResultPtr> results = recognizer->recognizePattern(os);
72  bool found = false;
73  for (RecognitionResultPtr& result : results) {
74  if (result->patternName == pattern->name) {
75  vector<RecognitionResultPtr> all;
76  all.push_back(result);
77  stack<RecognitionResultPtr> subPatterns;
78 
79  for (RecognitionResultPtr& subPattern : result->subPatterns) {
80  subPatterns.push(subPattern);
81  }
82 
83  while (subPatterns.size() > 0) {
84  RecognitionResultPtr sp = subPatterns.top();
85  subPatterns.pop();
86  all.push_back(sp);
87  for (RecognitionResultPtr& subPattern : sp->subPatterns) {
88  subPatterns.push(subPattern);
89  }
90  }
91 
92  for (RecognitionResultPtr& pattern : all) {
93  for (ObjectPtr& obj : pattern->recognizedSet->objects) {
94  set<pair<string, string> >::iterator match = mappedTypes.find(make_pair(obj->type, obj->observedId));
95  if (match != mappedTypes.end()) {
96  mappedTypes.erase(match);
97  identifySum++;
98  }
99  }
100  }
101 
102  found = true;
103  confidenceSum += result->confidence;
104  break;
105  }
106  }
107  cout << (found ? "." : ",");
108  cout.flush();
109  idx++;
110  if ((onlyN >= 0 && onlyN - 1 == idx)) {
111  break;
112  }
113  }
114  cout << endl;
115 
116  double meanConfidence = confidenceSum / (double) setCount;
117  cout << (meanConfidence >= thresholdConfidence ? "SUCCESS" : "FAILURE") << " mean confidence for pattern '"
118  << pattern->name << "' is " << meanConfidence << endl;
119  if (detectGeneric) {
120  double identifyRate = (double) identifySum / (double) objectCount;
121  cout << "Identification rate: " << identifyRate << endl;
122  }
123 }
124 
125 int main(int argc, char** argv) {
126  double bin_size = 0.005;
127 
128  po::options_description desc("Allowed options");
129  desc.add_options()("help,h", "produce help message")("database-file,d",
130  po::value<string>()->default_value("record.sqlite"), "database file to use")("generic-mode,g",
131  po::bool_switch(&detectGeneric),
132  "test object type inference by removing object type and id from recognition input")("bin_size,s",
133  po::value<double>(&bin_size)->default_value(0.0001), "recognizer bin_size")("pattern-name,p",
134  po::value<vector<string> >(), "patters to validate instead of all")("onlyN,o",
135  po::value<int>(&onlyN)->default_value(-1), "only the n'th set")("maxRuns,m",
136  po::value<int>(&maxRuns)->default_value(-1), "test a maximum of m sets");
137 
138  po::variables_map vm;
139  po::store(po::parse_command_line(argc, argv, desc), vm);
140  po::notify(vm);
141 
142  if (vm.count("help")) {
143  cout << desc << "\n";
144  return 1;
145  }
146 
147  TableHelper t(vm["database-file"].as<string>());
148  RecognizerPtr r(new Recognizer(vm["database-file"].as<string>(), bin_size, 10, false));
149  if (vm.count("pattern-name")) {
150  vector<string> patternNames = vm["pattern-name"].as<vector<string> >();
151  for (string& name : patternNames) {
152  RecordedPatternPtr pattern = t.getRecordedPattern(name);
153  if (!pattern) {
154  cout << "Pattern " << name << " not found!";
155  continue;
156  }
157  validatePattern(pattern, r);
158  }
159  } else {
160  vector<string> patternNames = t.getRecordedPatternNames();
161  for (string& name : patternNames) {
162  RecordedPatternPtr pattern = t.getRecordedPattern(name);
163  if (!pattern) {
164  cout << "Pattern " << name << " not found!";
165  continue;
166  }
167  validatePattern(pattern, r);
168  }
169  }
170 }
void validatePattern(RecordedPatternPtr pattern, RecognizerPtr recognizer)
Definition: validator.cpp:36
boost::shared_ptr< Recognizer > RecognizerPtr
Definition: Recognizer.hpp:171
int maxRuns
Definition: validator.cpp:33
int runs
Definition: validator.cpp:34
boost::shared_ptr< RecordedPattern > RecordedPatternPtr
boost::shared_ptr< ObjectSet > ObjectSetPtr
Definition: ObjectSet.hpp:53
boost::shared_ptr< RecognitionResult > RecognitionResultPtr
bool detectGeneric
Definition: validator.cpp:31
int onlyN
Definition: validator.cpp:32
std::vector< std::string > getRecordedPatternNames() const
this namespace contains all generally usable classes.
boost::shared_ptr< Object > ObjectPtr
Definition: Object.hpp:82
int main(int argc, char **argv)
Definition: validator.cpp:125
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