KSquare Utilities
ClassAssignments.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 <map>
7 
8 #include "MemoryDebug.h"
9 using namespace std;
10 
11 
12 #include "KKBaseTypes.h"
13 #include "OSservices.h"
14 using namespace KKB;
15 
16 
17 #include "ClassAssignments.h"
18 #include "MLClass.h"
19 using namespace KKMLL;
20 
21 
22 
23 
24 
27 {
28 }
29 
30 
31 
34 {
35  kkint32 x = 0;
36  for (MLClassList::const_iterator idx = classes.begin (); idx != classes.end (); idx++)
37  {
38  insert (pair<kkint16, MLClassPtr> (x, *idx));
39  classLookUp.insert (pair<MLClassPtr, kkint16> (*idx, x));
40  x++;
41  }
42 }
43 
44 
45 
47 {
48  kkint32 memoryConsumedEstimated = sizeof (ClassAssignments)
49  + (classLookUp.size () * (sizeof (MLClassPtr) + sizeof (kkint16)));
50  return memoryConsumedEstimated;
51 }
52 
53 
54 
55 
56 void ClassAssignments::AddMLClass (MLClassPtr mlClass,
57  kkint16 num,
58  RunLog& log
59  )
60 {
61  ClassLookUpIterator idx;
62  idx = classLookUp.find (mlClass);
63  if (idx != classLookUp.end ())
64  {
65  log.Level (-1) << endl << endl
66  << "ClassAssignments::AddMLClass ***ERROR*** Duplicate Class Being Added[" << mlClass->Name () << "]." << endl
67  << " Num Found [" << idx->second << "]" << endl
68  << " New Num Being Added [" << num << endl
69  << endl;
70  return;
71  }
72 
73  insert (pair<kkint16, MLClassPtr> (num, mlClass));
74  classLookUp.insert (pair<MLClassPtr, kkint16> (mlClass, num));
75 } /* AddMLClass */
76 
77 
78 
79 
80 
81 MLClassPtr ClassAssignments::GetMLClass (kkint16 num) const
82 {
83  multimap<kkint16, MLClassPtr>::const_iterator p;
84  p = find (num);
85  if (p == end ())
86  return NULL;
87  return p->second;
88 } /* GetMLClass */
89 
90 
91 
92 
93 
95 {
96  ClassAssignments::const_iterator idx;
97 
98  MLClassList results;
99  for (idx = begin (); idx != end (); idx++)
100  {
101  if (idx->first == num)
102  results.PushOnBack (idx->second);
103  }
104 
105  return results;
106 } /* GetMLClasses */
107 
108 
109 
110 
112 {
113  VectorShort nums;
114 
115  ClassAssignments::const_iterator idx;
116 
117  for (idx = begin (); idx != end (); idx++)
118  {
119  nums.push_back (idx->first);
120  }
121 
122  sort (nums.begin (), nums.end ());
123 
124  VectorShort results;
125 
126  kkint16 lastNum = -999;
127  VectorShort::const_iterator idx2;
128  for (idx2 = nums.begin (); idx2 != nums.end (); idx2++)
129  {
130  if (*idx2 != lastNum)
131  {
132  lastNum = *idx2;
133  results.push_back (lastNum);
134  }
135  }
136 
137  return results;
138 } /* GetUniqueListOfAssignments */
139 
140 
141 
142 
143 
144 MLClassPtr ClassAssignments::GetMLClassByIndex (size_t idx)
145 {
146  if ((idx < 0) || (idx >= (size_t)size ()))
147  {
148  cerr << endl
149  << endl
150  << "ClassAssignments::GetMLClassByIndex *** ERROR ***" << endl
151  << " idx[" << idx << "] is out of range." << endl
152  << endl;
153  return NULL;
154  }
155 
156  iterator i = begin ();
157  for (size_t x = 0; x < idx; x++)
158  i++;
159 
160  return i->second;
161 } /* GetMLClassByIndex */
162 
163 
164 
165 
166 
167 kkint16 ClassAssignments::GetNumForClass (MLClassPtr mlClass) const
168 {
169  ClassLookUp::const_iterator idx;
170  idx = classLookUp.find (mlClass);
171  if (idx == classLookUp.end ())
172  return -1;
173  else
174  return idx->second;
175 } /* GetNumForClass */
176 
177 
178 
179 
180 void ClassAssignments::Save (const KKStr& fileName,
181  bool& successful
182  )
183 {
184  ofstream outFile (fileName.Str ());
185 
186  for (iterator idx = begin (); idx != end (); idx++)
187  {
188  outFile << idx->second->Name () << "\t" << idx->first << endl;
189  }
190 
191  successful = true;
192  return;
193 }
194 
195 
196 
197 
199 {
200  KKStr result ((kkint32)(size () * 20));
201 
202  result << kkint32 (size ());
203 
204  ClassAssignments::const_iterator idx;
205  for (idx = begin (); idx != end (); idx++)
206  {
207  result << "\t" << idx->first << "\t" << idx->second->Name ();
208  }
209 
210  return result;
211 } /* ToString */
212 
213 
214 
215 
216 
217 /**
218 * @brief Loads class assignments from string that was originally generated by ToString
219 * @param[in] _toString, KKStr containing class assignments info, will expect to be formated
220 * the way ToString() creates them.
221 */
222 void ClassAssignments::ParseToString (const KKStr& _toString,
223  RunLog& _log
224  )
225 {
226  erase (begin (), end ());
227 
228  KKStr toString (_toString);
229 
230  // Remove Class count from string
231  toString.ExtractTokenInt ("\t\n\r");
232 
233  while (!toString.Empty ())
234  {
235  kkint32 assignmentNum = toString.ExtractTokenInt ("\t\n\r");
236  KKStr className = toString.ExtractToken ("\t\n\r");
237 
238  if (!className.Empty ())
239  {
240  MLClassPtr mlClass = MLClass::CreateNewMLClass (className);
241  AddMLClass (mlClass, assignmentNum, _log);
242  }
243  }
244 } /* ParseToString */
__int16 kkint16
16 bit signed integer.
Definition: KKBaseTypes.h:85
VectorShort GetUniqueListOfAssignments() const
__int32 kkint32
Definition: KKBaseTypes.h:88
void Save(const KKStr &fileName, bool &successful)
MLClassPtr GetMLClass(kkint16 num) const
KKStr ExtractToken(const char *delStr="\n\t\r ")
Definition: KKStr.cpp:2969
kkint16 GetNumForClass(MLClassPtr mlClass) const
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
MLClassList GetMLClasses(kkint16 num) const
KKTHread * KKTHreadPtr
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
bool Empty() const
Definition: KKStr.h:241
kkint32 MemoryConsumedEstimated() const
Binds MLClass objects to the appropriate number that the Learning Algorithm expects.
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
void AddMLClass(MLClassPtr mlClass, kkint16 num, RunLog &log)
MLClassPtr GetMLClassByIndex(size_t idx)
kkint32 ExtractTokenInt(const char *delStr)
Definition: KKStr.cpp:3129
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
void ParseToString(const KKStr &_toString, RunLog &_log)
Loads class assignments from string that was originally generated by ToString.
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)
ClassAssignments(const MLClassList &classes)
Creates assignment for all classes in &#39;classes.
std::vector< short > VectorShort
Definition: KKBaseTypes.h:142
Maintains a list of MLClass instances.
Definition: MLClass.h:233