38 #include <boost/foreach.hpp>
39 #include <boost/tokenizer.hpp>
40 #include <boost/program_options/options_description.hpp>
41 #include <boost/program_options/parsers.hpp>
42 #include <boost/program_options/variables_map.hpp>
44 #include "DGtal/base/Common.h"
46 using namespace DGtal;
101 bool LoadingStringFromFile( std::ifstream & file, std::string & value )
105 std::getline( file, value );
118 void split(
const std::string & s,
char delim, std::vector< std::string > & elems )
120 std::stringstream ss( s );
122 while( std::getline( ss, item, delim ))
124 elems.push_back( item );
138 int ComputeStatistics (
const std::string & inputdata1,
139 const std::string & inputdata2,
140 const unsigned int & idColumnData1,
141 const unsigned int & idColumnData2,
142 const bool & isMongeMean,
143 std::ofstream & output )
145 std::ifstream file1( inputdata1.c_str() );
146 std::ifstream file2( inputdata2.c_str() );
155 double h = - std::numeric_limits<double>::max();
157 unsigned int nb_elements = 0;
159 while(( LoadingStringFromFile( file1, s1 ) && LoadingStringFromFile( file2, s2 )) && !finish )
161 while ( s1[ 0 ] ==
'#' )
163 std::size_t p = s1.find(
"# h = " );
164 if ( p != std::string::npos )
166 h = atof((s1.erase( p, 5 )).c_str());
168 if( ! LoadingStringFromFile( file1, s1 ) )
175 while ( s2[ 0 ] ==
'#' )
177 if( ! LoadingStringFromFile( file2, s2 ) )
184 if ( s1 ==
"NA" || s1 ==
"-nan" || s1 ==
"-inf" || s1 ==
"inf" || s1 ==
"" || s1 ==
" " )
186 if ( s2 ==
"NA" || s2 ==
"-nan" || s2 ==
"-inf" || s2 ==
"inf" || s2 ==
"" || s2 ==
" " )
189 std::vector< std::string > elems1;
190 split( s1,
' ', elems1 );
191 std::vector< std::string > elems2;
192 split( s2,
' ', elems2 );
194 if( elems1.size() <= idColumnData1 )
196 std::cerr <<
"Can't found " << idColumnData1 <<
" column on file1 (" << inputdata1 <<
"). Is the file/column exist ?" << std::endl;
199 if( elems2.size() <= idColumnData2 )
201 std::cerr <<
"Can't found " << idColumnData2 <<
" column on file2 (" << inputdata2 <<
"). Is the file/column exist ?" << std::endl;
205 v1 = atof( elems1[ idColumnData1 ].c_str() );
206 v2 = atof( elems2[ idColumnData2 ].c_str() );
208 if( isMongeMean && (( v1 >= 0.0 ) ^ ( v2 >= 0.0 )))
213 absd1d2 = std::abs ( v1 - v2 );
214 if ( Linf < absd1d2 )
219 L2 += absd1d2 * absd1d2;
224 if( h == - std::numeric_limits<double>::max())
226 std::cerr <<
"Can't found h value on file1 (" << inputdata1 <<
"). Is the file exist ?" << std::endl;
230 double meanL1 = L1 / (double)nb_elements;
231 double meanL2 = ( sqrt ( L2 )) / (
double)nb_elements;
247 void missingParam( std::string param )
249 trace.
error() <<
" Parameter: " << param <<
" is required.";
254 namespace po = boost::program_options;
256 int main(
int argc,
char** argv )
258 po::options_description general_opt(
"Allowed options are");
259 general_opt.add_options()
260 (
"help,h",
"display this message")
261 (
"file1,f", po::value< std::string >(),
"File 1")
262 (
"file2,F", po::value< std::string >(),
"File 2")
263 (
"column1,c", po::value< unsigned int >(),
"Column of file 1" )
264 (
"column2,C", po::value< unsigned int >(),
"Column of file 2" )
265 (
"output,o", po::value< std::string >(),
"Output file")
266 (
"monge,m", po::value< bool >()->default_value(
false ),
"Is from Monge mean computation (optional)" );
270 po::variables_map vm;
273 po::store( po::parse_command_line( argc, argv, general_opt ), vm );
275 catch(
const std::exception & ex )
278 trace.
info() <<
"Error checking program options: " << ex.what() << std::endl;
281 if( !parseOK || vm.count(
"help") || argc <= 1 )
283 trace.
info()<<
"Compute satistics (L1, L2, Loo) from results of two estimators" <<std::endl
284 <<
"Basic usage: "<<std::endl
285 <<
"\tstatisticsEstimators --file1 <file1> --column1 <column1> --file2 <file2> --column2 <column2> --output <output>"<<std::endl
287 << general_opt << std::endl;
293 if (!(vm.count(
"file1"))) missingParam(
"--file1");
294 if (!(vm.count(
"file2"))) missingParam(
"--file2");
295 if (!(vm.count(
"column1"))) missingParam(
"--column1");
296 if (!(vm.count(
"column2"))) missingParam(
"--column2");
297 if (!(vm.count(
"output"))) missingParam(
"--output");
299 std::string filename1 = vm[
"file1"].as< std::string >();
300 std::string filename2 = vm[
"file2"].as< std::string >();
301 unsigned int column1 = vm[
"column1"].as<
unsigned int >();
302 unsigned int column2 = vm[
"column2"].as<
unsigned int >();
303 std::string output_filename = vm[
"output"].as< std::string >();
304 bool isMongeMean = vm[
"monge"].as<
bool >();
306 std::ifstream inFileEmptyTest; inFileEmptyTest.open(output_filename.c_str());
307 bool isNew = inFileEmptyTest.peek() == std::ifstream::traits_type::eof(); inFileEmptyTest.close();
308 std::ofstream file( output_filename.c_str(), std::ofstream::out | std::ofstream::app );
313 <<
"L1 Mean Error | "
314 <<
"L2 Mean Error | "
319 if ( ComputeStatistics( filename1, filename2, column1, column2, isMongeMean, file ) == 0 )
Trace trace(traceWriterTerm)