45#ifndef DOXYGEN_SHOULD_SKIP_THIS
51# include <agrum/base/external/json/json.hpp>
52using json = nlohmann::json;
53using ordered_json = nlohmann::ordered_json;
56 template < GUM_Numeric GUM_SCALAR >
60 template < GUM_Numeric GUM_SCALAR >
61 GumMRFWriter< GUM_SCALAR >::GumMRFWriter(
bool binary,
int indent) : MRFWriter< GUM_SCALAR >() {
63 _indent_ = (indent < -1) ? -1 : indent;
64 GUM_CONSTRUCTOR(GumMRFWriter);
67 template < GUM_Numeric GUM_SCALAR >
68 GumMRFWriter< GUM_SCALAR >::~GumMRFWriter() {
69 GUM_DESTRUCTOR(GumMRFWriter);
72 template < GUM_Numeric GUM_SCALAR >
73 void GumMRFWriter< GUM_SCALAR >::write(std::ostream& output,
74 const IMarkovRandomField< GUM_SCALAR >& mrf) {
75 if (!output.good())
GUM_ERROR(
IOError,
"Input/Output error : stream not writable.")
78 content[
"type"] =
"MRF";
79 content[
"GumJsonVersion"] =
"1.0";
82 content[
"nodes"] = ordered_json::array();
83 for (const auto& node: mrf.nodes()) {
84 content[
"nodes"].push_back(mrf.variable(node).toFast());
89 content[
"factors"] = ordered_json::array();
90 for (
const auto& [nodeSet, tensor_ptr]: mrf.factors()) {
93 for (Idx i = 0; i < tensor_ptr->nbrDim(); i++) {
94 factor[
"vars"].push_back(tensor_ptr->variable(i).name());
98 Instantiation I(*tensor_ptr);
99 for (I.setFirst(); !I.end(); ++I) {
100 vals.push_back((*tensor_ptr)[I]);
102 factor[
"values"] = vals;
103 content[
"factors"].push_back(factor);
107 for (
const auto& prop: mrf.properties()) {
108 content[
"properties"][prop] = mrf.property(prop);
114 output << content.dump(_indent_);
118 GUM_ERROR(
IOError,
"Writing in the ostream failed. Check if the stream is writable.")
122 template < GUM_Numeric GUM_SCALAR >
123 void GumMRFWriter< GUM_SCALAR >::write(std::string_view filePath,
124 IMarkovRandomField< GUM_SCALAR >& mrf) {
125 mrf.updateMetaData();
126 write(filePath,
static_cast< const IMarkovRandomField< GUM_SCALAR >&
>(mrf));
129 template < GUM_Numeric GUM_SCALAR >
130 void GumMRFWriter< GUM_SCALAR >::write(std::string_view filePath,
131 const IMarkovRandomField< GUM_SCALAR >& mrf) {
132 std::ofstream output(std::string(filePath),
133 _binary_ ? (std::ios_base::trunc | std::ios::binary)
134 : std::ios_base::trunc);
140 template < GUM_Numeric GUM_SCALAR >
141 std::string GumMRFWriter< GUM_SCALAR >::toString(
const IMarkovRandomField< GUM_SCALAR >& mrf) {
142 std::ostringstream oss;
Shared binary I/O helpers for GUM (jgum/bgum) serialization.
Definition of class for GUM (json) file output for Markov Random Fields.
Writes a MarkovRandomField in the GUM json format.
GumMRFWriter(bool binary=false)
Default constructor.
Exception : input/output problem.
#define GUM_ERROR(type, msg)
gum is the global namespace for all aGrUM entities
int defaultJgumIndent()
The indentation used by the GUM writers (jgum text format) when none is given: the value of the key j...
GUM_SHARED_PUBLIC void _writeVector_(std::ostream &os, const std::vector< uint8_t > &vec)
Writes a length-prefixed byte vector to a binary stream (bgum format).