MatrixFeatures.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2012 Fernando José Iglesias García
00008  * Copyright (C) 2012 Fernando José Iglesias García
00009  */
00010 
00011 #include <shogun/features/MatrixFeatures.h>
00012 
00013 namespace shogun {
00014 
00015 template< class ST > CMatrixFeatures< ST >::CMatrixFeatures()
00016 : CFeatures(0)
00017 {
00018     init();
00019 }
00020 
00021 template< class ST > CMatrixFeatures< ST >::CMatrixFeatures(
00022         int32_t num_vecs,
00023         int32_t num_feats)
00024 : CFeatures(0)
00025 {
00026     init();
00027     m_features     = SGMatrixList< ST >(num_vecs);
00028     m_num_vectors  = num_vecs;
00029     m_num_features = num_feats;
00030 }
00031 
00032 template< class ST > CMatrixFeatures< ST >::CMatrixFeatures(
00033         SGMatrixList< ST > feats, int32_t num_feats)
00034 : CFeatures(0)
00035 {
00036     init();
00037     set_features(feats, num_feats);
00038 }
00039 
00040 template< class ST > CMatrixFeatures< ST >::CMatrixFeatures(
00041         SGMatrix< ST > feats, int32_t feat_length, int32_t num_vecs)
00042 : CFeatures(0)
00043 {
00044     REQUIRE(feats.num_cols == feat_length*num_vecs, "The number of columns of feats "
00045             "must be equal to feat_length times num_vecs\n");
00046     init();
00047     SGMatrixList< ST > feats_list = SGMatrixList< ST >::split(feats, num_vecs);
00048     set_features(feats_list, feats.num_rows);
00049 }
00050 
00051 /* TODO */
00052 template< class ST > CFeatures* CMatrixFeatures< ST >::duplicate() const
00053 {
00054     return NULL;
00055 }
00056 
00057 template< class ST > CMatrixFeatures< ST >::~CMatrixFeatures()
00058 {
00059     cleanup();
00060 }
00061 
00062 /* TODO */
00063 template< class ST > EFeatureType CMatrixFeatures< ST >::get_feature_type() const
00064 {
00065     return F_UNKNOWN;
00066 }
00067 
00068 /* TODO */
00069 template< class ST > EFeatureClass CMatrixFeatures< ST >::get_feature_class() const
00070 {
00071     return C_UNKNOWN;
00072 }
00073 
00074 /* TODO */
00075 template< class ST > int32_t CMatrixFeatures< ST >::get_size() const
00076 {
00077     return 0;
00078 }
00079 
00080 template< class ST > SGMatrix< ST > CMatrixFeatures< ST >::get_feature_vector(
00081         int32_t num) const
00082 {
00083     if ( num < 0 || num >= get_num_vectors() )
00084     {
00085         SG_ERROR("The index of the feature vector to get must be between "
00086              "0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1);
00087     }
00088 
00089     return m_features[num];
00090 }
00091 
00092 template< class ST > void CMatrixFeatures< ST >::get_feature_vector_col(
00093         SGVector< ST > out, 
00094         int32_t num, 
00095         int32_t col) const
00096 {
00097     if ( num < 0 || num >= get_num_vectors() )
00098     {
00099         SG_ERROR("The index of the feature vector to get must be between "
00100              "0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1);
00101     }
00102 
00103     // Shorthands for the dimensions of the feature vector to get
00104     int32_t num_cols = m_features[num].num_cols;
00105     int32_t num_rows = m_features[num].num_rows;
00106 
00107     if ( col < 0 || col >= num_cols )
00108     {
00109         SG_ERROR("The index of the column to get must be between "
00110              "0 and %d (#columns of the feature vector)\n", num_cols);
00111     }
00112 
00113     if ( out.vlen < get_num_features() )
00114     {
00115         SG_ERROR("The vector out must have space to hold at least "
00116              "%d (get_num_features()) elements\n", get_num_features());
00117     }
00118 
00119     int32_t start = col*num_rows;
00120     for ( int32_t i = 0 ; i < get_num_features(); ++i )
00121     {
00122         out[i] = m_features[num][start + i];
00123     }
00124 }
00125 
00126 template< class ST > void CMatrixFeatures< ST >::set_feature_vector(
00127         SGMatrix< ST > const & vec,
00128         int32_t num)
00129 {
00130     if ( num < 0 || num >= get_num_vectors() )
00131     {
00132         SG_ERROR("The index of the feature vector to set must be between "
00133              "0 and %d (get_num_vectors()-1)\n", get_num_vectors()-1);
00134     }
00135 
00136     if ( get_num_features() != 0 && vec.num_rows != get_num_features() )
00137     {
00138         SG_ERROR("The feature vector to set must have the same features "
00139              "as the rest of the MatrixFeatures, %d "
00140              "(get_num_features())\n", get_num_features());
00141     }
00142 
00143     m_features[num] = vec;
00144 }
00145 
00146 template< class ST > void CMatrixFeatures< ST >::set_features(
00147         SGMatrixList< ST > features, int32_t num_feats)
00148 {
00149     m_features     = features;
00150     m_num_vectors  = features.num_matrices;
00151     m_num_features = num_feats;
00152 }
00153 
00154 template< class ST > void CMatrixFeatures< ST >::init()
00155 {
00156     SG_ADD(&m_num_vectors, "m_num_vectors", "Number of feature vectors",
00157             MS_NOT_AVAILABLE);
00158     SG_ADD(&m_num_features, "m_num_features",
00159             "Number of features per vector (optional)", MS_NOT_AVAILABLE);
00160     //TODO add SG_ADD for SGMatrixList
00161     //SG_ADD(&m_features, "m_features", "Matrix features", MS_NOT_AVAILABLE);
00162 
00163     m_num_vectors  = 0;
00164     m_num_features = 0;
00165 }
00166 
00167 template< class ST > void CMatrixFeatures< ST >::cleanup()
00168 {
00169     m_features     = SGMatrixList< ST >();
00170     m_num_vectors  = 0;
00171     m_num_features = 0;
00172 }
00173 
00174 template class CMatrixFeatures<bool>;
00175 template class CMatrixFeatures<char>;
00176 template class CMatrixFeatures<int8_t>;
00177 template class CMatrixFeatures<uint8_t>;
00178 template class CMatrixFeatures<int16_t>;
00179 template class CMatrixFeatures<uint16_t>;
00180 template class CMatrixFeatures<int32_t>;
00181 template class CMatrixFeatures<uint32_t>;
00182 template class CMatrixFeatures<int64_t>;
00183 template class CMatrixFeatures<uint64_t>;
00184 template class CMatrixFeatures<float32_t>;
00185 template class CMatrixFeatures<float64_t>;
00186 template class CMatrixFeatures<floatmax_t>;
00187 
00188 } /* namespace shogun */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation