KSquare Utilities
FeatureNumList.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 'Ignore' 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 'Ignore' 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 
53 
55  {
56  public:
60 
61 
62  FeatureNumList ();
63 
64 
65  /** @brief Copy constructor. */
66  FeatureNumList (FeatureNumListConst& featureNumList);
67 
68 
69  /** @brief Move constructor. */
70  FeatureNumList (FeatureNumList &&featureNumList);
71 
72 
73  /** @brief Constructs an instance with no features selected and expecting a maximum of 'maxFeatureNum'. */
74  FeatureNumList (kkuint32 _maxFeatureNum);
75 
76 
77  /*
78  *@brief Constructs an instance with no features selected and derives the maximum number of features from _fileDesc.
79  */
80  FeatureNumList (FileDescPtr _fileDesc);
81 
82 
83  /**
84  *@brief Constructs a 'FeatureNumList' instance using the set bits in 'bitString' to indicate which features are selected.
85  *@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
86  * 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
87  * features.
88  *@param[in] bitString A bit string that indicates which features are selected. Bits that are set to 1 indicate that
89  * the corresponding feature is selected.
90  */
91  FeatureNumList (const BitString& bitString);
92 
93 
94  /**
95  * @brief Constructs a 'FeatureNumList' instance from a string that contains a list of selected features.
96  * @details The list can consist of single features and/or ranges of features. Ranges of features
97  * are specified by using the dash('-') character between two numbers. The comma(',')
98  * character will be used as a separator.
99  * @code
100  * ex's:
101  * "1,2,3,10,20" Selects [1,2,3,10, 20].
102  * "1,4-7,9-12,13" Selects [1,4,,5,6,7,9,10,11,12,13]
103  * @endcode
104  * @see ParseToString
105  * @param[in] _featureListStr Comma separated string that contains list of selected features; a range of
106  * features can be specified using the dash('-') character. ex: The string "1,3,5-7,9" indicates
107  * that features 1,3,5,6,7,9 are selected.
108  * @param[out] _valid returns false if '_featureListStr' is not a valid format.
109  */
110  FeatureNumList (const KKStr& _featureListStr,
111  bool& _valid
112  );
113 
114 
115  ~FeatureNumList ();
116 
117 
118  // Access Methods.
119  const kkuint16* FeatureNums () const {return featureNums;}
120  kkint32 MaxFeatureNum () const {return maxFeatureNum;}
121  kkint32 NumOfFeatures () const {return numOfFeatures;}
122  kkint32 NumSelFeatures () const {return numOfFeatures;}
123 
124 
125  /**
126  *@brief Adds 'featureNum' to the list of selected features.
127  *@details If it is already selected nothing happens. If 'featureNum' exceeds the maxFeatureNum then
128  * 'maxfeatureNum' will be set to this value.
129  */
130  void AddFeature (kkuint16 featureNum);
131 
132  /** @brief Returns true if all features are selected. */
133  bool AllFeaturesSelected (FileDescPtr fileDesc) const;
134 
135  /** @brief Create a FeatureNumList object where all features are selected, except ones that are flagged as Ignore in '__fileDesc'. */
136  static FeatureNumList AllFeatures (FileDescPtr fileDesc);
137 
138  /** @brief Compare with another featureNumList returning -1, 0, and 1 indicating less_than, equal, or greater_than. */
139  kkint32 Compare (const FeatureNumList& _features) const;
140 
141  /** @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. */
142  FeatureNumList Complement () const;
143 
144  /**
145  *@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.
146  *@returns A dynamically allocated array that will consist of a list of selected features.
147  */
149 
150  bool IsSubSet (const FeatureNumList& z); /**< @brief Returns true if 'z' is a subset of this instance. */
151 
152  bool InList (kkuint16 featureNum) const; /**< @brief returns true if '_featureNum' is one of the selected features. */
153 
155 
156 
157  /**
158  * @brief Will select the features specified in "featureListStr".
159  * @details The format is a comma delimited string, where each number represents a feature, ranges can be specified with
160  * a dash("-"). "All" will select all features that are not flagged as a 'Ignore' in the associated FileDesc instance.
161  * @code
162  * ex's: String Selected Features
163  * "1,2,3,10,20" [1,2,3,10, 20].
164  * "1,4-7,9-12,23" [1,4,5,6,7,9,10,11,12,23]
165  * "All" Selects all features that '_fileDesc' includes accept those that are
166  * flagged as 'Ignore' in the associated FileDesc instance.
167  * @endcode
168  */
169  void ParseToString (const KKStr& _str,
170  bool& _valid
171  );
172 
173  void ReadXML (XmlStream& s,
174  XmlTagConstPtr tag,
175  VolConstBool& cancelFlag,
176  RunLog& log
177  );
178 
179  /**
180  * @brief Generates a new FeatureNumList object that will select at random 'numToKeep' features from this instance.
181  * @param[in] numToKeep Number of features to select randomly from existing instance.
182  * @return Dynamically allocated instance of a ImageFeaturesList with randomly selected features.
183  */
185 
186  void SetAllFeatures (FileDescPtr fileDesc); /**< @brief Selects all features except those flagged as 'Ignore' in the associated FileDesc. */
187 
188  bool Test (kkuint16 _featureNum) const; /**< @brief Indicates whether feature '_featureNum' is selected. */
189 
190  void ToBitString (BitString& bitStr) const;
191 
192  KKStr ToHexString () const; /**< @brief Uses 'maxFeatureNum to determine length of hex string. */
193 
194  KKStr ToHexString (FileDescPtr fileDesc) const; /**< @brief Uses 'fileDesc' to determine length of hex string. */
195 
196 
197  /** @brief Returns comma delimited list of all features selected; will make use of range specification. */
198  KKStr ToString () const;
199 
200 
201  KKStr ToCommaDelStr () const {return ToString ();}
202 
203 
204  /** @brief Turns off all features so that no feature is selected. */
205  void UnSet ();
206 
207  /** @brief Turns off specified feature 'featureNum'; if 'featureNum' is not turned on then nothing happens; same as using 'operator-='. */
208  void UnSet (kkuint16 featureNum);
209 
210  void WriteXML (const KKStr& varName, std::ostream& o) const;
211 
212 
213  ///<summary>
214  /// Returns back selected feature. A FeatureNumList instance consists of a list of selected features. It is logically like an array of selected
215  /// features that is the same length as the number of selected features.
216  ///</summary>
217  ///<param name="idx"> The position in this instance that you want to return. </param>
218  ///<returns> Selected feature at position &quot;_idx&quot;.</returns>
219  kkuint16 operator[] (kkint32 idx) const;
220 
221  FeatureNumList& operator= (const FeatureNumList& _features);
222  FeatureNumList& operator= (FeatureNumList&& _features);
223  FeatureNumList& operator= (const FeatureNumListPtr _features);
224  FeatureNumList operator+ (const FeatureNumList& rightSide) const; /**< @brief Returns new FeatureNumList that is a union of this instance and 'rightSide'. */
225  FeatureNumList operator+ (kkuint16 rightSide) const; /**< @brief Returns new FeatureNumList that is a union of this instance and 'rightSide'. */
226  FeatureNumList& operator+= (const FeatureNumList& rightSide); /**< @brief Returns this FeatureNumList that is a union of this instance and 'rightSide'. */
227  FeatureNumList& operator+= (kkuint16 featureNum); /**< @brief Returns this FeatureNumList that is a union of this instance and 'rightSide'. */
228  FeatureNumList operator- (const FeatureNumList& rightSide) const; /**< Removes features that are selected in 'rightSide' from this instance and returns the result. */
229  FeatureNumList operator- (kkuint16 rightSide) const; /**< Returns this instance with the feature specified by 'rightSide' removed. */
230  FeatureNumList& operator-= (kkuint16 rightSide); /**< Remove the feature specified by 'rightSide' from this instance. */
231  FeatureNumList operator* (const FeatureNumList& rightSide) const; /**<*@brief Returns new instance that is the intersection of features. */
232  bool operator== (const FeatureNumList& _features) const; /**< @brief Indicates if the two FeatureNumLiost instances have the same features selected. */
233  bool operator> (const FeatureNumList& _features) const; /**< @brief Indicates if the Left FeatureNumList instances is greater than the right one. */
234  bool operator< (const FeatureNumList& _features) const; /**< @brief Indicates if the Left FeatureNumList instances is less than the right one. */
235 
236  private:
237  void AllocateArraySize (kkuint16 size); /**< @brief Make sure that FeatureNums is allocated to at least this size. */
238 
239  static VectorUint16* StrToUInt16Vetor (const KKStr& s);
240 
241  kkuint16* featureNums; /**< @brief The feature numbers in this array are always kept in ascending order.
242  * @details There will be 'numOfFeatures' in this array. 'featureNumsAllocatedSize'
243  * indicates the size allocated, if more space is needed you need to call
244  * 'AllocateArraySize' to increase it.
245  */
246  kkint32 featureNumsAllocatedSize;
247  kkint32 maxFeatureNum;
248  kkint32 numOfFeatures;
249  }; /* FeatureNumList */
250 
251 
255 
256  #define _FeatureNumList_Defined_
257 
258 
259  std::ostream& operator<< (std::ostream& os, const FeatureNumList& features);
260 
261  std::ostream& operator<< (std::ostream& os, const FeatureNumListPtr& features);
262 
265 } /* namespace KKMLL */
266 
267 #endif
Provides a detailed description of the attributes of a dataset.
Definition: FileDesc.h:72
FeatureNumListConst * FeatureNumListConstPtr
__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.
FeatureNumList(FileDescPtr _fileDesc)
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 const FeatureNumListConst
void ParseToString(const KKStr &_str, bool &_valid)
Will select the features specified in "featureListStr".
FeatureNumList & operator+=(kkuint16 featureNum)
Returns this FeatureNumList that is a union of this instance and &#39;rightSide&#39;.
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.
XmlElementFeatureNumList * XmlElementFeatureNumListPtr
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(FeatureNumList &&featureNumList)
Move constructor.
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.
FeatureNumList * FeatureNumListPtr
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...
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
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
FileDesc * FileDescPtr
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...
FeatureNumList(kkuint32 _maxFeatureNum)
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
FeatureNumListConst * FeatureNumListConstPtr
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)
void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
std::vector< kkuint16 > VectorUint16
Vector of unsigned 16 bit integers.
Definition: KKBaseTypes.h:141
FeatureNumList const FeatureNumListConst
XmlElementTemplate< FeatureNumList > XmlElementFeatureNumList
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.
void WriteXML(const KKStr &varName, std::ostream &o) const
KKStr ToCommaDelStr() const
const kkuint16 * FeatureNums() const
FeatureNumList & operator=(FeatureNumList &&_features)
FeatureNumList & operator-=(kkuint16 rightSide)
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163