38#include <boost/foreach.hpp>
39#include <boost/tokenizer.hpp>
43#include "DGtal/base/Common.h"
106bool LoadingStringFromFile( std::ifstream & file, std::string & value )
110 std::getline( file, value );
123void split(
const std::string & s,
char delim, std::vector< std::string > & elems )
125 std::stringstream ss( s );
127 while( std::getline( ss, item, delim ))
129 elems.push_back( item );
143int ComputeStatistics (
const std::string & inputdata1,
144 const std::string & inputdata2,
145 const unsigned int & idColumnData1,
146 const unsigned int & idColumnData2,
147 const bool & isMongeMean,
148 std::ofstream & output )
150 std::ifstream file1( inputdata1.c_str() );
151 std::ifstream file2( inputdata2.c_str() );
160 double h = - std::numeric_limits<double>::max();
162 unsigned int nb_elements = 0;
164 while(( LoadingStringFromFile( file1, s1 ) && LoadingStringFromFile( file2, s2 )) && !finish )
166 while ( s1[ 0 ] ==
'#' )
168 std::size_t p = s1.find(
"# h = " );
169 if ( p != std::string::npos )
171 h = atof((s1.erase( p, 5 )).c_str());
173 if( ! LoadingStringFromFile( file1, s1 ) )
180 while ( s2[ 0 ] ==
'#' )
182 if( ! LoadingStringFromFile( file2, s2 ) )
189 if ( s1 ==
"NA" || s1 ==
"-nan" || s1 ==
"-inf" || s1 ==
"inf" || s1 ==
"" || s1 ==
" " )
191 if ( s2 ==
"NA" || s2 ==
"-nan" || s2 ==
"-inf" || s2 ==
"inf" || s2 ==
"" || s2 ==
" " )
194 std::vector< std::string > elems1;
195 split( s1,
' ', elems1 );
196 std::vector< std::string > elems2;
197 split( s2,
' ', elems2 );
199 if( elems1.size() <= idColumnData1 )
201 std::cerr <<
"Can't found " << idColumnData1 <<
" column on file1 (" << inputdata1 <<
"). Is the file/column exist ?" << std::endl;
204 if( elems2.size() <= idColumnData2 )
206 std::cerr <<
"Can't found " << idColumnData2 <<
" column on file2 (" << inputdata2 <<
"). Is the file/column exist ?" << std::endl;
210 v1 = atof( elems1[ idColumnData1 ].c_str() );
211 v2 = atof( elems2[ idColumnData2 ].c_str() );
213 if( isMongeMean && (( v1 >= 0.0 ) ^ ( v2 >= 0.0 )))
218 absd1d2 = std::abs ( v1 - v2 );
219 if ( Linf < absd1d2 )
224 L2 += absd1d2 * absd1d2;
229 if( h == - std::numeric_limits<double>::max())
231 std::cerr <<
"Can't found h value on file1 (" << inputdata1 <<
"). Is the file exist ?" << std::endl;
235 double meanL1 = L1 / (double)nb_elements;
236 double meanL2 = ( sqrt ( L2 )) / (
double)nb_elements;
247int main(
int argc,
char** argv )
251 std::string filename1;
252 std::string filename2;
253 unsigned int column1;
254 unsigned int column2;
255 std::string output_filename;
256 bool isMongeMean {
false};
258 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");
259 app.add_option(
"-f,--file1,1",filename1,
"File 1.")->required()->check(CLI::ExistingFile);
260 app.add_option(
"-F,--file2,2",filename2,
"File 2.")->required()->check(CLI::ExistingFile);
261 app.add_option(
"--column1,-c", column1,
"Column of file 1" )->required();
262 app.add_option(
"--column2,-C", column2,
"Column of file 2" )->required();
263 app.add_option(
"--output,-o,2", output_filename,
"Output file")->required();
264 app.add_option(
"--monge,-m", isMongeMean,
"Is from Monge mean computation (optional, default false)");
266 app.get_formatter()->column_width(40);
267 CLI11_PARSE(app, argc, argv);
270 std::ifstream inFileEmptyTest; inFileEmptyTest.open(output_filename.c_str());
271 bool isNew = inFileEmptyTest.peek() == std::ifstream::traits_type::eof(); inFileEmptyTest.close();
272 std::ofstream file( output_filename.c_str(), std::ofstream::out | std::ofstream::app );
277 <<
"L1 Mean Error | "
278 <<
"L2 Mean Error | "
283 if ( ComputeStatistics( filename1, filename2, column1, column2, isMongeMean, file ) == 0 )