FeatureMatrixT.h 14 KB

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