KSquare Utilities
Orderings.h
Go to the documentation of this file.
1 #ifndef _ORDERINGS_
2 #define _ORDERINGS_
3 
4 
5 /**
6  *@class KKMLL::Orderings
7  *@author Kurt Kramer
8  *@brief Used to maintain multiple orderings of a single list of FeatureVector objects.
9  *@details
10  * Will maintain multiple orderings of a single FeatureVectorList. These orderings will be saved in
11  * a text file for recall later. This will allow the user to be able to repeat experiments using the
12  * same ordering of data again. The Idea is that the 1st time this orderings is created the order
13  * will be randomly driven with Stratification by Class. This ordering will then be saved in data
14  * Files for future recall. More than one order can be maintained for a single list. An example of
15  * a good use of this is RandomSplits.
16  */
17 
18 
19 
20 #include "FileDesc.h"
21 #include "RunLog.h"
22 #include "KKStr.h"
23 
24 
25 namespace KKMLL
26 {
27 
28 
30 class FeatureVector;
32 #endif
33 
34 
36 class FeatureVectorList;
38 #endif
39 
40 
41 #ifndef _MLCLASS_
42 class MLClass;
43 typedef MLClass* MLClassPtr;
44 class MLClassList;
46 #endif
47 
48 
49 #ifndef _FILEDESC_
50 class FileDesc;
51 typedef FileDesc* FileDescPtr;
52 class FileDescList;
54 #endif
55 
56 
57 
58 class Orderings
59 {
60 public:
61 
63 
64  /**
65  *@brief Constructs Orderings object from ImageFeatursList object.
66  *@details Use this when an existing list does not exist. Will create 'numOfOrderings'
67  * separate lists of 'data' that are randomly ordered and stratified by
68  * 'numOfFolds'.
69  *
70  *@param[in] _data ImagFeaturesList object,
71  *@param[in] _numOfOrderings Number of separate orderings of data need.
72  *@param[in] _numOfFolds Used to help stratify data in each fold.
73  */
74  Orderings (const FeatureVectorListPtr _data,
75  kkuint32 _numOfOrderings,
76  kkuint32 _numOfFolds,
77  RunLog& _log
78  );
79 
80 
81  /**
82  *@brief Constructs Orderings of a FeatureVectorList from a previous construction that was saved
83  * in a data file.
84  *@details Will load object from the Feature File '_featureFileName' and retrieve the
85  * different orderings from a separate index file who's name will be
86  * osExtention (FeatureFileName) + ".idx". The load routine will validate that all
87  * FeatureVector instances are accounted for in each ordering. If the index file does not
88  * exist it then the 'successful' flag will bet to false.
89  *
90  *@param[in] _featureFileName File to load FeatureVector' objects from. This will be used as
91  * master list for 'Orderings'.
92  *@param[in] _driver Feature File driver to utilize.
93  *@param[in] _log Log file to write messages to.
94  *@param[in] v If flag turns to 'TRUE' then will terminate the load process and return to caller.
95  */
96  Orderings (const KKStr& _featureFileName,
97  FeatureFileIOPtr _driver,
98  RunLog& _log,
99  bool& cancelFlag
100  );
101 
102 
103  /**
104  *@brief Constructs a Orderings object from a FeatureLectorList object.
105  *@details Will use 'data' as master list of FeatureVector objects.The orderings will be loaded
106  * from 'indexFileName'. It is expected that the size of the files will match. The
107  * load routine will validate that all FeatureVector objects are accounted for in each
108  * ordering.
109  *
110  *@param[in] _data Master List of FeatureVector instances.
111  *@param[in] _indexFileName File where orderings of 'data' are to be loaded from.
112  *@param[in] _numOfOrderings
113  *@param[in] _numOfFolds
114  */
115  Orderings (const FeatureVectorListPtr _data,
116  const KKStr& _indexFileName,
117  kkuint32 _numOfOrderings,
118  kkuint32 _numOfFolds,
119  RunLog& _log
120  );
121 
122 
123  /**
124  *@brief Constructs a Orderings objects for a specified FeatureVectorList using a previously
125  * built Orderings data index file.
126  *@details Will use FileName from "data" parameter to derive both 'featureFileName' and
127  * 'indexFileName' using the 'FileName' method from FeatureVectorList. It is expected
128  * that a separate index file by the name osDeletExtention (FeatureFileName) + ".idx"
129  * will exist. The orderings will be loaded from that file.
130  *@param[in] _data FeatureVectorList that we want different orderings of.
131  *@param[in] _log Logger.
132  */
133  Orderings (FeatureVectorListPtr _data,
134  RunLog& _log
135  );
136 
137  ~Orderings ();
138 
139 
140 
141  /**
142  *@brief Constructs a Orderings object for a specified FeatureVectorList.
143  *@details Will use FileName from "_data" parameter to derive both 'featureFileName' and
144  * 'indexFileName' using the 'FileName' method from FeatureVectorList. If a separate
145  * Index file does not exist it will randomly create orderings and save the orderings
146  * in a new Index file.
147  *@param[in] _data FeatureVectorList that we want different orderings of.
148  *@param[in] _numOfOrderings Expected number of orderings.
149  *@param[in] _numOfFolds Number of folds each ordering should be stratified by.
150  *@param[in] _log Logger.
151  */
152  static
153  OrderingsPtr CreateOrderingsObjFromFileIfAvaliable (const FeatureVectorListPtr _data,
154  kkuint32 _numOfOrderings,
155  kkuint32 _numOfFolds,
156  RunLog& _log
157  );
158 
159 
160  /***************************************************************************/
161  /* Access Methods */
162  /***************************************************************************/
163  FeatureVectorListPtr Data () const {return data;}
164  const KKStr& FeatureFileName () const {return featureFileName;}
165  const FileDescPtr FileDesc () const {return fileDesc;}
166  const MLClassListPtr MLClasses () const {return mlClasses;}
167  const KKStr& IndexFileName () const {return indexFileName;}
168  kkuint32 NumOfFolds () const {return numOfFolds;}
169  kkuint32 NumOfOrderings () const {return numOfOrderings;}
170  kkuint32 Size () const {return (kkuint32)orderings.size ();}
171  bool Valid () const {return valid;}
172 
173 
174 
175 
176  const
177  FeatureVectorListPtr Ordering (kkuint32 orderingIdx) const;
178 
179 
180 
181 
182  void Save ();
183 
184  void Save (const KKStr& _indexFileName,
185  RunLog& _log
186  );
187 
188 
189 private:
190  void CreateOrderings (RunLog& log);
191  void DeleteOrderings ();
192  void Load (RunLog& log);
193 
194  void Load (const KKStr& _indexFileName,
195  bool& successful,
196  RunLog& log
197  );
198 
199 
200  FeatureVectorListPtr data;
201  KKStr featureFileName;
202  FileDescPtr fileDesc;
203  MLClassListPtr mlClasses;
204  KKStr indexFileName;
205  kkuint32 numOfFolds;
206  kkuint32 numOfOrderings;
207  vector<FeatureVectorListPtr> orderings;
208  bool valid;
209 };
210 
211 
213 
214 } /* KKMLL */
215 
216 #endif
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
Orderings(const FeatureVectorListPtr _data, kkuint32 _numOfOrderings, kkuint32 _numOfFolds, RunLog &_log)
Constructs Orderings object from ImageFeatursList object.
Definition: Orderings.cpp:25
void Save(const KKStr &_indexFileName, RunLog &_log)
Definition: Orderings.cpp:528
#define _FeatureVector_Defined_
const KKStr & FeatureFileName() const
Definition: Orderings.h:164
kkuint32 NumOfOrderings() const
Definition: Orderings.h:169
const MLClassListPtr MLClasses() const
Definition: Orderings.h:166
FeatureVectorListPtr Data() const
Definition: Orderings.h:163
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
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
bool Valid() const
Definition: Orderings.h:171
kkuint32 Size() const
Definition: Orderings.h:170
const FeatureVectorListPtr Ordering(kkuint32 orderingIdx) const
Definition: Orderings.cpp:614
#define _FeatureVectorList_Defined_
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
Orderings * OrderingsPtr
Definition: Orderings.h:62
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
const KKStr & IndexFileName() const
Definition: Orderings.h:167
Orderings * OrderingsPtr
Orderings(FeatureVectorListPtr _data, RunLog &_log)
Constructs a Orderings objects for a specified FeatureVectorList using a previously built Orderings d...
Definition: Orderings.cpp:77
const FileDescPtr FileDesc() const
Definition: Orderings.h:165