SimpleFeatures.h

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) 1999-2008 Soeren Sonnenburg
00008  * Written (W) 1999-2008 Gunnar Raetsch
00009  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #ifndef _SIMPLEFEATURES__H__
00013 #define _SIMPLEFEATURES__H__
00014 
00015 #include "lib/common.h"
00016 #include "lib/Mathematics.h"
00017 #include "lib/Cache.h"
00018 #include "lib/io.h"
00019 #include "lib/Cache.h"
00020 #include "preproc/SimplePreProc.h"
00021 #include "features/Features.h"
00022 
00023 #include <string.h>
00024 
00025 
00026 template <class ST> class CSimpleFeatures;
00027 template <class ST> class CSimplePreProc;
00028 
00055 template <class ST> class CSimpleFeatures: public CFeatures
00056 {
00057     public:
00062         CSimpleFeatures(int32_t size=0)
00063         : CFeatures(size), num_vectors(0), num_features(0),
00064             feature_matrix(NULL), feature_cache(NULL) {}
00065 
00067         CSimpleFeatures(const CSimpleFeatures & orig)
00068         : CFeatures(orig), num_vectors(orig.num_vectors),
00069             num_features(orig.num_features),
00070             feature_matrix(orig.feature_matrix),
00071             feature_cache(orig.feature_cache)
00072         {
00073             if (orig.feature_matrix)
00074             {
00075                 free_feature_matrix();
00076                 feature_matrix=new ST(num_vectors*num_features);
00077                 memcpy(feature_matrix, orig.feature_matrix, sizeof(float64_t)*num_vectors*num_features);
00078             }
00079         }
00080 
00087         CSimpleFeatures(ST* fm, int32_t num_feat, int32_t num_vec)
00088         : CFeatures(0), num_vectors(0), num_features(0),
00089             feature_matrix(NULL), feature_cache(NULL)
00090         {
00091             set_feature_matrix(fm, num_feat, num_vec);
00092         }
00093 
00100         CSimpleFeatures(char* fname)
00101         : CFeatures(fname), num_vectors(0), num_features(0),
00102             feature_matrix(NULL), feature_cache(NULL) {}
00103 
00108         virtual CFeatures* duplicate() const
00109         {
00110             return new CSimpleFeatures<ST>(*this);
00111         }
00112 
00113         virtual ~CSimpleFeatures()
00114         {
00115             SG_DEBUG("deleting simplefeatures (0x%p)\n", this);
00116             free_features();
00117         }
00118 
00122         void free_feature_matrix()
00123         {
00124             delete[] feature_matrix;
00125             feature_matrix = NULL;
00126             num_vectors=0;
00127             num_features=0;
00128         }
00129 
00133         void free_features()
00134         {
00135             free_feature_matrix();
00136             delete feature_cache;
00137             feature_cache = NULL;
00138         }
00139 
00150         ST* get_feature_vector(int32_t num, int32_t& len, bool& dofree)
00151         {
00152             len=num_features;
00153 
00154             if (feature_matrix)
00155             {
00156                 dofree=false;
00157                 return &feature_matrix[num*num_features];
00158             } 
00159             else
00160             {
00161                 SG_DEBUG( "compute feature!!!\n") ;
00162 
00163                 ST* feat=NULL;
00164                 dofree=false;
00165 
00166                 if (feature_cache)
00167                 {
00168                     feat=feature_cache->lock_entry(num);
00169 
00170                     if (feat)
00171                         return feat;
00172                     else
00173                     {
00174                         feat=feature_cache->set_entry(num);
00175                     }
00176                 }
00177 
00178                 if (!feat)
00179                     dofree=true;
00180                 feat=compute_feature_vector(num, len, feat);
00181 
00182 
00183                 if (get_num_preproc())
00184                 {
00185                     int32_t tmp_len=len;
00186                     ST* tmp_feat_before = feat;
00187                     ST* tmp_feat_after = NULL;
00188 
00189                     for (int32_t i=0; i<get_num_preproc(); i++)
00190                     {
00191                         tmp_feat_after=((CSimplePreProc<ST>*) get_preproc(i))->apply_to_feature_vector(tmp_feat_before, tmp_len);
00192 
00193                         if (i!=0)   // delete feature vector, except for the the first one, i.e., feat
00194                             delete[] tmp_feat_before;
00195                         tmp_feat_before=tmp_feat_after;
00196                     }
00197 
00198                     memcpy(feat, tmp_feat_after, sizeof(ST)*tmp_len);
00199                     delete[] tmp_feat_after;
00200 
00201                     len=tmp_len ;
00202                     SG_DEBUG( "len: %d len2: %d\n", len, num_features);
00203                 }
00204                 return feat ;
00205             }
00206         }
00207 
00214         void free_feature_vector(ST* feat_vec, int32_t num, bool dofree)
00215         {
00216             if (feature_cache)
00217                 feature_cache->unlock_entry(num);
00218 
00219             if (dofree)
00220                 delete[] feat_vec ;
00221         }
00222 
00230         void get_fm(ST** dst, int32_t* d1, int32_t* d2)
00231         {
00232             ASSERT(feature_matrix);
00233 
00234             int64_t num=num_features*num_vectors;
00235             *d1=num_features;
00236             *d2=num_vectors;
00237             *dst=(ST*) malloc(sizeof(ST)*num);
00238             memcpy(*dst, feature_matrix, num * sizeof(ST));
00239         }
00240 
00248         ST* get_feature_matrix(int32_t &num_feat, int32_t &num_vec)
00249         {
00250             num_feat=num_features;
00251             num_vec=num_vectors;
00252             return feature_matrix;
00253         }
00254 
00265         virtual void set_feature_matrix(ST* fm, int32_t num_feat, int32_t num_vec)
00266         {
00267             free_feature_matrix();
00268             feature_matrix=fm;
00269             num_features=num_feat;
00270             num_vectors=num_vec;
00271         }
00272 
00282         virtual void copy_feature_matrix(ST* src, int32_t num_feat, int32_t num_vec)
00283         {
00284             free_feature_matrix();
00285             feature_matrix=new ST[((int64_t) num_feat)*num_vec];
00286             memcpy(feature_matrix, src, (sizeof(ST)*((int64_t) num_feat)*num_vec));
00287 
00288             num_features=num_feat;
00289             num_vectors=num_vec;
00290         }
00291 
00297         virtual bool apply_preproc(bool force_preprocessing=false)
00298         {
00299             SG_DEBUG( "force: %d\n", force_preprocessing);
00300 
00301             if ( feature_matrix && get_num_preproc())
00302             {
00303 
00304                 for (int32_t i=0; i<get_num_preproc(); i++)
00305                 { 
00306                     if ( (!is_preprocessed(i) || force_preprocessing) )
00307                     {
00308                         set_preprocessed(i);
00309 
00310                         SG_INFO( "preprocessing using preproc %s\n", get_preproc(i)->get_name());
00311                         if (((CSimplePreProc<ST>*) get_preproc(i))->apply_to_feature_matrix(this) == NULL)
00312                             return false;
00313                     }
00314                 }
00315                 return true;
00316             }
00317             else
00318             {
00319                 if (!feature_matrix)
00320                     SG_ERROR( "no feature matrix\n");
00321 
00322                 if (!get_num_preproc())
00323                     SG_ERROR( "no preprocessors available\n");
00324 
00325                 return false;
00326             }
00327         }
00328 
00333         virtual int32_t get_size() { return sizeof(ST); }
00334 
00335 
00340         virtual inline int32_t  get_num_vectors() { return num_vectors; }
00341 
00346         inline int32_t  get_num_features() { return num_features; }
00347 
00352         inline void set_num_features(int32_t num)
00353         { 
00354             num_features= num;
00355 
00356             if (num_features && num_vectors)
00357             {
00358                 delete feature_cache;
00359                 feature_cache= new CCache<ST>(get_cache_size(), num_features, num_vectors);
00360             }
00361         }
00362 
00367         inline void set_num_vectors(int32_t num)
00368         {
00369             num_vectors= num;
00370             if (num_features && num_vectors)
00371             {
00372                 delete feature_cache;
00373                 feature_cache= new CCache<ST>(get_cache_size(), num_features, num_vectors);
00374             }
00375         }
00376 
00381         inline virtual EFeatureClass get_feature_class() { return C_SIMPLE; }
00382 
00387         inline virtual EFeatureType get_feature_type();
00388 
00395         virtual bool reshape(int32_t p_num_features, int32_t p_num_vectors)
00396         {
00397             if (p_num_features*p_num_vectors == this->num_features * this->num_vectors)
00398             {
00399                 this->num_features=p_num_features;
00400                 this->num_vectors=p_num_vectors;
00401                 return true;
00402             }
00403             else
00404                 return false;
00405         }
00406 
00407     protected:
00419         virtual ST* compute_feature_vector(int32_t num, int32_t& len, ST* target=NULL)
00420         {
00421             len=0;
00422             return NULL;
00423         }
00424 
00426         int32_t num_vectors;
00427 
00429         int32_t num_features;
00430 
00432         ST* feature_matrix;
00433 
00435         CCache<ST>* feature_cache;
00436 };
00437 
00442 template<> inline EFeatureType CSimpleFeatures<float64_t>::get_feature_type()
00443 {
00444     return F_DREAL;
00445 }
00446 
00451 template<> inline EFeatureType CSimpleFeatures<float32_t>::get_feature_type()
00452 {
00453     return F_SHORTREAL;
00454 }
00455 
00460 template<> inline EFeatureType CSimpleFeatures<int16_t>::get_feature_type()
00461 {
00462     return F_SHORT;
00463 }
00464 
00469 template<> inline EFeatureType CSimpleFeatures<char>::get_feature_type()
00470 {
00471     return F_CHAR;
00472 }
00473 
00478 template<> inline EFeatureType CSimpleFeatures<uint8_t>::get_feature_type()
00479 {
00480     return F_BYTE;
00481 }
00482 
00487 template<> inline EFeatureType CSimpleFeatures<int32_t>::get_feature_type()
00488 {
00489     return F_INT;
00490 }
00491 
00496 template<> inline EFeatureType CSimpleFeatures<uint16_t>::get_feature_type()
00497 {
00498     return F_WORD;
00499 }
00500 
00505 template<> inline EFeatureType CSimpleFeatures<uint64_t>::get_feature_type()
00506 {
00507     return F_ULONG;
00508 }
00509 #endif

SHOGUN Machine Learning Toolbox - Documentation