Improved error messages of Evidence::addEvidenceTabFile
authorJoris Mooij <joris.mooij@tuebingen.mpg.de>
Wed, 11 Nov 2009 13:49:39 +0000 (14:49 +0100)
committerJoris Mooij <joris.mooij@tuebingen.mpg.de>
Wed, 11 Nov 2009 13:49:39 +0000 (14:49 +0100)
include/dai/emalg.h
src/evidence.cpp

index ec523b4..5a47a0e 100644 (file)
@@ -25,9 +25,6 @@
 
 /// \file
 /// \brief Defines classes related to Expectation Maximization: EMAlg, ParameterEstimation, CondProbEstimation and SharedParameters
-/// \todo Describe EM file format
-/// \todo Improve documentation
-/// \author Charles Vaske
 
 
 namespace dai {
index 1452166..f712491 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <dai/util.h>
 #include <dai/evidence.h>
+#include <boost/lexical_cast.hpp>
 
 
 namespace dai {
@@ -35,41 +36,44 @@ void Evidence::addEvidenceTabFile( std::istream &is, FactorGraph &fg ) {
 void Evidence::addEvidenceTabFile( std::istream &is, std::map<std::string, Var> &varMap ) {
     std::string line;
     getline( is, line );
+    size_t line_number = 0;
 
     // Parse header
     std::vector<std::string> header_fields;
     tokenizeString( line, header_fields );
     std::vector<std::string>::const_iterator p_field = header_fields.begin();
     if( p_field == header_fields.end() )
-        DAI_THROW(INVALID_EVIDENCE_FILE);
+        DAI_THROWE(INVALID_EVIDENCE_FILE,"Empty header line");
 
     std::vector<Var> vars;
     for( ; p_field != header_fields.end(); ++p_field ) {
         std::map<std::string, Var>::iterator elem = varMap.find( *p_field );
         if( elem == varMap.end() )
-            DAI_THROW(INVALID_EVIDENCE_FILE);
+            DAI_THROWE(INVALID_EVIDENCE_FILE,"Variable " + *p_field + " not known");
         vars.push_back( elem->second );
     }
 
     // Read samples
     while( getline(is, line) ) {
+        line_number++;
+
         std::vector<std::string> fields;
         tokenizeString( line, fields );
         if( fields.size() != vars.size() )
-            DAI_THROW(INVALID_EVIDENCE_FILE);
+            DAI_THROWE(INVALID_EVIDENCE_FILE,"Invalid number of fields in line " + boost::lexical_cast<std::string>(line_number));
 
-        Observation sampleData;
+        Observation sample;
         for( size_t i = 0; i < vars.size(); ++i ) {
             if( fields[i].size() > 0 ) { // skip if missing observation
                 if( fields[i].find_first_not_of("0123456789") != std::string::npos )
-                    DAI_THROW(INVALID_EVIDENCE_FILE);
+                    DAI_THROWE(INVALID_EVIDENCE_FILE,"Invalid state " + fields[i] + " in line " + boost::lexical_cast<std::string>(line_number));
                 size_t state = atoi( fields[i].c_str() );
                 if( state >= vars[i].states() )
-                    DAI_THROW(INVALID_EVIDENCE_FILE);
-                sampleData[vars[i]] = state;
+                    DAI_THROWE(INVALID_EVIDENCE_FILE,"State " + fields[i] + " too large in line " + boost::lexical_cast<std::string>(line_number));
+                sample[vars[i]] = state;
             }
         }
-        _samples.push_back( sampleData );
+        _samples.push_back( sample );
     } // finished sample line
 }