123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
- /**
- * @file FeatureMatrixT.h
- * @brief A feature matrix, storing (sparse) features sorted per dimension (Interface)
- * @author Alexander Freytag
- * @date 07-12-2011 (dd-mm-yyyy)
- */
- #ifndef FEATUREMATRIXINCLUDE
- #define FEATUREMATRIXINCLUDE
- // STL includes
- #include <vector>
- #include <set>
- #include <map>
- #include <iostream>
- #include <limits>
- // NICE-core includes
- #include <core/basics/Exception.h>
- #include <core/basics/Persistent.h>
- //
- #include <core/vector/MatrixT.h>
- #include <core/vector/SparseVectorT.h>
- //
- #ifdef NICE_USELIB_MATIO
- #include <core/matlabAccess/MatFileIO.h>
- #endif
-
- // gp-hik-core includes
- #include "SortedVectorSparse.h"
- #include "gp-hik-core/parameterizedFunctions/ParameterizedFunction.h"
- namespace NICE {
- /**
- * @class FeatureMatrixT
- * @brief A feature matrix, storing (sparse) features sorted per dimension
- * @author Alexander Freytag
- */
-
- template<class T> class FeatureMatrixT : public NICE::Persistent
- {
- protected:
- uint ui_n;
- uint ui_d;
- std::vector<NICE::SortedVectorSparse<T> > features;
-
- //! verbose flag for output after calling the restore-function
- bool b_verbose;
- //! debug flag for output during debugging
- bool b_debug;
- public:
-
- //! STL-like typedef for type of elements
- typedef T value_type;
- //! STL-like typedef for const element reference
- typedef const T& const_reference;
- //! STL-like typedef for iterator
- typedef T* iterator;
- //! STL-like typedef for const iterator
- typedef const T* const_iterator;
- //! STL-like typedef for element reference
- typedef T& reference;
-
- //------------------------------------------------------
- // several constructors and destructors
- //------------------------------------------------------
-
- /**
- * @brief Default constructor
- * @author Alexander Freytag
- * @date 07-12-2011 (dd-mm-yyyy)
- */
- FeatureMatrixT();
-
- /**
- * @brief Recommended constructor
- * @author Alexander Freytag
- * @date 07-12-2011 (dd-mm-yyyy)
- */
- FeatureMatrixT(const std::vector<std::vector<T> > & _features,
- const uint & _dim = 0
- );
-
- #ifdef NICE_USELIB_MATIO
- /**
- * @brief Constructor reading data from matlab-files
- * @author Alexander Freytag
- * @date 10-01-2012 (dd-mm-yyyy)
- */
- FeatureMatrixT(const sparse_t & _features,
- const uint & _dim = 0
- );//, const int & nrFeatures);
- #endif
- /** just another constructor for sparse features */
- FeatureMatrixT(const std::vector< const NICE::SparseVector * > & _features,
- const bool _dimensionsOverExamples = false,
- const uint & _dim = 0
- );
-
- #ifdef NICE_USELIB_MATIO
- /**
- * @brief Constructor reading data from matlab-files and providing the possibility to
- * restrict the number of examples to a certain subset
- *
- * @param _features sparse data matrix (sett MatFileIO)
- * @param examples set of example indices
- */
- FeatureMatrixT(const sparse_t & _features,
- const std::map<uint, uint> & _examples ,
- const uint & _dim = 0);
- #endif
- /**
- * @brief Default destructor
- * @author Alexander Freytag
- * @date 07-12-2011 (dd-mm-yyyy)
- */
- ~FeatureMatrixT();
-
- //------------------------------------------------------
- // several get and set methods including access operators
- //------------------------------------------------------
-
- /**
- * @brief Get number of examples
- * @author Alexander Freytag
- * @date 07-12-2011 (dd-mm-yyyy)
- */
- uint get_n() const;
- /**
- * @brief Get number of dimensions
- * @author Alexander Freytag
- * @date 07-12-2011 (dd-mm-yyyy)
- */
- uint get_d() const;
-
- /**
- * @brief Sets the given dimension and re-sizes internal data structure. WARNING: this will completely remove your current data!
- * @author Alexander Freytag
- * @date 06-12-2011 (dd-mm-yyyy)
- */
- void set_d(const uint & _d);
-
- /** set verbose flag used for restore-functionality*/
- void setVerbose( const bool & _verbose);
- bool getVerbose( ) const;
-
- /** set debug flag used for debug output*/
- void setDebug( const bool & _debug);
- bool getDebug( ) const;
-
-
- /**
- * @brief Compare F with this
- * @author Alexander Freytag
- * @date 05-01-2012 (dd-mm-yyyy)
- * @pre Dimensions of \c F and \c this must be equal
- * @param F data to compare with
- * @return true if \c F and \c this are equal
- */
- inline bool operator==(const FeatureMatrixT<T> & _F) const;
-
- /**
- * @brief Compare \c F with \c this.
- * @author Alexander Freytag
- * @date 05-01-2012 (dd-mm-yyyy)
- * @pre Size of \c F and \c this must be equal
- * @param F data to compare with
- * @return true if \c F and \c this are not equal
- */
- inline bool operator!= (const FeatureMatrixT<T> & _F) const;
- /**
- * @brief Copy data from \c F to \c this.
- * @author Alexander Freytag
- * @date 05-01-2012 (dd-mm-yyyy)
- * @param v New data
- * @return \c *this
- */
- inline FeatureMatrixT<T>& operator=(const FeatureMatrixT<T> & _F);
-
- /**
- * @brief Matrix-like operator for element access, performs validity check
- * @author Alexander Freytag
- * @date 07-12-2011 (dd-mm-yyyy)
- */
- inline T operator()(const uint _row,
- const uint _col
- ) const;
-
- /**
- * @brief Element access without validity check
- * @author Alexander Freytag
- * @date 08-12-2011 (dd-mm-yyyy)
- */
- inline T getUnsafe(const uint _row,
- const uint _col
- ) const;
- /**
- * @brief Element access of original values without validity check
- * @author Erik Rodner
- */
- inline T getOriginal(const uint _row,
- const uint _col
- ) const;
- /**
- * @brief Sets a specified element to the given value, performs validity check
- * @author Alexander Freytag
- * @date 07-12-2011 (dd-mm-yyyy)
- */
- inline void set (const uint _row,
- const uint _col,
- const T & _newElement,
- bool _setTransformedValue = false
- );
-
- /**
- * @brief Sets a specified element to the given value, without validity check
- * @author Alexander Freytag
- * @date 08-12-2011 (dd-mm-yyyy)
- */
- inline void setUnsafe (const uint _row,
- const uint _col,
- const T & _newElement,
- bool _setTransformedValue = false
- );
-
- /**
- * @brief Access to all element entries of a specified dimension, including validity check
- * @author Alexander Freytag
- * @date 08-12-2011 (dd-mm-yyyy)
- */
- void getDimension(const uint & _dim,
- NICE::SortedVectorSparse<T> & _dimension
- ) const;
-
- /**
- * @brief Access to all element entries of a specified dimension, without validity check
- * @author Alexander Freytag
- * @date 08-12-2011 (dd-mm-yyyy)
- */
- void getDimensionUnsafe(const uint & _dim,
- NICE::SortedVectorSparse<T> & _dimension
- ) const;
-
- /**
- * @brief Finds the first element in a given dimension, which equals elem (orig feature value, not the transformed one)
- * @author Alexander Freytag
- * @date 08-12-2011 (dd-mm-yyyy)
- */
- void findFirstInDimension(const uint & _dim,
- const T & _elem,
- uint & _position
- ) const;
-
- /**
- * @brief Finds the last element in a given dimension, which equals elem (orig feature value, not the transformed one)
- * @author Alexander Freytag
- * @date 08-12-2011 (dd-mm-yyyy)1
- */
- void findLastInDimension(const uint & _dim,
- const T & _elem,
- uint & _position
- ) const;
-
- /**
- * @brief Finds the first element in a given dimension, which is larger as elem (orig feature value, not the transformed one)
- * @author Alexander Freytag
- * @date 08-12-2011 (dd-mm-yyyy)
- */
- void findFirstLargerInDimension(const uint & _dim,
- const T & elem,
- uint & position
- ) const;
-
- /**
- * @brief Finds the last element in a given dimension, which is smaller as elem (orig feature value, not the transformed one)
- * @author Alexander Freytag
- * @date 08-12-2011 (dd-mm-yyyy)
- */
- void findLastSmallerInDimension(const uint & _dim,
- const T & _elem,
- uint & _position
- ) const;
-
-
- T getLargestValue ( const bool & _getTransformedValue = false ) const;
-
- NICE::VectorT<T> getLargestValuePerDimension ( const double & _quantile=1.0,
- const bool & _getTransformedValue = false
- ) const;
-
- //------------------------------------------------------
- // high level methods
- //------------------------------------------------------
-
- /**
- * @brief apply a parameterized function to the feature matrix
- * @author Alexander Freytag
- * @date 04-05-2012 (dd-mm-yyyy)
- *
- * @param pf the parameterized function (optional), if not given, nothing will be done
- */
- void applyFunctionToFeatureMatrix ( const NICE::ParameterizedFunction *_pf = NULL );
-
- /**
- * @brief Computes the ratio of sparsity across the matrix
- * @author Alexander Freytag
- * @date 11-01-2012 (dd-mm-yyyy)
- */
- double computeSparsityRatio() const;
- /**
- * @brief add a new feature and insert its elements in the already ordered structure
- * @author Alexander Freytag
- * @date 07-12-2011 (dd-mm-yyyy)
- */
- void add_feature(const std::vector<T> & _feature,
- const NICE::ParameterizedFunction *_pf = NULL
- );
- /**
- * @brief add a new feature and insert its elements in the already ordered structure, will be casted to type T
- * @author Alexander Freytag
- * @date 25-04-2012 (dd-mm-yyyy)
- */
- void add_feature(const NICE::SparseVector & _feature,
- const NICE::ParameterizedFunction *_pf = NULL
- );
- /**
- * @brief add several new features and insert their elements in the already ordered structure
- * @author Alexander Freytag
- * @date 07-12-2011 (dd-mm-yyyy)
- */
- void add_features(const std::vector<std::vector<T> > & _features );
-
- /**
- * @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
- * @author Alexander Freytag
- * @date 07-12-2011 (dd-mm-yyyy)
- */
- void set_features(const std::vector<std::vector<T> > & _features,
- std::vector<std::vector<uint> > & _permutations,
- const uint & _dim = 0
- );
- void set_features(const std::vector<std::vector<T> > & _features,
- std::vector<std::map<uint,uint> > & _permutations,
- const uint & _dim = 0
- );
- void set_features(const std::vector<std::vector<T> > & _features,
- const uint & _dim = 0
- );
- void set_features(const std::vector< const NICE::SparseVector * > & _features,
- const bool _dimensionsOverExamples = false,
- const uint & _dim = 0
- );
-
- /**
- * @brief get a permutation vector for each dimension
- *
- * @param resulting permutation matrix
- */
- void getPermutations( std::vector<std::vector<uint> > & _permutations) const;
- void getPermutations( std::vector<std::map<uint,uint> > & _permutations) const;
-
- /**
- * @brief Prints the whole Matrix (outer loop over dimension, inner loop over features)
- * @author Alexander Freytag
- * @date 07-12-2011 (dd-mm-yyyy)
- */
- void print(std::ostream & _os) const;
-
- /**
- * @brief Computes the whole non-sparse matrix. WARNING: this may result in a really memory-consuming data-structure!
- * @author Alexander Freytag
- * @date 12-01-2012 (dd-mm-yyyy)
- */
- void computeNonSparseMatrix(NICE::MatrixT<T> & _matrix,
- bool _transpose = false
- ) const;
-
- /**
- * @brief Computes the whole non-sparse matrix. WARNING: this may result in a really memory-consuming data-structure!
- * @author Alexander Freytag
- * @date 12-01-2012 (dd-mm-yyyy)
- */
- void computeNonSparseMatrix(std::vector<std::vector<T> > & _matrix,
- bool _transpose = false
- ) const;
-
- /**
- * @brief Swaps to specified elements, performing a validity check
- * @author Alexander Freytag
- * @date 08-12-2011 (dd-mm-yyyy)
- */
- void swap(const uint & _row1,
- const uint & _col1,
- const uint & _row2,
- const uint & _col2
- );
-
- /**
- * @brief Swaps to specified elements, without performing a validity check
- * @author Alexander Freytag
- * @date 08-12-2011 (dd-mm-yyyy)
- */
- void swapUnsafe(const uint & _row1,
- const uint & _col1,
- const uint & _row2,
- const uint & _col2
- );
- /**
- * @brief direct access to elements
- *
- * @param dim feature index
- *
- * @return sorted feature values
- */
- const SortedVectorSparse<T> & getFeatureValues ( uint _dim ) const { return this->features[_dim]; };
-
- /**
- * @brief direct read/write access to elements
- *
- * @param dim feature index
- *
- * @return sorted feature values
- */
- SortedVectorSparse<T> & getFeatureValues ( uint _dim ) { return this->features[_dim]; };
-
-
- /**
- * @brief compute the diagonal elements of the HIK kernel matrix induced by the features
- *
- * @param diagonalElements resulting vector
- */
- void hikDiagonalElements( Vector & _diagonalElements ) const;
- /**
- * @brief Compute the trace of the HIK kernel matrix induced by the features
- *
- * @return value of the trace
- */
- double hikTrace() const;
-
- /**
- * @brief Return the number of nonzero elements in a specified dimension, that are currently stored in the feature matrix
- *
- * @return number of nonzero elements on the specified dimension
- */
- uint getNumberOfNonZeroElementsPerDimension(const uint & _dim) const;
-
- /**
- * @brief Return the number of zero elements in a specified dimension, that are currently stored in the feature matrix
- *
- * @return number of nonzero elements on the specified dimension
- */
- uint getNumberOfZeroElementsPerDimension(const uint & _dim) const;
-
- /** Persistent interface */
- virtual void restore ( std::istream & _is, int _format = 0 );
- virtual void store ( std::ostream & _os, int _format = 0 ) const;
- virtual void clear ( );
- };
- //! default definition for a FeatureMatrix
- typedef FeatureMatrixT<double> FeatureMatrix;
- typedef FeatureMatrixT<bool> BoolFeatureMatrix;
- typedef FeatureMatrixT<char> CharFeatureMatrix;
- typedef FeatureMatrixT<int> IntFeatureMatrix;
- typedef FeatureMatrixT<float> FloatFeatureMatrix;
- } // namespace
- #ifdef __GNUC__
- #include "gp-hik-core/FeatureMatrixT.tcc"
- #endif
- #endif
|