KSquare Utilities
Orderings.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <vector>
3 #include <map>
4 #include <stdio.h>
5 #include <iostream>
6 #include <fstream>
7 #include "MemoryDebug.h"
8 using namespace std;
9 
10 
11 #include "KKBaseTypes.h"
12 #include "KKException.h"
13 #include "OSservices.h"
14 using namespace KKB;
15 
16 
17 #include "Orderings.h"
18 #include "FeatureFileIO.h"
19 #include "FileDesc.h"
20 #include "MLClass.h"
21 #include "FeatureVector.h"
22 using namespace KKMLL;
23 
24 
25 Orderings::Orderings (FeatureVectorListPtr _data,
26  kkuint32 _numOfOrderings,
27  kkuint32 _numOfFolds,
28  RunLog& _log
29  ):
30 
31  data (new FeatureVectorList (*_data, false)),
32  fileDesc (_data->FileDesc ()),
33  mlClasses (_data->ExtractListOfClasses ()),
34  numOfFolds (_numOfFolds),
35  numOfOrderings (_numOfOrderings),
36  valid (true)
37 
38 {
39  featureFileName = data->FileName ();
40  if (!featureFileName.Empty ())
41  indexFileName = osRemoveExtension (featureFileName) + ".idx";
42 
43  CreateOrderings (_log);
44 }
45 
46 
47 
48 
49 Orderings::Orderings (const FeatureVectorListPtr _data,
50  const KKStr& _indexFileName,
51  kkuint32 _numOfOrderings,
52  kkuint32 _numOfFolds,
53  RunLog& _log
54  ):
55 
56  data (new FeatureVectorList (*_data, false)),
57  fileDesc (_data->FileDesc ()),
58  mlClasses (_data->ExtractListOfClasses ()),
59  indexFileName (_indexFileName),
60  numOfFolds (_numOfFolds),
61  numOfOrderings (_numOfOrderings),
62  valid (true)
63 
64 {
65  if (!osFileExists (indexFileName))
66  {
67  CreateOrderings (_log);
68  Save ();
69  }
70  else
71  {
72  Load (_log);
73  }
74 }
75 
76 
77 Orderings::Orderings (FeatureVectorListPtr _data,
78  RunLog& _log
79  ):
80 
81  data (new FeatureVectorList (*_data, false)),
82  fileDesc (_data->FileDesc ()),
83  mlClasses (_data->ExtractListOfClasses ()),
84  indexFileName (),
85  numOfFolds (0),
86  numOfOrderings (0),
87  valid (true)
88 
89 {
90  featureFileName = data->FileName ();
91  if (featureFileName.Empty ())
92  {
93  _log.Level (-1) << endl << endl
94  << "Orderings *** ERROR *** No File Name in FeatureVectorList object." << endl
95  << endl;
96  valid = false;
97  return;
98  }
99 
100  bool successful = true;
101  indexFileName = osRemoveExtension (featureFileName) + ".idx";
102 
103  Load (indexFileName, successful, _log);
104  if (!successful)
105  {
106  _log.Level (-1) << endl
107  << "Orderings::Orderings Error Loading existing ordering[" << indexFileName << "]" << endl
108  << endl;
109  valid = false;
110  }
111 
112  return;
113 }
114 
115 
116 
117 
118 
119 
120 Orderings::Orderings (const KKStr& _featureFileName,
121  FeatureFileIOPtr _driver,
122  RunLog& _log,
123  bool& _cancelFlag
124  ):
125 
126  data (NULL),
127  featureFileName (_featureFileName),
128  fileDesc (NULL),
129  mlClasses (NULL),
130  numOfFolds (0),
131  numOfOrderings (0),
132  valid (true)
133 
134 {
135  bool changesdMade = false;
136  bool successful = true;
137 
138  mlClasses = new MLClassList ();
139 
140  data = _driver->LoadFeatureFile (featureFileName,
141  *mlClasses,
142  -1, // Load in all data
143  _cancelFlag,
144  successful,
145  changesdMade,
146  _log
147  );
148 
149  if (_cancelFlag)
150  {
151  _log.Level (-1) << endl
152  << "Orderings ***ERROR*** CancelFlag was set, load was canceled." << endl
153  << endl;
154  valid = false;
155  return;
156  }
157 
158 
159  if (!successful)
160  {
161  _log.Level (-1) << endl
162  << "Orderings ***ERROR*** Loading Feature File[" << featureFileName << "]" << endl
163  << endl;
164  valid = false;
165  return;
166  }
167 
168  fileDesc = data->FileDesc ();
169 
170  indexFileName = osRemoveExtension (featureFileName) + ".idx";
171  Load (indexFileName, successful, _log);
172  if (!successful)
173  {
174  _log.Level (-1) << endl
175  << "Orderings *** ERROR *** Loading Index File[" << indexFileName << "]" << endl
176  << endl;
177  valid = false;
178  return;
179  }
180 }
181 
182 
183 
184 
186 {
187  DeleteOrderings ();
188  delete data;
189 }
190 
191 
192 
193 OrderingsPtr Orderings::CreateOrderingsObjFromFileIfAvaliable (const FeatureVectorListPtr _data,
194  kkuint32 _numOfOrderings,
195  kkuint32 _numOfFolds,
196  RunLog& _log
197  )
198 {
199  KKStr _featureFileName = _data->FileName ();
200 
201  if (_featureFileName.Empty ())
202  {
203  _log.Level (-1) << endl
204  << "CreateOrderingsObjFromFileIfAvaliable *** ERROR *** FileName empty." << endl
205  << endl;
206  return NULL;
207  }
208 
209  KKStr _indexFileName = osRemoveExtension (_featureFileName) + ".idx";
210 
211  OrderingsPtr orderings = new Orderings (_data, _indexFileName, _numOfOrderings, _numOfFolds, _log);
212  if (orderings->Valid ())
213  {
214  if ((orderings->NumOfOrderings () != _numOfOrderings) ||
215  (orderings->NumOfFolds () != _numOfFolds)
216  )
217  {
218  _log.Level (-1) << endl
219  << "CreateOrderingsObjFromFileIfAvaliable *** ERROR *** Dimension Mismatched." << endl
220  << endl
221  << "Dimensions Expected NumOfOrderings[" << _numOfOrderings << "] NumOfFolds[" << _numOfFolds << "]" << endl
222  << "Dimensions Found NumOfOrderings[" << orderings->NumOfOrderings () << "] NumOfFolds[" << orderings->NumOfFolds () << "]" << endl
223  << endl;
224  delete orderings;
225  return NULL;
226  }
227  }
228  else
229  {
230  delete orderings;
231  orderings = new Orderings (_data, _numOfOrderings, _numOfFolds, _log);
232  if (!orderings->Valid ())
233  {
234  delete orderings;
235  return NULL;
236  }
237  else
238  {
239  orderings->Save ();
240  }
241  }
242 
243  return orderings;
244 } /* CreateOrderingsObjFromFileIfAvaliable */
245 
246 
247 
248 
249 void Orderings::CreateOrderings (RunLog& log)
250 {
251  DeleteOrderings ();
252 
253  FeatureVectorListPtr workList = new FeatureVectorList (*data, false);
254  while (orderings.size () < numOfOrderings)
255  {
256  workList->RandomizeOrder ();
257  FeatureVectorListPtr ordering = workList->StratifyAmoungstClasses (mlClasses, -1, numOfFolds, log);
258  orderings.push_back (ordering);
259  }
260 
261  delete workList;
262 } /* CreateOrderings */
263 
264 
265 
266 
267 void Orderings::DeleteOrderings ()
268 {
269  while (orderings.size () > 0)
270  {
271  FeatureVectorListPtr ordering = orderings.back ();
272  orderings.pop_back ();
273  delete ordering;
274  }
275 } /* DeleteOrderings */
276 
277 
278 void Orderings::Load (RunLog& log)
279 {
280  bool successful = true;
281  Load (indexFileName, successful, log);
282  if (!successful)
283  valid = false;
284 
285 } /* Load */
286 
287 
288 
289 void Orderings::Load (const KKStr& _indexFileName,
290  bool& successful,
291  RunLog& log
292  )
293 {
294  log.Level (10) << endl << endl << endl << endl;
295 
296  log.Level (10) << "Orderings::Load indexFileName[" << _indexFileName << "]" << endl;
297 
298  successful = true;
299 
300  DeleteOrderings ();
301  indexFileName = _indexFileName;
302  if (indexFileName.Empty ())
303  {
304  // We have a problem, no way of creating a file name.
305  log.Level (-1) << endl << endl
306  << "Orderings::Load *** ERROR *** No Index File Name Specified." << endl
307  << endl;
308  successful = false;
309  valid = false;
310  return;
311  }
312 
313  ifstream i (indexFileName.Str ());
314  if (!i.is_open ())
315  {
316  successful = false;
317  valid = false;
318  log.Level (-1) << endl << endl
319  << "Orderings::Load *** ERROR *** Opening[" << indexFileName << "]" << endl
320  << endl;
321  return;
322  }
323 
324  KKStr line;
325 
326  {
327  // Make sure 1st line is Orderings.
328  do {
329  i >> line;
330  cout << "Beginning Orderings Line[" << line << "]" << endl;
331  } while ((!i.eof ()) && (line.Empty ()));
332 
333  line.Upper ();
334  if (line != "//ORDERINGS")
335  {
336  log.Level (-1) << endl << endl
337  << "Orderings::Load *** ERROR *** Invalid File Heading[" << indexFileName << "]" << endl
338  << " First Line[" << line << "]" << endl
339  << " Expected [//Orderings]" << endl
340  << endl;
341  successful = false;
342  valid = false;
343  i.close ();
344  return;
345  }
346  }
347 
348  bool headerFields = true;
349  KKStr field;
350  numOfOrderings = 0;
351  numOfFolds = 0;
352  while (headerFields && (!i.eof ()))
353  {
354  i >> field;
355  field.Upper ();
356 
357  if (field == "//FEATUREFILENAME")
358  i >> featureFileName;
359 
360  else if (field == "//NUMOFORDERINGS")
361  i >> numOfOrderings;
362 
363  else if (field == "//NUMOFFOLDS")
364  i >> numOfFolds;
365 
366  else if (field == "//ENDOFHEADER")
367  headerFields = false;
368  }
369 
370  if (numOfOrderings < 1)
371  {
372  log.Level (-1) << endl << endl
373  << "Orderings::Load *** ERROR *** Invalid Header Field[" << indexFileName << "]" << endl
374  << " NumOfOrderings[" << numOfOrderings << "]" << endl
375  << endl;
376  successful = false;
377  valid = false;
378  i.close ();
379  return;
380  }
381 
382 
383  log.Level (10) << "Orderings::Load featureFileName[" << featureFileName << "]" << endl;
384  log.Level (10) << "Orderings::Load numOfOrderings [" << numOfOrderings << "]" << endl;
385  log.Level (10) << "Orderings::Load numOfFolds [" << numOfFolds << "]" << endl;
386 
387  kkuint32 orderingIDX = 0;
388 
389  for (orderingIDX = 0; orderingIDX < numOfOrderings; orderingIDX++)
390  {
391  vector<bool> indexLoaded (data->QueueSize (), false);
392 
393  kkint32 imagesInOrdering = 0;
394 
395  FeatureVectorListPtr ordering = new FeatureVectorList (fileDesc, false);
396  orderings.push_back (ordering);
397 
398  {
399  // Get Ordering Header Record.
400  i >> field;
401  field.Upper ();
402 
403  if ((i.eof ()) || (field != "//ORDERINGNUM"))
404  {
405  log.Level (-1) << endl << endl
406  << "Orderings::Load *** ERROR *** Index File [" << indexFileName << "] Incomplete." << endl
407  << endl;
408  successful = false;
409  valid = false;
410  i.close ();
411  return;
412  }
413 
414  kkint32 orderingNum;
415  i >> orderingNum;
416 
417  if (orderingNum != kkint32 (orderingIDX))
418  {
419  log.Level (-1) << endl << endl
420  << "Orderings::Load *** ERROR *** Orderings out of sequence." << endl
421  << " Expected[" << orderingIDX << "]" << endl
422  << " Found [" << orderingNum << "]" << endl
423  << endl;
424  successful = false;
425  valid = false;
426  i.close ();
427  return;
428  }
429 
430  i >> field; // Load "Count" label.
431  field.Upper ();
432  if (field != "COUNT")
433  {
434  log.Level (-1) << endl << endl
435  << "Orderings::Load *** ERROR *** Orderings out of sequence." << endl
436  << " Missing Count Label for Ordering[" << orderingNum << "]" << endl
437  << endl;
438  successful = false;
439  valid = false;
440  i.close ();
441  return;
442  }
443 
444  i >> imagesInOrdering;
445  }
446 
447 
448  i >> field;
449  field.Upper ();
450 
451  while ((field != "//ENDOFORDERING") && (!i.eof ()))
452  {
453  kkint32 imageIdx = atoi (field.Str ());
454 
455  if ((imageIdx < 0) || (imageIdx >= data->QueueSize ()))
456  {
457  log.Level (-1) << endl << endl
458  << "Orderings::Load *** ERROR *** Invalid Index Encountered." << endl
459  << " Index [" << imageIdx << "]" << endl
460  << " Size of Data [" << data->QueueSize () << "]" << endl
461  << endl;
462  successful = false;
463  valid = false;
464  i.close ();
465  return;
466  }
467 
468  if (indexLoaded[imageIdx])
469  {
470  log.Level (-1) << endl << endl
471  << "Orderings::Load *** ERROR *** Duplicate Index in same ordering." << endl
472  << " Index [" << imageIdx << "]" << endl
473  << " OrderingNum [" << orderingIDX << "]" << endl
474  << endl;
475  successful = false;
476  valid = false;
477  i.close ();
478  return;
479  }
480 
481  indexLoaded[imageIdx] = true;
482  ordering->PushOnBack (data->IdxToPtr (imageIdx));
483 
484  i >> field;
485  field.Upper ();
486  }
487 
488  if (ordering->QueueSize () < imagesInOrdering)
489  {
490  log.Level (-1) << endl << endl
491  << "Orderings::Load *** ERROR *** Missing Indexes for Ordering[" << orderingIDX << "]." << endl
492  << " Expected [" << imagesInOrdering << "]" << endl
493  << " Found [" << ordering->QueueSize () << "]" << endl
494  << endl;
495  successful = false;
496  valid = false;
497  i.close ();
498  return;
499  }
500 
501  if (field == "//ENDOFORDERING")
502  {
503  kkint32 endOfOrderingNum;
504  i >> endOfOrderingNum;
505  }
506 
507  cout << "Ordering " << orderingIDX << " of " << numOfOrderings << " Loaded" << endl;
508  }
509 
510  if (orderings.size () != numOfOrderings)
511  {
512  log.Level (-1) << endl << endl
513  << "Orderings::Load *** ERROR *** Not orderings were loaded." << endl
514  << " Expected [" << numOfOrderings << "]" << endl
515  << " Number Found [" << (kkuint32)orderings.size () << "]" << endl
516  << endl;
517  successful = false;
518  valid = false;
519  i.close ();
520  return;
521  }
522 
523  i.close ();
524 } /* LoadIndexsFromFile */
525 
526 
527 
528 void Orderings::Save (const KKStr& _indexFileName,
529  RunLog& _log
530  )
531 {
532  if (indexFileName.Empty ())
533  {
534  // We have a problem, no way of creating a file name.
535  _log.Level (-1) << endl << endl
536  << "Orderings::Save *** ERROR *** No Index File Name Specified." << endl
537  << endl;
539  exit (-1);
540  return;
541  }
542 
543  indexFileName = _indexFileName;
544  Save ();
545 } /* Save */
546 
547 
548 
549 
550 
551 void Orderings::Save ()
552 {
553  // Build an index by relative location in master list data, so that we can
554  // quickly determine index for other orderings.
555  map<FeatureVectorPtr,kkint32> index;
556  kkint32 idx = 0;
557  for (idx = 0; idx < data->QueueSize (); idx++)
558  {
559  FeatureVectorPtr example = data->IdxToPtr (idx);
560  index.insert (pair<FeatureVectorPtr,kkint32>(example, idx));
561  }
562 
563  KKStr tempName = featureFileName;
564  if (tempName.Empty ())
565  tempName = indexFileName;
566 
567  map<FeatureVectorPtr,kkint32>::const_iterator indexIDX;
568 
569  ofstream o (indexFileName.Str ());
570 
571  o << "//Orderings" << endl;
572  o << "//FeatureFileName" << "\t" << tempName << endl;
573  o << "//NumOfOrderings" << "\t" << numOfOrderings << endl;
574  o << "//NumOfFolds" << "\t" << numOfFolds << endl;
575  o << "//DateCreated" << "\t" << osGetLocalDateTime () << endl;
576  o << "//EndOfHeader" << endl;
577 
578  kkuint32 orderingIDX;
579 
580  for (orderingIDX = 0; orderingIDX < orderings.size (); orderingIDX++)
581  {
582  FeatureVectorListPtr ordering = orderings[orderingIDX];
583  o << "//OrderingNum" << "\t" << orderingIDX << "\t" << "Count" << "\t" << ordering->QueueSize () << endl;
584 
585  FeatureVectorList::const_iterator idx;
586  for (idx = ordering->begin (); idx != ordering->end (); idx++)
587  {
588  const FeatureVectorPtr example = *idx;
589  indexIDX = index.find (example);
590  if (indexIDX == index.end ())
591  {
592  // We have a very serious problem, for some reason we
593  // could not locate the FeatureVector object in the master list.
594 
595  KKStr errMsg;
596  errMsg << "Orderings::Save ***ERROR*** FileName[" << indexFileName << "] Could not locate Image in data list";
597  cerr << endl << errMsg << endl << endl;
598  throw KKException (errMsg);
599  }
600 
601  o << indexIDX->second << endl;
602  }
603  o << "//EndOfOrdering" << "\t" << orderingIDX << endl;
604 
605  cout << "Saved " << orderingIDX << " of " << orderings.size () << endl;
606  }
607 
608  o.close ();
609 } /* Save */
610 
611 
612 
613 const
614 FeatureVectorListPtr Orderings::Ordering (kkuint32 orderingIdx) const
615 {
616  if ((orderingIdx >= orderings.size ()))
617  {
618  KKStr errMsg;
619  errMsg << "Orderings::Ordering ***ERROR*** Index Out Of Range; Number Of Orderings [" << (kkuint32)orderings.size () << "] OrderingIdx [" << (kkuint32)orderingIdx << "]";
620  cerr << endl << errMsg << endl << endl;
621  throw KKException (errMsg);
622  }
623 
624  return orderings[orderingIdx];
625 } /* Ordering */
Orderings(const FeatureVectorListPtr _data, const KKStr &_indexFileName, kkuint32 _numOfOrderings, kkuint32 _numOfFolds, RunLog &_log)
Constructs a Orderings object from a FeatureLectorList object.
Definition: Orderings.cpp:49
Used to maintain multiple orderings of a single list of FeatureVector objects.
Definition: Orderings.h:58
kkuint32 NumOfFolds() const
Definition: Orderings.h:168
void Save(const KKStr &_indexFileName, RunLog &_log)
Definition: Orderings.cpp:528
KKStr osRemoveExtension(const KKStr &_fullFileName)
__int32 kkint32
Definition: KKBaseTypes.h:88
FeatureVectorList(const FeatureVectorList &examples, bool _owner)
Create a duplicate list, depending on the &#39;_owner&#39; parameter may also duplicate the contents...
void osWaitForEnter()
MLClassListPtr ExtractListOfClasses() const
kkuint32 NumOfOrderings() const
Definition: Orderings.h:169
const FileDescPtr FileDesc() const
bool operator==(const char *rtStr) const
Definition: KKStr.cpp:1588
KKStr operator+(const char *right) const
Definition: KKStr.cpp:3986
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
virtual FeatureVectorListPtr LoadFeatureFile(const KKStr &_fileName, MLClassList &_mlClasses, kkint32 _maxCount, VolConstBool &_cancelFlag, bool &_successful, bool &_changesMade, RunLog &_log)
Loads the contents of a feature data file and returns a ImageFeaturesList container object...
FeatureVectorList(FileDescPtr _fileDesc, bool _owner)
Will create a new empty list of FeatureVector&#39;s.
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
bool operator!=(const char *rtStr) const
Definition: KKStr.cpp:1596
Orderings(const KKStr &_featureFileName, FeatureFileIOPtr _driver, RunLog &_log, bool &cancelFlag)
Constructs Orderings of a FeatureVectorList from a previous construction that was saved in a data fil...
Definition: Orderings.cpp:120
Container class for FeatureVector derived objects.
KKTHread * KKTHreadPtr
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
bool Valid() const
Definition: Orderings.h:171
FeatureFileIO * FeatureFileIOPtr
Definition: FileDesc.h:45
bool Empty() const
Definition: KKStr.h:241
const FeatureVectorListPtr Ordering(kkuint32 orderingIdx) const
Definition: Orderings.cpp:614
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
Orderings * OrderingsPtr
Definition: Orderings.h:62
bool osFileExists(const KKStr &_fileName)
Definition: OSservices.cpp:568
const KKStr & FileName() const
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)
static OrderingsPtr CreateOrderingsObjFromFileIfAvaliable(const FeatureVectorListPtr _data, kkuint32 _numOfOrderings, kkuint32 _numOfFolds, RunLog &_log)
Constructs a Orderings object for a specified FeatureVectorList.
Definition: Orderings.cpp:193
Orderings(FeatureVectorListPtr _data, RunLog &_log)
Constructs a Orderings objects for a specified FeatureVectorList using a previously built Orderings d...
Definition: Orderings.cpp:77
Maintains a list of MLClass instances.
Definition: MLClass.h:233