38#include <boost/foreach.hpp>
39#include <boost/tokenizer.hpp>
43#include "DGtal/base/Common.h"
107bool LoadingStringFromFile( std::ifstream & file, std::string & value )
111 std::getline( file, value );
124void split(
const std::string & s,
char delim, std::vector< std::string > & elems )
126 std::stringstream ss( s );
128 while( std::getline( ss, item, delim ))
130 elems.push_back( item );
144int ComputeStatistics (
const std::string & inputdata1,
145 const std::string & inputdata2,
146 const unsigned int & idColumnData1,
147 const unsigned int & idColumnData2,
148 const bool & isMongeMean,
149 std::ofstream & output )
151 std::ifstream file1( inputdata1.c_str() );
152 std::ifstream file2( inputdata2.c_str() );
161 double h = - std::numeric_limits<double>::max();
163 unsigned int nb_elements = 0;
165 while(( LoadingStringFromFile( file1, s1 ) && LoadingStringFromFile( file2, s2 )) && !finish )
167 while ( s1[ 0 ] ==
'#' )
169 std::size_t p = s1.find(
"# h = " );
170 if ( p != std::string::npos )
172 h = atof((s1.erase( p, 5 )).c_str());
174 if( ! LoadingStringFromFile( file1, s1 ) )
181 while ( s2[ 0 ] ==
'#' )
183 if( ! LoadingStringFromFile( file2, s2 ) )
190 if ( s1 ==
"NA" || s1 ==
"-nan" || s1 ==
"-inf" || s1 ==
"inf" || s1 ==
"" || s1 ==
" " )
192 if ( s2 ==
"NA" || s2 ==
"-nan" || s2 ==
"-inf" || s2 ==
"inf" || s2 ==
"" || s2 ==
" " )
195 std::vector< std::string > elems1;
196 split( s1,
' ', elems1 );
197 std::vector< std::string > elems2;
198 split( s2,
' ', elems2 );
200 if( elems1.size() <= idColumnData1 )
202 std::cerr <<
"Can't found " << idColumnData1 <<
" column on file1 (" << inputdata1 <<
"). Is the file/column exist ?" << std::endl;
205 if( elems2.size() <= idColumnData2 )
207 std::cerr <<
"Can't found " << idColumnData2 <<
" column on file2 (" << inputdata2 <<
"). Is the file/column exist ?" << std::endl;
211 v1 = atof( elems1[ idColumnData1 ].c_str() );
212 v2 = atof( elems2[ idColumnData2 ].c_str() );
214 if( isMongeMean && (( v1 >= 0.0 ) ^ ( v2 >= 0.0 )))
219 absd1d2 = std::abs ( v1 - v2 );
220 if ( Linf < absd1d2 )
225 L2 += absd1d2 * absd1d2;
230 if( h == - std::numeric_limits<double>::max())
232 std::cerr <<
"Can't found h value on file1 (" << inputdata1 <<
"). Is the file exist ?" << std::endl;
236 double meanL1 = L1 / (double)nb_elements;
237 double meanL2 = ( sqrt ( L2 )) / (
double)nb_elements;
248int main(
int argc,
char** argv )
252 std::string filename1;
253 std::string filename2;
254 unsigned int column1;
255 unsigned int column2;
256 std::string output_filename;
257 bool isMongeMean {
false};
259 app.description(
"Computes satistics (L1, L2, Loo) from results of two estimators.\n Typical use example:\n \t statisticsEstimators --file1 <file1> --column1 <column1> --file2 <file2> --column2 <column2> --output <output>\n");
260 app.add_option(
"-f,--file1,1",filename1,
"File 1.")->required()->check(CLI::ExistingFile);
261 app.add_option(
"-F,--file2,2",filename2,
"File 2.")->required()->check(CLI::ExistingFile);
262 app.add_option(
"--column1,-c", column1,
"Column of file 1" )->required();
263 app.add_option(
"--column2,-C", column2,
"Column of file 2" )->required();
264 app.add_option(
"--output,-o,2", output_filename,
"Output file")->required();
265 app.add_option(
"--monge,-m", isMongeMean,
"Is from Monge mean computation (optional, default false)");
267 app.get_formatter()->column_width(40);
268 CLI11_PARSE(app, argc, argv);
271 std::ifstream inFileEmptyTest; inFileEmptyTest.open(output_filename.c_str());
272 bool isNew = inFileEmptyTest.peek() == std::ifstream::traits_type::eof(); inFileEmptyTest.close();
273 std::ofstream file( output_filename.c_str(), std::ofstream::out | std::ofstream::app );
278 <<
"L1 Mean Error | "
279 <<
"L2 Mean Error | "
284 if ( ComputeStatistics( filename1, filename2, column1, column2, isMongeMean, file ) == 0 )