LabeledSet.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * @file LabeledSet.h
  3. * @brief Labeled set of vectors
  4. * @author Erik Rodner
  5. * @date 07.09.2007
  6. */
  7. #ifndef LABELEDSETINCLUDE
  8. #define LABELEDSETINCLUDE
  9. #include <vector>
  10. #include <map>
  11. #include "core/basics/Persistent.h"
  12. #include "core/vector/VVector.h"
  13. #include "LabeledSetSelection.h"
  14. #include "ImageInfo.h"
  15. #define LOOP_ALL(ls) for(LabeledSetVector::const_iterator iLOOP_ALL = (ls).begin() ; iLOOP_ALL != (ls).end() ; iLOOP_ALL++) \
  16. for ( std::vector<NICE::Vector *>::const_iterator jLOOP_ALL = iLOOP_ALL->second.begin(); \
  17. jLOOP_ALL != iLOOP_ALL->second.end(); \
  18. jLOOP_ALL++ )
  19. #define EACH(classno,x) int (classno) = iLOOP_ALL->first; \
  20. const NICE::Vector & (x) = *(*jLOOP_ALL);
  21. #define LOOP_ALL_NONCONST(ls) for(LabeledSetVector::iterator iLOOP_ALL = (ls).begin() ; iLOOP_ALL != (ls).end() ; iLOOP_ALL++) \
  22. for ( std::vector<NICE::Vector *>::iterator jLOOP_ALL = iLOOP_ALL->second.begin(); \
  23. jLOOP_ALL != iLOOP_ALL->second.end(); \
  24. jLOOP_ALL++ )
  25. #define EACH_NONCONST(classno,x) int (classno) = iLOOP_ALL->first; \
  26. NICE::Vector & (x) = *(*jLOOP_ALL);
  27. #define LOOP_ALL_S(ls) for(LabeledSet::const_iterator iLOOP_ALL = (ls).begin() ; iLOOP_ALL != (ls).end() ; iLOOP_ALL++) \
  28. for ( std::vector<ImageInfo *>::const_iterator jLOOP_ALL = iLOOP_ALL->second.begin(); \
  29. jLOOP_ALL != iLOOP_ALL->second.end(); \
  30. jLOOP_ALL++ )
  31. #define EACH_S(classno,x) int (classno) = iLOOP_ALL->first; \
  32. const std::string & (x) = (*jLOOP_ALL)->img();
  33. #define EACH_INFO(classno,x) int (classno) = iLOOP_ALL->first; \
  34. const ImageInfo & (x) = *(*jLOOP_ALL);
  35. namespace OBJREC {
  36. class LabeledSet :
  37. public std::map< int, std::vector<ImageInfo *> >
  38. {
  39. public:
  40. typedef std::vector<std::string *> ElementIterator;
  41. typedef std::pair<size_t, const ImageInfo *> ElementPointer;
  42. typedef std::vector< ElementPointer > Permutation;
  43. private:
  44. bool selection;
  45. Permutation insertOrder;
  46. public:
  47. void add_reference ( int classno, ImageInfo *pointer );
  48. LabeledSet ( bool selection = false );
  49. ~LabeledSet ();
  50. int numClasses () const
  51. {
  52. return size();
  53. }
  54. int count ( int classno ) const;
  55. int count () const;
  56. void clear ();
  57. void add ( int classno, ImageInfo *x );
  58. void getPermutation ( Permutation & permutation ) const;
  59. void getClasses ( std::vector<int> & classes ) const;
  60. void printInformation () const;
  61. friend class LabeledSetSelection<LabeledSet>;
  62. };
  63. /** simple labeled set of vectors as a specialization of std::map<> */
  64. class LabeledSetVector :
  65. public std::map< int, std::vector<NICE::Vector *> >,
  66. public NICE::Persistent
  67. {
  68. public:
  69. typedef std::vector<NICE::Vector *> ElementIterator;
  70. typedef std::pair<int, const NICE::Vector *> ElementPointer;
  71. typedef std::vector< ElementPointer > Permutation;
  72. enum {
  73. FILEFORMAT_INDEX = 0,
  74. FILEFORMAT_NOINDEX = 1,
  75. FILEFORMAT_RAW = 2,
  76. FILEFORMAT_INDEX_SPARSE_ONE = 3
  77. };
  78. private:
  79. bool selection;
  80. Permutation insertOrder;
  81. public:
  82. LabeledSetVector ( bool selection = false );
  83. ~LabeledSetVector ();
  84. void add_reference ( int classno, NICE::Vector *pointer );
  85. int dimension () const;
  86. int numClasses () const
  87. {
  88. return size();
  89. }
  90. void restore ( std::istream & is, int format = FILEFORMAT_INDEX );
  91. void restoreRAW ( std::istream & is );
  92. void restoreASCII ( std::istream & is, int format = FILEFORMAT_INDEX );
  93. void store ( std::ostream & os, int format = FILEFORMAT_INDEX ) const;
  94. static void storeElement ( std::ostream & os, int classno, const NICE::Vector & x, int format = FILEFORMAT_INDEX );
  95. ElementPointer pickRandomSample () const;
  96. int count ( int classno ) const;
  97. /**
  98. * @brief count all features
  99. *
  100. * @return int number of features
  101. **/
  102. int count () const;
  103. int pickRandomSample ( int classno, ElementPointer & i ) const;
  104. void clear ();
  105. /** most important function: add a labeled vector */
  106. void add ( int classno, const NICE::Vector & x );
  107. void getPermutation ( Permutation & permutation ) const;
  108. void getClasses ( std::vector<int> & classes ) const;
  109. void printInformation () const;
  110. void setSelection ( bool _selection = true ) {
  111. selection = _selection;
  112. };
  113. /**
  114. * returns the highest class number (not the number of classes!)
  115. */
  116. int getMaxClassno() const;
  117. /**
  118. * converts LabeledSetVector to a NICE::VVector Set (containing data) and a Labelvector (containing labels for each Data)
  119. * @param vecSet dataset (output)
  120. * @param vecSetLabels labels (output)
  121. */
  122. void getFlatRepresentation ( NICE::VVector & vecSet, NICE::Vector & vecSetLabels ) const;
  123. /**
  124. * @brief set all pointers to the data to NULL, i.e., keep the data in storage, but remove the pointers of this data struct
  125. */
  126. void removePointersToDataWithoutDeletion();
  127. friend class LabeledSetSelection<LabeledSetVector>;
  128. };
  129. } // namespace
  130. #endif