RepositorySOMDataFile.h

00001 #ifndef RepositorySOMDataFile_H
00002 #define RepositorySOMDataFile_H
00003 
00010 #include <stdio.h>
00011 #include "xfile.h"
00012 #include <string>
00013 #include "TokenFinder.h"
00014 #include "WordIter.h"
00015 #include "TokenIter.h"
00016 
00017 #include <iostream>
00018 #include "ISOMDataRepository.h"
00019 #include "Data.h"
00020 #include "defs.h"
00021 
00022 
00023 using std::cout;
00024 
00025 class RepositorySOMDataFile : public ISOMDataRepository {
00026 public:
00028         void save( SOMData data );
00029 
00031         SOMData *  load( vector<string> params, int flag );
00032 };
00033 
00034 void 
00035 RepositorySOMDataFile::save( SOMData data ) {
00036         cout << "Save SOMData\n";
00037 };
00038 
00039 
00040 SOMData *
00041 RepositorySOMDataFile::load( vector<string> params, int flag  ) {
00042         long storage_type = 0;
00043         long lSize;
00044         char* buffer;
00045         typedef vector<float> VecInt;
00046         typedef vector< VecInt > MatrixInt;
00047         TvecLabel lvec;
00048         MatrixInt mat;
00049         VecInt vec;
00050         MatrixInt::iterator im;
00051         VecInt::iterator        iv;
00052         int dimension;
00053         int cont, contd;
00054         
00055   
00056         CxFile dataf( storage_type );
00057         dataf.Open(  params[0].c_str(), "rb" ); 
00058         
00059         lSize = dataf.Size();
00060 
00061         buffer = (char*) malloc (lSize);
00062                         
00063         if (buffer == NULL) exit (2);
00064                         
00065         dataf.Read( buffer, 1, lSize );
00066 
00067         string data( buffer );
00068 
00069         token::WordIter wi(data);
00070 
00071         cont = 0; contd = 1;
00072     while (wi != token::WordIter())
00073     {
00074                 if ( isValid( *wi ) ) {
00075                         if (cont==0) 
00076                         {
00077                                 dimension =  strtoint( *wi );                           
00078                         }
00079                         else 
00080                         {
00081                                 if ( contd == dimension ) 
00082                                 {
00083                                         contd = 1;
00084                                         vec.push_back( strtofloat(*wi) );
00085                                         mat.push_back( vec );
00086                                         vec.clear();                            
00087                                 }
00088                                 else
00089                                         if ( isNumeric( *wi ) ) 
00090                                         {
00091                                                 vec.push_back( strtofloat(*wi) );
00092                                                 contd++;
00093                                         }
00094                                         else
00095                                                 lvec.push_back( *wi );                                                  
00096                         }                                               
00097                         cont++;
00098                 }               
00099         ++wi;           
00100     } 
00101 
00102         TMatrix mdata = create_matrix( 1, mat.size(), 1, dimension );
00103         int i, j; i = j = 1;
00104                         
00105         for (im=mat.begin(); im<mat.end(); im++) {
00106                 j = 1;                          
00107                 for (iv=(*im).begin(); iv < (*im).end(); iv++) 
00108                 {
00109                         mdata[i][j] = *iv;                                                                      
00110                         j++;
00111                 }
00112                 i++;
00113         }
00114 
00115         SOMData * som_data = new SOMData;
00116         
00117         som_data->setDimension( dimension );
00118         som_data->setDataSize( mat.size() );
00119         som_data->setEntries( mdata );
00120         som_data->setLabel( lvec );
00121                         
00122         free(buffer);
00123         dataf.Close();
00124 
00125         return som_data;
00126 };
00127 
00128 #endif
00129 
00130 
00131 /*
00132 #ifndef RepositorySOMDataFile_H
00133 #define RepositorySOMDataFile_H
00134 
00135 
00136 
00137 #include <stdio.h>
00138 #include "xfile.h"
00139 #include <string>
00140 #include "TokenFinder.h"
00141 #include "WordIter.h"
00142 #include "TokenIter.h"
00143 
00144 #include <iostream>
00145 #include "ISOMDataRepository.h"
00146 #include "Data.h"
00147 #include "Defs.h"
00148 
00149 
00150 
00151 
00152 class RepositorySOMDataFile : public ISOMDataRepository {
00153 public:
00155         void save( SOMData data );
00156 
00158         SOMData *  load( vector<string> params, int flag );
00159 };
00160 
00161 void 
00162 RepositorySOMDataFile::save( SOMData data ) {
00163         cout << "Save SOMData\n";
00164 };
00165 
00166 
00167 SOMData *
00168 RepositorySOMDataFile::load( vector<string> params, int flag ) {
00169         long storage_type = 0;
00170         long lSize;
00171         char* buffer;
00172         typedef vector<float> VecInt;
00173         typedef vector< VecInt > MatrixInt;
00174         TvecLabel lvec;
00175         MatrixInt mat;
00176         VecInt vec;
00177         MatrixInt::iterator im;
00178         VecInt::iterator        iv;
00179         int dimension;
00180         int cont, contd;
00181         
00182   
00183         CxFile dataf( storage_type );
00184         dataf.Open(  params[0].c_str(), "rb" ); 
00185         
00186         lSize = dataf.Size();
00187 
00188         buffer = (char*) malloc (lSize);
00189                         
00190         if (buffer == NULL) exit (2);
00191                         
00192         dataf.Read( buffer, 1, lSize );
00193 
00194         string data( buffer );
00195 
00196         cout << data << endl;
00197 
00198         WordIter wi(data);
00199 
00200         cont = 0; contd = 1;
00201     while (wi != WordIter())
00202     {
00203                 if ( isValid( *wi ) ) {
00204                         if (cont==0) 
00205                         {
00206                                 dimension =  strtoint( *wi );                           
00207                         }
00208                         else 
00209                         {
00210                                 if ( contd == dimension ) 
00211                                 {
00212                                         contd = 1;
00213                                         vec.push_back( strtofloat(*wi) );
00214                                         mat.push_back( vec );
00215                                         vec.clear();                            
00216                                 }
00217                                 else
00218                                         if ( isNumeric( *wi ) ) 
00219                                         {
00220                                                 vec.push_back( strtofloat(*wi) );
00221                                                 contd++;
00222                                         }
00223                                         else
00224                                                 lvec.push_back( *wi );                                                  
00225                         }                                               
00226                         cont++;
00227                 }
00228                 
00229         ++wi;                           
00230     } 
00231 
00232         
00233         TMatrix mdata = create_matrix( 1, mat.size(), 1, dimension );
00234         int i, j; i = j = 1;
00235                         
00236         for (im=mat.begin(); im<mat.end(); im++) {
00237                 j = 1;                          
00238                 for (iv=(*im).begin(); iv < (*im).end(); iv++) 
00239                 {
00240                         mdata[i][j] = *iv;                                                                      
00241                         j++;
00242                 }
00243                 i++;
00244         }
00245 
00246         SOMData * som_data = new SOMData;
00247         
00248         som_data->setDimension( dimension );
00249         som_data->setDataSize( mat.size() );
00250         som_data->setEntries( mdata );
00251         som_data->setLabel( lvec );
00252                         
00253         free(buffer);
00254         dataf.Close();
00255 
00256   
00257 
00258         return som_data;        
00259 };
00260 
00261 #endif
00262 
00263   */

Generated on Tue Aug 7 16:03:33 2007 for SOMCode by  doxygen 1.5.3