KSquare Utilities
TrainingClass.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <string>
3 #include <iostream>
4 #include <fstream>
5 #include <map>
6 #include <vector>
7 #include "MemoryDebug.h"
8 using namespace std;
9 
10 #include "GlobalGoalKeeper.h"
11 #include "KKBaseTypes.h"
12 #include "OSservices.h"
13 #include "XmlStream.h"
14 using namespace KKB;
15 
16 
17 #include "TrainingClass.h"
19 #include "MLClass.h"
20 using namespace KKMLL;
21 
22 
23 
25  countFactor (0.0f),
26  directories (),
27  featureFileName (),
28  mlClass (NULL),
29  subClassifier (NULL),
30  weight (0.0f)
31 {
32 }
33 
34 
35 TrainingClass::TrainingClass (const VectorKKStr& _directories,
36  KKStr _name,
37  float _weight,
38  float _countFactor,
39  TrainingConfiguration2Ptr _subClassifier,
40  MLClassList& _mlClasses
41  ):
42  countFactor (_countFactor),
43  directories (_directories),
44  featureFileName (_name),
45  mlClass (NULL),
46  weight (_weight),
47  subClassifier (_subClassifier)
48 {
49  featureFileName << ".data"; // Will be equal to ClassName + ".data". ex: "Copepods.data"
50 
51  mlClass = _mlClasses.GetMLClassPtr (_name);
52  mlClass->CountFactor (_countFactor);
53 }
54 
55 
56 
58  countFactor (tc.countFactor),
59  directories (tc.directories),
60  featureFileName (tc.featureFileName),
61  mlClass (tc.mlClass),
62  subClassifier (NULL),
63  weight (tc.weight)
64 {
65 }
66 
67 
68 
69 
70 const
72 {
73  return mlClass->Name ();
74 }
75 
76 
77 
78 
80 {
81  if (idx >= directories.size ())
82  return KKStr::EmptyStr();
83  else
84  return directories[idx];
85 }
86 
87 
89 {
90  return directories.size ();
91 }
92 
93 
94 
95 
96 void TrainingClass::AddDirectory (const KKStr& _directory)
97 {
98  directories.push_back (_directory);
99 }
100 
101 
102 
103 void TrainingClass::AddDirectories (const VectorKKStr& _directories)
104 {
105  VectorKKStr::const_iterator idx;
106  for (idx = _directories.begin (); idx != _directories.end (); ++idx)
107  AddDirectory (*idx);
108 }
109 
110 
111 
112 
114  const KKStr& directory
115  )
116 {
117  while (idx >= directories.size ())
118  directories.push_back ("");
119  directories[idx] = directory;
120 }
121 
122 
123 
125  kkuint32 idx
126  )
127 {
128  KKStr rootDirWithSlash = "";
129  if (!rootDir.Empty ())
130  rootDirWithSlash = osAddSlash (rootDir);
131 
132  KKStr directory = "";
133 
134  if (idx >= directories.size ())
135  return KKStr::EmptyStr ();
136 
137  directory = directories[idx];
138 
139  if (directory.Empty ())
140  {
141  directory = rootDirWithSlash + mlClass->Name ();
142  return directory;
143  }
144 
145  directory = osSubstituteInEnvironmentVariables (directory);
146 
147  char firstChar = directory.FirstChar ();
148  if ((firstChar == '/') || (firstChar == '\\'))
149  return directory;
150 
151  if (directory[1] == ':')
152  return directory;
153 
154  return osSubstituteInEnvironmentVariables (rootDirWithSlash + directory);
155 } /* ExpandedDirectory */
156 
157 
158 
159 void TrainingClass::WriteXML (const KKStr& varName,
160  ostream& o
161  ) const
162 {
163  WriteXML (varName, "", o);
164 }
165 
166 void TrainingClass::WriteXML (const KKStr& varName,
167  const KKStr& rootDir,
168  ostream& o
169  ) const
170 {
171  bool noDirectories = false;
172  bool onlyOneDirectory = false;
173 
174  VectorKKStr tempDirectories;
175  for (auto idx: directories)
176  {
177  if (idx.StartsWith (rootDir))
178  {
179  KKStr rootPart = idx.SubStrPart (rootDir.Len ());
180  rootPart.ChopFirstChar ();
181  tempDirectories.push_back (rootPart);
182  }
183  else
184  {
185  tempDirectories.push_back (idx);
186  }
187  }
188 
189  if (tempDirectories.size () == 1)
190  {
191  if ((tempDirectories[0].EqualIgnoreCase (mlClass->Name ())) || (tempDirectories[0].Empty ()))
192  noDirectories = true;
193  else
194  onlyOneDirectory = true;
195  }
196 
197  else if (tempDirectories.size () < 1)
198  noDirectories = true;
199 
201  if (!noDirectories)
202  startTagType = XmlTag::TagTypes::tagStart;
203 
204  XmlTag startTag ("TrainingClass", startTagType);
205  if (!varName.Empty ())
206  startTag.AddAtribute ("VarName", varName);
207 
208  if (mlClass)
209  startTag.AddAtribute ("mlClass", mlClass->Name ());
210 
211  if (!featureFileName.Empty ())
212  startTag.AddAtribute ("FeatureFileName", featureFileName);
213 
214  if (weight != 0.0f)
215  startTag.AddAtribute ("Weight", weight);
216 
217  if (countFactor != 0.0f)
218  startTag.AddAtribute ("CountFactor", countFactor);
219 
220  if (subClassifier)
221  startTag.AddAtribute ("SubClassifier", SubClassifierName ());
222 
223  startTag.WriteXML (o);
224  o << endl;
225 
226  if (startTagType == XmlTag::TagTypes::tagStart)
227  {
228  for (auto idx: tempDirectories)
229  idx.WriteXML ("Directory", o);
230 
231  XmlTag endTag ("TrainingClass", XmlTag::TagTypes::tagEnd);
232  endTag.WriteXML (o);
233  o << endl;
234  }
235 } /* WriteXML */
236 
237 
238 
240  XmlTagConstPtr tag,
241  VolConstBool& cancelFlag,
242  RunLog& log
243  )
244 {
245  directories.clear ();
246  for (auto idx: tag->Attributes ())
247  {
248  const KKStr& n = idx->Name ();
249  const KKStr& v = idx->Value ();
250 
251  if (n.EqualIgnoreCase ("MLClass"))
252  MLClass (MLClass::CreateNewMLClass (v));
253 
254  else if (n.EqualIgnoreCase ("FeatureFileName"))
255  FeatureFileName (v);
256 
257  else if (n.EqualIgnoreCase ("Weight"))
258  Weight (v.ToFloat ());
259 
260  else if (n.EqualIgnoreCase ("CountFactor"))
261  CountFactor (v.ToFloat ());
262 
263  else if (n.EqualIgnoreCase ("SubClassifier"))
264  SubClassifierName (v);
265  }
266 
267  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
268  while (t && (!cancelFlag))
269  {
270  if ((t->VarName ().EqualIgnoreCase ("Directory")) && (typeid (*t) == typeid (XmlElementKKStr)))
271  {
272  XmlElementKKStrPtr xmlS = dynamic_cast<XmlElementKKStrPtr> (t);
273  if (xmlS && xmlS->Value ())
274  {
275  directories.push_back (*(xmlS->Value ()));
276  }
277  }
278 
279  delete t;
280  t = s.GetNextToken (cancelFlag, log);
281  }
282  delete t;
283  t = NULL;
284 } /* ReadXML */
285 
286 
287 
289  KKQueue<TrainingClass> (true),
290  rootDir ()
291 {
292 }
293 
294 
295 
297  bool owner
298  ):
300  rootDir (_rootDir)
301 {
302 }
303 
304 
305 
306 TrainingClassList::TrainingClassList (const TrainingClassList& tcl):
307  KKQueue<TrainingClass> (tcl),
308  rootDir (tcl.rootDir)
309 {
310 }
311 
312 
313 
315  bool _owner
316  ):
318  rootDir (tcl.rootDir)
319 {
320 }
321 
322 
323 
324 
326 {
327  PushOnBack (trainingClass);
328 }
329 
330 
331 
333 {
334  TrainingClassList::const_iterator idx;
335  for (idx = begin (); idx != end (); idx++)
336  {
337  TrainingClassPtr tc = *idx;
338  if (tc->MLClass () == _mlClass)
339  return tc;
340  }
341  return NULL;
342 } /* LocateByMLClass */
343 
344 
345 
347 {
348  TrainingClassPtr traningClass = NULL;
349  for (auto idx: *this)
350  {
351  if (idx->Name ().EqualIgnoreCase(className))
352  {
353  traningClass = idx;
354  break;
355  }
356  }
357  return traningClass;
358 } /* LocateByMLClassName */
359 
360 
361 
362 
364 {
365  iterator idx;
366 
367  for (idx = begin (); idx != end (); idx++)
368  {
369  TrainingClassPtr tc = *idx;
370  for (kkuint32 zed = 0; zed < tc->DirectoryCount (); ++ zed)
371  {
372  if (tc->ExpandedDirectory (rootDir, zed).EqualIgnoreCase (directory))
373  return tc;
374  }
375  }
376 
377  return NULL;
378 } /* LocateByDirectory */
379 
380 
381 
383 {
384  TrainingClassListPtr dupList = new TrainingClassList (rootDir, true);
385 
386  for (const_iterator idx = begin (); idx != end (); idx++)
387  {
388  const TrainingClassPtr tc = *idx;
389  dupList->PushOnBack (new TrainingClass (*tc));
390  }
391 
392  return dupList;
393 } /* TrainingClassListPtr */
394 
395 
396 
397 
398 void TrainingClassList::WriteXML (const KKStr& varName,
399  ostream& o
400  ) const
401 {
402  XmlTag tagStart ("TrainingClassList", XmlTag::TagTypes::tagStart);
403  if (!varName.Empty ())
404  tagStart.AddAtribute ("VarName", varName);
405  tagStart.AddAtribute ("Count", (kkint32)size ());
406  tagStart.WriteXML (o);
407  o << endl;
408 
409  if (!rootDir.Empty ())
410  rootDir.WriteXML ("RootDir", o);
411 
412  KKStr rootDirExpanded = osSubstituteInEnvironmentVariables (rootDir);
413 
414  TrainingClassList::const_iterator idx;
415  for (idx = begin (); idx != end (); ++idx)
416  {
417  TrainingClassPtr tc = *idx;
418  tc->WriteXML ("TrainingClass", rootDirExpanded, o);
419  //XmlElementTrainingClass::WriteXML (*tc, KKStr::EmptyStr (), o);
420  }
421 
422  XmlTag tagEnd ("TrainingClassList", XmlTag::TagTypes::tagEnd);
423  tagEnd.WriteXML (o);
424  o << endl;
425 }
426 
427 
428 
430  XmlTagPtr tag,
431  VolConstBool& cancelFlag,
432  RunLog& log
433  )
434 {
435  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
436  while (t && (!cancelFlag))
437  {
439  {
440  XmlElementPtr e = dynamic_cast<XmlElementPtr> (t);
441  if (e)
442  {
443  KKStr varName = e->VarName ();
444  if (typeid (*e) == typeid (XmlElementKKStr))
445  {
446  XmlElementKKStrPtr s = dynamic_cast<XmlElementKKStrPtr> (e);
447  if (s)
448  {
449  if (varName.EqualIgnoreCase ("RootDir"))
450  RootDir (*(s->Value ()));
451  }
452  }
453  else if (typeid (*t) == typeid (XmlElementTrainingClass))
454  {
455  XmlElementTrainingClassPtr tokenTrainingClass = dynamic_cast<XmlElementTrainingClassPtr> (t);
456  TrainingClassPtr tc = tokenTrainingClass->TakeOwnership ();
457  if (tc)
458  AddTrainingClass (tc);
459  tc = NULL;
460  }
461  else
462  {
463  log.Level (-1) << "XmlElementTrainingClassList ***ERROR*** Un-expected Section Element[" << e->SectionName () << "]" << endl;
464  }
465  }
466  }
467 
468  delete t;
469  t = s.GetNextToken (cancelFlag, log);
470  }
471  delete t;
472  t = NULL;
473 } /* ReadXML */
474 
475 
477  {
478  public:
479  XmlFactoryTrainingClass (): XmlFactory ("TrainingClass") {}
480 
482  XmlStream& s,
483  VolConstBool& cancelFlag,
484  RunLog& log
485  )
486  {
487  return new XmlElementTrainingClass(tag, s, cancelFlag, log);
488  }
489 
491 
493  {
494  if (factoryInstance == NULL)
495  {
497  if (!factoryInstance)
498  {
501  }
503  }
504  return factoryInstance;
505  }
506  };
507 
508 
511 
512 
513 XmlFactoryMacro(TrainingClassList)
TrainingClass(const TrainingClass &tc)
XmlTag(const KKStr &_name, TagTypes _tagType)
Definition: XmlStream.cpp:586
TrainingClassList(const KKStr &rootDir, bool owner)
TrainingClassPtr LocateByMLClassName(const KKStr &className)
XmlElementTemplate< TrainingClass > XmlElementTrainingClass
bool EqualIgnoreCase(const char *s2) const
Definition: KKStr.cpp:1257
static XmlFactoryTrainingClass * factoryInstance
const KKStr & Directory(kkuint32 idx) const
TrainingClassPtr LocateByDirectory(const KKStr &directory)
TrainingClass(const VectorKKStr &_directories, KKStr _name, float _weight, float _countFactor, TrainingConfiguration2Ptr _subClassifier, MLClassList &_mlClasses)
Constructor, Creates a new instance of TrainingClass and populates fields with respective data from p...
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
char FirstChar() const
Definition: KKStr.cpp:1970
static void RegisterFactory(XmlFactory *factory)
Definition: XmlStream.cpp:897
bool EqualIgnoreCase(const KKStr &s2) const
Definition: KKStr.cpp:1250
kkuint32 DirectoryCount() const
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
void CountFactor(float _countFactor)
Definition: MLClass.h:140
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
XmlFactory(const KKStr &_clasName)
Definition: XmlStream.cpp:931
KKStr ExpandedDirectory(const KKStr &rootDir, kkuint32 idx)
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
KKB::KKStr osAddSlash(const KKStr &fileSpec)
KKTHread * KKTHreadPtr
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
virtual void ReadXML(XmlStream &s, XmlTagPtr tag, VolConstBool &cancelFlag, RunLog &log)
XmlElement * XmlElementPtr
Definition: XmlStream.h:21
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Definition: XmlStream.cpp:602
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
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
virtual void WriteXML(const KKStr &varName, const KKStr &rootDir, std::ostream &o) const
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
virtual XmlElementTrainingClass * ManufatureXmlElement(XmlTagPtr tag, XmlStream &s, VolConstBool &cancelFlag, RunLog &log)
const KKStr & Name() const
virtual const KKStr & VarName() const
Definition: XmlStream.cpp:794
const KKStr & Name() const
Definition: MLClass.h:154
void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: KKStr.cpp:4420
KKStr operator+(const KKStr &right) const
Definition: KKStr.cpp:3998
void AddTrainingClass(TrainingClassPtr trainingClass)
static XmlFactoryTrainingClass * FactoryInstance()
const MLClassPtr MLClass() const
Definition: TrainingClass.h:70
TrainingClassList * TrainingClassListPtr
KKB::KKStr osSubstituteInEnvironmentVariables(const KKStr &src)
Substitute in the value of the environment variables into &#39;src&#39;.
Definition: OSservices.cpp:977
Specify where training examples and other related data for a MLClass that is needed to train a classi...
Definition: TrainingClass.h:38
TrainingClassPtr LocateByMLClass(MLClassPtr _mlClass) const
void Directory(kkuint32 idx, const KKStr &directory)
XmlElementKKStr * XmlElementKKStrPtr
Definition: XmlStream.h:670
char operator[](kkint32 i) const
Definition: KKStr.cpp:3413
void WriteXML(std::ostream &o)
Definition: XmlStream.cpp:723
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)
void AddDirectories(const VectorKKStr &_directories)
virtual MLClassPtr GetMLClassPtr(const KKStr &_name)
return pointer to instance with &#39;_name&#39;; if none exists, create one and add to list.
Definition: MLClass.cpp:861
const KKStr & SubClassifierName() const
Definition: TrainingClass.h:73
virtual TokenTypes TokenType()=0
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
Maintains a list of MLClass instances.
Definition: MLClass.h:233
Maintains one instance of a GoalKeeper object that can be used anywhere in the application.
TrainingClass * TrainingClassPtr
TrainingClassList(const TrainingClassList &tcl, bool _owner)
virtual const KKStr & VarName() const
Definition: XmlStream.h:269
#define XmlFactoryMacro(NameOfClass)
Definition: XmlStream.h:688
TrainingClassList * DuplicateListAndContents() const
void AddAtribute(const KKStr &attributeName, double attributeValue)
Definition: XmlStream.cpp:638
VectorKKStr(const VectorKKStr &v)
Definition: KKStr.cpp:5270
void AddDirectory(const KKStr &_directory)
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163