OpenGM  2.3.x
Discrete Graphical Model Library
output_debug_utils.hxx
Go to the documentation of this file.
1 #ifndef OUTPUT_DEBUG_UTILS_HXX_
2 #define OUTPUT_DEBUG_UTILS_HXX_
3 
4 #ifdef WITH_HDF5
5 #include <opengm/opengm.hxx>
10 #endif
11 
12 #ifdef TRWS_DEBUG_OUTPUT
13 
14 #include <iostream>
15 #include <fstream>
16 #include <vector>
17 #include <valarray>
18 #include <string>
19 #include <list>
20 
21 namespace OUT{
22 
23 template<class M>
24 class nullstreamT: public std::ostream
25 {
26 public:
27  static nullstreamT* Instance()
28  {
29  if (!_pInstance) _pInstance = new nullstreamT();
30  return _pInstance;
31  };
32 
33  template <class T>
34  nullstreamT& operator << (T& t){return *this;}
35 private:
36  nullstreamT(): std::ios(0), std::ostream(0){}; // Private so that it can not be called
37  nullstreamT(nullstreamT const&){}; // copy constructor is private
38  nullstreamT& operator=(nullstreamT const&){return *this;}; // assignment operator is private
39  static nullstreamT* _pInstance;
40 };
41 
42 template<class M>
43 nullstreamT<M>* nullstreamT<M>::_pInstance=0;
44 
45 typedef nullstreamT<int> nullstream;
46 
47  template<typename ArrayType>
48  std::ostream& operator << (std::ostream& logger,const std::vector<ArrayType>& arr)
49  {
50  for (size_t i=0;i<arr.size();++i)
51  logger << arr[i]<<"; ";
52  logger <<std::endl;
53  return logger;
54  };
55 
56  template<typename ArrayType>
57  std::ostream& operator << (std::ostream& logger,const std::list<ArrayType>& lst)
58  {
59  typename std::list<ArrayType>::const_iterator beg=lst.begin(), end=lst.end();
60  for (;beg!=end;++beg)
61  logger << *beg<<"; ";
62  logger <<std::endl;
63  return logger;
64  };
65 
66  template<typename ArrayType>
67  std::ostream& operator << (std::ostream& logger,const std::vector<std::vector<ArrayType> >& arr)
68  {
69  for (size_t i=0;i<arr.size();++i)
70  logger << arr[i];
71  return logger;
72  };
73 
74  template<typename T>
75  std::ostream& operator << (std::ostream& logger,const std::valarray<T> & arr)
76  {
77  for (size_t i=0;i<arr.size();++i)
78  logger << arr[i]<<", ";
79  return logger;
80  };
81 
82  template<typename Type1,typename Type2>
83  std::ostream& operator << (std::ostream& logger, const std::pair<Type1,Type2>& p)
84  {
85  logger <<"("<<p.first<<","<<p.second<<")";
86  return logger;
87  };
88 
89  template<class Iterator>
90  void saveContainer(std::ostream& fout, Iterator begin, Iterator end)
91  {
92  for ( ; begin!=end; ++begin)
93  fout <<std::scientific<<*begin<<"; ";
94  fout << std::endl;
95  }
96 
97  template<class Iterator>
98  void saveContainer(const std::string& filename, Iterator begin, Iterator end)
99  {
100  std::ofstream fout(filename.c_str());
101  saveContainer(fout, begin, end);
102  fout.close();
103  };
104 };
105 #endif //TRWS_DEBUG_OUTPUT
106 
107 #ifdef WITH_HDF5
108 
109 namespace opengm{
110 template<class GM> void store_into_explicit(const GM& gm, const std::string& file,const std::string& modelname="gm" )
111 {
112  typedef typename GM::FunctionIdentifier FunctionIdentifierType;
113  typedef typename GM::IndexType IndexType;
114  typedef typename GM::LabelType LabelType;
115  typedef typename GM::ValueType ValueType;
116 
117  typedef GraphicalModel<ValueType, Adder, ExplicitFunction<ValueType,IndexType,LabelType>, typename GM::SpaceType> ExplicitModel;
118 
119  std::vector<LabelType> numLabels(gm.numberOfVariables());
120  for(IndexType varId=0; varId<gm.numberOfVariables(); ++varId){
121  numLabels[varId] = gm.numberOfLabels(varId);
122  }
123  ExplicitModel explicitGm = ExplicitModel( typename GM::SpaceType(numLabels.begin(), numLabels.end() ));
124 
125  std::vector< std::vector<IndexType> > factorIndices(gm.numberOfFactors());
126  for(IndexType factorId=0; factorId<gm.numberOfFactors(); ++factorId)
127  for(IndexType varId=0; varId < gm.numberOfVariables(factorId); ++varId)
128  factorIndices[factorId].push_back( gm.variableOfFactor(factorId,varId) );
129 
130  for(IndexType factorId=0; factorId<gm.numberOfFactors(); ++factorId) {
131 
132  // implemented storing only for unary and pairwise factors
133  if( factorIndices[factorId].size() == 1 ) {
134  const LabelType shape[] = { numLabels[factorIndices[factorId][0]] };
135  ExplicitFunction<double> function(shape, shape + 1);
136  for(size_t s = 0; s<shape[0]; ++s){
137  std::vector<LabelType> it(1);
138  it[0] = s;
139  function(s) = gm[factorId](it.begin());
140  }
141  FunctionIdentifierType funcId = explicitGm.addFunction( function );
142  explicitGm.addFactor( funcId, factorIndices[factorId].begin(), factorIndices[factorId].end());
143  } else if( factorIndices[factorId].size() == 2 ) {
144  const LabelType shape[] = { numLabels[factorIndices[factorId][0]], numLabels[factorIndices[factorId][1]] };
145  ExplicitFunction<double> function(shape, shape + 2);
146  for(LabelType s1 = 0; s1<shape[0]; ++s1){
147  for(LabelType s2 = 0; s2<shape[1]; ++s2){
148  std::vector<LabelType> it(2);
149  it[0] = s1;
150  it[1] = s2;
151  function(s1,s2) = gm[factorId](it.begin());
152  }
153  }
154  FunctionIdentifierType funcId = explicitGm.addFunction( function );
155  explicitGm.addFactor( funcId, factorIndices[factorId].begin(), factorIndices[factorId].end());
156  } else throw std::runtime_error("store_into_explicit: Factors of order > 2 are not supported yet.");
157 
158  }
159 
160  hdf5::save(explicitGm, file, modelname);
161 }
162 }//namespace opengm
163 
164 #endif
165 
166 #endif
The OpenGM namespace.
Definition: config.hxx:43
STL namespace.
std::ostream & operator<<(std::ostream &out, const Tribool &t)
Definition: tribool.hxx:151
void save(const GM &, const std::string &, const std::string &)
save a graphical model to an HDF5 file