FeatureMatrixT.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * @file FeatureMatrixT.h
  3. * @brief A feature matrix, storing (sparse) features sorted per dimension (Interface)
  4. * @author Alexander Freytag
  5. * @date 07-12-2011 (dd-mm-yyyy)
  6. */
  7. #ifndef FEATUREMATRIXINCLUDE
  8. #define FEATUREMATRIXINCLUDE
  9. #include <vector>
  10. #include <set>
  11. #include <map>
  12. #include <iostream>
  13. #include <limits>
  14. #include <core/basics/Exception.h>
  15. #include "core/basics/Persistent.h"
  16. #include <core/vector/MatrixT.h>
  17. #include <core/vector/SparseVectorT.h>
  18. #ifdef NICE_USELIB_MATIO
  19. #include <core/matlabAccess/MatFileIO.h>
  20. #endif
  21. #include "SortedVectorSparse.h"
  22. #include "gp-hik-core/parameterizedFunctions/ParameterizedFunction.h"
  23. namespace NICE {
  24. /**
  25. * @class FeatureMatrixT
  26. * @brief A feature matrix, storing (sparse) features sorted per dimension
  27. * @author Alexander Freytag
  28. */
  29. template<class T> class FeatureMatrixT : NICE::Persistent
  30. {
  31. protected:
  32. int n;
  33. int d;
  34. std::vector<NICE::SortedVectorSparse<T> > features;
  35. //! verbose flag for output after calling the restore-function
  36. bool verbose;
  37. //! debug flag for output during debugging
  38. bool debug;
  39. public:
  40. //! STL-like typedef for type of elements
  41. typedef T value_type;
  42. //! STL-like typedef for const element reference
  43. typedef const T& const_reference;
  44. //! STL-like typedef for iterator
  45. typedef T* iterator;
  46. //! STL-like typedef for const iterator
  47. typedef const T* const_iterator;
  48. //! STL-like typedef for element reference
  49. typedef T& reference;
  50. //------------------------------------------------------
  51. // several constructors and destructors
  52. //------------------------------------------------------
  53. /**
  54. * @brief Default constructor
  55. * @author Alexander Freytag
  56. * @date 07-12-2011 (dd-mm-yyyy)
  57. */
  58. FeatureMatrixT();
  59. /**
  60. * @brief Recommended constructor
  61. * @author Alexander Freytag
  62. * @date 07-12-2011 (dd-mm-yyyy)
  63. */
  64. FeatureMatrixT(const std::vector<std::vector<T> > & _features, const int & _dim = -1);
  65. #ifdef NICE_USELIB_MATIO
  66. /**
  67. * @brief Constructor reading data from matlab-files
  68. * @author Alexander Freytag
  69. * @date 10-01-2012 (dd-mm-yyyy)
  70. */
  71. FeatureMatrixT(const sparse_t & _features, const int & _dim = -1);//, const int & nrFeatures);
  72. #endif
  73. /** just another constructor for sparse features */
  74. FeatureMatrixT(const std::vector< SparseVector * > & X, const bool dimensionsOverExamples = false, const int & _dim = -1);
  75. #ifdef NICE_USELIB_MATIO
  76. /**
  77. * @brief Constructor reading data from matlab-files and providing the possibility to
  78. * restrict the number of examples to a certain subset
  79. *
  80. * @param _features sparse data matrix (sett MatFileIO)
  81. * @param examples set of example indices
  82. */
  83. FeatureMatrixT(const sparse_t & _features, const std::map<int, int> & examples , const int & _dim = -1);
  84. #endif
  85. /**
  86. * @brief Default destructor
  87. * @author Alexander Freytag
  88. * @date 07-12-2011 (dd-mm-yyyy)
  89. */
  90. ~FeatureMatrixT();
  91. //------------------------------------------------------
  92. // several get and set methods including access operators
  93. //------------------------------------------------------
  94. /**
  95. * @brief Get number of examples
  96. * @author Alexander Freytag
  97. * @date 07-12-2011 (dd-mm-yyyy)
  98. */
  99. int get_n() const;
  100. /**
  101. * @brief Get number of dimensions
  102. * @author Alexander Freytag
  103. * @date 07-12-2011 (dd-mm-yyyy)
  104. */
  105. int get_d() const;
  106. /**
  107. * @brief Sets the given dimension and re-sizes internal data structure. WARNING: this will completely remove your current data!
  108. * @author Alexander Freytag
  109. * @date 06-12-2011 (dd-mm-yyyy)
  110. */
  111. void set_d(const int & _d);
  112. /** set verbose flag used for restore-functionality*/
  113. void setVerbose( const bool & _verbose);
  114. bool getVerbose( ) const;
  115. /** set debug flag used for debug output*/
  116. void setDebug( const bool & _debug);
  117. bool getDebug( ) const;
  118. /**
  119. * @brief Compare F with this
  120. * @author Alexander Freytag
  121. * @date 05-01-2012 (dd-mm-yyyy)
  122. * @pre Dimensions of \c F and \c this must be equal
  123. * @param F data to compare with
  124. * @return true if \c F and \c this are equal
  125. */
  126. inline bool operator==(const FeatureMatrixT<T> & F) const;
  127. /**
  128. * @brief Compare \c F with \c this.
  129. * @author Alexander Freytag
  130. * @date 05-01-2012 (dd-mm-yyyy)
  131. * @pre Size of \c F and \c this must be equal
  132. * @param F data to compare with
  133. * @return true if \c F and \c this are not equal
  134. */
  135. inline bool operator!= (const FeatureMatrixT<T> & F) const;
  136. /**
  137. * @brief Copy data from \c F to \c this.
  138. * @author Alexander Freytag
  139. * @date 05-01-2012 (dd-mm-yyyy)
  140. * @param v New data
  141. * @return \c *this
  142. */
  143. inline FeatureMatrixT<T>& operator=(const FeatureMatrixT<T> & F);
  144. /**
  145. * @brief Matrix-like operator for element access, performs validity check
  146. * @author Alexander Freytag
  147. * @date 07-12-2011 (dd-mm-yyyy)
  148. */
  149. inline T operator()(const int row, const int col) const;
  150. /**
  151. * @brief Element access without validity check
  152. * @author Alexander Freytag
  153. * @date 08-12-2011 (dd-mm-yyyy)
  154. */
  155. inline T getUnsafe(const int row, const int col) const;
  156. /**
  157. * @brief Element access of original values without validity check
  158. * @author Erik Rodner
  159. */
  160. inline T getOriginal(const int row, const int col) const;
  161. /**
  162. * @brief Sets a specified element to the given value, performs validity check
  163. * @author Alexander Freytag
  164. * @date 07-12-2011 (dd-mm-yyyy)
  165. */
  166. inline void set (const int row, const int col, const T & newElement, bool setTransformedValue = false);
  167. /**
  168. * @brief Sets a specified element to the given value, without validity check
  169. * @author Alexander Freytag
  170. * @date 08-12-2011 (dd-mm-yyyy)
  171. */
  172. inline void setUnsafe (const int row, const int col, const T & newElement, bool setTransformedValue = false);
  173. /**
  174. * @brief Access to all element entries of a specified dimension, including validity check
  175. * @author Alexander Freytag
  176. * @date 08-12-2011 (dd-mm-yyyy)
  177. */
  178. void getDimension(const int & dim, NICE::SortedVectorSparse<T> & dimension) const;
  179. /**
  180. * @brief Access to all element entries of a specified dimension, without validity check
  181. * @author Alexander Freytag
  182. * @date 08-12-2011 (dd-mm-yyyy)
  183. */
  184. void getDimensionUnsafe(const int & dim, NICE::SortedVectorSparse<T> & dimension) const;
  185. /**
  186. * @brief Finds the first element in a given dimension, which equals elem (orig feature value, not the transformed one)
  187. * @author Alexander Freytag
  188. * @date 08-12-2011 (dd-mm-yyyy)
  189. */
  190. void findFirstInDimension(const int & dim, const T & elem, int & position) const;
  191. /**
  192. * @brief Finds the last element in a given dimension, which equals elem (orig feature value, not the transformed one)
  193. * @author Alexander Freytag
  194. * @date 08-12-2011 (dd-mm-yyyy)1
  195. */
  196. void findLastInDimension(const int & dim, const T & elem, int & position) const;
  197. /**
  198. * @brief Finds the first element in a given dimension, which is larger as elem (orig feature value, not the transformed one)
  199. * @author Alexander Freytag
  200. * @date 08-12-2011 (dd-mm-yyyy)
  201. */
  202. void findFirstLargerInDimension(const int & dim, const T & elem, int & position) const;
  203. /**
  204. * @brief Finds the last element in a given dimension, which is smaller as elem (orig feature value, not the transformed one)
  205. * @author Alexander Freytag
  206. * @date 08-12-2011 (dd-mm-yyyy)
  207. */
  208. void findLastSmallerInDimension(const int & dim, const T & elem, int & position) const;
  209. //------------------------------------------------------
  210. // high level methods
  211. //------------------------------------------------------
  212. /**
  213. * @brief apply a parameterized function to the feature matrix
  214. * @author Alexander Freytag
  215. * @date 04-05-2012 (dd-mm-yyyy)
  216. *
  217. * @param pf the parameterized function (optional), if not given, nothing will be done
  218. */
  219. void applyFunctionToFeatureMatrix ( const NICE::ParameterizedFunction *pf = NULL );
  220. /**
  221. * @brief Computes the ratio of sparsity across the matrix
  222. * @author Alexander Freytag
  223. * @date 11-01-2012 (dd-mm-yyyy)
  224. */
  225. double computeSparsityRatio();
  226. /**
  227. * @brief add a new feature and insert its elements in the already ordered structure
  228. * @author Alexander Freytag
  229. * @date 07-12-2011 (dd-mm-yyyy)
  230. */
  231. void add_feature(const std::vector<T> & feature, const NICE::ParameterizedFunction *pf = NULL);
  232. /**
  233. * @brief add a new feature and insert its elements in the already ordered structure, will be casted to type T
  234. * @author Alexander Freytag
  235. * @date 25-04-2012 (dd-mm-yyyy)
  236. */
  237. void add_feature(const NICE::SparseVector & feature, const NICE::ParameterizedFunction *pf = NULL);
  238. /**
  239. * @brief add several new features and insert their elements in the already ordered structure
  240. * @author Alexander Freytag
  241. * @date 07-12-2011 (dd-mm-yyyy)
  242. */
  243. void add_features(const std::vector<std::vector<T> > & _features );
  244. /**
  245. * @brief set the stored features to new values - which means deleting the old things and inserting the new ones. Return resulting permutation according to each dimension
  246. * @author Alexander Freytag
  247. * @date 07-12-2011 (dd-mm-yyyy)
  248. */
  249. void set_features(const std::vector<std::vector<T> > & _features, std::vector<std::vector<int> > & permutations, const int & _dim = -1);
  250. void set_features(const std::vector<std::vector<T> > & _features, std::vector<std::map<int,int> > & permutations, const int & _dim = -1);
  251. void set_features(const std::vector<std::vector<T> > & _features, const int & _dim = -1);
  252. void set_features(const std::vector< NICE::SparseVector * > & _features, const bool dimensionsOverExamples = false, const int & _dim = -1);
  253. /**
  254. * @brief get a permutation vector for each dimension
  255. *
  256. * @param resulting permutation matrix
  257. */
  258. void getPermutations( std::vector<std::vector<int> > & permutations) const;
  259. void getPermutations( std::vector<std::map<int,int> > & permutations) const;
  260. /**
  261. * @brief Prints the whole Matrix (outer loop over dimension, inner loop over features)
  262. * @author Alexander Freytag
  263. * @date 07-12-2011 (dd-mm-yyyy)
  264. */
  265. void print(std::ostream & os) const;
  266. /**
  267. * @brief Computes the whole non-sparse matrix. WARNING: this may result in a really memory-consuming data-structure!
  268. * @author Alexander Freytag
  269. * @date 12-01-2012 (dd-mm-yyyy)
  270. */
  271. void computeNonSparseMatrix(NICE::MatrixT<T> & matrix, bool transpose = false) const;
  272. /**
  273. * @brief Computes the whole non-sparse matrix. WARNING: this may result in a really memory-consuming data-structure!
  274. * @author Alexander Freytag
  275. * @date 12-01-2012 (dd-mm-yyyy)
  276. */
  277. void computeNonSparseMatrix(std::vector<std::vector<T> > & matrix, bool transpose = false) const;
  278. /**
  279. * @brief Swaps to specified elements, performing a validity check
  280. * @author Alexander Freytag
  281. * @date 08-12-2011 (dd-mm-yyyy)
  282. */
  283. void swap(const int & row1, const int & col1, const int & row2, const int & col2);
  284. /**
  285. * @brief Swaps to specified elements, without performing a validity check
  286. * @author Alexander Freytag
  287. * @date 08-12-2011 (dd-mm-yyyy)
  288. */
  289. void swapUnsafe(const int & row1, const int & col1, const int & row2, const int & col2);
  290. /**
  291. * @brief direct access to elements
  292. *
  293. * @param dim feature index
  294. *
  295. * @return sorted feature values
  296. */
  297. const SortedVectorSparse<T> & getFeatureValues ( int dim ) const { return features[dim]; };
  298. /**
  299. * @brief direct read/write access to elements
  300. *
  301. * @param dim feature index
  302. *
  303. * @return sorted feature values
  304. */
  305. SortedVectorSparse<T> & getFeatureValues ( int dim ) { return features[dim]; };
  306. /**
  307. * @brief compute the diagonal elements of the HIK kernel matrix induced by the features
  308. *
  309. * @param diagonalElements resulting vector
  310. */
  311. void hikDiagonalElements( Vector & diagonalElements ) const;
  312. /**
  313. * @brief Compute the trace of the HIK kernel matrix induced by the features
  314. *
  315. * @return value of the trace
  316. */
  317. double hikTrace() const;
  318. /**
  319. * @brief Return the number of nonzero elements in a specified dimension, that are currently stored in the feature matrix
  320. *
  321. * @return number of nonzero elements on the specified dimension
  322. */
  323. int getNumberOfNonZeroElementsPerDimension(const int & dim) const;
  324. /**
  325. * @brief Return the number of zero elements in a specified dimension, that are currently stored in the feature matrix
  326. *
  327. * @return number of nonzero elements on the specified dimension
  328. */
  329. int getNumberOfZeroElementsPerDimension(const int & dim) const;
  330. /** Persistent interface */
  331. virtual void restore ( std::istream & is, int format = 0 );
  332. virtual void store ( std::ostream & os, int format = 0 ) const;
  333. virtual void clear ( );
  334. };
  335. //! default definition for a FeatureMatrix
  336. typedef FeatureMatrixT<double> FeatureMatrix;
  337. typedef FeatureMatrixT<bool> BoolFeatureMatrix;
  338. typedef FeatureMatrixT<char> CharFeatureMatrix;
  339. typedef FeatureMatrixT<int> IntFeatureMatrix;
  340. typedef FeatureMatrixT<float> FloatFeatureMatrix;
  341. } // namespace
  342. #ifdef __GNUC__
  343. #include "gp-hik-core/FeatureMatrixT.tcc"
  344. #endif
  345. #endif