Serialization/Deserialization

We all need a way to save (serialize) and load (deserialize) our Bayesian Belief Networks (BBNs) and join trees (JTs). Here’s how to do so. Note that serde (serialization/deserialization) features are just writing to JSON or CSV formats and loading back from the such files. The code takes care of the serde process.

Serializing a BBN

JSON Serialization Format

 1from pybbn.graph.dag import Bbn
 2from pybbn.graph.edge import Edge, EdgeType
 3from pybbn.graph.node import BbnNode
 4from pybbn.graph.variable import Variable
 5
 6# create graph
 7a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8])
 8b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1])
 9bbn = Bbn().add_node(a).add_node(b) \
10    .add_edge(Edge(a, b, EdgeType.DIRECTED))
11
12# serialize
13Bbn.to_json(bbn, 'simple-bbn.json')

You will get a file simple-bbn.json written out with the following content.

 1{
 2  "nodes": {
 3    "0": {
 4      "probs": [
 5        0.2,
 6        0.8
 7      ],
 8      "variable": {
 9        "id": 0,
10        "name": "a",
11        "values": [
12          "t",
13          "f"
14        ]
15      }
16    },
17    "1": {
18      "probs": [
19        0.1,
20        0.9,
21        0.9,
22        0.1
23      ],
24      "variable": {
25        "id": 1,
26        "name": "b",
27        "values": [
28          "t",
29          "f"
30        ]
31      }
32    }
33  },
34  "edges": [
35    {
36      "pa": 0,
37      "ch": 1
38    }
39  ]
40}

CSV Serialization Format

 1from pybbn.graph.dag import Bbn
 2from pybbn.graph.edge import Edge, EdgeType
 3from pybbn.graph.node import BbnNode
 4from pybbn.graph.variable import Variable
 5
 6# create graph
 7a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8])
 8b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1])
 9bbn = Bbn().add_node(a).add_node(b) \
10    .add_edge(Edge(a, b, EdgeType.DIRECTED))
11
12# serialize
13Bbn.to_csv(bbn, 'simple-bbn.csv')

You will get a file simple-bbn.csv written out with the following content.

10,a,t,f,|,0.2,0.8
21,b,t,f,|,0.1,0.9,0.9,0.1
30,1,directed

Deserializing a BBN

JSON Deserialization Format

1from pybbn.graph.dag import Bbn
2
3# deserialize
4bbn = Bbn.from_json('simple-bbn.json')

CSV Deserialization Format

1from pybbn.graph.dag import Bbn
2
3# deserialize
4bbn = Bbn.from_csv('simple-bbn.csv')

Join Tree Serde

A join tree may also be serialized and deserialized. Only json format is supported for now.

Serializing a Join Tree

 1import json
 2
 3from pybbn.graph.dag import Bbn
 4from pybbn.graph.edge import EdgeType, Edge
 5from pybbn.graph.jointree import JoinTree
 6from pybbn.graph.node import BbnNode
 7from pybbn.graph.variable import Variable
 8from pybbn.pptc.inferencecontroller import InferenceController
 9
10a = BbnNode(Variable(0, 'a', ['t', 'f']), [0.2, 0.8])
11b = BbnNode(Variable(1, 'b', ['t', 'f']), [0.1, 0.9, 0.9, 0.1])
12bbn = Bbn().add_node(a).add_node(b) \
13    .add_edge(Edge(a, b, EdgeType.DIRECTED))
14jt = InferenceController.apply(bbn)
15
16with open('simple-join-tree.json', 'w') as f:
17    d = JoinTree.to_dict(jt, bbn)
18    j = json.dumps(d, sort_keys=True, indent=2)
19    f.write(j)

You will get a file simple-join-tree.json written out with the following content.

 1{
 2  "bbn_nodes": {
 3    "0": {
 4      "probs": [
 5        0.2,
 6        0.8
 7      ],
 8      "variable": {
 9        "id": 0,
10        "name": "a",
11        "values": [
12          "t",
13          "f"
14        ]
15      }
16    },
17    "1": {
18      "probs": [
19        0.1,
20        0.9,
21        0.9,
22        0.1
23      ],
24      "variable": {
25        "id": 1,
26        "name": "b",
27        "values": [
28          "t",
29          "f"
30        ]
31      }
32    }
33  },
34  "jt": {
35    "edges": [],
36    "nodes": {
37      "0-1": {
38        "node_ids": [
39          0,
40          1
41        ],
42        "type": "clique"
43      }
44    },
45    "parent_info": {
46      "0": [],
47      "1": [
48        0
49      ]
50    }
51  }
52}

Deserializing a Join Tree

 1import json
 2
 3from pybbn.graph.jointree import JoinTree
 4from pybbn.pptc.inferencecontroller import InferenceController
 5
 6with open('simple-join-tree.json', 'r') as f:
 7    j = f.read()
 8    d = json.loads(j)
 9    jt = JoinTree.from_dict(d)
10    jt = InferenceController.apply_from_serde(jt)