/// \file
-/// \brief Defines classes Evidence and SampleData
+/// \brief Defines classes Evidence and Observation
/// \todo Improve documentation
namespace dai {
-/// Stores a set of observations on a graphical model.
-class SampleData {
+/// Stores observed values of a subset of variables
+class Observation {
private:
- std::string _name;
std::map<Var, size_t> _obs;
+
public:
- /// Construct an empty object
- SampleData() : _name(), _obs() {}
- /// Set the name of the sample
- void name(const std::string& name) { _name = name; }
- /// Get the name of the sample
- const std::string& name() const { return _name; }
- /// Read from the observation map
+ /// Default constructor
+ Observation() : _obs() {}
+ /// Get all observations
const std::map<Var, size_t>& observations() const { return _obs; }
/// Add an observation
- void addObservation(Var node, size_t setting);
- /// Add evidence by clamping variables to observed values.
- void applyEvidence(InfAlg& alg) const;
+ void addObservation( Var node, size_t setting );
+ /// Clamp variables to observed values
+ void applyEvidence( InfAlg& alg ) const;
};
-/// Store observations from a graphical model.
+/// Stores multiple observations of variables
class Evidence {
private:
- std::map<std::string, SampleData> _samples;
-public:
- /// Start with empty obects, then fill with calls to addEvidenceTabFile()
- Evidence() : _samples() {}
-
- /** Read in tab-data from a stream. Each line contains one sample, and
- * the first line is a header line with names. The first column contains
- * names for each of the samples.
- */
- void addEvidenceTabFile(std::istream& is, std::map< std::string, Var >& varMap);
-
- /** Read in tab-data from a stream. Each line contains one sample,
- * and the first line is a header line with variable IDs. The first
- * column contains names for each of the samples.
- */
- void addEvidenceTabFile(std::istream& is, FactorGraph& fg);
-
- /// Total number of samples in this evidence file
- size_t nrSamples() const { return _samples.size(); }
-
- /// @name iterator interface
- //@{
- typedef std::map< std::string, SampleData >::iterator iterator;
- typedef std::map< std::string, SampleData >::const_iterator const_iterator;
- iterator begin() { return _samples.begin(); }
- const_iterator begin() const { return _samples.begin(); }
- iterator end() { return _samples.end(); }
- const_iterator end() const { return _samples.end(); }
- //@}
+ std::vector<Observation> _samples;
+
+ public:
+ /// Default constructor
+ Evidence() : _samples() {}
+
+ /** Read in tab-data from a stream. Each line contains one sample, and
+ * the first line is a header line with names. The first column contains
+ * names for each of the samples.
+ */
+ void addEvidenceTabFile(std::istream& is, std::map<std::string, Var>& varMap);
+
+ /** Read in tab-data from a stream. Each line contains one sample,
+ * and the first line is a header line with variable IDs. The first
+ * column contains names for each of the samples.
+ */
+ void addEvidenceTabFile(std::istream& is, FactorGraph& fg);
+
+ /// Total number of samples in this evidence file
+ size_t nrSamples() const { return _samples.size(); }
+
+ /// @name iterator interface
+ //@{
+ typedef std::vector<Observation>::iterator iterator;
+ typedef std::vector<Observation>::const_iterator const_iterator;
+
+ iterator begin() { return _samples.begin(); }
+ const_iterator begin() const { return _samples.begin(); }
+ iterator end() { return _samples.end(); }
+ const_iterator end() const { return _samples.end(); }
+ //@}
};
namespace dai {
-void SampleData::addObservation( Var node, size_t setting ) {
+void Observation::addObservation( Var node, size_t setting ) {
_obs[node] = setting;
}
-void SampleData::applyEvidence( InfAlg& alg ) const {
+void Observation::applyEvidence( InfAlg& alg ) const {
std::map<Var, size_t>::const_iterator i = _obs.begin();
for( ; i != _obs.end(); ++i )
alg.clamp( i->first, i->second );
if( p_field == header_fields.end() )
DAI_THROW(INVALID_EVIDENCE_LINE);
- ++p_field; // first column are sample labels
for( ; p_field != header_fields.end(); ++p_field ) {
std::map<std::string, Var>::iterator elem = varMap.find( *p_field );
if( elem == varMap.end() )
tokenizeString( line, fields );
- if( fields.size() != vars.size() + 1 )
+ if( fields.size() != vars.size() )
DAI_THROW(INVALID_EVIDENCE_LINE);
- SampleData& sampleData = _samples[fields[0]];
- sampleData.name( fields[0] ); // in case of a new sample
+ Observation sampleData;
for( size_t i = 0; i < vars.size(); ++i ) {
- if( fields[i+1].size() > 0 ) { // skip if missing observation
- if( fields[i+1].find_first_not_of("0123456789") != std::string::npos )
+ if( fields[i].size() > 0 ) { // skip if missing observation
+ if( fields[i].find_first_not_of("0123456789") != std::string::npos )
DAI_THROW(INVALID_EVIDENCE_OBSERVATION);
- size_t state = atoi( fields[i+1].c_str() );
+ size_t state = atoi( fields[i].c_str() );
if( state >= vars[i].states() )
DAI_THROW(INVALID_EVIDENCE_OBSERVATION);
sampleData.addObservation( vars[i], state );
}
}
+ _samples.push_back( sampleData );
} // finished sample line
}