1 #ifndef OUTPUT_DEBUG_UTILS_HXX_
2 #define OUTPUT_DEBUG_UTILS_HXX_
12 #ifdef TRWS_DEBUG_OUTPUT
24 class nullstreamT:
public std::ostream
27 static nullstreamT* Instance()
29 if (!_pInstance) _pInstance =
new nullstreamT();
36 nullstreamT():
std::ios(0),
std::ostream(0){};
37 nullstreamT(nullstreamT
const&){};
38 nullstreamT& operator=(nullstreamT
const&){
return *
this;};
39 static nullstreamT* _pInstance;
43 nullstreamT<M>* nullstreamT<M>::_pInstance=0;
45 typedef nullstreamT<int> nullstream;
47 template<
typename ArrayType>
48 std::ostream& operator << (std::ostream& logger,const std::vector<ArrayType>& arr)
50 for (
size_t i=0;i<arr.size();++i)
51 logger << arr[i]<<
"; ";
56 template<
typename ArrayType>
57 std::ostream& operator << (std::ostream& logger,const std::list<ArrayType>& lst)
59 typename std::list<ArrayType>::const_iterator beg=lst.begin(), end=lst.end();
66 template<
typename ArrayType>
67 std::ostream& operator << (std::ostream& logger,const std::vector<std::vector<ArrayType> >& arr)
69 for (
size_t i=0;i<arr.size();++i)
75 std::ostream& operator << (std::ostream& logger,const std::valarray<T> & arr)
77 for (
size_t i=0;i<arr.size();++i)
78 logger << arr[i]<<
", ";
82 template<
typename Type1,
typename Type2>
83 std::ostream& operator << (std::ostream& logger, const std::pair<Type1,Type2>& p)
85 logger <<
"("<<p.first<<
","<<p.second<<
")";
89 template<
class Iterator>
90 void saveContainer(std::ostream& fout, Iterator begin, Iterator end)
92 for ( ; begin!=end; ++begin)
93 fout <<std::scientific<<*begin<<
"; ";
97 template<
class Iterator>
98 void saveContainer(
const std::string& filename, Iterator begin, Iterator end)
100 std::ofstream fout(filename.c_str());
101 saveContainer(fout, begin, end);
105 #endif //TRWS_DEBUG_OUTPUT
110 template<
class GM>
void store_into_explicit(
const GM& gm,
const std::string& file,
const std::string& modelname=
"gm" )
112 typedef typename GM::FunctionIdentifier FunctionIdentifierType;
113 typedef typename GM::IndexType IndexType;
114 typedef typename GM::LabelType
LabelType;
115 typedef typename GM::ValueType ValueType;
117 typedef GraphicalModel<ValueType, Adder, ExplicitFunction<ValueType,IndexType,LabelType>,
typename GM::SpaceType> ExplicitModel;
119 std::vector<LabelType> numLabels(gm.numberOfVariables());
120 for(IndexType varId=0; varId<gm.numberOfVariables(); ++varId){
121 numLabels[varId] = gm.numberOfLabels(varId);
123 ExplicitModel explicitGm = ExplicitModel(
typename GM::SpaceType(numLabels.begin(), numLabels.end() ));
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) );
130 for(IndexType factorId=0; factorId<gm.numberOfFactors(); ++factorId) {
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);
139 function(s) = gm[factorId](it.begin());
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);
151 function(s1,s2) = gm[factorId](it.begin());
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.");
std::ostream & operator<<(std::ostream &out, const Tribool &t)
void save(const GM &, const std::string &, const std::string &)
save a graphical model to an HDF5 file