RealFeatures.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) 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 #include "features/RealFeatures.h"
00013 #include "lib/File.h"
00014 
00015 bool CRealFeatures::load(CHAR* fname)
00016 {
00017     bool status=false;
00018     num_vectors=1;
00019     num_features=0;
00020     CFile f(fname, 'r', F_DREAL);
00021     LONG numf=0 ;
00022     free_feature_matrix();
00023     feature_matrix=f.load_real_data(NULL, numf);
00024     num_features=numf;
00025 
00026 
00027     if (!f.is_ok()) {
00028       SG_ERROR( "loading file \"%s\" failed", fname);
00029     }
00030     else
00031         status=true;
00032 
00033     return status;
00034 }
00035 
00036 bool CRealFeatures::save(CHAR* fname)
00037 {
00038     INT len;
00039     bool free;
00040     DREAL* fv;
00041 
00042     CFile f(fname, 'w', F_DREAL);
00043 
00044     for (INT i=0; i< (INT) num_vectors && f.is_ok(); i++)
00045     {
00046         if (!(i % (num_vectors/10+1)))
00047             SG_PRINT( "%02d%%.", (int) (100.0*i/num_vectors));
00048         else if (!(i % (num_vectors/200+1)))
00049             SG_PRINT( ".");
00050 
00051         fv=get_feature_vector(i, len, free);
00052         f.save_real_data(fv, len);
00053         free_feature_vector(fv, i, free) ;
00054     }
00055 
00056     if (f.is_ok())
00057         SG_INFO( "%d vectors with %d features each successfully written (filesize: %ld)\n", num_vectors, num_features, num_vectors*num_features*sizeof(DREAL));
00058 
00059     return true;
00060 }
00061 
00062 
00063 bool CRealFeatures::Align_char_features(CCharFeatures* cf, CCharFeatures* Ref, DREAL gapCost)
00064 {
00065     ASSERT(cf);
00066 
00067     num_vectors=cf->get_num_vectors();
00068     num_features=Ref->get_num_vectors();
00069 
00070     INT len=num_vectors*num_features;
00071     free_feature_matrix();
00072     feature_matrix=new DREAL[len];
00073     INT num_cf_feat=0;
00074     INT num_cf_vec=0;
00075     INT num_ref_feat=0;
00076     INT num_ref_vec=0;
00077     CHAR* fm_cf=cf->get_feature_matrix(num_cf_feat, num_cf_vec);
00078     CHAR* fm_ref=Ref->get_feature_matrix(num_ref_feat, num_ref_vec);
00079 
00080     ASSERT(num_cf_vec==num_vectors);
00081     ASSERT(num_ref_vec==num_features);
00082 
00083     SG_INFO( "computing aligments of %i vectors to %i reference vectors: ", num_cf_vec, num_ref_vec) ;
00084     for (INT i=0; i< num_ref_vec; i++)
00085       {
00086         if (i%10==0)
00087           SG_PRINT( "%i..", i) ;
00088         for (INT j=0; j<num_cf_vec; j++)
00089           feature_matrix[i+j*num_features] = CMath::Align(&fm_cf[j*num_cf_feat], &fm_ref[i*num_ref_feat], num_cf_feat, num_ref_feat, gapCost);
00090       } ;
00091 
00092     SG_INFO( "created %i x %i matrix (0x%p)\n", num_features, num_vectors, feature_matrix) ;
00093     return true;
00094 }

SHOGUN Machine Learning Toolbox - Documentation