FeatureMatrixT.tcc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. //------------------------------------------------------
  436. // high level methods
  437. //------------------------------------------------------
  438. template <typename T>
  439. void FeatureMatrixT<T>::applyFunctionToFeatureMatrix ( const NICE::ParameterizedFunction *_pf )
  440. {
  441. if (_pf != NULL)
  442. {
  443. // REMARK: might be inefficient due to virtual calls
  444. if ( !_pf->isOrderPreserving() )
  445. fthrow(Exception, "ParameterizedFunction::applyFunctionToFeatureMatrix: this function is optimized for order preserving transformations");
  446. uint d = this->get_d();
  447. for (uint dim = 0; dim < d; dim++)
  448. {
  449. std::multimap< double, typename SortedVectorSparse<double>::dataelement> & nonzeroElements = this->getFeatureValues(dim).nonzeroElements();
  450. for ( SortedVectorSparse<double>::elementpointer i = nonzeroElements.begin(); i != nonzeroElements.end(); i++ )
  451. {
  452. SortedVectorSparse<double>::dataelement & de = i->second;
  453. //TODO check, wether the element is "sparse" afterwards
  454. de.second = _pf->f( dim, i->first );
  455. }
  456. }
  457. /*for ( int i = 0 ; i < featureMatrix.get_n(); i++ )
  458. for ( int index = 0 ; index < featureMatrix.get_d(); index++ )
  459. featureMatrix.set(index, i, f( (uint)index, featureMatrix.getOriginal(index,i) ), isOrderPreserving() );*/
  460. }
  461. else
  462. {
  463. //no pf given -> nothing to do
  464. }
  465. }
  466. //Computes the ratio of sparsity across the matrix
  467. template <typename T>
  468. double FeatureMatrixT<T>:: computeSparsityRatio() const
  469. {
  470. double ratio(0.0);
  471. for (typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin(); it != this->features.end(); it++)
  472. {
  473. ratio += (*it).getZeros() / (double) (*it).getN();
  474. }
  475. if (this->features.size() != 0)
  476. ratio /= double(this->features.size());
  477. return ratio;
  478. }
  479. // add a new feature and insert its elements at the end of each dimension vector
  480. template <typename T>
  481. void FeatureMatrixT<T>::add_feature( const std::vector<T> & _feature,
  482. const NICE::ParameterizedFunction *_pf
  483. )
  484. {
  485. if (this->ui_n == 0)
  486. {
  487. this->set_d( _feature.size() );
  488. }
  489. if ( _feature.size() != this->ui_d)
  490. {
  491. fthrow(Exception, "FeatureMatrixT<T>::add_feature - number of dimensions does not fit");
  492. return;
  493. }
  494. for (uint dimension = 0; dimension < this->features.size(); dimension++)
  495. {
  496. if (_pf != NULL)
  497. this->features[dimension].insert( _feature[dimension], _pf->f( dimension, _feature[dimension]) );
  498. else
  499. this->features[dimension].insert( _feature[dimension] );
  500. }
  501. this->ui_n++;
  502. }
  503. // add a new feature and insert its elements at the end of each dimension vector
  504. template <typename T>
  505. void FeatureMatrixT<T>::add_feature(const NICE::SparseVector & _feature,
  506. const ParameterizedFunction *_pf
  507. )
  508. {
  509. if (this->ui_n == 0)
  510. {
  511. this->set_d( _feature.size() );
  512. }
  513. if ( _feature.getDim() > this->ui_d)
  514. {
  515. fthrow(Exception, "FeatureMatrixT<T>::add_feature - number of dimensions does not fit");
  516. return;
  517. }
  518. for (NICE::SparseVector::const_iterator it = _feature.begin(); it != _feature.end(); it++)
  519. {
  520. if (_pf != NULL)
  521. this->features[it->first].insert( (T) it->second, _pf->f( it->first, (T) it->second), true /* _specifyFeatureNumber */, this->ui_n );
  522. else
  523. this->features[it->first].insert( (T) it->second, true /* _specifyFeatureNumber */, this->ui_n );
  524. }
  525. this->ui_n++;
  526. }
  527. // add several new features and insert their elements in the already ordered structure
  528. template <typename T>
  529. void FeatureMatrixT<T>::add_features(const std::vector<std::vector<T> > & _features )
  530. {
  531. //TODO do we need the parameterized function here as well? usually, we add several features and run applyFunctionToFeatureMatrix afterwards.
  532. // check this please :)
  533. //TODO assure that every feature has the same dimension
  534. if (this->ui_n == 0)
  535. {
  536. this->set_d(_features.size());
  537. }
  538. //pay attention: we assume now, that we have a vector (over dimensions) containing vectors over features (examples per dimension) - to be more efficient
  539. for (uint dim = 0; dim < this->ui_d; dim++)
  540. {
  541. this->features[dim].insert( _features[dim] );
  542. }
  543. //update the number of our features
  544. this->ui_n += _features[0].size();
  545. }
  546. template <typename T>
  547. void FeatureMatrixT<T>::set_features(const std::vector<std::vector<T> > & _features,
  548. std::vector<std::vector<uint> > & _permutations,
  549. const uint & _dim
  550. )
  551. {
  552. this->features.clear();
  553. this->set_d( std::max ( _dim, (const uint) _features.size() ) );
  554. if ( this->ui_d > 0 )
  555. this->ui_n = _features[0].size();
  556. //pay attention: we assume now, that we have a vector (over dimensions) containing vectors over features (examples per dimension) - to be more efficient
  557. for (uint dim = 0; dim < this->ui_d; dim++)
  558. {
  559. this->features[dim].insert( _features[dim] );
  560. }
  561. this->getPermutations( _permutations );
  562. }
  563. template <typename T>
  564. void FeatureMatrixT<T>::set_features(const std::vector<std::vector<T> > & _features,
  565. std::vector<std::map<uint,uint> > & _permutations,
  566. const uint & _dim
  567. )
  568. {
  569. this->features.clear();
  570. this->set_d( std::max ( _dim, _features.size() ) );
  571. if ( this->ui_d > 0 )
  572. this->ui_n = _features[0].size();
  573. //pay attention: we assume now, that we have a vector (over dimensions) containing vectors over features (examples per dimension) - to be more efficient
  574. for (uint dim = 0; dim < this->ui_d; dim++)
  575. {
  576. this->features[dim].insert( _features[dim] );
  577. }
  578. this->getPermutations( _permutations );
  579. }
  580. template <typename T>
  581. void FeatureMatrixT<T>::set_features(const std::vector<std::vector<T> > & _features,
  582. const uint & _dim
  583. )
  584. {
  585. this->features.clear();
  586. this->set_d( std::max ( _dim, (const uint) _features.size() ) );
  587. if ( this->ui_d > 0 )
  588. this->ui_n = _features[0].size();
  589. //pay attention: we assume now, that we have a vector (over dimensions) containing vectors over features (examples per dimension) - to be more efficient
  590. for (uint dim = 0; dim < this->ui_d; dim++)
  591. {
  592. if ( this->b_debug )
  593. {
  594. std::cerr << " dim: " << dim << " add " << _features[dim].size() << " examples " << std::endl;
  595. }
  596. this->features[dim].insert( _features[dim] );
  597. }
  598. }
  599. template <typename T>
  600. void FeatureMatrixT<T>::set_features(const std::vector< const NICE::SparseVector * > & _features,
  601. const bool _dimensionsOverExamples,
  602. const uint & _dim
  603. )
  604. {
  605. this->features.clear();
  606. if (_features.size() == 0)
  607. {
  608. std::cerr << "set_features without features" << std::endl;
  609. }
  610. // resize our data structure
  611. //therefore, let's first of all figure out if the user specified a dimension or not.
  612. uint dimTmp ( _dim );
  613. if (_dimensionsOverExamples) //do we have dim x examples ?
  614. {
  615. if ( _features.size() > dimTmp )
  616. {
  617. dimTmp = _features.size();
  618. }
  619. }
  620. else //we have examples x dimes (as usually done)
  621. {
  622. if (_features.size() > 0) //and have at least one example
  623. {
  624. try{
  625. if ( _features[0]->getDim() > dimTmp )
  626. {
  627. dimTmp = _features[0]->getDim();
  628. }
  629. }
  630. catch(...)
  631. {
  632. std::cerr << "FeatureMatrixT<T>::set_features -- something went wrong using getDim() of SparseVectors" << std::endl;
  633. }
  634. }
  635. }
  636. this->set_d( dimTmp );
  637. // set number of examples n
  638. if ( this->ui_d > 0 )
  639. {
  640. if ( _dimensionsOverExamples ) //do we have dim x examples ?
  641. this->ui_n = _features[0]->getDim(); //NOTE Pay attention: we assume, that this number is set!
  642. else //we have examples x dimes (as usually done)
  643. this->ui_n = _features.size();
  644. }
  645. // insert all values
  646. if ( _dimensionsOverExamples ) //do we have dim x examples ?
  647. {
  648. for (uint dim = 0; dim < this->ui_d; dim++)
  649. {
  650. this->features[dim].insert( _features[dim] );
  651. }
  652. }
  653. else //we have examples x dimes (as usually done)
  654. {
  655. if ( this->b_debug )
  656. std::cerr << "FeatureMatrixT<T>::set_features " << this->ui_n << " new examples" << std::endl;
  657. //loop over every example to add its content
  658. for (uint nr = 0; nr < this->ui_n; nr++)
  659. {
  660. if ( this->b_debug )
  661. std::cerr << "add feature nr. " << nr << " / " << _features.size() << " ";
  662. //loop over every dimension to add the specific value to the corresponding SortedVectorSparse
  663. for (NICE::SparseVector::const_iterator elemIt = _features[nr]->begin(); elemIt != _features[nr]->end(); elemIt++)
  664. {
  665. if ( this->b_debug )
  666. std::cerr << elemIt->first << "-" << elemIt->second << " ";
  667. //elemIt->first: dim, elemIt->second: value
  668. this->features[elemIt->first].insert( (T) elemIt->second, true /* _specifyFeatureNumber */, nr);
  669. }//for non-zero-values of the feature
  670. if ( this->b_debug )
  671. std::cerr << std::endl;
  672. }//for every new feature
  673. if ( this->b_debug )
  674. std::cerr << "FeatureMatrixT<T>::set_features done" << std::endl;
  675. }//if dimOverEx
  676. //set n for the internal data structure SortedVectorSparse
  677. for (typename std::vector<NICE::SortedVectorSparse<T> >::iterator it = this->features.begin(); it != this->features.end(); it++)
  678. (*it).setN( this->ui_n );
  679. }
  680. template <typename T>
  681. void FeatureMatrixT<T>::getPermutations( std::vector<std::vector<uint> > & _permutations) const
  682. {
  683. for (uint dim = 0; dim < this->ui_d; dim++)
  684. {
  685. std::vector<uint> perm ( (this->features[dim]).getPermutation() );
  686. _permutations.push_back(perm);
  687. }
  688. }
  689. template <typename T>
  690. void FeatureMatrixT<T>::getPermutations( std::vector<std::map<uint,uint> > & _permutations) const
  691. {
  692. for (uint dim = 0; dim < this->ui_d; dim++)
  693. {
  694. std::map<uint,uint> perm ( (this->features[dim]).getPermutationNonZeroReal() );
  695. _permutations.push_back(perm);
  696. }
  697. }
  698. // Prints the whole Matrix (outer loop over dimension, inner loop over features)
  699. template <typename T>
  700. void FeatureMatrixT<T>::print(std::ostream & _os) const
  701. {
  702. if (_os.good())
  703. {
  704. for (uint dim = 0; dim < this->ui_d; dim++)
  705. {
  706. this->features[dim].print(_os);
  707. }
  708. }
  709. }
  710. // Computes the whole non-sparse matrix. WARNING: this may result in a really memory-consuming data-structure!
  711. template <typename T>
  712. void FeatureMatrixT<T>::computeNonSparseMatrix(NICE::MatrixT<T> & _matrix,
  713. bool _transpose
  714. ) const
  715. {
  716. if ( _transpose )
  717. _matrix.resize(this->get_n(),this->get_d());
  718. else
  719. _matrix.resize(this->get_d(),this->get_n());
  720. _matrix.set((T)0.0);
  721. uint dimIdx(0);
  722. for (typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin(); it != this->features.end(); it++, dimIdx++)
  723. {
  724. std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer> nonzeroIndices= (*it).nonzeroIndices();
  725. for (typename std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer>::const_iterator inIt = nonzeroIndices.begin(); inIt != nonzeroIndices.end(); inIt++)
  726. {
  727. uint featIndex = ((*inIt).second)->second.first;
  728. if ( _transpose )
  729. _matrix(featIndex,dimIdx) =((*inIt).second)->second.second;
  730. else
  731. _matrix(dimIdx,featIndex) =((*inIt).second)->second.second;
  732. }
  733. }
  734. }
  735. // Computes the whole non-sparse matrix. WARNING: this may result in a really memory-consuming data-structure!
  736. template <typename T>
  737. void FeatureMatrixT<T>::computeNonSparseMatrix(std::vector<std::vector<T> > & _matrix,
  738. bool _transpose
  739. ) const
  740. {
  741. if ( _transpose )
  742. _matrix.resize(this->get_n());
  743. else
  744. _matrix.resize(this->get_d());
  745. // resizing the matrix
  746. for ( uint i = 0 ; i < _matrix.size(); i++ )
  747. if ( _transpose )
  748. _matrix[i] = std::vector<T>(this->get_d(), 0.0);
  749. else
  750. _matrix[i] = std::vector<T>(this->get_n(), 0.0);
  751. uint dimIdx(0);
  752. for (typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin(); it != this->features.end(); it++, dimIdx++)
  753. {
  754. std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer> nonzeroIndices= (*it).nonzeroIndices();
  755. for (typename std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer>::const_iterator inIt = nonzeroIndices.begin(); inIt != nonzeroIndices.end(); inIt++)
  756. {
  757. uint featIndex = ((*inIt).second)->second.first;
  758. if ( _transpose )
  759. _matrix[featIndex][dimIdx] =((*inIt).second)->second.second;
  760. else
  761. _matrix[dimIdx][featIndex] =((*inIt).second)->second.second;
  762. }
  763. }
  764. }
  765. // Swaps to specified elements, performing a validity check
  766. template <typename T>
  767. void FeatureMatrixT<T>::swap(const uint & _row1,
  768. const uint & _col1,
  769. const uint & _row2,
  770. const uint & _col2
  771. )
  772. {
  773. if ( (_row1 < 0) || (_col1 < 0) || (_row1 > this->ui_d) || (_col1 > this->ui_n) ||
  774. (_row2 < 0) || (_col2 < 0) || (_row2 > this->ui_d) || (_col2 > this->ui_n)
  775. )
  776. {
  777. return;
  778. }
  779. else
  780. {
  781. //swap
  782. T tmp = (*this)(_row1, _col1);
  783. (*this).set(_row1, _col1, (*this)(_row2,_col2));
  784. (*this).set(_row2, _col2, tmp);
  785. }
  786. }
  787. // Swaps to specified elements, without performing a validity check
  788. template <typename T>
  789. void FeatureMatrixT<T>::swapUnsafe(const uint & _row1,
  790. const uint & _col1,
  791. const uint & _row2,
  792. const uint & _col2
  793. )
  794. {
  795. //swap
  796. T tmp = (*this)(_row1, _col1);
  797. (*this).set(_row1, _col1, (*this)(_row2,_col2));
  798. (*this).set(_row2, _col2, tmp);
  799. }
  800. template <typename T>
  801. void FeatureMatrixT<T>::hikDiagonalElements( Vector & _diagonalElements ) const
  802. {
  803. uint dimIdx = 0;
  804. // the function calculates the diagonal elements of a HIK kernel matrix
  805. _diagonalElements.resize(this->ui_n);
  806. _diagonalElements.set(0.0);
  807. // loop through all dimensions
  808. for (typename std::vector<NICE::SortedVectorSparse<T> >::const_iterator it = this->features.begin(); it != this->features.end(); it++, dimIdx++)
  809. {
  810. std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer> nonzeroIndices= (*it).nonzeroIndices();
  811. // loop through all features
  812. for (typename std::map< uint, typename NICE::SortedVectorSparse<T>::elementpointer>::const_iterator inIt = nonzeroIndices.begin(); inIt != nonzeroIndices.end(); inIt++)
  813. {
  814. uint index = inIt->first;
  815. typename NICE::SortedVectorSparse<T>::elementpointer p = inIt->second;
  816. typename NICE::SortedVectorSparse<T>::dataelement de = p->second;
  817. _diagonalElements[index] += de.second;
  818. }
  819. }
  820. }
  821. template <typename T>
  822. double FeatureMatrixT<T>::hikTrace() const
  823. {
  824. Vector diagonalElements;
  825. this->hikDiagonalElements( diagonalElements );
  826. return diagonalElements.Sum();
  827. }
  828. template <typename T>
  829. uint FeatureMatrixT<T>::getNumberOfNonZeroElementsPerDimension(const uint & dim) const
  830. {
  831. //FIXME we could return a boolean indicating success and return the actual number via call-by-reference
  832. if ( (dim < 0) || (dim > this->ui_d))
  833. return 0;
  834. return this->features[dim].getNonZeros();
  835. }
  836. template <typename T>
  837. uint FeatureMatrixT<T>::getNumberOfZeroElementsPerDimension(const uint & dim) const
  838. {
  839. if ( (dim < 0) || (dim > this->ui_d))
  840. return 0;
  841. return this->ui_n - this-> features[dim].getNonZeros();
  842. }
  843. template <typename T>
  844. void FeatureMatrixT<T>::restore ( std::istream & _is,
  845. int _format
  846. )
  847. {
  848. bool b_restoreVerbose ( false );
  849. if ( _is.good() )
  850. {
  851. if ( b_restoreVerbose )
  852. std::cerr << " restore FeatureMatrixT" << std::endl;
  853. std::string tmp;
  854. _is >> tmp; //class name
  855. if ( ! this->isStartTag( tmp, "FeatureMatrixT" ) )
  856. {
  857. std::cerr << " WARNING - attempt to restore FeatureMatrixT, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  858. throw;
  859. }
  860. _is.precision ( std::numeric_limits<double>::digits10 + 1);
  861. bool b_endOfBlock ( false ) ;
  862. while ( !b_endOfBlock )
  863. {
  864. _is >> tmp; // start of block
  865. if ( this->isEndTag( tmp, "FeatureMatrixT" ) )
  866. {
  867. b_endOfBlock = true;
  868. continue;
  869. }
  870. tmp = this->removeStartTag ( tmp );
  871. if ( b_restoreVerbose )
  872. std::cerr << " currently restore section " << tmp << " in FeatureMatrixT" << std::endl;
  873. if ( tmp.compare("ui_n") == 0 )
  874. {
  875. _is >> this->ui_n;
  876. _is >> tmp; // end of block
  877. tmp = this->removeEndTag ( tmp );
  878. }
  879. else if ( tmp.compare("ui_d") == 0 )
  880. {
  881. _is >> this->ui_d;
  882. _is >> tmp; // end of block
  883. tmp = this->removeEndTag ( tmp );
  884. }
  885. else if ( tmp.compare("features") == 0 )
  886. {
  887. //NOTE assumes d to be read first!
  888. this->features.resize( this->ui_d);
  889. //now read features for every dimension
  890. for (uint dim = 0; dim < this->ui_d; dim++)
  891. {
  892. NICE::SortedVectorSparse<T> svs;
  893. this->features[dim] = svs;
  894. this->features[dim].restore(_is, _format);
  895. }
  896. _is >> tmp; // end of block
  897. tmp = this->removeEndTag ( tmp );
  898. }
  899. else
  900. {
  901. std::cerr << "WARNING -- unexpected FeatureMatrixT object -- " << tmp << " -- for restoration... aborting" << std::endl;
  902. throw;
  903. }
  904. }
  905. }
  906. else
  907. {
  908. std::cerr << "FeatureMatrixT<T>::restore -- InStream not initialized - restoring not possible!" << std::endl;
  909. throw;
  910. }
  911. }
  912. template <typename T>
  913. void FeatureMatrixT<T>::store ( std::ostream & _os,
  914. int _format
  915. ) const
  916. {
  917. if (_os.good())
  918. {
  919. // show starting point
  920. _os << this->createStartTag( "FeatureMatrixT" ) << std::endl;
  921. _os.precision (std::numeric_limits<double>::digits10 + 1);
  922. _os << this->createStartTag( "ui_n" ) << std::endl;
  923. _os << this->ui_n << std::endl;
  924. _os << this->createEndTag( "ui_n" ) << std::endl;
  925. _os << this->createStartTag( "ui_d" ) << std::endl;
  926. _os << this->ui_d << std::endl;
  927. _os << this->createEndTag( "ui_d" ) << std::endl;
  928. //now write features for every dimension
  929. _os << this->createStartTag( "features" ) << std::endl;
  930. for (uint dim = 0; dim < this->ui_d; dim++)
  931. {
  932. this->features[dim].store(_os,_format);
  933. }
  934. _os << this->createEndTag( "features" ) << std::endl;
  935. // done
  936. _os << this->createEndTag( "FeatureMatrixT" ) << std::endl;
  937. }
  938. else
  939. {
  940. std::cerr << "FeatureMatrixT<T>::store -- OutStream not initialized - storing not possible!" << std::endl;
  941. }
  942. }
  943. template <typename T>
  944. void FeatureMatrixT<T>::clear ()
  945. {}
  946. } // namespace
  947. // #endif