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 
00030 template <class ST> class CSimpleFeatures: public CFeatures
00031 {
00032     public:
00037         CSimpleFeatures(INT size=0)
00038         : CFeatures(size), num_vectors(0), num_features(0),
00039             feature_matrix(NULL), feature_cache(NULL) {}
00040 
00042         CSimpleFeatures(const CSimpleFeatures & orig)
00043         : CFeatures(orig), num_vectors(orig.num_vectors),
00044             num_features(orig.num_features),
00045             feature_matrix(orig.feature_matrix),
00046             feature_cache(orig.feature_cache)
00047         {
00048             if (orig.feature_matrix)
00049             {
00050                 free_feature_matrix();
00051                 feature_matrix=new ST(num_vectors*num_features);
00052                 memcpy(feature_matrix, orig.feature_matrix, sizeof(double)*num_vectors*num_features);
00053             }
00054         }
00055 
00062         CSimpleFeatures(ST* fm, INT num_feat, INT num_vec)
00063         : CFeatures(0), num_vectors(0), num_features(0),
00064             feature_matrix(NULL), feature_cache(NULL)
00065         {
00066             set_feature_matrix(fm, num_feat, num_vec);
00067         }
00068 
00075         CSimpleFeatures(CHAR* fname)
00076         : CFeatures(fname), num_vectors(0), num_features(0),
00077             feature_matrix(NULL), feature_cache(NULL) {}
00078 
00083         virtual CFeatures* duplicate() const
00084         {
00085             return new CSimpleFeatures<ST>(*this);
00086         }
00087 
00088         virtual ~CSimpleFeatures()
00089         {
00090             SG_DEBUG("deleting simplefeatures (0x%p)\n", this);
00091             free_features();
00092         }
00093 
00097         void free_feature_matrix()
00098         {
00099             delete[] feature_matrix;
00100             feature_matrix = NULL;
00101             num_vectors=0;
00102             num_features=0;
00103         }
00104 
00108         void free_features()
00109         {
00110             free_feature_matrix();
00111             delete feature_cache;
00112             feature_cache = NULL;
00113         }
00114 
00125         ST* get_feature_vector(INT num, INT& len, bool& dofree)
00126         {
00127             len=num_features;
00128 
00129             if (feature_matrix)
00130             {
00131                 dofree=false;
00132                 return &feature_matrix[num*num_features];
00133             } 
00134             else
00135             {
00136                 SG_DEBUG( "compute feature!!!\n") ;
00137 
00138                 ST* feat=NULL;
00139                 dofree=false;
00140 
00141                 if (feature_cache)
00142                 {
00143                     feat=feature_cache->lock_entry(num);
00144 
00145                     if (feat)
00146                         return feat;
00147                     else
00148                     {
00149                         feat=feature_cache->set_entry(num);
00150                     }
00151                 }
00152 
00153                 if (!feat)
00154                     dofree=true;
00155                 feat=compute_feature_vector(num, len, feat);
00156 
00157 
00158                 if (get_num_preproc())
00159                 {
00160                     INT tmp_len=len;
00161                     ST* tmp_feat_before = feat;
00162                     ST* tmp_feat_after = NULL;
00163 
00164                     for (INT i=0; i<get_num_preproc(); i++)
00165                     {
00166                         tmp_feat_after=((CSimplePreProc<ST>*) get_preproc(i))->apply_to_feature_vector(tmp_feat_before, tmp_len);
00167 
00168                         if (i!=0)   // delete feature vector, except for the the first one, i.e., feat
00169                             delete[] tmp_feat_before;
00170                         tmp_feat_before=tmp_feat_after;
00171                     }
00172 
00173                     memcpy(feat, tmp_feat_after, sizeof(ST)*tmp_len);
00174                     delete[] tmp_feat_after;
00175 
00176                     len=tmp_len ;
00177                     SG_DEBUG( "len: %d len2: %d\n", len, num_features);
00178                 }
00179                 return feat ;
00180             }
00181         }
00182 
00189         void free_feature_vector(ST* feat_vec, INT num, bool dofree)
00190         {
00191             if (feature_cache)
00192                 feature_cache->unlock_entry(num);
00193 
00194             if (dofree)
00195                 delete[] feat_vec ;
00196         }
00197 
00205         void get_fm(ST** dst, INT* d1, INT* d2)
00206         {
00207             ASSERT(feature_matrix);
00208 
00209             LONG num=num_features*num_vectors;
00210             *d1=num_features;
00211             *d2=num_vectors;
00212             *dst=(ST*) malloc(sizeof(ST)*num);
00213             memcpy(*dst, feature_matrix, num * sizeof(ST));
00214         }
00215 
00223         ST* get_feature_matrix(INT &num_feat, INT &num_vec)
00224         {
00225             num_feat=num_features;
00226             num_vec=num_vectors;
00227             return feature_matrix;
00228         }
00229 
00240         virtual void set_feature_matrix(ST* fm, INT num_feat, INT num_vec)
00241         {
00242             free_feature_matrix();
00243             feature_matrix=fm;
00244             num_features=num_feat;
00245             num_vectors=num_vec;
00246         }
00247 
00257         virtual void copy_feature_matrix(ST* src, INT num_feat, INT num_vec)
00258         {
00259             free_feature_matrix();
00260             feature_matrix=new ST[((LONG) num_feat)*num_vec];
00261             memcpy(feature_matrix, src, (sizeof(ST)*((LONG) num_feat)*num_vec));
00262 
00263             num_features=num_feat;
00264             num_vectors=num_vec;
00265         }
00266 
00272         virtual bool apply_preproc(bool force_preprocessing=false)
00273         {
00274             SG_DEBUG( "force: %d\n", force_preprocessing);
00275 
00276             if ( feature_matrix && get_num_preproc())
00277             {
00278 
00279                 for (INT i=0; i<get_num_preproc(); i++)
00280                 { 
00281                     if ( (!is_preprocessed(i) || force_preprocessing) )
00282                     {
00283                         set_preprocessed(i);
00284 
00285                         SG_INFO( "preprocessing using preproc %s\n", get_preproc(i)->get_name());
00286                         if (((CSimplePreProc<ST>*) get_preproc(i))->apply_to_feature_matrix(this) == NULL)
00287                             return false;
00288                     }
00289                 }
00290                 return true;
00291             }
00292             else
00293             {
00294                 if (!feature_matrix)
00295                     SG_ERROR( "no feature matrix\n");
00296 
00297                 if (!get_num_preproc())
00298                     SG_ERROR( "no preprocessors available\n");
00299 
00300                 return false;
00301             }
00302         }
00303 
00308         virtual INT get_size() { return sizeof(ST); }
00309 
00310 
00315         virtual inline INT  get_num_vectors() { return num_vectors; }
00316 
00321         inline INT  get_num_features() { return num_features; }
00322 
00327         inline void set_num_features(INT num)
00328         { 
00329             num_features= num;
00330 
00331             if (num_features && num_vectors)
00332             {
00333                 delete feature_cache;
00334                 feature_cache= new CCache<ST>(get_cache_size(), num_features, num_vectors);
00335             }
00336         }
00337 
00342         inline void set_num_vectors(INT num)
00343         {
00344             num_vectors= num;
00345             if (num_features && num_vectors)
00346             {
00347                 delete feature_cache;
00348                 feature_cache= new CCache<ST>(get_cache_size(), num_features, num_vectors);
00349             }
00350         }
00351 
00356         inline virtual EFeatureClass get_feature_class() { return C_SIMPLE; }
00357 
00362         inline virtual EFeatureType get_feature_type();
00363 
00370         virtual bool reshape(INT p_num_features, INT p_num_vectors)
00371         {
00372             if (p_num_features*p_num_vectors == this->num_features * this->num_vectors)
00373             {
00374                 this->num_features=p_num_features;
00375                 this->num_vectors=p_num_vectors;
00376                 return true;
00377             }
00378             else
00379                 return false;
00380         }
00381 
00382     protected:
00394         virtual ST* compute_feature_vector(INT num, INT& len, ST* target=NULL)
00395         {
00396             len=0;
00397             return NULL;
00398         }
00399 
00401         INT num_vectors;
00402 
00404         INT num_features;
00405 
00407         ST* feature_matrix;
00408 
00410         CCache<ST>* feature_cache;
00411 };
00412 
00417 template<> inline EFeatureType CSimpleFeatures<DREAL>::get_feature_type()
00418 {
00419     return F_DREAL;
00420 }
00421 
00426 template<> inline EFeatureType CSimpleFeatures<SHORTREAL>::get_feature_type()
00427 {
00428     return F_SHORTREAL;
00429 }
00430 
00435 template<> inline EFeatureType CSimpleFeatures<SHORT>::get_feature_type()
00436 {
00437     return F_SHORT;
00438 }
00439 
00444 template<> inline EFeatureType CSimpleFeatures<CHAR>::get_feature_type()
00445 {
00446     return F_CHAR;
00447 }
00448 
00453 template<> inline EFeatureType CSimpleFeatures<BYTE>::get_feature_type()
00454 {
00455     return F_BYTE;
00456 }
00457 
00462 template<> inline EFeatureType CSimpleFeatures<INT>::get_feature_type()
00463 {
00464     return F_INT;
00465 }
00466 
00471 template<> inline EFeatureType CSimpleFeatures<WORD>::get_feature_type()
00472 {
00473     return F_WORD;
00474 }
00475 
00480 template<> inline EFeatureType CSimpleFeatures<ULONG>::get_feature_type()
00481 {
00482     return F_ULONG;
00483 }
00484 #endif

SHOGUN Machine Learning Toolbox - Documentation