KSquare Utilities
BinaryClassParms.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <stdio.h>
3 #include <iostream>
4 #include <fstream>
5 #include <vector>
6 #include "MemoryDebug.h"
7 using namespace std;
8 
9 
10 #include "GlobalGoalKeeper.h"
11 #include "KKBaseTypes.h"
12 #include "RunLog.h"
13 #include "KKStr.h"
14 using namespace KKB;
15 
16 #include "BinaryClassParms.h"
17 #include "FileDesc.h"
18 #include "MLClass.h"
19 using namespace KKMLL;
20 
21 #include "svm.h"
22 using namespace SVM233;
23 
24 
26  MLClassPtr _class2,
27  const svm_parameter& _param,
28  FeatureNumListConstPtr _selectedFeatures,
29  float _weight
30  ):
31  class1 (_class1),
32  class2 (_class2),
33  param (_param),
34  selectedFeatures (NULL),
35  weight (_weight)
36 {
37  if (_selectedFeatures)
38  selectedFeatures = new FeatureNumList (*_selectedFeatures);
39 }
40 
41 
42 
43 
45  class1 (binaryClassParms.class1),
46  class2 (binaryClassParms.class2),
47  param (binaryClassParms.param),
48  selectedFeatures (NULL),
49  weight (binaryClassParms.weight)
50 {
51  if (binaryClassParms.selectedFeatures)
52  selectedFeatures = new FeatureNumList (*binaryClassParms.selectedFeatures);
53 }
54 
55 
56 
58 {
59  delete selectedFeatures;
60  selectedFeatures = NULL;
61 }
62 
63 
65 {
67 }
68 
69 
70 void BinaryClassParms::SelectedFeatures (const FeatureNumList& _selectedFeatures)
71 {
72  delete selectedFeatures;
73  selectedFeatures = new FeatureNumList (_selectedFeatures);
74 }
75 
76 
78 {
79  delete selectedFeatures;
80  selectedFeatures = new FeatureNumList (*_selectedFeatures);
81 }
82 
83 
84 
85 
86 
88 {
89  if (class1)
90  return class1->Name ();
91  else
92  return "";
93 } /* Class1Name */
94 
95 
96 
98 {
99  if (class2)
100  return class2->Name ();
101  else
102  return "";
103 } /* Class2Name */
104 
105 
107 {
108  if (!selectedFeatures)
109  selectedFeatures = new FeatureNumList (fileDesc);
110  return selectedFeatures;
111 }
112 
113 
114 
116 {
117  KKStr result;
118  result << "Class1" << "\t" << Class1Name () << "\t"
119  << "Class2" << "\t" << Class2Name () << "\t"
120  << "Svm_Parameter" << "\t" << param.ToCmdLineStr () << "\t";
121 
122  if (selectedFeatures)
123  result << "SelectedFeatures" << "\t" << selectedFeatures->ToString () << "\t";
124 
125  result << "Weight" << "\t" << weight;
126 
127  return result;
128 } /* ToTabDelString */
129 
130 
131 
132 BinaryClassParmsPtr BinaryClassParms::CreateFromTabDelStr (const KKStr& _str,
133  RunLog& _log
134  )
135 {
136  MLClassPtr class1 = NULL;
137  MLClassPtr class2 = NULL;
138  svm_parameter* svm_param = NULL;
139  FeatureNumListPtr selectedFeatures = NULL;
140  float weight = 0.0f;
141 
142  KKStr str (_str);
143  while (!str.Empty())
144  {
145  KKStr field = str.ExtractQuotedStr ("\n\r\t", true);
146  field.Upper ();
147  KKStr value = str.ExtractQuotedStr ("\n\r\t", true);
148 
149  if (field == "CLASS1")
150  {
151  class1 = MLClass::CreateNewMLClass (value);
152  }
153 
154  else if (field == "CLASS2")
155  {
156  class2 = MLClass::CreateNewMLClass (value);
157  }
158 
159  else if (field == "SVM_PARAMETER")
160  {
161  svm_param = new svm_parameter (value);
162  }
163 
164  else if (field == "SELECTEDFEATURES")
165  {
166  bool valid = false;
167  selectedFeatures = new FeatureNumList (value, valid);
168  }
169 
170  else if (field == "WEIGHT")
171  weight = float (atof (value.Str ()));
172  }
173 
174  BinaryClassParmsPtr binaryClassParms = (svm_param == NULL) ? NULL :new BinaryClassParms (class1, class2, *svm_param, selectedFeatures, weight);
175 
176  delete selectedFeatures; selectedFeatures = NULL;
177  delete svm_param; svm_param = NULL;
178 
179  return binaryClassParms;
180 } /* CreateFromTabDelStr */
181 
182 
183 
184 
186 {
187  kkint32 memoryConsumedEstimated = sizeof (*this) + param.MemoryConsumedEstimated ();
188 
189  if (selectedFeatures)
190  memoryConsumedEstimated += selectedFeatures->MemoryConsumedEstimated ();
191 
192  return memoryConsumedEstimated;
193 }
194 
195 
196 
198  KKQueue<BinaryClassParms> (true)
199 {
200 }
201 
202 
203 
206 {
207 }
208 
209 
210 
211 BinaryClassParmsList::BinaryClassParmsList (const BinaryClassParmsList& binaryClassList):
212  KKQueue<BinaryClassParms> (binaryClassList)
213 {
214 }
215 
216 
217 
219  bool _owner
220  ):
222 {
223 }
224 
225 
226 
228 {
229 }
230 
231 
232 
234 {
235  kkint32 memoryConsumedEstimated = sizeof (BinaryClassParmsList);
236  BinaryClassParmsList::const_iterator idx;
237  for (idx = begin (); idx != end (); ++idx)
238  memoryConsumedEstimated += (*idx)->MemoryConsumedEstimated ();
239  return memoryConsumedEstimated;
240 }
241 
242 
243 
244 BinaryClassParmsListPtr BinaryClassParmsList::CreateFromXML (FILE* i,
245  FileDescPtr fileDesc,
246  RunLog& log
247  )
248 {
249  BinaryClassParmsListPtr binaryClassParmsList = new BinaryClassParmsList (true);
250  binaryClassParmsList->ReadXML (i, fileDesc, log);
251  return binaryClassParmsList;
252 }
253 
254 
255 
256 BinaryClassParmsListPtr BinaryClassParmsList::CreateFromXML (istream& i,
257  FileDescPtr fileDesc,
258  RunLog& log
259  )
260 {
261  BinaryClassParmsListPtr binaryClassParmsList = new BinaryClassParmsList (true);
262  binaryClassParmsList->ReadXML (i, fileDesc, log);
263  return binaryClassParmsList;
264 }
265 
266 
267 
268 
269 /**
270  @brief Returns the Average number of selected features.
271  */
273 {
274  if (size () < 1)
275  return 0.0f;
276 
277  const_iterator idx;
278 
279  kkuint32 featureCountTotal = 0;
280 
281  for (idx = begin (); idx != end (); idx++)
282  {
283  const BinaryClassParmsPtr bcp = *idx;
284  featureCountTotal += bcp->NumOfFeatures (fileDesc);
285  }
286 
287  return (float)featureCountTotal / (float)size ();
288 }
289 
290 
291 
292 
293 BinaryClassParmsPtr BinaryClassParmsList::LookUp (MLClassPtr _class1,
294  MLClassPtr _class2
295  ) const
296 {
297  KeyField kf (_class1, _class2);
298  ClassIndexType::const_iterator idx;
299  idx = classIndex.find (kf);
300  if (idx == classIndex.end ())
301  {
302  kf.class1 = _class2;
303  kf.class2 = _class1;
304  idx = classIndex.find (kf);
305  }
306 
307  if (idx == classIndex.end ())
308  return NULL;
309  else
310  return idx->second;
311 } /* LookUp */
312 
313 
314 
315 
316 BinaryClassParmsListPtr BinaryClassParmsList::DuplicateListAndContents () const
317 {
318  BinaryClassParmsListPtr duplicatedQueue = new BinaryClassParmsList (true);
319 
320  for (const_iterator idx = begin (); idx != end (); idx++)
321  {
322  BinaryClassParmsPtr e = *idx;
323  duplicatedQueue->PushOnBack (new BinaryClassParms (*e));
324  }
325 
326  return duplicatedQueue;
327 } /* DuplicateListAndContents */
328 
329 
330 
331 
332 void BinaryClassParmsList::WriteXML (ostream& o) const
333 {
334  o << "<BinaryClassParmsList>" << endl;
335 
336  const_iterator idx;
337 
338  for (idx = begin (); idx != end (); idx++)
339  {
340  o << "<BinaryClassParms>" << "\t"
341  << (*idx)->ToTabDelString ().QuotedStr () << "\t"
342  << "</BinaryClassParms>"
343  << endl;
344  }
345 
346  o << "</BinaryClassParmsList>" << endl;
347 } /* WriteXML */
348 
349 
350 
352  FileDescPtr fileDesc,
353  RunLog& log
354  )
355 {
356  DeleteContents ();
357 
358  char buff [10240];
359 
360  while (fgets (buff, sizeof (buff), i))
361  {
362  KKStr ln (buff);
363 
364  KKStr field = ln.ExtractQuotedStr ("\n\r\t", true);
365  field.Upper ();
366 
367  if (field == "</BINARYCLASSPARMSLIST>")
368  {
369  break;
370  }
371 
372  else if (field == "<BINARYCLASSPARMS>")
373  {
374  KKStr binaryClassParmsStr = ln.ExtractQuotedStr ("\n\r\t", true);
375  PushOnBack (BinaryClassParms::CreateFromTabDelStr (binaryClassParmsStr, log));
376  }
377  }
378 } /* ReadXML */
379 
380 
381 
382 
383 void BinaryClassParmsList::ReadXML (istream& i,
384  FileDescPtr fileDesc,
385  RunLog& log
386  )
387 {
388  DeleteContents ();
389 
390  char buff [10240];
391 
392  while (i.getline (buff, sizeof (buff)))
393  {
394  KKStr ln (buff);
395 
396  KKStr field = ln.ExtractQuotedStr ("\n\r\t", true);
397  field.Upper ();
398 
399  if (field == "</BINARYCLASSPARMSLIST>")
400  {
401  break;
402  }
403 
404  else if (field == "<BINARYCLASSPARMS>")
405  {
406  KKStr binaryClassParmsStr = ln.ExtractQuotedStr ("\n\r\t", true);
407  PushOnBack (BinaryClassParms::CreateFromTabDelStr (binaryClassParmsStr, log));
408  }
409  }
410 } /* ReadXML */
411 
412 
413 
414 
415 
416 void BinaryClassParmsList::PushOnBack (BinaryClassParmsPtr binaryParms)
417 {
418  BinaryClassParmsPtr existingEntry = LookUp (binaryParms->Class1 (), binaryParms->Class2 ());
419  if (existingEntry)
420  {
421  // We have a duplicate entry
422  KKStr errMsg (128);
423  errMsg << "BinaryClassParmsList::PushOnBack ***ERROR*** Duplicate Entry " << binaryParms->Class1Name () << "\t" << binaryParms->Class2Name () << endl;
424  cerr << errMsg << endl;
425  throw KKException (errMsg);
426  }
427 
428  KKQueue<BinaryClassParms>::PushOnBack (binaryParms);
429  KeyField kf (binaryParms->Class1 (), binaryParms->Class2 ());
430  classIndex.insert(ClassIndexPair (kf, binaryParms));
431 } /* PushOnBack */
432 
433 
434 
435 
436 void BinaryClassParmsList::PushOnFront (BinaryClassParmsPtr binaryParms)
437 {
438  BinaryClassParmsPtr existingEntry = LookUp (binaryParms->Class1 (), binaryParms->Class2 ());
439  if (existingEntry)
440  {
441  // We have a duplicate entry
442  KKStr errMsg (128);
443  errMsg << "BinaryClassParmsList::PushOnFront ***ERROR*** Duplicate Entry " << binaryParms->Class1Name () << "\t" << binaryParms->Class2Name () << endl;
444  cerr << errMsg << endl;
445  throw KKException (errMsg);
446  }
447 
448  KKQueue<BinaryClassParms>::PushOnFront (binaryParms);
449  KeyField kf (binaryParms->Class1 (), binaryParms->Class2 ());
450  classIndex.insert(ClassIndexPair (kf, binaryParms));
451 } /* PushOnFront */
452 
453 
454 
455 
456 
457 BinaryClassParmsList::KeyField::KeyField (MLClassPtr _class1,
458  MLClassPtr _class2
459  ):
460  class1 (_class1),
461  class2 (_class2)
462 {
463 }
464 
465 
466 
467 
468 bool BinaryClassParmsList::KeyField::operator< (const KeyField& p2) const
469 {
470  kkint32 x = class1->Name ().Compare(p2.class1->Name ());
471  if (x < 0)
472  return true;
473 
474  else if (x > 0)
475  return false;
476 
477  else
478  return class2->Name () < p2.class2->Name ();
479 }
480 
481 
482 
483 
484 
485 
486 
487 
488 
489 
490 void BinaryClassParmsList::WriteXML (const KKStr& varName,
491  ostream& o
492  ) const
493 {
494  XmlTag startTag ("BinaryClassParmsList", XmlTag::TagTypes::tagStart);
495  if (!varName.Empty ())
496  startTag.AddAtribute ("VarName", varName);
497  startTag.WriteXML (o);
498  o << endl;
499 
500  for (auto idx : *this)
501  {
502  XmlContent::WriteXml (idx->ToTabDelString (), o);
503  o << endl;
504  }
505 
506 
507  XmlTag endTag ("BinaryClassParmsList", XmlTag::TagTypes::tagEnd);
508  endTag.WriteXML (o);
509  o << endl;
510 } /* WriteXML */
511 
512 
513 
514 
515 
517  XmlTagConstPtr tag,
518  VolConstBool& cancelFlag,
519  RunLog& log
520  )
521 {
522  DeleteContents ();
523  classIndex.clear ();
524 
525  bool errorsFound = false;
526  XmlTokenPtr t = s.GetNextToken (cancelFlag, log);
527  while (t && (!cancelFlag) && (!errorsFound))
528  {
530  {
531  XmlContentPtr content = dynamic_cast<XmlContentPtr> (t);
532  if (content && (content->Content ()))
533  {
534  BinaryClassParmsPtr bcp = BinaryClassParms::CreateFromTabDelStr (*(content->Content ()), log);
535  if (bcp)
536  PushOnBack (bcp);
537  }
538  }
539  delete t;
540  t = s.GetNextToken (cancelFlag, log);
541  }
542  delete t;
543  t = NULL;
544 } /* ReadXML */
545 
546 
547 XmlFactoryMacro(BinaryClassParmsList)
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
kkint32 MemoryConsumedEstimated() const
Definition: svm.cpp:886
MLClass * MLClassPtr
Definition: MLClass.h:46
BinaryClassParms(MLClassPtr _class1, MLClassPtr _class2, const svm_parameter &_param, FeatureNumListConstPtr _selectedFeatures, float _weight)
Constructor for &#39;BinaryClassParms&#39; where caller supplies the two classes and parameters for that spec...
__int32 kkint32
Definition: KKBaseTypes.h:88
void SelectedFeatures(const FeatureNumListConst &_selectedFeatures)
Similar to SVMparam except it is specialized for two classes.
decision_function svm_train_one(const svm_problem *prob, const svm_parameter *param, double Cp, double Cn, std::set< kkint32 > &BSVIndex)
Definition: svm.cpp:3154
Keeps track of selected features.
FeatureNumList(FileDescPtr _fileDesc)
BinaryClassParmsPtr LookUp(MLClassPtr _class1, MLClassPtr _class2) const
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
KKStr ToCmdLineStr() const
Definition: svm.cpp:1069
static BinaryClassParmsListPtr CreateFromXML(std::istream &i, FileDescPtr fileDesc, RunLog &log)
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
kkuint16 NumOfFeatures(FileDescPtr fileDesc) const
XmlContent * XmlContentPtr
Definition: XmlStream.h:24
bool operator==(const char *rtStr) const
Definition: KKStr.cpp:1588
void ReadXML(std::istream &i, FileDescPtr fileDesc, RunLog &log)
static BinaryClassParmsListPtr CreateFromXML(std::FILE *i, FileDescPtr fileDesc, RunLog &log)
XmlToken * XmlTokenPtr
Definition: XmlStream.h:18
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
float FeatureCountNet(FileDescPtr fileDesc) const
Returns the Average number of selected features.
FeatureNumList(const FeatureNumList &featureNumList)
Copy constructor.
KKTHread * KKTHreadPtr
svm_parameter(KKStr &paramStr)
Definition: svm.cpp:983
BinaryClassParms(const BinaryClassParms &binaryClassParms)
void WriteXML(std::ostream &o) const
FeatureNumList * FeatureNumListPtr
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
Definition: XmlStream.cpp:602
kkint32 NumOfFeatures() const
void SelectedFeatures(FeatureNumListConstPtr _selectedFeatures)
bool Empty() const
Definition: KKStr.h:241
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
BinaryClassParmsList * BinaryClassParmsListPtr
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, std::ostream &o) const
BinaryClassParmsList(const BinaryClassParmsList &binaryClassList, bool _owner)
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
void Upper()
Converts all characters in string to their Upper case equivalents via &#39;toupper&#39;.
Definition: KKStr.cpp:2461
virtual void PushOnBack(BinaryClassParmsPtr binaryParms)
FileDesc * FileDescPtr
KKStr ExtractQuotedStr(const char *delChars, bool decodeEscapeCharacters)
Definition: KKStr.cpp:3282
MLClassPtr Class2() const
const KKStr & Name() const
Definition: MLClass.h:154
void ReadXML(FILE *i, FileDescPtr fileDesc, RunLog &log)
kkint32 MemoryConsumedEstimated() const
KKStr ToString() const
Returns comma delimited list of all features selected; will make use of range specification.
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
FeatureNumList(const KKStr &_featureListStr, bool &_valid)
Constructs a &#39;FeatureNumList&#39; instance from a string that contains a list of selected features...
static BinaryClassParmsPtr CreateFromTabDelStr(const KKStr &_str, RunLog &_log)
void WriteXML(std::ostream &o)
Definition: XmlStream.cpp:723
FeatureNumListConst * FeatureNumListConstPtr
KKStrPtr const Content() const
Definition: XmlStream.h:338
Used for logging messages.
Definition: RunLog.h:49
BinaryClassParmsList * DuplicateListAndContents() const
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
bool operator<(const KKStr &right) const
Definition: KKStr.cpp:1635
virtual void PushOnFront(BinaryClassParmsPtr binaryParms)
virtual TokenTypes TokenType()=0
kkint32 Compare(const KKStr &s2) const
Definition: KKStr.cpp:844
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
Definition: XmlStream.cpp:116
KKException(const KKStr &_exceptionStr)
Definition: KKException.cpp:45
svm_parameter(const svm_parameter &parameters)
Definition: svm.cpp:768
BinaryClassParms * BinaryClassParmsPtr
kkint32 MemoryConsumedEstimated() const
kkint32 MemoryConsumedEstimated() const
FeatureNumListConstPtr SelectedFeaturesFD(FileDescPtr fileDesc) const
#define XmlFactoryMacro(NameOfClass)
Definition: XmlStream.h:688
MLClassPtr Class1() const
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163