libDAI version 0.3.2
[libdai.git] / include / dai / evidence.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 /// \file
10 /// \brief Defines class Evidence, which stores multiple observations of joint states of variables
11
12
13 #ifndef __defined_libdai_evidence_h
14 #define __defined_libdai_evidence_h
15
16
17 #include <istream>
18 #include <dai/daialg.h>
19
20
21 namespace dai {
22
23
24 /// Stores a data set consisting of multiple samples, where each sample is the observed joint state of some variables.
25 /** \note Each sample can describe the joint state of a different set of variables,
26 * in order to be able to deal with missing data.
27 *
28 * \author Charles Vaske
29 */
30 class Evidence {
31 public:
32 /// Stores joint state of a set of variables
33 typedef std::map<Var, size_t> Observation;
34
35 private:
36 /// Each sample is an observed joint state of some variables
37 std::vector<Observation> _samples;
38
39 public:
40 /// Default constructor
41 Evidence() : _samples() {}
42
43 /// Construct from \a samples
44 Evidence( std::vector<Observation> &samples ) : _samples(samples) {}
45
46 /// Read in tabular data from a stream and add the read samples to \c *this.
47 /** \param is Input stream in .tab file format, describing joint observations of variables in \a fg
48 * \param fg Factor graph describing the corresponding variables
49 * \see \ref fileformats-evidence
50 * \throw INVALID_EVIDENCE_FILE if the input stream is not valid
51 */
52 void addEvidenceTabFile( std::istream& is, FactorGraph& fg );
53
54 /// Returns number of stored samples
55 size_t nrSamples() const { return _samples.size(); }
56
57 /// \name Iterator interface
58 //@{
59 /// Iterator over the samples
60 typedef std::vector<Observation>::iterator iterator;
61 /// Constant iterator over the samples
62 typedef std::vector<Observation>::const_iterator const_iterator;
63
64 /// Returns iterator that points to the first sample
65 iterator begin() { return _samples.begin(); }
66 /// Returns constant iterator that points to the first sample
67 const_iterator begin() const { return _samples.begin(); }
68 /// Returns iterator that points beyond the last sample
69 iterator end() { return _samples.end(); }
70 /// Returns constant iterator that points beyond the last sample
71 const_iterator end() const { return _samples.end(); }
72 //@}
73
74 private:
75 /// Read in tabular data from a stream and add the read samples to \c *this.
76 void addEvidenceTabFile( std::istream& is, std::map<std::string, Var> &varMap );
77 };
78
79
80 } // end of namespace dai
81
82
83 #endif