FeatureMatrixT.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 * > & _features,
  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. T getLargestValue ( const bool & _getTransformedValue = false ) const;
  254. NICE::VectorT<T> getLargestValuePerDimension ( const double & _quantile=1.0,
  255. const bool & _getTransformedValue = false
  256. ) const;
  257. //------------------------------------------------------
  258. // high level methods
  259. //------------------------------------------------------
  260. /**
  261. * @brief apply a parameterized function to the feature matrix
  262. * @author Alexander Freytag
  263. * @date 04-05-2012 (dd-mm-yyyy)
  264. *
  265. * @param pf the parameterized function (optional), if not given, nothing will be done
  266. */
  267. void applyFunctionToFeatureMatrix ( const NICE::ParameterizedFunction *_pf = NULL );
  268. /**
  269. * @brief Computes the ratio of sparsity across the matrix
  270. * @author Alexander Freytag
  271. * @date 11-01-2012 (dd-mm-yyyy)
  272. */
  273. double computeSparsityRatio() const;
  274. /**
  275. * @brief add a new feature and insert its elements in the already ordered structure
  276. * @author Alexander Freytag
  277. * @date 07-12-2011 (dd-mm-yyyy)
  278. */
  279. void add_feature(const std::vector<T> & _feature,
  280. const NICE::ParameterizedFunction *_pf = NULL
  281. );
  282. /**
  283. * @brief add a new feature and insert its elements in the already ordered structure, will be casted to type T
  284. * @author Alexander Freytag
  285. * @date 25-04-2012 (dd-mm-yyyy)
  286. */
  287. void add_feature(const NICE::SparseVector & _feature,
  288. const NICE::ParameterizedFunction *_pf = NULL
  289. );
  290. /**
  291. * @brief add several new features and insert their elements in the already ordered structure
  292. * @author Alexander Freytag
  293. * @date 07-12-2011 (dd-mm-yyyy)
  294. */
  295. void add_features(const std::vector<std::vector<T> > & _features );
  296. /**
  297. * @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
  298. * @author Alexander Freytag
  299. * @date 07-12-2011 (dd-mm-yyyy)
  300. */
  301. void set_features(const std::vector<std::vector<T> > & _features,
  302. std::vector<std::vector<uint> > & _permutations,
  303. const uint & _dim = 0
  304. );
  305. void set_features(const std::vector<std::vector<T> > & _features,
  306. std::vector<std::map<uint,uint> > & _permutations,
  307. const uint & _dim = 0
  308. );
  309. void set_features(const std::vector<std::vector<T> > & _features,
  310. const uint & _dim = 0
  311. );
  312. void set_features(const std::vector< const NICE::SparseVector * > & _features,
  313. const bool _dimensionsOverExamples = false,
  314. const uint & _dim = 0
  315. );
  316. /**
  317. * @brief get a permutation vector for each dimension
  318. *
  319. * @param resulting permutation matrix
  320. */
  321. void getPermutations( std::vector<std::vector<uint> > & _permutations) const;
  322. void getPermutations( std::vector<std::map<uint,uint> > & _permutations) const;
  323. /**
  324. * @brief Prints the whole Matrix (outer loop over dimension, inner loop over features)
  325. * @author Alexander Freytag
  326. * @date 07-12-2011 (dd-mm-yyyy)
  327. */
  328. void print(std::ostream & _os) const;
  329. /**
  330. * @brief Computes the whole non-sparse matrix. WARNING: this may result in a really memory-consuming data-structure!
  331. * @author Alexander Freytag
  332. * @date 12-01-2012 (dd-mm-yyyy)
  333. */
  334. void computeNonSparseMatrix(NICE::MatrixT<T> & _matrix,
  335. bool _transpose = false
  336. ) const;
  337. /**
  338. * @brief Computes the whole non-sparse matrix. WARNING: this may result in a really memory-consuming data-structure!
  339. * @author Alexander Freytag
  340. * @date 12-01-2012 (dd-mm-yyyy)
  341. */
  342. void computeNonSparseMatrix(std::vector<std::vector<T> > & _matrix,
  343. bool _transpose = false
  344. ) const;
  345. /**
  346. * @brief Swaps to specified elements, performing a validity check
  347. * @author Alexander Freytag
  348. * @date 08-12-2011 (dd-mm-yyyy)
  349. */
  350. void swap(const uint & _row1,
  351. const uint & _col1,
  352. const uint & _row2,
  353. const uint & _col2
  354. );
  355. /**
  356. * @brief Swaps to specified elements, without performing a validity check
  357. * @author Alexander Freytag
  358. * @date 08-12-2011 (dd-mm-yyyy)
  359. */
  360. void swapUnsafe(const uint & _row1,
  361. const uint & _col1,
  362. const uint & _row2,
  363. const uint & _col2
  364. );
  365. /**
  366. * @brief direct access to elements
  367. *
  368. * @param dim feature index
  369. *
  370. * @return sorted feature values
  371. */
  372. const SortedVectorSparse<T> & getFeatureValues ( uint _dim ) const { return this->features[_dim]; };
  373. /**
  374. * @brief direct read/write access to elements
  375. *
  376. * @param dim feature index
  377. *
  378. * @return sorted feature values
  379. */
  380. SortedVectorSparse<T> & getFeatureValues ( uint _dim ) { return this->features[_dim]; };
  381. /**
  382. * @brief compute the diagonal elements of the HIK kernel matrix induced by the features
  383. *
  384. * @param diagonalElements resulting vector
  385. */
  386. void hikDiagonalElements( Vector & _diagonalElements ) const;
  387. /**
  388. * @brief Compute the trace of the HIK kernel matrix induced by the features
  389. *
  390. * @return value of the trace
  391. */
  392. double hikTrace() const;
  393. /**
  394. * @brief Return the number of nonzero elements in a specified dimension, that are currently stored in the feature matrix
  395. *
  396. * @return number of nonzero elements on the specified dimension
  397. */
  398. uint getNumberOfNonZeroElementsPerDimension(const uint & _dim) const;
  399. /**
  400. * @brief Return the number of zero elements in a specified dimension, that are currently stored in the feature matrix
  401. *
  402. * @return number of nonzero elements on the specified dimension
  403. */
  404. uint getNumberOfZeroElementsPerDimension(const uint & _dim) const;
  405. /** Persistent interface */
  406. virtual void restore ( std::istream & _is, int _format = 0 );
  407. virtual void store ( std::ostream & _os, int _format = 0 ) const;
  408. virtual void clear ( );
  409. };
  410. //! default definition for a FeatureMatrix
  411. typedef FeatureMatrixT<double> FeatureMatrix;
  412. typedef FeatureMatrixT<bool> BoolFeatureMatrix;
  413. typedef FeatureMatrixT<char> CharFeatureMatrix;
  414. typedef FeatureMatrixT<int> IntFeatureMatrix;
  415. typedef FeatureMatrixT<float> FloatFeatureMatrix;
  416. } // namespace
  417. #ifdef __GNUC__
  418. #include "gp-hik-core/FeatureMatrixT.tcc"
  419. #endif
  420. #endif