KSquare Utilities
FeatureNumList2.h
Go to the documentation of this file.
1 #if !defined(_FEATURENUMLIST2_)
2 #define _FEATURENUMLIST2_
3 
4 /**
5  *@class KKMLL::FeatureNumList
6  *@brief Keeps track of selected features.
7  *@details Used by SVMModel and the newer 'Model', 'ModelParam' based classes.
8  * Each instance of this class will have an associated 'FileDesc' instance. It
9  * is meant to keep track of selected features from that instance('FileDesc').
10  * <br />
11  * This class is meant to work with both the FeaureVector, Model, and ModelParm
12  * based classes. This will allow us to select specific features
13  * from the FeatureVector instances that are to be used.
14  * <br />
15  * At no time are feature numbers that are out of range of the associated
16  * FileDesc instance to be selected.
17  * <br />
18  * Two of the constructors allow you to specify a list of features in a string.
19  * The list can consist of single features and/or ranges of features. Ranges of
20  * features are specified by using the dash('-') character between two numbers.
21  * The comma(',') character will be used as a separator. "All" specifies all are
22  * to be selected except those that are flagged as 'IgnoreAttribute' in the
23  * associated FileDesc instance. The list should be in ascending order.
24  *
25  *@code
26  * Example Strings:
27  * "1,2,3,10,20" Selects [1,2,3,10, 20].
28  * "1,4-7,9-12,13" Selects [1,4,,5,6,7,9,10,11,12,13]
29  * "All" Selects all features that '_fileDesc' includes accept
30  * those that are flagged as 'IgnoreAttribute' in the
31  * associated FileDesc instance.
32  *@endcode
33  */
34 
35 #include "KKBaseTypes.h"
36 #include "BitString.h"
37 #include "RunLog.h"
38 #include "KKStr.h"
39 
40 #include "Attribute.h"
41 
42 
43 namespace KKMLL
44 {
45 #if !defined(_FileDesc_Defined_)
46  class FileDesc;
47  typedef FileDesc* FileDescPtr;
48 #endif
49 
50 
51  class FeatureNumList;
52  typedef FeatureNumList* FeatureNumListPtr;
53 
54  class FeatureNumList
55  {
56  public:
58 
59  /** @brief Copy constructor. */
60  FeatureNumList (const FeatureNumList& featureNumList);
61 
62  /*
63  *@brief Constructs an instance with no features selected and expecting a maximum of 'maxFeatureNum'.
64  */
65  FeatureNumList (kkuint32 _maxFeatureNum);
66 
67 
68  /**
69  *@brief Constructs a 'FeatureNumList' instance using the set bits in 'bitString' to indicate which features are selected.
70  *@details For each bit position in 'bitString' that is set to '1' the corresponding feature will be selected. So the bit string '0110111' with consists of
71  * bits 0, 1, 2, 5, and 6 set to one will cause the features elected to be set to (0, 1, 2, 4, 6). The length of 'bitString' will indicate the maximum
72  * features.
73  *@param[in] bitString A bit string that indicates which features are selected. Bits that are set to 1 indicate that
74  * the corresponding feature is selected.
75  */
76  FeatureNumList (const BitString& bitString);
77 
78 
79  /**
80  * @brief Constructs a 'FeatureNumList' instance from a string that contains a list of selected features.
81  * @details The list can consist of single features and/or ranges of features. Ranges of features
82  * are specified by using the dash('-') character between two numbers. The comma(',')
83  * character will be used as a separator.
84  * @code
85  * ex's:
86  * "1,2,3,10,20" Selects [1,2,3,10, 20].
87  * "1,4-7,9-12,13" Selects [1,4,,5,6,7,9,10,11,12,13]
88  * @endcode
89  * @see ExtractFeatureNumsFromStr
90  * @param[in] _featureListStr Comma separated string that contains list of selected features; a range of
91  * features can be specified using the dash('-') character. ex: The string "1,3,5-7,9" indicates
92  * that features 1,3,5,6,7,9 are selected.
93  * @param[out] _valid returns false if '_featureListStr' is not a valid format.
94  */
95  FeatureNumList (const KKStr& _featureListStr,
96  bool& _valid
97  );
98 
99 
100  ~FeatureNumList ();
101 
102 
103 
104  // Access Methods.
105  const kkuint16* FeatureNums () const {return featureNums;}
106  kkint32 MaxFeatureNum () const {return maxFeatureNum;}
107  kkint32 NumOfFeatures () const {return numOfFeatures;}
108  kkint32 NumSelFeatures () const {return numOfFeatures;}
109 
110 
111 
112 
113  /**
114  *@brief Adds 'featureNum' to the list of selected features.
115  *@details If it is already selected nothing happens. If 'featureNum' exceeds the maxFeatureNum then
116  * 'maxfeatureNum' will be set to this value.
117  */
118  void AddFeature (kkuint16 featureNum);
119 
120  /** @brief Returns true if all features are selected. */
121  bool AllFeaturesSelected (FileDescPtr fileDesc) const;
122 
123  /** @brief Create a FeatureNumList object where all features are selected, except ones that are flagged as IgnoreAttribute in '__fileDesc'. */
124  static FeatureNumList AllFeatures (FileDescPtr fileDesc);
125 
126  /** @brief Compare with another featureNumList returning -1, 0, and 1 indicating less_than, equal, or greater_than. */
127  kkint32 Compare (const FeatureNumList& _features) const;
128 
129  /** @brief Perform a complement of selected features. That is if a feature is selected turn it off and if it is not selected then turn it on. */
130  FeatureNumList Complement () const;
131 
132  /**
133  *@brief Allocates a array of kkint32's that is a copy of FeatureNums. The caller will own the array and is responsible for deleting it.
134  *@returns A dynamically allocated array that will consist of a list of selected features.
135  */
137 
138 
139  /**
140  * @brief Will select the features specified in "featureListStr".
141  * @details The format is a comma delimited string, where each number represents a feature, ranges can be specified with
142  * a dash("-"). "All" will select all features that are not flagged as a 'IgnoreAttribute' in the associated FileDesc instance.
143  * @code
144  * ex's: String Selected Features
145  * "1,2,3,10,20" [1,2,3,10, 20].
146  * "1,4-7,9-12,23" [1,4,5,6,7,9,10,11,12,23]
147  * "All" Selects all features that '_fileDesc' includes accept those that are
148  * flagged as 'IgnoreAttribute' in the associated FileDesc instance.
149  * @endcode
150  */
151  static
152  FeatureNumListPtr ExtractFeatureNumsFromStr (const KKStr& featureListStr);
153 
154  bool IsSubSet (const FeatureNumList& z); /**< @brief Returns true if 'z' is a subset of this instance. */
155 
156  bool InList (kkuint16 featureNum) const; /**< @brief returns true if '_featureNum' is one of the selected features. */
157 
158  void Load (const KKStr& _fileName,
159  bool& _successful,
160  RunLog& _log
161  );
162 
164 
165  /**
166  * @brief Generates a new FeatureNumList object that will select at random 'numToKeep' features from this instance.
167  * @param[in] numToKeep Number of features to select randomly from existing instance.
168  * @return Dynamically allocated instance of a ImageFeaturesList with randomly selected features.
169  */
171 
172  void Save (const KKStr& fileName);
173 
174  void SaveXML (std::ostream& o);
175 
176  void SetAllFeatures (FileDescPtr fileDesc); /**< @brief Selects all features except those flagged as 'IgnoreAttribute' in the associated FileDesc. */
177 
178  bool Test (kkuint16 _featureNum) const; /**< @brief Indicates whether feature '_featureNum' is selected. */
179 
180  void ToBitString (BitString& bitStr) const;
181 
182  KKStr ToHexString () const; /**< @brief Uses 'maxFeatureNum to determine length of hex string. */
183 
184  KKStr ToHexString (FileDescPtr fileDesc) const; /**< @brief Uses 'fileDesc' to determine length of hex string. */
185 
186 
187  /** @brief Returns comma delimited list of all features selected; will make use of range specification. */
188  KKStr ToString () const;
189 
190 
191  KKStr ToCommaDelStr () const {return ToString ();}
192 
193 
194  /** @brief Turns off all features so that no feature is selected. */
195  void UnSet ();
196 
197  /** @brief Turns off specified feature 'featureNum'; if 'featureNum' is not turned on then nothing happens; same as using 'operator-='. */
198  void UnSet (kkuint16 featureNum);
199 
200  /**
201  *@brief Returns back the selected feature.
202  *@details A FeatureNumList instance consists of a list of selected features. It is logically like an
203  * array of selected features that is the same length as the number of selected features.
204  *@code
205  * Example code that scans the FeatureNumList object 'goodFeatures'
206  *
207  * void PrintSelectedFeatures (const FeatureNumList& goodFeatures)
208  * {
209  * cout << "Selected Features: ";
210  * for (kkint32 x = 0; x < goodFeatures.NumOfFeatures ();
211  * {
212  * if (x > 0) cout << ",";
213  * cout << goodFeatures[x];
214  * }
215  * cout << endl;
216  * }
217  *@endcode
218  *@param[in] _idx The position in this instance that you want to return.
219  *@return Selected feature at position '_idx'.
220  */
221  kkuint16 operator[] (kkint32 idx) const;
222 
223  FeatureNumList& operator= (const FeatureNumList& _features);
224  FeatureNumList& operator= (const FeatureNumListPtr _features);
225  FeatureNumList operator+ (const FeatureNumList& rightSide) const; /**< @brief Returns new FeatureNumList that is a union of this instance and 'rightSide'. */
226  FeatureNumList operator+ (kkuint16 rightSide) const; /**< @brief Returns new FeatureNumList that is a union of this instance and 'rightSide'. */
227  FeatureNumList& operator+= (const FeatureNumList& rightSide); /**< @brief Returns this FeatureNumList that is a union of this instance and 'rightSide'. */
228  FeatureNumList& operator+= (kkuint16 featureNum); /**< @brief Returns this FeatureNumList that is a union of this instance and 'rightSide'. */
229  FeatureNumList operator- (const FeatureNumList& rightSide) const; /**< Removes features that are selected in 'rightSide' from this instance and returns the result. */
230  FeatureNumList operator- (kkuint16 rightSide) const; /**< Returns this instance with the feature specified by 'rightSide' removed. */
231  FeatureNumList& operator-= (kkuint16 rightSide); /**< Remove the feature specified by 'rightSide' from this instance. */
232  FeatureNumList operator* (const FeatureNumList& rightSide) const; /**<*@brief Returns new instance that is the intersection of features. */
233  bool operator== (const FeatureNumList& _features) const; /**< @brief Indicates if the two FeatureNumLiost instances have the same features selected. */
234  bool operator> (const FeatureNumList& _features) const; /**< @brief Indicates if the Left FeatureNumList instances is greater than the right one. */
235  bool operator< (const FeatureNumList& _features) const; /**< @brief Indicates if the Left FeatureNumList instances is less than the right one. */
236 
237  private:
238  /*
239  * @brief Constructs an instance with no features selected and no associated 'FileDesc' instance.
240  * This is a private constructor and is used for internal use of 'FeatureNumList only.
241  */
242  FeatureNumList ();
243 
244  void AllocateArraySize (kkuint16 size); /**< @brief Make sure that FeatureNums is allocated to at least this size. */
245 
246  static VectorUint16* StrToUInt16Vetor (const KKStr& s);
247 
248  kkuint16* featureNums; /**< @brief The feature numbers in this array are always kept in ascending order.
249  * @details There will be 'numOfFeatures' in this array. 'featureNumsAllocatedSize'
250  * indicates the size allocated, if more space is needed you need to call
251  * 'AllocateArraySize' to increase it.
252  */
253  kkint32 featureNumsAllocatedSize;
254  kkint32 maxFeatureNum;
255  kkint32 numOfFeatures;
256  }; /* FeatureNumList */
257 
258 
259  typedef FeatureNumList* FeatureNumListPtr;
260 
261  #define _FeatureNumList_Defined_
262 
263 
264  std::ostream& operator<< ( std::ostream& os,
265  const FeatureNumList& features
266  );
267 
268 
269  std::ostream& operator<< ( std::ostream& os,
270  const FeatureNumListPtr& features
271  );
272 
273  extern
274  const char* FeatureDecriptions[];
275 } /* namespace KKMLL */
276 
277 #endif
void Save(const KKStr &fileName)
Provides a detailed description of the attributes of a dataset.
Definition: FileDesc.h:72
__int32 kkint32
Definition: KKBaseTypes.h:88
FeatureNumListPtr RandomlySelectFeatures(kkint32 numToKeep) const
Generates a new FeatureNumList object that will select at random &#39;numToKeep&#39; features from this insta...
FeatureNumList operator+(kkuint16 rightSide) const
Returns new FeatureNumList that is a union of this instance and &#39;rightSide&#39;.
FeatureNumList operator+(const FeatureNumList &rightSide) const
Returns new FeatureNumList that is a union of this instance and &#39;rightSide&#39;.
bool Test(kkuint16 _featureNum) const
Indicates whether feature &#39;_featureNum&#39; is selected.
Keeps track of selected features.
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
FeatureNumList & operator=(const FeatureNumListPtr _features)
bool InList(kkuint16 featureNum) const
returns true if &#39;_featureNum&#39; is one of the selected features.
KKStr ToHexString() const
FeatureNumList & operator+=(kkuint16 featureNum)
Returns this FeatureNumList that is a union of this instance and &#39;rightSide&#39;.
void SaveXML(std::ostream &o)
const char * FeatureDecriptions[]
FeatureNumList(const BitString &bitString)
Constructs a &#39;FeatureNumList&#39; instance using the set bits in &#39;bitString&#39; to indicate which features a...
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
FeatureNumList(const FeatureNumList &featureNumList)
Copy constructor.
kkuint16 * CreateFeatureNumArray() const
Allocates a array of kkint32&#39;s that is a copy of FeatureNums. The caller will own the array and is re...
FeatureNumList operator-(const FeatureNumList &rightSide) const
Allows you to manage very long bit strings.
Definition: BitString.h:31
FeatureNumList Complement() const
Perform a complement of selected features. That is if a feature is selected turn it off and if it is ...
kkuint16 operator[](kkint32 idx) const
Returns back the selected feature.
bool IsSubSet(const FeatureNumList &z)
Returns true if &#39;z&#39; is a subset of this instance.
void UnSet()
Turns off all features so that no feature is selected.
kkint32 MaxFeatureNum() const
void ToBitString(BitString &bitStr) const
kkint32 NumOfFeatures() const
void AddFeature(kkuint16 featureNum)
Adds &#39;featureNum&#39; to the list of selected features. If it is already selected nothing happens...
void UnSet(kkuint16 featureNum)
Turns off specified feature &#39;featureNum&#39;; if &#39;featureNum&#39; is not turned on then nothing happens; same...
void SetAllFeatures(FileDescPtr fileDesc)
Selects all features except those flagged as &#39;IgnoreAttribute&#39; in the associated FileDesc.
KKStr ToHexString(FileDescPtr fileDesc) const
Uses &#39;fileDesc&#39; to determine length of hex string.
bool operator>(const FeatureNumList &_features) const
Indicates if the Left FeatureNumList instances is greater than the right one.
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
kkint32 Compare(const FeatureNumList &_features) const
Compare with another featureNumList returning -1, 0, and 1 indicating less_than, equal, or greater_than.
FeatureNumList operator-(kkuint16 rightSide) const
FeatureNumList & operator=(const FeatureNumList &_features)
static FeatureNumList AllFeatures(FileDescPtr fileDesc)
Create a FeatureNumList object where all features are selected, except ones that are flagged as Ignor...
void Load(const KKStr &_fileName, bool &_successful, RunLog &_log)
FeatureNumList(kkuint32 _maxFeatureNum)
static FeatureNumListPtr ExtractFeatureNumsFromStr(const KKStr &featureListStr)
Will select the features specified in "featureListStr".
KKStr ToString() const
Returns comma delimited list of all features selected; will make use of range specification.
kkint32 NumSelFeatures() const
bool AllFeaturesSelected(FileDescPtr fileDesc) const
Returns true if all features are selected.
FeatureNumList(const KKStr &_featureListStr, bool &_valid)
Constructs a &#39;FeatureNumList&#39; instance from a string that contains a list of selected features...
FeatureNumList * FeatureNumListPtr
FeatureNumList operator*(const FeatureNumList &rightSide) const
Returns new instance that is the intersection of features.
bool operator==(const FeatureNumList &_features) const
Indicates if the two FeatureNumLiost instances have the same features selected.
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)
std::vector< kkuint16 > VectorUint16
Vector of unsigned 16 bit integers.
Definition: KKBaseTypes.h:141
FeatureNumList & operator+=(const FeatureNumList &rightSide)
Returns this FeatureNumList that is a union of this instance and &#39;rightSide&#39;.
kkint32 MemoryConsumedEstimated() const
bool operator<(const FeatureNumList &_features) const
Indicates if the Left FeatureNumList instances is less than the right one.
KKStr ToCommaDelStr() const
const kkuint16 * FeatureNums() const
FeatureNumList & operator-=(kkuint16 rightSide)