00001
00002
00003 #include "PhysicsTools/StatPatternRecognition/interface/SprExperiment.hh"
00004 #include "PhysicsTools/StatPatternRecognition/interface/SprVarTransformerReader.hh"
00005 #include "PhysicsTools/StatPatternRecognition/interface/SprAbsVarTransformer.hh"
00006 #include "PhysicsTools/StatPatternRecognition/interface/SprPCATransformer.hh"
00007 #include "PhysicsTools/StatPatternRecognition/src/SprMatrix.hh"
00008
00009 #include <fstream>
00010 #include <sstream>
00011 #include <utility>
00012 #include <cassert>
00013
00014 using namespace std;
00015
00016
00017 SprAbsVarTransformer* SprVarTransformerReader::read(const char* filename)
00018 {
00019
00020 string fname = filename;
00021 ifstream is(fname.c_str());
00022 if( !is ) {
00023 cerr << "Unable to open file " << fname.c_str() << endl;
00024 return 0;
00025 }
00026
00027
00028 return SprVarTransformerReader::read(is);
00029 }
00030
00031
00032 SprAbsVarTransformer* SprVarTransformerReader::read(std::istream& is)
00033 {
00034
00035 string line;
00036 unsigned nLine = 0;
00037
00038
00039 nLine++;
00040 if( !getline(is,line) ) {
00041 cerr << "Unable to read VarTransformer from line " << nLine << endl;
00042 return 0;
00043 }
00044 istringstream ist(line);
00045 string dummy, transformerName, version;
00046 ist >> dummy >> transformerName >> version;
00047
00048
00049 if( transformerName.empty() ) {
00050 cerr << "Unable to read VarTransformer name on line " << nLine << endl;
00051 return false;
00052 }
00053 SprAbsVarTransformer* t = 0;
00054 if( transformerName == "PCA" )
00055 t = SprVarTransformerReader::readPCATransformer(is,nLine);
00056 else {
00057 cerr << "Unknown VarTransformer name specified on line " << nLine << endl;
00058 return 0;
00059 }
00060 if( t == 0 ) return 0;
00061
00062
00063 vector<string> oldVars, newVars;
00064 if( !SprVarTransformerReader::readVars(is,nLine,oldVars,newVars) ||
00065 oldVars.empty() || newVars.empty() ) {
00066 cerr << "Unable to read VarTransformer variables." << endl;
00067 return 0;
00068 }
00069 t->setOldVars(oldVars);
00070 t->setNewVars(newVars);
00071
00072
00073 return t;
00074 }
00075
00076
00077 bool SprVarTransformerReader::readVars(std::istream& is, unsigned& nLine,
00078 std::vector<std::string>& oldVars,
00079 std::vector<std::string>& newVars)
00080 {
00081
00082 oldVars.clear();
00083
00084
00085 string line;
00086 for( int i=0;i<2;i++ ) {
00087 nLine++;
00088 if( !getline(is,line) ) {
00089 cerr << "Unable to read VarTransformer from line " << nLine << endl;
00090 return false;
00091 }
00092 }
00093
00094
00095 while( getline(is,line) ) {
00096 nLine++;
00097
00098
00099 line.erase( 0, line.find_first_not_of(' ') );
00100 line.erase( line.find_last_not_of(' ')+1 );
00101
00102
00103 line.erase( 0, line.find_first_not_of('=') );
00104 line.erase( line.find_last_not_of('=')+1 );
00105
00106
00107 if( line.empty() ) break;
00108
00109
00110 istringstream ist(line);
00111 int index = -1;
00112 string var;
00113 ist >> index >> var;
00114 if( index != oldVars.size() ) {
00115 cerr << "Incorrect VarTransformer variable index on line "
00116 << nLine << endl;
00117 return false;
00118 }
00119 oldVars.push_back(var);
00120 }
00121
00122
00123 newVars.clear();
00124
00125
00126 for( int i=0;i<2;i++ ) {
00127 nLine++;
00128 if( !getline(is,line) ) {
00129 cerr << "Unable to read VarTransformer from line " << nLine << endl;
00130 return false;
00131 }
00132 }
00133
00134
00135 while( getline(is,line) ) {
00136 nLine++;
00137
00138
00139 line.erase( 0, line.find_first_not_of(' ') );
00140 line.erase( line.find_last_not_of(' ')+1 );
00141
00142
00143 line.erase( 0, line.find_first_not_of('=') );
00144 line.erase( line.find_last_not_of('=')+1 );
00145
00146
00147 if( line.empty() ) break;
00148
00149
00150 istringstream ist(line);
00151 int index = -1;
00152 string var;
00153 ist >> index >> var;
00154 if( index != newVars.size() ) {
00155 cerr << "Incorrect VarTransformer variable index on line "
00156 << nLine << endl;
00157 return false;
00158 }
00159 newVars.push_back(var);
00160 }
00161
00162
00163 return true;
00164 }
00165
00166
00167 SprPCATransformer* SprVarTransformerReader::readPCATransformer(
00168 std::istream& is, unsigned& nLine)
00169 {
00170
00171 string line;
00172 nLine++;
00173 if( !getline(is,line) ) {
00174 cerr << "Unable to read VarTransformer from line " << nLine << endl;
00175 return 0;
00176 }
00177 istringstream ist(line);
00178 string dummy;
00179 int dim = -1;
00180 ist >> dummy >> dim;
00181 if( dim <= 0 ) {
00182 cerr << "Unable to read dimensionality from VarTransformer line "
00183 << nLine << endl;
00184 return 0;
00185 }
00186
00187
00188 vector<pair<double,int> > eigenValues(dim);
00189 nLine++;
00190 if( !getline(is,line) ) {
00191 cerr << "Unable to read VarTransformer from line " << nLine << endl;
00192 return 0;
00193 }
00194 istringstream ist_eigen(line);
00195 ist_eigen >> dummy;
00196 for( int d=0;d<dim;d++ )
00197 ist_eigen >> eigenValues[d].first;
00198 nLine++;
00199 if( !getline(is,line) ) {
00200 cerr << "Unable to read VarTransformer from line " << nLine << endl;
00201 return 0;
00202 }
00203 istringstream ist_index(line);
00204 ist_index >> dummy;
00205 for( int d=0;d<dim;d++ ) {
00206 ist_index >> eigenValues[d].second;
00207 assert( eigenValues[d].second >= 0 );
00208 }
00209
00210
00211 SprMatrix U(dim,dim);
00212 for( int i=0;i<dim;i++ ) {
00213 nLine++;
00214 if( !getline(is,line) ) {
00215 cerr << "Unable to read VarTransformer from line " << nLine << endl;
00216 return 0;
00217 }
00218 istringstream istU(line);
00219 int d = -1;
00220 istU >> d;
00221 if( d != i ) {
00222 cerr << "Dimension of VarTransformer does not macth on line "
00223 << nLine << endl;
00224 return 0;
00225 }
00226 for( int j=0;j<dim;j++ )
00227 istU >> dummy >> dummy >> U[i][j];
00228 }
00229
00230
00231 SprPCATransformer* t = new SprPCATransformer(U,eigenValues);
00232
00233
00234 return t;
00235 }