testRANSACRegression.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * @file testRANSACRegression.cpp
  3. * @brief test of RANSAC regression
  4. * @author Frank Prüfer
  5. * @date 09/11/2013
  6. */
  7. #include <sstream>
  8. #include <iostream>
  9. #include <fstream>
  10. #include <sstream>
  11. #include <string>
  12. #include <vector>
  13. #include <stdlib.h>
  14. #include <assert.h>
  15. #include "core/basics/Config.h"
  16. #include "core/vector/VectorT.h"
  17. #include "core/vector/VVector.h"
  18. #include "vislearning/baselib/ICETools.h"
  19. #include "vislearning/regression/linregression/RANSACReg.h"
  20. using namespace OBJREC;
  21. using namespace NICE;
  22. using namespace std;
  23. void csvline_populate ( vector<string> &record,
  24. const string& line,
  25. char delimiter )
  26. {
  27. int linepos=0;
  28. int inquotes=false;
  29. char c;
  30. int linemax=line.length();
  31. string curstring;
  32. record.clear();
  33. while(line[linepos]!=0 && linepos < linemax)
  34. {
  35. c = line[linepos];
  36. if (!inquotes && curstring.length()==0 && c=='"')
  37. {
  38. //beginquotechar
  39. inquotes=true;
  40. }
  41. else if (inquotes && c=='"')
  42. {
  43. //quotechar
  44. if ( (linepos+1 <linemax) && (line[linepos+1]=='"') )
  45. {
  46. //encountered 2 double quotes in a row (resolves to 1 double quote)
  47. curstring.push_back(c);
  48. linepos++;
  49. }
  50. else
  51. {
  52. //endquotechar
  53. inquotes=false;
  54. }
  55. }
  56. else if (!inquotes && c==delimiter)
  57. {
  58. //end of field
  59. record.push_back( curstring );
  60. curstring="";
  61. }
  62. else if (!inquotes && (c=='\r' || c=='\n') )
  63. {
  64. record.push_back( curstring );
  65. return;
  66. }
  67. else
  68. {
  69. curstring.push_back(c);
  70. }
  71. linepos++;
  72. }
  73. record.push_back( curstring );
  74. }
  75. void loadData( NICE::VVector &Data,
  76. NICE::Vector &y,
  77. const string &path,
  78. const string &xdat,
  79. const string &ydat )
  80. {
  81. vector<string> row;
  82. string line;
  83. cerr<<"Preloading Data...";
  84. ifstream in( (path+xdat).c_str() );
  85. if ( in.fail() )
  86. {
  87. cout << "File not found" <<endl;
  88. exit(EXIT_FAILURE);
  89. }
  90. int numData = 0;
  91. while ( getline(in, line) && in.good() )
  92. {
  93. csvline_populate(row, line, ',');
  94. vector<double> vec;
  95. for (int i = 0; i < (int)row.size(); i++)
  96. {
  97. double dval = 0.0;
  98. dval = atof(row[i].data() );
  99. vec.push_back(dval);
  100. }
  101. NICE::Vector nvec(vec);
  102. Data.push_back(nvec);
  103. numData++;
  104. }
  105. in.close();
  106. cerr<<"Finished."<<endl<<"Starting to get preloaded Labels...";
  107. in.open( (path+ydat).c_str() );
  108. if ( in.fail() )
  109. {
  110. cout << "File not found! Setting default value 0.0..." <<endl;
  111. y.resize(numData);
  112. y.set(0.0);
  113. }
  114. else
  115. {
  116. y.resize(numData);
  117. int count = 0;
  118. while(getline(in, line) && in.good() )
  119. {
  120. csvline_populate(row, line, ',');
  121. for ( int i = 0; i < (int)row.size(); i++ )
  122. {
  123. double dval = 0.0;
  124. dval = atof(row[i].data() );
  125. y.set(count,dval);
  126. count++;
  127. }
  128. }
  129. in.close();
  130. }
  131. cerr<<"Finished."<<endl;
  132. }
  133. void testFrame ( Config conf,
  134. NICE::VVector &xdata,
  135. NICE::Vector &y )
  136. {
  137. cerr<<"\nStarting test framework..."<<endl;
  138. /*------------Initialize Variables-----------*/
  139. ofstream storeEvalData;
  140. double trainRatio = conf.gD( "debug", "training_ratio", .9 );
  141. int trainingSize = (int)(trainRatio*xdata.size());
  142. int testingSize = xdata.size() - trainingSize;
  143. vector<int> indices;
  144. for ( int i = 0; i < (int)xdata.size(); i++ )
  145. indices.push_back(i);
  146. int nfolds = conf.gI( "debug", "nfolds", 10 );
  147. Vector mef_v ( nfolds );
  148. Vector corr_v ( nfolds );
  149. Vector resub_v ( nfolds );
  150. Vector diff_v ( nfolds );
  151. bool saveConfig = conf.gB( "debug", "save_config", false );
  152. /*------------Store Configuration------------*/
  153. string filename = conf.gS( "debug", "filename" );
  154. if ( saveConfig )
  155. {
  156. cout << "Configuration will be stored in: " << filename << "_config" << endl;
  157. storeEvalData.open ( (filename+"_config").c_str() );
  158. storeEvalData.close();
  159. } else
  160. {
  161. cout << "Configuration will not be stored." << endl;
  162. }
  163. /*------------Setting up PreRDF--------------*/
  164. for ( int k = 0; k < nfolds; k++)
  165. {
  166. string fold;
  167. ostringstream convert;
  168. convert << k;
  169. fold = convert.str();
  170. cout << "\nFOLD " << k << ":\n======" << endl;
  171. cerr << "Initializing LinRegression...";
  172. RANSACReg *RReg = new RANSACReg ( &conf );
  173. cerr << "Finished." << endl;
  174. cerr << "Teaching the LinRegression algorithm...";
  175. NICE::VVector trainData, testData;
  176. NICE::Vector trainVals ( trainingSize );
  177. NICE::Vector testVals ( testingSize );
  178. random_shuffle( indices.begin(), indices.end() );
  179. for ( int i = 0; i < trainingSize; i++ )
  180. {
  181. trainData.push_back ( xdata[ indices[i] ] );
  182. trainVals.set( i, y[ indices[i] ] );
  183. }
  184. for ( int j = 0; j < testingSize; j++ )
  185. {
  186. testData.push_back ( xdata[ indices[j+trainingSize] ] );
  187. testVals.set( j, y[ indices[j+trainingSize] ] );
  188. }
  189. RReg->teach ( trainData, trainVals );
  190. cerr << "Finished." << endl;
  191. /*-------------Testing RDF-GP--------------*/
  192. cerr << "\nGetting prediction values for all data points...";
  193. NICE::Vector predictionValues( testingSize );
  194. predictionValues.set ( 0.0 );
  195. for ( int j = 0; j < testingSize; j++ )
  196. {
  197. predictionValues[j] = RReg->predict( testData[j] );
  198. }
  199. cerr << "Finished." << endl;
  200. /*---------------Evaluation----------------*/
  201. NICE::Vector diff = testVals - predictionValues;
  202. double mod_var = diff.StdDev()*diff.StdDev();
  203. double tar_var = testVals.StdDev()*testVals.StdDev();
  204. mef_v.set( k, (1-mod_var/tar_var) );
  205. NICE::Vector meanv( predictionValues.size() );
  206. meanv.set( diff.Mean() );
  207. NICE::Vector lhs = diff - meanv;
  208. meanv.set( testVals.Mean() );
  209. NICE::Vector rhs = testVals - meanv;
  210. lhs *= rhs;
  211. double corr = lhs.Mean() / sqrt( diff.StdDev()*diff.StdDev()*testVals.StdDev()*testVals.StdDev() );
  212. corr_v.set( k, corr );
  213. diff *= diff;
  214. diff_v.set( k, diff.Mean());
  215. resub_v.set( k, (diff.Mean() / tar_var) );
  216. }
  217. /*------------------Output-------------------*/
  218. cout << "\nSimple Cross Validation Stats:\n==============================" << endl;
  219. cout << " Modelling Efficiency: " << mef_v.Mean() << endl;
  220. cout << " Correlation: " << corr_v.Mean() << endl;
  221. cout << " Mean Square Error: " << diff_v.Mean() << endl;
  222. cout << " Standardized MSE: " << resub_v.Mean() << endl;
  223. }
  224. int main (int argc, char **argv) {
  225. Config conf ( argc, argv ); //get config from user input
  226. string path = conf.gS( "debug", "path", "." );
  227. string dataset = conf.gS( "debug", "dataset", "flux" );
  228. NICE::VVector xdata;
  229. NICE::Vector y;
  230. loadData(xdata, y, path, (dataset+"_x.csv"), (dataset+"_y.csv") ); //load all data
  231. testFrame( conf, xdata, y );
  232. return 0;
  233. }