FeatureMatrixT.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. uint ui_n;
  38. uint ui_d;
  39. std::vector<NICE::SortedVectorSparse<T> > features;
  40. //! verbose flag for output after calling the restore-function
  41. bool b_verbose;
  42. //! debug flag for output during debugging
  43. bool b_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,
  70. const uint & _dim = 0
  71. );
  72. #ifdef NICE_USELIB_MATIO
  73. /**
  74. * @brief Constructor reading data from matlab-files
  75. * @author Alexander Freytag
  76. * @date 10-01-2012 (dd-mm-yyyy)
  77. */
  78. FeatureMatrixT(const sparse_t & _features,
  79. const uint & _dim = 0
  80. );//, const int & nrFeatures);
  81. #endif
  82. /** just another constructor for sparse features */
  83. FeatureMatrixT(const std::vector< const NICE::SparseVector * > & _X,
  84. const bool _dimensionsOverExamples = false,
  85. const uint & _dim = 0
  86. );
  87. #ifdef NICE_USELIB_MATIO
  88. /**
  89. * @brief Constructor reading data from matlab-files and providing the possibility to
  90. * restrict the number of examples to a certain subset
  91. *
  92. * @param _features sparse data matrix (sett MatFileIO)
  93. * @param examples set of example indices
  94. */
  95. FeatureMatrixT(const sparse_t & _features,
  96. const std::map<uint, uint> & _examples ,
  97. const uint & _dim = 0);
  98. #endif
  99. /**
  100. * @brief Default destructor
  101. * @author Alexander Freytag
  102. * @date 07-12-2011 (dd-mm-yyyy)
  103. */
  104. ~FeatureMatrixT();
  105. //------------------------------------------------------
  106. // several get and set methods including access operators
  107. //------------------------------------------------------
  108. /**
  109. * @brief Get number of examples
  110. * @author Alexander Freytag
  111. * @date 07-12-2011 (dd-mm-yyyy)
  112. */
  113. uint get_n() const;
  114. /**
  115. * @brief Get number of dimensions
  116. * @author Alexander Freytag
  117. * @date 07-12-2011 (dd-mm-yyyy)
  118. */
  119. uint get_d() const;
  120. /**
  121. * @brief Sets the given dimension and re-sizes internal data structure. WARNING: this will completely remove your current data!
  122. * @author Alexander Freytag
  123. * @date 06-12-2011 (dd-mm-yyyy)
  124. */
  125. void set_d(const uint & _d);
  126. /** set verbose flag used for restore-functionality*/
  127. void setVerbose( const bool & _verbose);
  128. bool getVerbose( ) const;
  129. /** set debug flag used for debug output*/
  130. void setDebug( const bool & _debug);
  131. bool getDebug( ) const;
  132. /**
  133. * @brief Compare F with this
  134. * @author Alexander Freytag
  135. * @date 05-01-2012 (dd-mm-yyyy)
  136. * @pre Dimensions 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 equal
  139. */
  140. inline bool operator==(const FeatureMatrixT<T> & _F) const;
  141. /**
  142. * @brief Compare \c F with \c this.
  143. * @author Alexander Freytag
  144. * @date 05-01-2012 (dd-mm-yyyy)
  145. * @pre Size of \c F and \c this must be equal
  146. * @param F data to compare with
  147. * @return true if \c F and \c this are not equal
  148. */
  149. inline bool operator!= (const FeatureMatrixT<T> & _F) const;
  150. /**
  151. * @brief Copy data from \c F to \c this.
  152. * @author Alexander Freytag
  153. * @date 05-01-2012 (dd-mm-yyyy)
  154. * @param v New data
  155. * @return \c *this
  156. */
  157. inline FeatureMatrixT<T>& operator=(const FeatureMatrixT<T> & _F);
  158. /**
  159. * @brief Matrix-like operator for element access, performs validity check
  160. * @author Alexander Freytag
  161. * @date 07-12-2011 (dd-mm-yyyy)
  162. */
  163. inline T operator()(const uint _row,
  164. const uint _col
  165. ) const;
  166. /**
  167. * @brief Element access without validity check
  168. * @author Alexander Freytag
  169. * @date 08-12-2011 (dd-mm-yyyy)
  170. */
  171. inline T getUnsafe(const uint _row,
  172. const uint _col
  173. ) const;
  174. /**
  175. * @brief Element access of original values without validity check
  176. * @author Erik Rodner
  177. */
  178. inline T getOriginal(const uint _row,
  179. const uint _col
  180. ) const;
  181. /**
  182. * @brief Sets a specified element to the given value, performs validity check
  183. * @author Alexander Freytag
  184. * @date 07-12-2011 (dd-mm-yyyy)
  185. */
  186. inline void set (const uint _row,
  187. const uint _col,
  188. const T & _newElement,
  189. bool _setTransformedValue = false
  190. );
  191. /**
  192. * @brief Sets a specified element to the given value, without validity check
  193. * @author Alexander Freytag
  194. * @date 08-12-2011 (dd-mm-yyyy)
  195. */
  196. inline void setUnsafe (const uint _row,
  197. const uint _col,
  198. const T & _newElement,
  199. bool _setTransformedValue = false
  200. );
  201. /**
  202. * @brief Access to all element entries of a specified dimension, including validity check
  203. * @author Alexander Freytag
  204. * @date 08-12-2011 (dd-mm-yyyy)
  205. */
  206. void getDimension(const uint & _dim,
  207. NICE::SortedVectorSparse<T> & _dimension
  208. ) const;
  209. /**
  210. * @brief Access to all element entries of a specified dimension, without validity check
  211. * @author Alexander Freytag
  212. * @date 08-12-2011 (dd-mm-yyyy)
  213. */
  214. void getDimensionUnsafe(const uint & _dim,
  215. NICE::SortedVectorSparse<T> & _dimension
  216. ) const;
  217. /**
  218. * @brief Finds the first element in a given dimension, which equals elem (orig feature value, not the transformed one)
  219. * @author Alexander Freytag
  220. * @date 08-12-2011 (dd-mm-yyyy)
  221. */
  222. void findFirstInDimension(const uint & _dim,
  223. const T & _elem,
  224. uint & _position
  225. ) const;
  226. /**
  227. * @brief Finds the last element in a given dimension, which equals elem (orig feature value, not the transformed one)
  228. * @author Alexander Freytag
  229. * @date 08-12-2011 (dd-mm-yyyy)1
  230. */
  231. void findLastInDimension(const uint & _dim,
  232. const T & _elem,
  233. uint & _position
  234. ) const;
  235. /**
  236. * @brief Finds the first element in a given dimension, which is larger as elem (orig feature value, not the transformed one)
  237. * @author Alexander Freytag
  238. * @date 08-12-2011 (dd-mm-yyyy)
  239. */
  240. void findFirstLargerInDimension(const uint & _dim,
  241. const T & elem,
  242. uint & position
  243. ) const;
  244. /**
  245. * @brief Finds the last element in a given dimension, which is smaller as elem (orig feature value, not the transformed one)
  246. * @author Alexander Freytag
  247. * @date 08-12-2011 (dd-mm-yyyy)
  248. */
  249. void findLastSmallerInDimension(const uint & _dim,
  250. const T & _elem,
  251. uint & _position
  252. ) const;
  253. //------------------------------------------------------
  254. // high level methods
  255. //------------------------------------------------------
  256. /**
  257. * @brief apply a parameterized function to the feature matrix
  258. * @author Alexander Freytag
  259. * @date 04-05-2012 (dd-mm-yyyy)
  260. *
  261. * @param pf the parameterized function (optional), if not given, nothing will be done
  262. */
  263. void applyFunctionToFeatureMatrix ( const NICE::ParameterizedFunction *_pf = NULL );
  264. /**
  265. * @brief Computes the ratio of sparsity across the matrix
  266. * @author Alexander Freytag
  267. * @date 11-01-2012 (dd-mm-yyyy)
  268. */
  269. double computeSparsityRatio() const;
  270. /**
  271. * @brief add a new feature and insert its elements in the already ordered structure
  272. * @author Alexander Freytag
  273. * @date 07-12-2011 (dd-mm-yyyy)
  274. */
  275. void add_feature(const std::vector<T> & _feature,
  276. const NICE::ParameterizedFunction *_pf = NULL
  277. );
  278. /**
  279. * @brief add a new feature and insert its elements in the already ordered structure, will be casted to type T
  280. * @author Alexander Freytag
  281. * @date 25-04-2012 (dd-mm-yyyy)
  282. */
  283. void add_feature(const NICE::SparseVector & _feature,
  284. const NICE::ParameterizedFunction *_pf = NULL
  285. );
  286. /**
  287. * @brief add several new features and insert their elements in the already ordered structure
  288. * @author Alexander Freytag
  289. * @date 07-12-2011 (dd-mm-yyyy)
  290. */
  291. void add_features(const std::vector<std::vector<T> > & _features );
  292. /**
  293. * @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
  294. * @author Alexander Freytag
  295. * @date 07-12-2011 (dd-mm-yyyy)
  296. */
  297. void set_features(const std::vector<std::vector<T> > & _features,
  298. std::vector<std::vector<uint> > & _permutations,
  299. const uint & _dim = 0
  300. );
  301. void set_features(const std::vector<std::vector<T> > & _features,
  302. std::vector<std::map<uint,uint> > & _permutations,
  303. const uint & _dim = 0
  304. );
  305. void set_features(const std::vector<std::vector<T> > & _features,
  306. const uint & _dim = 0
  307. );
  308. void set_features(const std::vector< const NICE::SparseVector * > & _features,
  309. const bool _dimensionsOverExamples = false,
  310. const uint & _dim = 0
  311. );
  312. /**
  313. * @brief get a permutation vector for each dimension
  314. *
  315. * @param resulting permutation matrix
  316. */
  317. void getPermutations( std::vector<std::vector<uint> > & _permutations) const;
  318. void getPermutations( std::vector<std::map<uint,uint> > & _permutations) const;
  319. /**
  320. * @brief Prints the whole Matrix (outer loop over dimension, inner loop over features)
  321. * @author Alexander Freytag
  322. * @date 07-12-2011 (dd-mm-yyyy)
  323. */
  324. void print(std::ostream & _os) const;
  325. /**
  326. * @brief Computes the whole non-sparse matrix. WARNING: this may result in a really memory-consuming data-structure!
  327. * @author Alexander Freytag
  328. * @date 12-01-2012 (dd-mm-yyyy)
  329. */
  330. void computeNonSparseMatrix(NICE::MatrixT<T> & _matrix,
  331. bool _transpose = false
  332. ) const;
  333. /**
  334. * @brief Computes the whole non-sparse matrix. WARNING: this may result in a really memory-consuming data-structure!
  335. * @author Alexander Freytag
  336. * @date 12-01-2012 (dd-mm-yyyy)
  337. */
  338. void computeNonSparseMatrix(std::vector<std::vector<T> > & _matrix,
  339. bool _transpose = false
  340. ) const;
  341. /**
  342. * @brief Swaps to specified elements, performing a validity check
  343. * @author Alexander Freytag
  344. * @date 08-12-2011 (dd-mm-yyyy)
  345. */
  346. void swap(const uint & _row1,
  347. const uint & _col1,
  348. const uint & _row2,
  349. const uint & _col2
  350. );
  351. /**
  352. * @brief Swaps to specified elements, without performing a validity check
  353. * @author Alexander Freytag
  354. * @date 08-12-2011 (dd-mm-yyyy)
  355. */
  356. void swapUnsafe(const uint & _row1,
  357. const uint & _col1,
  358. const uint & _row2,
  359. const uint & _col2
  360. );
  361. /**
  362. * @brief direct access to elements
  363. *
  364. * @param dim feature index
  365. *
  366. * @return sorted feature values
  367. */
  368. const SortedVectorSparse<T> & getFeatureValues ( uint _dim ) const { return this->features[_dim]; };
  369. /**
  370. * @brief direct read/write access to elements
  371. *
  372. * @param dim feature index
  373. *
  374. * @return sorted feature values
  375. */
  376. SortedVectorSparse<T> & getFeatureValues ( uint _dim ) { return this->features[_dim]; };
  377. /**
  378. * @brief compute the diagonal elements of the HIK kernel matrix induced by the features
  379. *
  380. * @param diagonalElements resulting vector
  381. */
  382. void hikDiagonalElements( Vector & _diagonalElements ) const;
  383. /**
  384. * @brief Compute the trace of the HIK kernel matrix induced by the features
  385. *
  386. * @return value of the trace
  387. */
  388. double hikTrace() const;
  389. /**
  390. * @brief Return the number of nonzero elements in a specified dimension, that are currently stored in the feature matrix
  391. *
  392. * @return number of nonzero elements on the specified dimension
  393. */
  394. uint getNumberOfNonZeroElementsPerDimension(const uint & _dim) const;
  395. /**
  396. * @brief Return the number of zero elements in a specified dimension, that are currently stored in the feature matrix
  397. *
  398. * @return number of nonzero elements on the specified dimension
  399. */
  400. uint getNumberOfZeroElementsPerDimension(const uint & _dim) const;
  401. /** Persistent interface */
  402. virtual void restore ( std::istream & _is, int _format = 0 );
  403. virtual void store ( std::ostream & _os, int _format = 0 ) const;
  404. virtual void clear ( );
  405. };
  406. //! default definition for a FeatureMatrix
  407. typedef FeatureMatrixT<double> FeatureMatrix;
  408. typedef FeatureMatrixT<bool> BoolFeatureMatrix;
  409. typedef FeatureMatrixT<char> CharFeatureMatrix;
  410. typedef FeatureMatrixT<int> IntFeatureMatrix;
  411. typedef FeatureMatrixT<float> FloatFeatureMatrix;
  412. } // namespace
  413. #ifdef __GNUC__
  414. #include "gp-hik-core/FeatureMatrixT.tcc"
  415. #endif
  416. #endif