FeatureMatrixT.tcc 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. /**
  2. * @file FeatureMatrixT.tcc
  3. * @brief A feature matrix, storing (sparse) features sorted per dimension (Implementation)
  4. * @author Alexander Freytag
  5. * @date 07-12-2011 (dd-mm-yyyy)
  6. */
  7. // #ifndef FEATUREMATRIX_TCC
  8. // #define FEATUREMATRIX_TCC
  9. // gp-hik-core includes
  10. #include "FeatureMatrixT.h"
  11. namespace NICE {
  12. //------------------------------------------------------
  13. // several constructors and destructors
  14. //------------------------------------------------------
  15. // Default constructor
  16. template <typename T>
  17. FeatureMatrixT<T>::FeatureMatrixT()
  18. {
  19. this->ui_n = 0;
  20. this->ui_d = 0;
  21. this->features.clear();
  22. this->b_verbose = false;
  23. this->b_debug = false;
  24. }
  25. // Recommended constructor
  26. template <typename T>
  27. FeatureMatrixT<T>::FeatureMatrixT(const std::vector<std::vector<T> > & _features,
  28. const uint & _dim
  29. )
  30. {
  31. this->ui_n = 0;
  32. if (_dim < 0)
  33. this->ui_d = (*_features.begin()).size();
  34. else
  35. this->ui_d = _dim;
  36. for (typename std::vector<std::vector<T> >::const_iterator it = _features.begin(); it != _features.end(); it++)
  37. {
  38. add_feature(*it);
  39. }
  40. this->b_verbose = false;
  41. this->b_debug = false;
  42. }
  43. //Constructor reading data from a vector of sparse vector pointers
  44. template <typename T>
  45. FeatureMatrixT<T>::
  46. FeatureMatrixT(const std::vector< const NICE::SparseVector * > & _X,
  47. const bool _dimensionsOverExamples,
  48. const uint & _dim
  49. )
  50. {
  51. this->features.clear();
  52. // resize our data structure
  53. //FIXME adapt these security checks to uint ...
  54. if (_dim >= 0) //did the user specified the number of dimensions?
  55. set_d( _dim );
  56. else //dimensions not specified by users
  57. {
  58. if (_dimensionsOverExamples) //do we have dim x examples ?
  59. {
  60. set_d( _X.size());
  61. }
  62. else //we have examples x dimes (as usually done)
  63. {
  64. if ( _X.size() > 0) //and have at least one example
  65. set_d( _X[0]->getDim() );
  66. else //no example, so set the dim to 0, since we have no idea at all
  67. {
  68. set_d(0);
  69. }
  70. }
  71. }
  72. // set number of examples n
  73. if ( this->ui_d > 0 )
  74. {
  75. if (_dimensionsOverExamples) //do we have dim x examples ?
  76. this->ui_n = _X[0]->getDim(); //NOTE Pay attention: we assume, that this number is set!
  77. else //we have examples x dimes (as usually done)
  78. this->ui_n = _X.size();
  79. }
  80. // insert all values
  81. if (_dimensionsOverExamples) //do we have dim x examples ?
  82. {
  83. for (uint dim = 0; dim < this->ui_d; dim++)
  84. {
  85. this->features[dim].insert( _X[dim] );
  86. }
  87. }
  88. else //we have examples x dimes (as usually done)
  89. {
  90. //loop over every example to add its content
  91. for (uint nr = 0; nr < this->ui_n; nr++)
  92. {
  93. //loop over every dimension to add the specific value to the corresponding SortedVectorSparse
  94. for (NICE::SparseVector::const_iterator elemIt = _X[nr]->begin(); elemIt != _X[nr]->end(); elemIt++)
  95. {
  96. //elemIt->first: dim, elemIt->second: value
  97. this->features[elemIt->first].insert( (T) elemIt->second, nr);
  98. }//for non-zero-values of the feature
  99. }//for every new feature
  100. }//if dimOverEx
  101. //set n for the internal data structure SortedVectorSparse
  102. for (typename std::vector<NICE::SortedVectorSparse<T> >::iterator it = this->features.begin(); it != this->features.end(); it++)
  103. (*it).setN( this->ui_n );
  104. }
  105. #ifdef NICE_USELIB_MATIO
  106. //Constructor reading data from matlab-files
  107. template <typename T>
  108. FeatureMatrixT<T>::
  109. FeatureMatrixT(const sparse_t & _features,
  110. const uint & _dim
  111. )
  112. {
  113. if (_dim < 0)
  114. set_d( _features.njc -1 );
  115. else
  116. set_d( _dim );
  117. uint nMax(0);
  118. for ( uint i = 0; i < _features.njc-1; i++ ) //walk over dimensions
  119. {
  120. for ( uint j = _features.jc[i]; j < _features.jc[i+1] && j < _features.ndata; j++ ) //walk over single features, which are sparsely represented
  121. {
  122. this->features[i].insert(((T*)_features.data)[j], _features.ir[ j]);
  123. if ((_features.ir[ j])>nMax)
  124. nMax = _features.ir[ j];
  125. }
  126. }
  127. for (typename std::vector<NICE::SortedVectorSparse<T> >::iterator it = this->features.begin(); it != this->features.end(); it++)
  128. {
  129. (*it).setN(nMax+1);
  130. }
  131. this->ui_n = nMax+1;
  132. this->b_verbose = false;
  133. }
  134. //Constructor reading data from matlab-files
  135. template <typename T>
  136. FeatureMatrixT<T>::
  137. FeatureMatrixT(const sparse_t & _features,
  138. const std::map<uint, uint> & _examples,
  139. const uint & _dim)
  140. {
  141. if (_dim < 0)
  142. set_d(_features.njc -1);
  143. else
  144. set_d(_dim);
  145. uint nMax(0);
  146. for ( uint i = 0; i < _features.njc-1; i++ ) //walk over dimensions
  147. {
  148. for ( uint j = _features.jc[i]; j < _features.jc[i+1] && j < _features.ndata; j++ ) //walk over single features, which are sparsely represented
  149. {
  150. uint example_index = _features.ir[ j];
  151. std::map<uint, uint>::const_iterator it = examples.find(example_index);
  152. if ( it != examples.end() ) {
  153. this->features[i].insert(((T*)_features.data)[j], it->second /* new index */);
  154. if (it->second > nMax)
  155. nMax = it->second;
  156. }
  157. }
  158. }
  159. for (typename std::vector<NICE::SortedVectorSparse<T> >::iterator it = this->features.begin(); it != this->features.end(); it++)
  160. (*it).setN(nMax+1);
  161. this->ui_n = nMax+1;
  162. this->b_verbose = false;
  163. }
  164. #endif
  165. // Default destructor
  166. template <typename T>
  167. FeatureMatrixT<T>::~FeatureMatrixT()
  168. {
  169. }
  170. //------------------------------------------------------
  171. // several get and set methods including access operators
  172. //------------------------------------------------------
  173. // Get number of examples
  174. template <typename T>
  175. uint FeatureMatrixT<T>::get_n() const
  176. {
  177. return this->ui_n;
  178. }
  179. // Get number of dimensions
  180. template <typename T>
  181. uint FeatureMatrixT<T>::get_d() const
  182. {
  183. return this->ui_d;
  184. }
  185. // Sets the given dimension and re-sizes internal data structure. WARNING: this will completely remove your current data!
  186. template <typename T>
  187. void FeatureMatrixT<T>::set_d(const uint & _d)
  188. {
  189. this->ui_d = _d;
  190. this->features.resize( this->ui_d );
  191. }
  192. template <typename T>
  193. void FeatureMatrixT<T>::setVerbose( const bool & _verbose)
  194. {
  195. this->b_verbose = _verbose;
  196. }
  197. template <typename T>
  198. bool FeatureMatrixT<T>::getVerbose( ) const
  199. {
  200. return this->b_verbose;
  201. }
  202. template <typename T>
  203. void FeatureMatrixT<T>::setDebug( const bool & _debug)
  204. {
  205. this->b_debug = _debug;
  206. }
  207. template <typename T>
  208. bool FeatureMatrixT<T>::getDebug( ) const
  209. {
  210. return this->b_debug;
  211. }
  212. // Matrix-like operator for element access, performs validity check
  213. template <typename T>
  214. inline T FeatureMatrixT<T>::operator()(const uint _row,
  215. const uint _col
  216. ) const
  217. {
  218. if ( (_row < 0) || (_col < 0) || (_row > this->ui_d) || (_col > this->ui_n) )
  219. {
  220. fthrow(Exception, "FeatureMatrixT: out of bounds");
  221. }
  222. else
  223. return ( this->features[_row]).access(_col);
  224. }
  225. template<class T>
  226. inline bool
  227. FeatureMatrixT<T>::operator==(const FeatureMatrixT<T> & _F) const
  228. {
  229. if ( ( this->get_n() != _F.get_n()) || (this->get_d() != _F.get_d()) )
  230. {
  231. fthrow(Exception, "FeatureMatrixT<T>::operator== : (n != F.get_n()) || (d != F.get_d()) -- number of dimensions does not fit");
  232. }
  233. else if ((this->ui_n == 0) || (this->ui_d == 0))
  234. {
  235. return true;
  236. }
  237. for (uint i = 0; i < this->ui_d; i++)
  238. {
  239. for (uint j = 0; j < this->ui_n; j++)
  240. {
  241. // FIXME: it would be more efficient if we compare SortedVectorSparse objects here
  242. if(!((*this)(i,j) == _F(i,j)))
  243. return false;
  244. }
  245. }
  246. return true;
  247. }
  248. template<class T>
  249. inline bool
  250. FeatureMatrixT<T>::operator!=(const FeatureMatrixT<T> & _F) const
  251. {
  252. if ( ( (*this).get_n() != _F.get_n()) ||
  253. ( (*this).get_d() != _F.get_d())
  254. )
  255. {
  256. fthrow(Exception, "FeatureMatrixT::operator!=(): (n != F.get_n()) || (d != F.get_d()) -- number of dimensions does not fit");
  257. }
  258. else if ((this->ui_n == 0) || (this->ui_d == 0))
  259. {
  260. return false;
  261. }
  262. for (uint i = 0; i < this->ui_d; i++)
  263. {
  264. for (uint j = 0; j < this->ui_n; j++)
  265. {
  266. if(!((*this)(i,j) == _F(i,j)))
  267. return true;
  268. }
  269. }
  270. return false;
  271. }
  272. template<typename T>
  273. inline FeatureMatrixT<T>&
  274. FeatureMatrixT<T>::operator=(const FeatureMatrixT<T> & _F)
  275. {
  276. this->set_d(_F.get_d());
  277. this->ui_n = _F.get_n();
  278. for (uint i = 0; i < (*this).get_d(); i++)
  279. {
  280. // use the operator= of SortedVectorSparse
  281. features[i] = _F[i];
  282. }
  283. return *this;
  284. }
  285. // Element access without validity check
  286. template <typename T>
  287. inline T FeatureMatrixT<T>::getUnsafe(const uint _row,
  288. const uint _col
  289. ) const
  290. {
  291. return (this->features[_row]).access(_col);
  292. }
  293. //! Element access of original values without validity check
  294. template <typename T>
  295. inline T FeatureMatrixT<T>::getOriginal(const uint _row,
  296. const uint _col
  297. ) const
  298. {
  299. return (this->features[_row]).accessOriginal(_col);
  300. }
  301. // Sets a specified element to the given value, performs validity check
  302. template <typename T>
  303. inline void FeatureMatrixT<T>::set (const uint _row,
  304. const uint _col,
  305. const T & _newElement,
  306. bool _setTransformedValue
  307. )
  308. {
  309. if ( (_row < 0) || (_col < 0) || (_row > this->ui_d) || (_col > this->ui_n) )
  310. {
  311. return;
  312. }
  313. else
  314. (this->features[_row]).set ( _col, _newElement, _setTransformedValue );
  315. }
  316. // Sets a specified element to the given value, without validity check
  317. template <typename T>
  318. inline void FeatureMatrixT<T>::setUnsafe (const uint _row,
  319. const uint _col,
  320. const T & _newElement,
  321. bool _setTransformedValue
  322. )
  323. {
  324. (this->features[_row]).set ( _col, _newElement, _setTransformedValue );
  325. }
  326. // Acceess to all element entries of a specified dimension, including validity check
  327. template <typename T>
  328. void FeatureMatrixT<T>::getDimension(const uint & _dim,
  329. NICE::SortedVectorSparse<T> & _dimension
  330. ) const
  331. {
  332. if ( (_dim < 0) || (_dim > this->ui_d) )
  333. {
  334. return;
  335. }
  336. else
  337. _dimension = this->features[_dim];
  338. }
  339. // Acceess to all element entries of a specified dimension, without validity check
  340. template <typename T>
  341. void FeatureMatrixT<T>::getDimensionUnsafe(const uint & _dim,
  342. NICE::SortedVectorSparse<T> & _dimension
  343. ) const
  344. {
  345. _dimension = this->features[_dim];
  346. }
  347. // Finds the first element in a given dimension, which equals elem
  348. template <typename T>
  349. void FeatureMatrixT<T>::findFirstInDimension(const uint & _dim,
  350. const T & _elem,
  351. uint & _position
  352. ) const
  353. {
  354. _position = 0;
  355. if ( _dim > this->ui_d )
  356. return;
  357. std::pair< typename SortedVectorSparse<T>::elementpointer, typename SortedVectorSparse<T>::elementpointer > eit;
  358. eit = this->features[_dim].nonzeroElements().equal_range ( _elem );
  359. _position = distance( this->features[_dim].nonzeroElements().begin(), eit.first );
  360. if ( _elem > this->features[_dim].getTolerance() )
  361. _position += this->features[_dim].getZeros();
  362. }
  363. // Finds the last element in a given dimension, which equals elem
  364. template <typename T>
  365. void FeatureMatrixT<T>::findLastInDimension(const uint & _dim,
  366. const T & _elem,
  367. uint & _position
  368. ) const
  369. {
  370. _position = 0;
  371. if ( _dim > this->ui_d )
  372. return;
  373. std::pair< typename SortedVectorSparse<T>::const_elementpointer, typename SortedVectorSparse<T>::const_elementpointer > eit = this->features[_dim].nonzeroElements().equal_range ( _elem );
  374. _position = distance( this->features[_dim].nonzeroElements().begin(), eit.second );
  375. if ( _elem > this->features[_dim].getTolerance() )
  376. _position += this->features[_dim].getZeros();
  377. }
  378. // Finds the first element in a given dimension, which is larger as elem
  379. template <typename T>
  380. void FeatureMatrixT<T>::findFirstLargerInDimension(const uint & _dim,
  381. const T & _elem,
  382. uint & _position
  383. ) const
  384. {
  385. _position = 0;
  386. if ( _dim > this->ui_d )
  387. return;
  388. //no non-zero elements?
  389. if (this->features[_dim].getNonZeros() <= 0)
  390. {
  391. // if element is greater than zero, than is should be added at the last position
  392. if (_elem > this->features[_dim].getTolerance() )
  393. _position = this->ui_n;
  394. //if not, position is 0
  395. return;
  396. }
  397. if (this->features[_dim].getNonZeros() == 1)
  398. {
  399. // if element is greater than the only nonzero element, then it is larger than everything else
  400. if (this->features[_dim].nonzeroElements().begin()->first <= _elem)
  401. _position = this->ui_n;
  402. //if not, but the element is still greater than zero, than
  403. else if (_elem > this->features[_dim].getTolerance() )
  404. _position = this->ui_n -1;
  405. return;
  406. }
  407. // standard case - not everything is zero and not only a single element is zero
  408. // find pointer to last non-zero element
  409. // FIXME no idea why this should be necessary...
  410. typename SortedVectorSparse<T>::const_elementpointer it = this->features[_dim].nonzeroElements().end(); //this is needed !!!
  411. // find pointer to first element largern than the given value
  412. it = this->features[_dim].nonzeroElements().upper_bound ( _elem ); //if all values are smaller, this does not do anything at all
  413. _position = distance( this->features[_dim].nonzeroElements().begin(), it );
  414. if ( _elem > this->features[_dim].getTolerance() )
  415. {
  416. //position += features[dim].getZeros();
  417. _position += this->ui_n - this->features[_dim].getNonZeros();
  418. }
  419. }
  420. // Finds the last element in a given dimension, which is smaller as elem
  421. template <typename T>
  422. void FeatureMatrixT<T>::findLastSmallerInDimension(const uint & _dim,
  423. const T & _elem,
  424. uint & _position
  425. ) const
  426. {
  427. _position = 0;
  428. if ( (_dim < 0) || (_dim > this->ui_d))
  429. return;
  430. typename SortedVectorSparse<T>::const_elementpointer it = this->features[_dim].nonzeroElements().lower_bound ( _elem );
  431. _position = distance( this->features[_dim].nonzeroElements().begin(), it );
  432. if ( it->first > this->features[_dim].getTolerance() )
  433. _position += this->features[_dim].getZeros();
  434. }
  435. template <typename T>
  436. T FeatureMatrixT<T>::getLargestValue () const
  437. {
  438. T vmax = (T) 0;
  439. for ( std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin();
  440. it != this->features.end();
  441. it++
  442. )
  443. {
  444. if ( it->getLargestValueUnsafe( ) > vmax )
  445. {
  446. vmax = it->getLargestValueUnsafe( 1.0 /*quantile, we are interested in the largest value*/);
  447. }
  448. }
  449. return vmax;
  450. }
  451. template <typename T>
  452. NICE::Vector<T> FeatureMatrixT<T>::getLargestValuePerDimension ( const double & _quantile ) const
  453. {
  454. NICE::Vector<T> vmax ( this->get_d() );
  455. NICE::Vector<T>::iterator vmaxIt = vmax.begin();
  456. for ( std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin();
  457. it != this->features.end();
  458. it++, vmaxIt+
  459. )
  460. {
  461. *vmaxIt = it->getLargestValueUnsafe( _quantile );
  462. }
  463. }
  464. //------------------------------------------------------
  465. // high level methods
  466. //------------------------------------------------------
  467. template <typename T>
  468. void FeatureMatrixT<T>::applyFunctionToFeatureMatrix ( const NICE::ParameterizedFunction *_pf )
  469. {
  470. if (_pf != NULL)
  471. {
  472. // REMARK: might be inefficient due to virtual calls
  473. if ( !_pf->isOrderPreserving() )
  474. fthrow(Exception, "ParameterizedFunction::applyFunctionToFeatureMatrix: this function is optimized for order preserving transformations");
  475. uint d = this->get_d();
  476. for (uint dim = 0; dim < d; dim++)
  477. {
  478. std::multimap< double, typename SortedVectorSparse<double>::dataelement> & nonzeroElements = this->getFeatureValues(dim).nonzeroElements();
  479. for ( SortedVectorSparse<double>::elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++ )
  480. {
  481. SortedVectorSparse<double>::dataelement & de = i->second;
  482. //TODO check, wether the element is "sparse" afterwards
  483. de.second = _pf->f( dim, i->first );
  484. }
  485. }
  486. /*for ( int i = 0 ; i < featureMatrix.get_n(); i++ )
  487. for ( int index = 0 ; index < featureMatrix.get_d(); index++ )
  488. featureMatrix.set(index, i, f( (uint)index, featureMatrix.getOriginal(index,i) ), isOrderPreserving() );*/
  489. }
  490. else
  491. {
  492. //no pf given -> nothing to do
  493. }
  494. }
  495. //Computes the ratio of sparsity across the matrix
  496. template <typename T>
  497. double FeatureMatrixT<T>:: computeSparsityRatio() const
  498. {
  499. double ratio(0.0);
  500. for (typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin(); it != this->features.end(); it++)
  501. {
  502. ratio += (*it).getZeros() / (double) (*it).getN();
  503. }
  504. if (this->features.size() != 0)
  505. ratio /= double(this->features.size());
  506. return ratio;
  507. }
  508. // add a new feature and insert its elements at the end of each dimension vector
  509. template <typename T>
  510. void FeatureMatrixT<T>::add_feature( const std::vector<T> & _feature,
  511. const NICE::ParameterizedFunction *_pf
  512. )
  513. {
  514. if (this->ui_n == 0)
  515. {
  516. this->set_d( _feature.size() );
  517. }
  518. if ( _feature.size() != this->ui_d)
  519. {
  520. fthrow(Exception, "FeatureMatrixT<T>::add_feature - number of dimensions does not fit");
  521. return;
  522. }
  523. for (uint dimension = 0; dimension < this->features.size(); dimension++)
  524. {
  525. if (_pf != NULL)
  526. this->features[dimension].insert( _feature[dimension], _pf->f( dimension, _feature[dimension]) );
  527. else
  528. this->features[dimension].insert( _feature[dimension] );
  529. }
  530. this->ui_n++;
  531. }
  532. // add a new feature and insert its elements at the end of each dimension vector
  533. template <typename T>
  534. void FeatureMatrixT<T>::add_feature(const NICE::SparseVector & _feature,
  535. const ParameterizedFunction *_pf
  536. )
  537. {
  538. if (this->ui_n == 0)
  539. {
  540. this->set_d( _feature.size() );
  541. }
  542. if ( _feature.getDim() > this->ui_d)
  543. {
  544. fthrow(Exception, "FeatureMatrixT<T>::add_feature - number of dimensions does not fit");
  545. return;
  546. }
  547. for (NICE::SparseVector::const_iterator it = _feature.begin(); it != _feature.end(); it++)
  548. {
  549. if (_pf != NULL)
  550. this->features[it->first].insert( (T) it->second, _pf->f( it->first, (T) it->second), true /* _specifyFeatureNumber */, this->ui_n );
  551. else
  552. this->features[it->first].insert( (T) it->second, true /* _specifyFeatureNumber */, this->ui_n );
  553. }
  554. this->ui_n++;
  555. }
  556. // add several new features and insert their elements in the already ordered structure
  557. template <typename T>
  558. void FeatureMatrixT<T>::add_features(const std::vector<std::vector<T> > & _features )
  559. {
  560. //TODO do we need the parameterized function here as well? usually, we add several features and run applyFunctionToFeatureMatrix afterwards.
  561. // check this please :)
  562. //TODO assure that every feature has the same dimension
  563. if (this->ui_n == 0)
  564. {
  565. this->set_d(_features.size());
  566. }
  567. //pay attention: we assume now, that we have a vector (over dimensions) containing vectors over features (examples per dimension) - to be more efficient
  568. for (uint dim = 0; dim < this->ui_d; dim++)
  569. {
  570. this->features[dim].insert( _features[dim] );
  571. }
  572. //update the number of our features
  573. this->ui_n += _features[0].size();
  574. }
  575. template <typename T>
  576. void FeatureMatrixT<T>::set_features(const std::vector<std::vector<T> > & _features,
  577. std::vector<std::vector<uint> > & _permutations,
  578. const uint & _dim
  579. )
  580. {
  581. this->features.clear();
  582. this->set_d( std::max ( _dim, (const uint) _features.size() ) );
  583. if ( this->ui_d > 0 )
  584. this->ui_n = _features[0].size();
  585. //pay attention: we assume now, that we have a vector (over dimensions) containing vectors over features (examples per dimension) - to be more efficient
  586. for (uint dim = 0; dim < this->ui_d; dim++)
  587. {
  588. this->features[dim].insert( _features[dim] );
  589. }
  590. this->getPermutations( _permutations );
  591. }
  592. template <typename T>
  593. void FeatureMatrixT<T>::set_features(const std::vector<std::vector<T> > & _features,
  594. std::vector<std::map<uint,uint> > & _permutations,
  595. const uint & _dim
  596. )
  597. {
  598. this->features.clear();
  599. this->set_d( std::max ( _dim, _features.size() ) );
  600. if ( this->ui_d > 0 )
  601. this->ui_n = _features[0].size();
  602. //pay attention: we assume now, that we have a vector (over dimensions) containing vectors over features (examples per dimension) - to be more efficient
  603. for (uint dim = 0; dim < this->ui_d; dim++)
  604. {
  605. this->features[dim].insert( _features[dim] );
  606. }
  607. this->getPermutations( _permutations );
  608. }
  609. template <typename T>
  610. void FeatureMatrixT<T>::set_features(const std::vector<std::vector<T> > & _features,
  611. const uint & _dim
  612. )
  613. {
  614. this->features.clear();
  615. this->set_d( std::max ( _dim, (const uint) _features.size() ) );
  616. if ( this->ui_d > 0 )
  617. this->ui_n = _features[0].size();
  618. //pay attention: we assume now, that we have a vector (over dimensions) containing vectors over features (examples per dimension) - to be more efficient
  619. for (uint dim = 0; dim < this->ui_d; dim++)
  620. {
  621. if ( this->b_debug )
  622. {
  623. std::cerr << " dim: " << dim << " add " << _features[dim].size() << " examples " << std::endl;
  624. }
  625. this->features[dim].insert( _features[dim] );
  626. }
  627. }
  628. template <typename T>
  629. void FeatureMatrixT<T>::set_features(const std::vector< const NICE::SparseVector * > & _features,
  630. const bool _dimensionsOverExamples,
  631. const uint & _dim
  632. )
  633. {
  634. this->features.clear();
  635. if (_features.size() == 0)
  636. {
  637. std::cerr << "set_features without features" << std::endl;
  638. }
  639. // resize our data structure
  640. //therefore, let's first of all figure out if the user specified a dimension or not.
  641. uint dimTmp ( _dim );
  642. if (_dimensionsOverExamples) //do we have dim x examples ?
  643. {
  644. if ( _features.size() > dimTmp )
  645. {
  646. dimTmp = _features.size();
  647. }
  648. }
  649. else //we have examples x dimes (as usually done)
  650. {
  651. if (_features.size() > 0) //and have at least one example
  652. {
  653. try{
  654. if ( _features[0]->getDim() > dimTmp )
  655. {
  656. dimTmp = _features[0]->getDim();
  657. }
  658. }
  659. catch(...)
  660. {
  661. std::cerr << "FeatureMatrixT<T>::set_features -- something went wrong using getDim() of SparseVectors" << std::endl;
  662. }
  663. }
  664. }
  665. this->set_d( dimTmp );
  666. // set number of examples n
  667. if ( this->ui_d > 0 )
  668. {
  669. if ( _dimensionsOverExamples ) //do we have dim x examples ?
  670. this->ui_n = _features[0]->getDim(); //NOTE Pay attention: we assume, that this number is set!
  671. else //we have examples x dimes (as usually done)
  672. this->ui_n = _features.size();
  673. }
  674. // insert all values
  675. if ( _dimensionsOverExamples ) //do we have dim x examples ?
  676. {
  677. for (uint dim = 0; dim < this->ui_d; dim++)
  678. {
  679. this->features[dim].insert( _features[dim] );
  680. }
  681. }
  682. else //we have examples x dimes (as usually done)
  683. {
  684. if ( this->b_debug )
  685. std::cerr << "FeatureMatrixT<T>::set_features " << this->ui_n << " new examples" << std::endl;
  686. //loop over every example to add its content
  687. for (uint nr = 0; nr < this->ui_n; nr++)
  688. {
  689. if ( this->b_debug )
  690. std::cerr << "add feature nr. " << nr << " / " << _features.size() << " ";
  691. //loop over every dimension to add the specific value to the corresponding SortedVectorSparse
  692. for (NICE::SparseVector::const_iterator elemIt = _features[nr]->begin(); elemIt != _features[nr]->end(); elemIt++)
  693. {
  694. if ( this->b_debug )
  695. std::cerr << elemIt->first << "-" << elemIt->second << " ";
  696. //elemIt->first: dim, elemIt->second: value
  697. this->features[elemIt->first].insert( (T) elemIt->second, true /* _specifyFeatureNumber */, nr);
  698. }//for non-zero-values of the feature
  699. if ( this->b_debug )
  700. std::cerr << std::endl;
  701. }//for every new feature
  702. if ( this->b_debug )
  703. std::cerr << "FeatureMatrixT<T>::set_features done" << std::endl;
  704. }//if dimOverEx
  705. //set n for the internal data structure SortedVectorSparse
  706. for (typename std::vector<NICE::SortedVectorSparse<T> >::iterator it = this->features.begin(); it != this->features.end(); it++)
  707. (*it).setN( this->ui_n );
  708. }
  709. template <typename T>
  710. void FeatureMatrixT<T>::getPermutations( std::vector<std::vector<uint> > & _permutations) const
  711. {
  712. for (uint dim = 0; dim < this->ui_d; dim++)
  713. {
  714. std::vector<uint> perm ( (this->features[dim]).getPermutation() );
  715. _permutations.push_back(perm);
  716. }
  717. }
  718. template <typename T>
  719. void FeatureMatrixT<T>::getPermutations( std::vector<std::map<uint,uint> > & _permutations) const
  720. {
  721. for (uint dim = 0; dim < this->ui_d; dim++)
  722. {
  723. std::map<uint,uint> perm ( (this->features[dim]).getPermutationNonZeroReal() );
  724. _permutations.push_back(perm);
  725. }
  726. }
  727. // Prints the whole Matrix (outer loop over dimension, inner loop over features)
  728. template <typename T>
  729. void FeatureMatrixT<T>::print(std::ostream & _os) const
  730. {
  731. if (_os.good())
  732. {
  733. for (uint dim = 0; dim < this->ui_d; dim++)
  734. {
  735. this->features[dim].print(_os);
  736. }
  737. }
  738. }
  739. // Computes the whole non-sparse matrix. WARNING: this may result in a really memory-consuming data-structure!
  740. template <typename T>
  741. void FeatureMatrixT<T>::computeNonSparseMatrix(NICE::MatrixT<T> & _matrix,
  742. bool _transpose
  743. ) const
  744. {
  745. if ( _transpose )
  746. _matrix.resize(this->get_n(),this->get_d());
  747. else
  748. _matrix.resize(this->get_d(),this->get_n());
  749. _matrix.set((T)0.0);
  750. uint dimIdx(0);
  751. for (typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin(); it != this->features.end(); it++, dimIdx++)
  752. {
  753. std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer> nonzeroIndices= (*it).nonzeroIndices();
  754. for (typename std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer>::const_iterator inIt = nonzeroIndices.begin(); inIt != nonzeroIndices.end(); inIt++)
  755. {
  756. uint featIndex = ((*inIt).second)->second.first;
  757. if ( _transpose )
  758. _matrix(featIndex,dimIdx) =((*inIt).second)->second.second;
  759. else
  760. _matrix(dimIdx,featIndex) =((*inIt).second)->second.second;
  761. }
  762. }
  763. }
  764. // Computes the whole non-sparse matrix. WARNING: this may result in a really memory-consuming data-structure!
  765. template <typename T>
  766. void FeatureMatrixT<T>::computeNonSparseMatrix(std::vector<std::vector<T> > & _matrix,
  767. bool _transpose
  768. ) const
  769. {
  770. if ( _transpose )
  771. _matrix.resize(this->get_n());
  772. else
  773. _matrix.resize(this->get_d());
  774. // resizing the matrix
  775. for ( uint i = 0 ; i < _matrix.size(); i++ )
  776. if ( _transpose )
  777. _matrix[i] = std::vector<T>(this->get_d(), 0.0);
  778. else
  779. _matrix[i] = std::vector<T>(this->get_n(), 0.0);
  780. uint dimIdx(0);
  781. for (typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin(); it != this->features.end(); it++, dimIdx++)
  782. {
  783. std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer> nonzeroIndices= (*it).nonzeroIndices();
  784. for (typename std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer>::const_iterator inIt = nonzeroIndices.begin(); inIt != nonzeroIndices.end(); inIt++)
  785. {
  786. uint featIndex = ((*inIt).second)->second.first;
  787. if ( _transpose )
  788. _matrix[featIndex][dimIdx] =((*inIt).second)->second.second;
  789. else
  790. _matrix[dimIdx][featIndex] =((*inIt).second)->second.second;
  791. }
  792. }
  793. }
  794. // Swaps to specified elements, performing a validity check
  795. template <typename T>
  796. void FeatureMatrixT<T>::swap(const uint & _row1,
  797. const uint & _col1,
  798. const uint & _row2,
  799. const uint & _col2
  800. )
  801. {
  802. if ( (_row1 < 0) || (_col1 < 0) || (_row1 > this->ui_d) || (_col1 > this->ui_n) ||
  803. (_row2 < 0) || (_col2 < 0) || (_row2 > this->ui_d) || (_col2 > this->ui_n)
  804. )
  805. {
  806. return;
  807. }
  808. else
  809. {
  810. //swap
  811. T tmp = (*this)(_row1, _col1);
  812. (*this).set(_row1, _col1, (*this)(_row2,_col2));
  813. (*this).set(_row2, _col2, tmp);
  814. }
  815. }
  816. // Swaps to specified elements, without performing a validity check
  817. template <typename T>
  818. void FeatureMatrixT<T>::swapUnsafe(const uint & _row1,
  819. const uint & _col1,
  820. const uint & _row2,
  821. const uint & _col2
  822. )
  823. {
  824. //swap
  825. T tmp = (*this)(_row1, _col1);
  826. (*this).set(_row1, _col1, (*this)(_row2,_col2));
  827. (*this).set(_row2, _col2, tmp);
  828. }
  829. template <typename T>
  830. void FeatureMatrixT<T>::hikDiagonalElements( Vector & _diagonalElements ) const
  831. {
  832. uint dimIdx = 0;
  833. // the function calculates the diagonal elements of a HIK kernel matrix
  834. _diagonalElements.resize(this->ui_n);
  835. _diagonalElements.set(0.0);
  836. // loop through all dimensions
  837. for (typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin(); it != this->features.end(); it++, dimIdx++)
  838. {
  839. std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer> nonzeroIndices= (*it).nonzeroIndices();
  840. // loop through all features
  841. for (typename std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer>::const_iterator inIt = nonzeroIndices.begin(); inIt != nonzeroIndices.end(); inIt++)
  842. {
  843. uint index = inIt->first;
  844. typename NICE::SortedVectorSparse<T>::elementpointer p = inIt->second;
  845. typename NICE::SortedVectorSparse<T>::dataelement de = p->second;
  846. _diagonalElements[index] += de.second;
  847. }
  848. }
  849. }
  850. template <typename T>
  851. double FeatureMatrixT<T>::hikTrace() const
  852. {
  853. Vector diagonalElements;
  854. this->hikDiagonalElements( diagonalElements );
  855. return diagonalElements.Sum();
  856. }
  857. template <typename T>
  858. uint FeatureMatrixT<T>::getNumberOfNonZeroElementsPerDimension(const uint & dim) const
  859. {
  860. //FIXME we could return a boolean indicating success and return the actual number via call-by-reference
  861. if ( (dim < 0) || (dim > this->ui_d))
  862. return 0;
  863. return this->features[dim].getNonZeros();
  864. }
  865. template <typename T>
  866. uint FeatureMatrixT<T>::getNumberOfZeroElementsPerDimension(const uint & dim) const
  867. {
  868. if ( (dim < 0) || (dim > this->ui_d))
  869. return 0;
  870. return this->ui_n - this-> features[dim].getNonZeros();
  871. }
  872. template <typename T>
  873. void FeatureMatrixT<T>::restore ( std::istream & _is,
  874. int _format
  875. )
  876. {
  877. bool b_restoreVerbose ( false );
  878. if ( _is.good() )
  879. {
  880. if ( b_restoreVerbose )
  881. std::cerr << " restore FeatureMatrixT" << std::endl;
  882. std::string tmp;
  883. _is >> tmp; //class name
  884. if ( ! this->isStartTag( tmp, "FeatureMatrixT" ) )
  885. {
  886. std::cerr << " WARNING - attempt to restore FeatureMatrixT, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  887. throw;
  888. }
  889. _is.precision ( std::numeric_limits<double>::digits10 + 1);
  890. bool b_endOfBlock ( false ) ;
  891. while ( !b_endOfBlock )
  892. {
  893. _is >> tmp; // start of block
  894. if ( this->isEndTag( tmp, "FeatureMatrixT" ) )
  895. {
  896. b_endOfBlock = true;
  897. continue;
  898. }
  899. tmp = this->removeStartTag ( tmp );
  900. if ( b_restoreVerbose )
  901. std::cerr << " currently restore section " << tmp << " in FeatureMatrixT" << std::endl;
  902. if ( tmp.compare("ui_n") == 0 )
  903. {
  904. _is >> this->ui_n;
  905. _is >> tmp; // end of block
  906. tmp = this->removeEndTag ( tmp );
  907. }
  908. else if ( tmp.compare("ui_d") == 0 )
  909. {
  910. _is >> this->ui_d;
  911. _is >> tmp; // end of block
  912. tmp = this->removeEndTag ( tmp );
  913. }
  914. else if ( tmp.compare("features") == 0 )
  915. {
  916. //NOTE assumes d to be read first!
  917. this->features.resize( this->ui_d);
  918. //now read features for every dimension
  919. for (uint dim = 0; dim < this->ui_d; dim++)
  920. {
  921. NICE::SortedVectorSparse<T> svs;
  922. this->features[dim] = svs;
  923. this->features[dim].restore(_is, _format);
  924. }
  925. _is >> tmp; // end of block
  926. tmp = this->removeEndTag ( tmp );
  927. }
  928. else
  929. {
  930. std::cerr << "WARNING -- unexpected FeatureMatrixT object -- " << tmp << " -- for restoration... aborting" << std::endl;
  931. throw;
  932. }
  933. }
  934. }
  935. else
  936. {
  937. std::cerr << "FeatureMatrixT<T>::restore -- InStream not initialized - restoring not possible!" << std::endl;
  938. throw;
  939. }
  940. }
  941. template <typename T>
  942. void FeatureMatrixT<T>::store ( std::ostream & _os,
  943. int _format
  944. ) const
  945. {
  946. if (_os.good())
  947. {
  948. // show starting point
  949. _os << this->createStartTag( "FeatureMatrixT" ) << std::endl;
  950. _os.precision (std::numeric_limits<double>::digits10 + 1);
  951. _os << this->createStartTag( "ui_n" ) << std::endl;
  952. _os << this->ui_n << std::endl;
  953. _os << this->createEndTag( "ui_n" ) << std::endl;
  954. _os << this->createStartTag( "ui_d" ) << std::endl;
  955. _os << this->ui_d << std::endl;
  956. _os << this->createEndTag( "ui_d" ) << std::endl;
  957. //now write features for every dimension
  958. _os << this->createStartTag( "features" ) << std::endl;
  959. for (uint dim = 0; dim < this->ui_d; dim++)
  960. {
  961. this->features[dim].store(_os,_format);
  962. }
  963. _os << this->createEndTag( "features" ) << std::endl;
  964. // done
  965. _os << this->createEndTag( "FeatureMatrixT" ) << std::endl;
  966. }
  967. else
  968. {
  969. std::cerr << "FeatureMatrixT<T>::store -- OutStream not initialized - storing not possible!" << std::endl;
  970. }
  971. }
  972. template <typename T>
  973. void FeatureMatrixT<T>::clear ()
  974. {}
  975. } // namespace
  976. // #endif