KSquare Utilities
ModelParamDual.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 
3 #include <stdio.h>
4 
5 #include <fstream>
6 #include <string>
7 #include <iostream>
8 #include <vector>
9 
10 
11 #include "MemoryDebug.h"
12 using namespace std;
13 
14 
15 #include "GlobalGoalKeeper.h"
16 #include "KKBaseTypes.h"
17 #include "OSservices.h"
18 #include "RunLog.h"
19 using namespace KKB;
20 
21 
22 #include "ModelParamDual.h"
23 #include "FileDesc.h"
24 #include "MLClass.h"
25 #include "KKMLLTypes.h"
27 using namespace KKMLL;
28 
29 
30 
32  ModelParam (),
33  configFileName1 (),
34  configFileName2 (),
35  fullHierarchyMustMatch (false),
36  otherClass (NULL),
37  probFusionMethod (ProbFusionMethod::Or)
38 {
39 }
40 
41 
42 
43 
44 ModelParamDual::ModelParamDual (const KKStr& _configFileName1,
45  const KKStr& _configFileName2,
46  bool _fullHierarchyMustMatch
47  ):
48 
49  ModelParam (),
50  configFileName1 (_configFileName1),
51  configFileName2 (_configFileName2),
52  fullHierarchyMustMatch (_fullHierarchyMustMatch),
53  otherClass (NULL),
54  probFusionMethod (ProbFusionMethod::Or)
55 {
56 }
57 
58 
59 
60 
61 
62 
63 
64 
66  ModelParam (_param),
67  configFileName1 (_param.configFileName1),
68  configFileName2 (_param.configFileName2),
69  fullHierarchyMustMatch (_param.fullHierarchyMustMatch),
70  otherClass (_param.otherClass),
71  probFusionMethod (_param.probFusionMethod)
72 {
73 }
74 
75 
76 
78 {
79 }
80 
81 
83 {
84  if (s.EqualIgnoreCase ("And"))
85  return ProbFusionMethod::And;
86 
87  else if (s.EqualIgnoreCase ("Or"))
88  return ProbFusionMethod::Or;
89 
90  else
92 }
93 
94 
96 {
97  if ((pfm < ProbFusionMethod::Null) || (pfm > ProbFusionMethod::And))
98  return KKStr::EmptyStr ();
99 
100  switch (pfm)
101  {
103  return "And";
104  break;
105 
107  return "Or";
108  break;
109  }
110 
111  return KKStr::EmptyStr ();
112 }
113 
114 
115 
116 
117 ModelParamDualPtr ModelParamDual::Duplicate () const
118 {
119  return new ModelParamDual (*this);
120 } /* Duplicate */
121 
122 
123 
124 
125 void ModelParamDual::ParseCmdLineParameter (const KKStr& parameter,
126  const KKStr& value,
127  bool& parameterUsed,
128  RunLog& log
129  )
130 {
131  if (parameter.EqualIgnoreCase ("-ConfigFileName1") ||
132  parameter.EqualIgnoreCase ("-Config1") ||
133  parameter.EqualIgnoreCase ("-cfn1") ||
134  parameter.EqualIgnoreCase ("-classifier1") ||
135  parameter.EqualIgnoreCase ("-c1")
136  )
137  {
138  configFileName1 = value;
139  parameterUsed = true;
140  if (!TrainingConfiguration2::ConfigFileExists (configFileName1))
141  {
142  log.Level (-1) << "ModelParamDual::ParseCmdLineParameter ***ERROR*** Configuration File1[" << configFileName1 << "] Does not exist." << endl;
143  ValidParam (false);
144  }
145  }
146 
147  else
148  if (parameter.EqualIgnoreCase ("-ConfigFileName2") ||
149  parameter.EqualIgnoreCase ("-Config2") ||
150  parameter.EqualIgnoreCase ("-cfn2") ||
151  parameter.EqualIgnoreCase ("-classifier2") ||
152  parameter.EqualIgnoreCase ("-c2")
153  )
154  {
155  configFileName2 = value;
156  parameterUsed = true;
157  if (!TrainingConfiguration2::ConfigFileExists (configFileName2))
158  {
159  log.Level (-1) << "ModelParamDual::ParseCmdLineParameter ***ERROR*** Configuration File2[" << configFileName2 << "] Does not exist." << endl;
160  ValidParam (false);
161  }
162  }
163 
164  else
165  if (parameter.EqualIgnoreCase ("-FullHierarchyMustMatch") ||
166  parameter.EqualIgnoreCase ("-FHMM")
167  )
168  {
169  if (value.Empty ())
170  fullHierarchyMustMatch = true;
171  else
172  fullHierarchyMustMatch = value.ToBool ();
173  parameterUsed = true;
174  }
175 
176  else
177  if (parameter.EqualIgnoreCase ("-OtherClass") ||
178  parameter.EqualIgnoreCase ("-Other") ||
179  parameter.EqualIgnoreCase ("-OC")
180  )
181  {
182  if (value.Empty ())
183  {
184  log.Level (-1) << "ModelParamDual::ParseCmdLineParameter ***ERROR*** -OtherClass parameter must specify a class." << endl;
185  ValidParam (false);
186  }
187  else
188  {
189  otherClass = MLClass::CreateNewMLClass (value);
190  }
191  parameterUsed = true;
192  }
193 
194  else
195  if (parameter.EqualIgnoreCase ("-ProbFusionMethod") ||
196  parameter.EqualIgnoreCase ("-PFM")
197  )
198  {
199  probFusionMethod = ProbFusionMethodFromStr (value);
200  }
201 } /* ParseCmdLineParameter */
202 
203 
204 
205 
206 /**
207  @brief // Will get called after the entire parameter string has been processed.
208  */
209 void ModelParamDual::ParseCmdLinePost (RunLog& log)
210 {
211  if (configFileName1.Empty () || configFileName2.Empty ())
212  {
213  log.Level (-1) << "ModelParamDual::ParseCmdLinePost ***ERROR*** You need to specify two configuration files -config1 and -config2" << endl;
214  ValidParam (false);
215  }
216 }
217 
218 
219 /**
220  * @brief Convert all parameters to a command line string.
221 */
223 {
224  KKStr cmdStr (300);
226 
227  if (!configFileName1.Empty ())
228  cmdStr << " " << "-Classifier1" << " " << configFileName1;
229 
230  if (!configFileName2.Empty ())
231  cmdStr << " " << "-Classifier2" << " " << configFileName2;
232 
233  if (fullHierarchyMustMatch)
234  cmdStr << " " << "-FullHierarchyMustMatch" << " " << "Yes";
235 
236  if (otherClass)
237  cmdStr << " " << "-OtherClass" << " " << otherClass->Name ();
238 
239  cmdStr << " " << "-ProbFusionMethod" << " " << ProbFusionMethodToStr (this->probFusionMethod);
240 
241  return cmdStr;
242 } /* ToCmdLineStr */
243 
244 
245 
246 
247 void ModelParamDual::WriteXML (const KKStr& varName,
248  ostream& o
249  ) const
250 {
251  XmlTag startTag ("ModelParamDual", XmlTag::TagTypes::tagStart);
252  if (!varName.Empty ())
253  startTag.AddAtribute ("VarName", varName);
254  startTag.WriteXML (o);
255  o << endl;
256 
257 
259 
260  configFileName1.WriteXML ("ConfigFileName1", o);
261  configFileName2.WriteXML ("ConfigFileName1", o);
262 
263  XmlElementBool::WriteXML (fullHierarchyMustMatch, "FullHierarchyMustMatch", o);
264  if (otherClass)
265  otherClass->WriteXML ("OtherClass", o);
266 
267  ProbFusionMethodToStr (probFusionMethod).WriteXML ("ProbFusionMethod", o);
268 
269  XmlTag endTag ("ModelParamDual", XmlTag::TagTypes::tagEnd);
270  endTag.WriteXML (o);
271  o << endl;
272 } /* WriteXML */
273 
274 
275 
276 
277 
279  XmlTagConstPtr tag,
280  VolConstBool& cancelFlag,
281  RunLog& log
282  )
283 {
284  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
285  while (t && (!cancelFlag))
286  {
288  if (t)
289  {
290  const KKStr& varName = t->VarName ();
291 
292  if (varName.EqualIgnoreCase ("ConfigFileName1"))
293  configFileName1 = *(dynamic_cast<XmlElementKKStrPtr> (t)->Value ());
294 
295  else if (varName.EqualIgnoreCase ("ConfigFileName2"))
296  configFileName2 = *(dynamic_cast<XmlElementKKStrPtr> (t)->Value ());
297 
298  else if (varName.EqualIgnoreCase ("FullHierarchyMustMatch"))
299  fullHierarchyMustMatch = dynamic_cast<XmlElementBoolPtr> (t)->Value ();
300 
301  else if ((varName.EqualIgnoreCase ("OtherClass")) && (typeid (*t) == typeid (XmlElementMLClass)))
302  otherClass = dynamic_cast<XmlElementMLClassPtr> (t)->Value ();
303 
304  else if (varName.EqualIgnoreCase ("ProbFusionMethod"))
305  probFusionMethod = ProbFusionMethodFromStr (*(dynamic_cast<XmlElementKKStrPtr> (t)->Value ()));
306  }
307  delete t;
308  t = s.GetNextToken (cancelFlag, log);
309  }
310  delete t;
311  t = NULL;
312 } /* ReadXML */
313 
314 
315 
316 XmlFactoryMacro(ModelParamDual)
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for &#39;size&#39; characters.
Definition: KKStr.cpp:655
XmlTag(const KKStr &_name, TagTypes _tagType)
Definition: XmlStream.cpp:586
virtual KKStr ToCmdLineStr() const
Creates a Command Line String that represents these parameters.
static ProbFusionMethod ProbFusionMethodFromStr(const KKStr &s)
bool EqualIgnoreCase(const char *s2) const
Definition: KKStr.cpp:1257
virtual KKStr ToCmdLineStr() const
Creates a a Command Line String that represents these parameters.
Definition: ModelParam.cpp:367
void WriteXMLFields(std::ostream &o) const
Definition: ModelParam.cpp:464
ModelParamDual(const ModelParamDual &_param)
bool Value() const
Definition: XmlStream.cpp:1028
MLClassPtr Value() const
Definition: MLClass.cpp:540
static KKStr ProbFusionMethodToStr(ProbFusionMethod pfm)
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
XmlElementBool * XmlElementBoolPtr
Definition: XmlStream.h:524
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
static void WriteXML(const bool b, const KKStr &varName, std::ostream &o)
Definition: XmlStream.cpp:1035
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
virtual void ValidParam(bool _validParam)
Definition: ModelParam.h:132
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
KKTHread * KKTHreadPtr
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Definition: XmlStream.cpp:602
XmlTokenPtr ReadXMLModelParamToken(XmlTokenPtr t)
Will process any tokens that belong to &#39;ModelParam&#39; and return NULL ones that are not will be passed ...
Definition: ModelParam.cpp:487
static bool ConfigFileExists(const KKStr &_configFileName)
bool Empty() const
Definition: KKStr.h:241
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
static const KKStr & EmptyStr()
Static method that returns an Empty String.
Definition: KKStr.cpp:3453
const KKStr & Name() const
Definition: MLClass.h:154
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: KKStr.cpp:4420
static MLClassPtr CreateNewMLClass(const KKStr &_name, kkint32 _classId=-1)
Static method used to create a new instance of a MLClass object.
Definition: MLClass.cpp:100
ModelParam(const ModelParam &_param)
Definition: ModelParam.cpp:61
void WriteXML(std::ostream &o)
Definition: XmlStream.cpp:723
KKStr & operator=(const KKStr &src)
Definition: KKStr.cpp:1390
Used for logging messages.
Definition: RunLog.h:49
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
virtual void WriteXML(const KKStr &varName, ostream &o) const
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: MLClass.cpp:448
ModelParamDual(const KKStr &_configFileName1, const KKStr &_configFileName2, bool _fullHierarchyMustMatch)
XmlElementMLClass * XmlElementMLClassPtr
Definition: MLClass.h:572
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
bool ToBool() const
Returns the bool equivalent of the string, ex &#39;Yes&#39; = true, &#39;No&#39; = false, &#39;True&#39; = true...
Definition: KKStr.cpp:3523
Abstract Base class for Machine Learning parameters.
Definition: ModelParam.h:35
virtual const KKStr & VarName() const
Definition: XmlStream.h:269
#define XmlFactoryMacro(NameOfClass)
Definition: XmlStream.h:688
virtual ModelParamDualPtr Duplicate() const
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163