KSquare Utilities
SizeDistribution.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 
3 #include <iostream>
4 #include <fstream>
5 #include <vector>
6 #include <map>
7 #include <string>
8 
9 
10 #include "MemoryDebug.h"
11 
12 using namespace std;
13 
14 
15 #include "KKBaseTypes.h"
16 #include "KKQueue.h"
17 #include "RunLog.h"
18 #include "KKStr.h"
19 using namespace KKB;
20 
21 
22 #include "SizeDistribution.h"
23 #include "MLClass.h"
24 #include "FeatureVector.h"
25 using namespace KKMLL;
26 
27 
28 
29 //#define BucketCount 50
30 //#define BucketSize 100
31 
32 
34 {
35 public:
37 
39  kkint32 _bucketCount,
40  kkint32 _bucketSize
41  ):
42  bucketCount (_bucketCount),
43  bucketSize (_bucketSize),
44  count (0),
45  name (_name),
46  nameUpper (_name),
47  sizeBuckets (NULL)
48 
49  {
51 
53  for (kkint32 x = 0; x < bucketCount; x++)
54  sizeBuckets[x] = 0;
55 
56  count = 0;
57  }
58 
59 
61  {
62  delete[] sizeBuckets;
63  }
64 
65 
66 
67 
68  void Increment (kkint32 size)
69  {
70  kkint32 bucket = (kkint32)(size / bucketSize);
71  if (bucket >= bucketCount)
72  bucket = bucketCount - 1;
73 
74  sizeBuckets[bucket]++;
75  count++;
76  }
77 
78 
79 
80  void AddIn (ClassTotalsPtr classTotals);
81 
82 
84  {
85  if ((idx < 0) && (idx >= bucketCount))
86  return 0;
87  return sizeBuckets[idx];
88  }
89 
90 
91  void PrintFormatedLine (ostream& _outFile);
92 
93  void PrintCSVLine (ostream& _outFile);
94 
95  void PrintTabDelLine (ostream& _outFile);
96 
97 
104 
105 }; /* ClassTotals */
106 
107 
108 
109 
111 {
112 public:
113  ClassTotalsList (bool _owner):
115  {
116  }
117 
119  {
120  }
121 
122 
123 
125  {
126  ClassTotals::ClassTotalsPtr classTotals = NULL;
127  for (auto temp: *this)
128  {
129  if (temp->name.EqualIgnoreCase (_name))
130  {
131  classTotals = temp;
132  break;
133  }
134  }
135  return classTotals;
136  }
137 }; /* ClassTotalsList */
138 
139 
140 
141 
143 {
144  KKStr s (name);
145  s.RightPad (20);
146  o << s;
147 
148  s = count;
149  s.LeftPad (9);
150  o << s;
151 
152  kkint32 bucket;
153 
154  for (bucket = 0; bucket < bucketCount; bucket++)
155  {
156  s = sizeBuckets [bucket];
157  s.LeftPad (8);
158  o << s;
159  }
160  o << endl;
161 } /* PrintFormatedLine */
162 
163 
164 
165 
166 
167 
168 
170 {
171  o << "\"" << name << "\"," << count;
172 
173  kkint32 bucket;
174 
175  for (bucket = 0; bucket < bucketCount; bucket++)
176  {
177  o << "," << sizeBuckets[bucket];
178  }
179  o << endl;
180 }
181 
182 
183 
184 
186 {
187  o << "\"" << name << "\"" << "\t" << count;
188 
189  kkint32 bucket;
190 
191  for (bucket = 0; bucket < bucketCount; bucket++)
192  {
193  o << "\t" << sizeBuckets[bucket];
194  }
195  o << endl;
196 }
197 
198 
199 
200 
201 
202 
204 {
205  kkint32 idx;
206 
207  count = count + classTotals->count;
208 
209  for (idx = 0; idx < bucketCount; idx++)
210  {
211  sizeBuckets[idx] = sizeBuckets[idx] + classTotals->sizeBuckets[idx];
212  }
213 
214 } /* AddIn */
215 
216 
217 
218 
220  kkint32 _bucketSize,
221  RunLog& _log
222  ):
223  bucketCount (_bucketCount),
224  bucketSize (_bucketSize),
225  log (_log),
226  totals (new SizeDistribution::ClassTotalsList (true))
227 
228 {
229  if (bucketCount < 1)
230  {
231  log.Level (-1) << endl << endl << endl
232  << "SizeDistribution::SizeDistribution *** Invalid Bucket Size[" << bucketSize << "] ***" << endl
233  << endl;
234  }
235 }
236 
237 
238 
240 {
241  delete totals;
242 }
243 
244 
245 
246 MLClassListPtr SizeDistribution::BuildMLClassList () const
247 {
248  MLClassListPtr mlClasses = new MLClassList ();
249 
250  ClassTotalsList::const_iterator cIDX;
251  for (cIDX = totals->begin (); cIDX != totals->end (); cIDX++)
252  {
253  ClassTotals::ClassTotalsPtr ct = *cIDX;
255  }
256 
257  mlClasses->SortByName ();
258 
259  return mlClasses;
260 } /* BuildMLClassList */
261 
262 
263 
265  kkint32 size
266  )
267 {
268  if (mlClass == NULL)
270 
271  ClassTotals::ClassTotalsPtr classTotals = totals->LookUp (mlClass->Name ());
272  if (!classTotals)
273  {
274  classTotals = new ClassTotals (mlClass->Name (), bucketCount, bucketSize);
275  totals->PushOnBack (classTotals);
276  }
277 
278  classTotals->Increment (size);
279 } /* Increment */
280 
281 
282 
283 
284 
286 {
287  PrintFormatedHeader (o);
288 
289  ClassTotals::ClassTotalsPtr classTotals = NULL;
290 
291  ClassTotals grandTotals ("Grand Totals", bucketCount, bucketSize);
292 
293  kkint32 idx;
294 
295  for (idx = 0; idx < totals->QueueSize (); idx++)
296  {
297  classTotals = totals->IdxToPtr (idx);
298  classTotals->PrintFormatedLine (o);
299  grandTotals.AddIn (classTotals);
300  }
301 
302  o << endl;
303  grandTotals.PrintFormatedLine (o);
304 } /* PrintFormatedDistributionMatrix */
305 
306 
307 
308 
309 
310 
312 {
313  PrintCSVHeader (o);
314 
315  ClassTotals::ClassTotalsPtr classTotals = NULL;
316 
317  ClassTotals grandTotals ("Grand Totals", bucketCount, bucketSize);
318 
319  kkint32 idx;
320 
321  for (idx = 0; idx < totals->QueueSize (); idx++)
322  {
323  classTotals = totals->IdxToPtr (idx);
324  classTotals->PrintCSVLine (o);
325  grandTotals.AddIn (classTotals);
326  }
327 
328  o << endl;
329  grandTotals.PrintCSVLine (o);
330 } /* PrintCSVDistributionMatrix */
331 
332 
333 
334 
336 {
337  PrintTabDelHeader (o);
338 
339  ClassTotals::ClassTotalsPtr classTotals = NULL;
340 
341  ClassTotals grandTotals ("Grand Totals", bucketCount, bucketSize);
342 
343  kkint32 idx;
344 
345  for (idx = 0; idx < totals->QueueSize (); idx++)
346  {
347  classTotals = totals->IdxToPtr (idx);
348  classTotals->PrintTabDelLine (o);
349  grandTotals.AddIn (classTotals);
350  }
351 
352  o << endl;
353  grandTotals.PrintTabDelLine (o);
354 } /* PrintTabDelDistributionMatrix */
355 
356 
357 
358 
359 
360 
361 
362 void SizeDistribution::PrintFormatedHeader (ostream& o) const
363 {
364  o << "Class Name TOTAL";
365  kkint32 imageSize = 0;
366  kkint32 bucket;
367 
368  for (bucket = 0; bucket < (bucketCount - 1); bucket++)
369  {
370  imageSize = imageSize + bucketSize;
371  KKStr sizeStr;
372  sizeStr << imageSize;
373  sizeStr.LeftPad (8);
374  o << sizeStr;
375  }
376 
377  KKStr s = ">";
378  s << imageSize;
379  s.LeftPad (8);
380  o << s;
381 
382  o << endl;
383 
384  o << "================== =====";
385 
386  for (bucket = 0; bucket < bucketCount; bucket++)
387  {
388  o << " ====";
389  }
390  o << endl;
391 } /* PrintFormatedHeader */
392 
393 
394 
395 
396 void SizeDistribution::PrintCSVHeader (ostream& o) const
397 {
398  o << "\"Class Name\",Sum,";
399 
400  kkint32 imageSize = 0;
401  kkint32 bucket;
402 
403  for (bucket = 0; bucket < (bucketCount - 1); bucket++)
404  {
405  imageSize = imageSize + bucketSize;
406  o << "," << imageSize;
407  }
408 
409  o << ",>" << imageSize;
410 
411  o << endl;
412 } /* PrintCSVHeader */
413 
414 
415 
416 
417 void SizeDistribution::PrintTabDelHeader (ostream& o) const
418 {
419  o << "\"Class Name\"" << "\t" << "Sum";
420 
421  kkint32 imageSize = 0;
422  kkint32 bucket;
423 
424  for (bucket = 0; bucket < (bucketCount - 1); bucket++)
425  {
426  imageSize = imageSize + bucketSize;
427  o << "\t" << imageSize;
428  }
429 
430  o << "\t" << ">" << imageSize;
431 
432  o << endl;
433 } /* PrintTabDelHeader */
434 
435 
436 
437 
439  VectorUlong* scanLinesPerMeterDepth
440  ) const
441 {
442 
443  KKStr hd1, hd2, hd3;
444 
445 
446  MLClassListPtr classes = BuildMLClassList ();
447  MLClassList::const_iterator cIDX;
448 
449 
450  // Find the first and last buckets with activity
451 
452  kkint32 firstBucket = -1;
453  kkint32 lastBucket = 0;
454 
455  for (kkint32 bucketIDX = 0; bucketIDX < bucketCount; bucketIDX++)
456  {
457  kkint32 bucketTotal = 0;
458  for (cIDX = classes->begin (); cIDX != classes->end (); cIDX++)
459  {
460  MLClassPtr mlClass = *cIDX;
461  ClassTotals::ClassTotalsPtr classTotals = totals->LookUp (mlClass->Name ());
462  bucketTotal += classTotals->BucketCount (bucketIDX);
463  }
464 
465  if (bucketTotal > 0)
466  {
467  if (firstBucket < 0)
468  firstBucket = bucketIDX;
469  lastBucket = bucketIDX;
470  }
471  }
472 
473  if (firstBucket < 0)
474  {
475  o << endl << endl
476  << "SizeDistribution::PrintByClassCollumns *** There is no SizeDistribution Data ***" << endl
477  << endl;
478  return;
479  }
480 
481 
482  VectorInt finalTotals (classes->size (), 0);
483  kkint32 grandTotal = 0;
484 
485  classes->ExtractThreeTitleLines (hd1, hd2, hd3);
486 
487  o << "" << "\t" << "" << "\t" << hd1 << "\t" << "" << endl
488  << "" << "\t" << "Scan" << "\t" << hd2 << "\t" << "Bucket" << endl
489  << "Depth" << "\t" << "Lines" << "\t" << hd3 << "\t" << "Total" << endl;
490 
491  kkuint64 totalScanLines = 0;
492  kkint32 imageSize = firstBucket * bucketSize;
493 
494  for (kkint32 bucketIDX = firstBucket; bucketIDX <= lastBucket; bucketIDX++)
495  {
496  kkint32 nextImageSize = imageSize + bucketSize;
497  kkuint64 scanLinesDepthForThisBucket = 0;
498  if (scanLinesPerMeterDepth != NULL)
499  {
500  for (kkint32 x = imageSize; x < Min (nextImageSize, (kkint32)scanLinesPerMeterDepth->size ()); x++)
501  scanLinesDepthForThisBucket += (*scanLinesPerMeterDepth)[x];
502  }
503 
504  o << imageSize << "\t" << scanLinesDepthForThisBucket;
505  totalScanLines += scanLinesDepthForThisBucket;
506 
507  kkint32 bucketTotal = 0;
508 
509  kkint32 intIDX = 0;
510  for (cIDX = classes->begin (); cIDX != classes->end (); cIDX++)
511  {
512  MLClassPtr mlClass = *cIDX;
513  ClassTotals::ClassTotalsPtr classTotals = totals->LookUp (mlClass->Name ());
514 
515  kkint32 qtyThisBucket = classTotals->BucketCount (bucketIDX);
516 
517  o << "\t" << qtyThisBucket;
518 
519  bucketTotal += qtyThisBucket;
520  finalTotals[intIDX] += qtyThisBucket;
521  grandTotal += qtyThisBucket;
522 
523  intIDX++;
524  }
525 
526  imageSize = nextImageSize;
527  o << "\t" << bucketTotal << endl;
528  }
529 
530  {
531  o << endl
532  << "FinalTotals" << "\t" << totalScanLines;
533  for (kkuint32 x = 0; x < classes->size (); x++)
534  {
535  o << "\t" << finalTotals[x];
536  }
537  o << "\t" << grandTotal << endl
538  << endl;
539  }
540 
541  delete classes; classes = NULL;
542 } /* ReportByClassCollumns */
MLClass * MLClassPtr
Definition: MLClass.h:46
__int32 kkint32
Definition: KKBaseTypes.h:88
void LeftPad(kkint32 width, uchar ch= ' ')
pads the string with enough &#39;ch&#39; characters on the left side until the string is as long as &#39;width&#39; c...
Definition: KKStr.cpp:2303
std::vector< int > VectorInt
Definition: KKBaseTypes.h:138
Represents a "Class" in the Machine Learning Sense.
Definition: MLClass.h:52
static MLClassPtr GetUnKnownClassStatic()
Definition: MLClass.cpp:261
void Increment(MLClassPtr mlClass, kkint32 size)
void PrintFormatedLine(ostream &_outFile)
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
void PrintTabDelDistributionMatrix(ostream &_outFile) const
ClassTotals::ClassTotalsPtr LookUp(KKStr _name)
KKTHread * KKTHreadPtr
SizeDistribution(kkint32 _bucketCount, kkint32 _bucketSize, RunLog &_log)
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
ClassTotals(KKStr _name, kkint32 _bucketCount, kkint32 _bucketSize)
virtual void PushOnBack(MLClassPtr mlClass)
Definition: MLClass.cpp:798
void PrintByClassCollumns(ostream &o, VectorUlong *scanLinesPerMeter) const
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
void ExtractThreeTitleLines(KKStr &titleLine1, KKStr &titleLine2, KKStr &titleLine3) const
Using the class names create three title lines where we split names by "_" characters between the thr...
Definition: MLClass.cpp:1068
void AddIn(ClassTotalsPtr classTotals)
unsigned __int64 kkuint64
Definition: KKBaseTypes.h:91
const KKStr & Name() const
Definition: MLClass.h:154
std::ostream &__cdecl operator<<(std::ostream &os, const KKStr &str)
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 RightPad(kkint32 width, char ch= ' ')
Pads string on the right side with specified character so that the string will be of specified length...
Definition: KKStr.cpp:2239
KKStr & operator=(kkint32 right)
Definition: KKStr.cpp:1478
Used to keep track of examples by size; typically used by &#39;CrossValidation&#39;; for each example predict...
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)
MLClassList * MLClassListPtr
Definition: MLClass.h:49
Maintains a list of MLClass instances.
Definition: MLClass.h:233
void PrintFormatedDistributionMatrix(ostream &_outFile) const
void PrintCSVDistributionMatrix(ostream &_outFile) const
std::vector< ulong > VectorUlong
Definition: KKBaseTypes.h:143