KSquare Utilities
FeatureEncoder.h
Go to the documentation of this file.
1 #ifndef _FEATUREENCODER_
2 #define _FEATUREENCODER_
3 //***********************************************************************
4 //* FeatureEncoder *
5 //* *
6 //* *
7 //*---------------------------------------------------------------------*
8 //* History *
9 //* *
10 //* Date Programmer Description *
11 //* *
12 //* 2005-09-10 Kurt Kramer Removing Feature compression and encoding *
13 //* code from FeatureEncoder *
14 //* *
15 //***********************************************************************
16 #include "RunLog.h"
17 #include "XmlStream.h"
18 
19 #include "Attribute.h"
20 #include "FeatureVector.h"
21 #include "FileDesc.h"
22 #include "MLClass.h"
23 #include "svm.h"
24 #include "SVMparam.h"
25 
26 
27 namespace KKMLL
28 {
29 
30  #ifndef _FEATURENUMLIST_
31  class FeatureNumList;
33  #endif
34 
35 
36  typedef struct svm_node* XSpacePtr;
37 
38 
40 
42 
43 
45  {
46  public:
47  FeatureEncoder ();
48 
49  FeatureEncoder (FileDescPtr _fileDesc,
50  MLClassPtr _class1,
51  MLClassPtr _class2,
52  const FeatureNumList& _selectedFeatures,
53  SVM_EncodingMethod _encodingMethod,
54  double _c_Param
55  );
56 
57 
58  ~FeatureEncoder ();
59 
60 
61  kkint32 CodedNumOfFeatures () const {return codedNumOfFeatures;}
62 
63 
64  MLClassPtr Class1 () const {return class1;}
65  MLClassPtr Class2 () const {return class2;}
66 
67  /**
68  * @brief Left over from BitReduction days; removed all code except that which processed the NO bit reduction option.
69  */
70  void CompressExamples (FeatureVectorListPtr srcExamples,
71  FeatureVectorListPtr compressedExamples,
72  ClassAssignments& assignments
73  );
74 
75 
77 
78  FeatureVectorListPtr CreateEncodedFeatureVector (FeatureVectorList& srcData);
79 
80 
81  /**
82  *@brief Compresses 'src' examples, allocating new 'xSpace' data structure.
83  *@param[in] src Examples that are to be compressed
84  *@param[in] assignments Class Assignments
85  *@param[in] xSpace will allocate enough xSpace nodes and place compressed results in this structure.
86  *@param[out] totalxSpaceUsed number nodes used in xSpace</param>
87  *@param[out] prob Data Structure that is used by SVMLib
88  *@param[in] log
89  */
90  void EncodeIntoSparseMatrix (FeatureVectorListPtr src,
91  ClassAssignments& assignments,
92  XSpacePtr& xSpace,
93  kkint32& totalxSpaceUsed,
94  struct svm_problem& prob,
95  RunLog& log
96  );
97 
98  XSpacePtr EncodeAExample (FeatureVectorPtr example);
99 
100 
101  void EncodeAExample (FeatureVectorPtr example,
102  svm_node* xSpace,
103  kkint32& xSpaceUsed
104  );
105 
106 
107  FeatureVectorListPtr EncodeAllExamples (const FeatureVectorListPtr srcData);
108 
109 
110  FeatureVectorPtr EncodeAExample (FileDescPtr encodedFileDesc,
111  FeatureVectorPtr src
112  );
113 
115 
116  virtual
117  void ReadXML (XmlStream& s,
118  XmlTagConstPtr tag,
119  VolConstBool& cancelFlag,
120  RunLog& log
121  );
122 
123  virtual
124  void WriteXML (const KKStr& varName,
125  ostream& o
126  ) const;
127 
128  kkint32 XSpaceNeededPerExample () {return xSpaceNeededPerExample;}
129 
130 
131  private:
132  /**
133  * Computes the number of XSpace nodes that need to be allocated to represent the contents of a specified FeatureVectorList. Will
134  * account for features that have a value of 0; that is features that have a value of '0' do not need a xSpace node allocated for
135  * them.
136  */
137  kkint32 DetermineNumberOfNeededXspaceNodes (FeatureVectorListPtr src) const;
138 
139 
140 
141  kkint32* cardinalityDest;
142  MLClassPtr class1;
143  MLClassPtr class2;
144  kkint32 codedNumOfFeatures;
145  double c_Param;
146  kkint32* destFeatureNums;
147  FileDescPtr destFileDesc;
148  FeWhatToDoPtr destWhatToDo;
149  SVM_EncodingMethod encodingMethod;
150  FileDescPtr fileDesc;
151  kkint32 numEncodedFeatures;
152  kkint32 numOfFeatures;
153  FeatureNumList selectedFeatures;
154  kkint32* srcFeatureNums;
155  kkint32 xSpaceNeededPerExample;
156  }; /* FeatureEncoder */
157 
158 
159  typedef FeatureEncoder* FeatureEncoderPtr;
160 
161 
162 } /* KKMLL */
163 
164 #endif
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
kkint32 CodedNumOfFeatures() const
MLClass * MLClassPtr
Definition: MLClass.h:46
FeatureVectorListPtr CreateEncodedFeatureVector(FeatureVectorList &srcData)
__int32 kkint32
Definition: KKBaseTypes.h:88
Keeps track of selected features.
MLClassPtr Class2() const
MLClassPtr Class1() const
SVM_EncodingMethod
Definition: SVMparam.h:46
~FeatureEncoder()
Frees any memory allocated by, and owned by the FeatureEncoder.
FileDescPtr CreateEncodedFileDesc(ostream *o)
FeatureVectorListPtr EncodeAllExamples(const FeatureVectorListPtr srcData)
kkint32 XSpaceNeededPerExample()
Container class for FeatureVector derived objects.
FeatureNumList * FeatureNumListPtr
virtual void WriteXML(const KKStr &varName, ostream &o) const
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
XSpacePtr EncodeAExample(FeatureVectorPtr example)
Converts a single example into the svm_problem format.
Binds MLClass objects to the appropriate number that the Learning Algorithm expects.
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
void EncodeIntoSparseMatrix(FeatureVectorListPtr src, ClassAssignments &assignments, XSpacePtr &xSpace, kkint32 &totalxSpaceUsed, struct svm_problem &prob, RunLog &log)
Compresses &#39;src&#39; examples, allocating new &#39;xSpace&#39; data structure.
FileDesc * FileDescPtr
FeatureVectorPtr EncodeAExample(FileDescPtr encodedFileDesc, FeatureVectorPtr src)
void CompressExamples(FeatureVectorListPtr srcExamples, FeatureVectorListPtr compressedExamples, ClassAssignments &assignments)
Left over from BitReduction days; removed all code except that which processed the NO bit reduction o...
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)
FeatureEncoder(FileDescPtr _fileDesc, MLClassPtr _class1, MLClassPtr _class2, const FeatureNumList &_selectedFeatures, SVM_EncodingMethod _encodingMethod, double _c_Param)
Constructs a Feature Encoder object.
void EncodeAExample(FeatureVectorPtr example, svm_node *xSpace, kkint32 &xSpaceUsed)
Converts a single example into the svm_problem format.
kkint32 MemoryConsumedEstimated() const
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163
FeWhatToDo * FeWhatToDoPtr