DocumentationHelper.cpp
Go to the documentation of this file.
1 
18 #include "DocumentationHelper.hpp"
19 #include "TableHelper.hpp"
20 #include "../typedef.hpp"
21 
22 namespace ISM {
23 
25  {
26  mSVGHelper->writeResult();
27  }
28 
29  void DocumentationHelper::storeOptimizationRun(std::vector<std::vector<std::pair<TopologyPtr, unsigned int>>>& history,
30  CostFunctionPtr<TopologyPtr> globalCostFunction,
31  double elapsedRuntime, const std::string& patternName)
32  {
33  unsigned int numEvaluatedTopologies = 0;
34  std::vector<TopologyPtr> selectedTopologies;
35 
36  for (unsigned int i = 0; i < history.size(); ++i) {
37  std::vector<std::pair<TopologyPtr, unsigned int>> round = history[i];
38 
39  numEvaluatedTopologies += round.size();
40  for (unsigned int j = 0; j < round.size(); ++j) {
41  if (round[j].second != 0)
42  {
43  selectedTopologies.push_back(round[j].first);
44  break;
45  }
46  }
47 
48  }
49 
50  mSVGHelper->processHistory(history, globalCostFunction, patternName);
51 
52  storeTopologiesToCSV(selectedTopologies, patternName);
53  storeMetadataToCSV(numEvaluatedTopologies, history.size(), elapsedRuntime, patternName);
54 
56  mReferenceTopology.reset();
57  }
58 
59  void DocumentationHelper::storeTopologiesToCSV(std::vector<TopologyPtr> selectedTopologies,
60  const std::string& patternName)
61  {
62  std::ostringstream s;
63  for (unsigned int i = 0; i < selectedTopologies.size(); ++i) {
64  s << i << ", " << selectedTopologies[i]->evaluationResult.falsePositives
65  << ", " << selectedTopologies[i]->evaluationResult.averageRecognitionRuntime << std::endl;
66  }
67  path filePath = mOutputPath / CSV_SUBFOLDER_NAME / (patternName + "_plot.csv");
68 
69  writeToFile(filePath, s.str());
70  }
71 
72  void DocumentationHelper::storeMetadataToCSV(unsigned int numEvaluatedTopologies, unsigned int numOptimizationRounds,
73  double elapsedRuntime, const std::string& patternName)
74  {
75  std::ostringstream s;
76  s << numEvaluatedTopologies << " , " << numOptimizationRounds << " , " << elapsedRuntime;
77  path filePath = mOutputPath / CSV_SUBFOLDER_NAME / (patternName + "_metadata.csv");
78 
79  writeToFile(filePath, s.str());
80  }
81 
83  const std::string& topologyName, std::map<std::string, std::vector<ISM::VoteSpecifierPtr>> objectDefinitons)
84  {
86  if(!boost::filesystem::exists(filePath))
87  boost::filesystem::create_directories(filePath);
88 
89  filePath /= topologyName;
90  if(!boost::filesystem::exists(filePath))
91  boost::filesystem::create_directories(filePath);
92 
93  ObjectRelations objectRelations = toStore->objectRelations;
94  std::ostringstream os;
95 
96  os << patternName;
97  for (ObjectRelations::iterator it = objectRelations.begin(); it != objectRelations.end(); ++it)
98  {
99  os << ":" << std::to_string(it->first);
100  }
101  writeToFile(filePath / "startFile.txt", os.str());
102 
103  os.str("");
104 
105  EvaluationResult er = toStore->evaluationResult;
106  os << "False Positives, False Negatives, Average Evaluation Duration" << std::endl;
107  os << er.falsePositives << ", " << er.falseNegatives << ", " << er.averageRecognitionRuntime;
108  writeToFile(filePath / "evaluationResult.csv", os.str());
109 
110  mDotHelper->storeISMToDot(filePath, patternName, topologyName, objectDefinitons);
111 
112  if (mReferenceTopology)
113  {
114  mDotHelper->storeRelationsToDot(filePath, patternName, topologyName,
115  objectRelations, mReferenceTopology->objectRelations);
116  }
117  else
118  {
119  mDotHelper->storeRelationsToDot(filePath, patternName, topologyName, objectRelations);
120  }
121  }
122 
123 
124  void DocumentationHelper::writeToFile(path filePath, const std::string& content)
125  {
126  std::ofstream file;
127  std::ios_base::iostate exceptionMask = file.exceptions() | std::ios::failbit | std::ios::badbit;
128  file.exceptions(exceptionMask);
129  try
130  {
131  file.open(filePath.string());
132  file << content;
133  file.flush();
134  file.close();
135  }
136  catch (std::ios_base::failure& e)
137  {
138  std::cerr << e.what() << "\n";
139  }
140  }
141 
142  void DocumentationHelper::writeIsmToDB(path dbPath, const IsmPtr & ism)
143  {
144  if (!mStoreISM) return;
145  LogHelper::logMessage("\nStoring ISM to : " + dbPath.string());
146 
147  if(!boost::filesystem::exists(dbPath.parent_path()))
148  boost::filesystem::create_directories(dbPath.parent_path());
149 
150  if (!boost::filesystem::exists(dbPath))
151  {
152  boost::filesystem::copy(mSourceDBPath, dbPath);
153  }
154 
155  try
156  {
157  TableHelperPtr localTableHelper(new TableHelper(dbPath.string()));
158  localTableHelper->createTablesIfNecessary();
159 
161 
162  for (const std::string& typeIt : ism->objectTypes)
163  {
164  localTableHelper->ensureModelObjectType(typeIt);
165  }
166  for (const std::pair<std::string, PatternPtr>& patternIt : ism->patternDefinitions)
167  {
168  localTableHelper->upsertModelPattern(patternIt.second->name,
169  patternIt.second->expectedMaxWeight);
170  }
171 
172  unsigned int numVotes = 0;
173  unsigned int numVotesInserted = 0;
174 
175  for (const std::pair<std::string, std::vector<VoteSpecifierPtr> >& objectTypeIt : ism->voteSpecifiersPerObject)
176  {
177  numVotes += objectTypeIt.second.size();
178  }
179 
180  for (std::pair<const std::string, std::vector<VoteSpecifierPtr> >& objectTypeIt : ism->voteSpecifiersPerObject)
181  {
182  if (objectTypeIt.second.size() == 0)
183  {
184  continue;
185  }
186  for (VoteSpecifierPtr& voteIt : objectTypeIt.second)
187  {
188  localTableHelper->insertModelVoteSpecifier(voteIt);
189  numVotesInserted++;
190  LogHelper::displayProgress(((double) numVotesInserted) / numVotes);
191  }
192 
193  }
194  }
195  catch (soci::soci_error& e)
196  {
197  std::cerr<<"soci error\n"<<e.what()<<std::endl;
198  }
199  }
200 
202  {
204  }
205 
206  void DocumentationHelper::storeIsm(const std::string& filename, const IsmPtr& ism)
207  {
208  if (!mStoreISM) return;
209 
210  path dbPath = mOutputPath / "SQL" / (filename + ".sqlite");
211  writeIsmToDB(dbPath, ism);
212  }
213 
215  {
216  std::string topologyName = "selected_topology_" + std::to_string(mSelectedTopologyCounter);
217  path filePath = mOutputPath / TOPOLOGIES_SUBFOLDER_NAME / patternName / SELECTED_TOPOLOGIES_FOLDER;
218 
219  if(!boost::filesystem::exists(filePath))
220  boost::filesystem::create_directories(filePath);
221 
222  if (mReferenceTopology)
223  {
224  mDotHelper->storeRelationsToDot(filePath, patternName, topologyName,
225  topology->objectRelations, mReferenceTopology->objectRelations);
226  }
227  else
228  {
229  mDotHelper->storeRelationsToDot(filePath, patternName, topologyName, topology->objectRelations);
230  }
231 
232  mReferenceTopology = topology;
234  }
235 }
boost::shared_ptr< CostFunction< InstanceType >> CostFunctionPtr
void storeTopologiesToCSV(std::vector< TopologyPtr > selectedTopologies, const std::string &patternNames)
void writeIsmToDB(path dbPath, const IsmPtr &ism)
void writeToFile(path filePath, const std::string &content)
void storeTopology(TopologyPtr toStore, const std::string &patternName, const std::string &topologyName, std::map< std::string, std::vector< ISM::VoteSpecifierPtr >> objectDefinitons)
static void displayProgress(double progress)
Definition: LogHelper.cpp:147
void storeMetadataToCSV(unsigned int numEvaluatedTopologies, unsigned int numOptimizationRounds, double elapsedRuntime, const std::string &patternName)
std::string patternName
boost::shared_ptr< TableHelper > TableHelperPtr
boost::shared_ptr< Topology > TopologyPtr
Definition: Topology.hpp:51
static void logMessage(const std::string &message, LogLevel logLevel=LOG_INFO, const char *logColor=LOG_COLOR_DEFAULT)
Definition: LogHelper.cpp:96
void storeOptimizationRun(std::vector< std::vector< std::pair< TopologyPtr, unsigned int >>> &history, CostFunctionPtr< TopologyPtr > globalCostFunction, double elapsedRuntime, const std::string &patternName)
std::map< unsigned int, ISM::ObjectRelationPtr, std::less< unsigned > > ObjectRelations
boost::shared_ptr< ImplicitShapeModel > IsmPtr
const std::string SELECTED_TOPOLOGIES_FOLDER
const std::string CSV_SUBFOLDER_NAME
boost::shared_ptr< VoteSpecifier > VoteSpecifierPtr
const std::string TOPOLOGIES_SUBFOLDER_NAME
this namespace contains all generally usable classes.
void storeIsm(const std::string &filename, const IsmPtr &ism)
void setReferenceTopology(TopologyPtr topology, const std::string &patternName)


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:40