Reading and Writing Bayesian Networks with pyBN

When first testing out a Bayesian network package, the first thing a user usually wants to do is read a Bayesian network from a file. The pyBN package makes reading BN's from files increadibly easy. Moreover a substantial number of networks are already included with the package!

The first step is to load the package:


In [3]:
from pyBN import *

If you already have a file in mind, then you can use the "read_bn" function to parse that file and return a BayesNet object. The best part about "read_bn" is that it can understand ANY file type -- just make sure you include the extension at the end of the filepath string and pyBN takes care of the rest!

Suppose I want to read one of the many included Bayesian networks in the pyBN package -- say, the well-known "cancer" BN. I simply write the absolute or relative path to the file, and pass it as an argument to "read_bn":


In [5]:
file = 'data/cancer.bif'
bn = read_bn(file)

Once you have read in a BayesNet object from file, a good first thing to do is to check out its properties. Every BayesNet object in pyBN has three attributes:

  • "V" : a list of vertices (random variables)
  • "E" : a list of edges (conditional dependencies between random variables)
  • "data" : a dictionary of dictionaries containing the Conditional Probability Tables (and other info) for each random variables

Let's observe these attributes for the "cancer" BN:


In [24]:
print bn.V
print bn.E


['Pollution', 'Smoker', 'Cancer', 'Xray', 'Dyspnoea']
[['Pollution', 'Cancer'], ['Smoker', 'Cancer'], ['Cancer', 'Xray'], ['Cancer', 'Dyspnoea']]

The underlying, official data storage for the pyBN package is based on the JSON format. This is best seen with the "data" attribute. Since the "data" attribute is a dictionary which conforms to the JSON format, it can be easily read/written from/to files. An added bonus is that JSON allows for well-formatted printing!


In [26]:
import json
print json.dumps(bn.data['Xray'],indent=2)


{
  "vals": [
    "positive", 
    "negative"
  ], 
  "parents": [
    "Cancer"
  ], 
  "cprob": [
    [
      0.9, 
      0.1
    ], 
    [
      0.2, 
      0.8
    ]
  ], 
  "numoutcomes": 2, 
  "children": []
}

The information and code presented above was a quick, yet near-sufficient explanation of the file reading capabilities of the pyBN package. Creating a BayesNet object from scratch -- or modifying an existing BayesNet object -- is definitely something we will look into in the future!

Now, let's turn to writing BayesNet object to file. Again, this is incredibly simple and relies on the "write_bn" function which operates similarly to the "read_bn" function -- simply specify the output file name and we will write passed-in BayesNet object to file based on the file extension given in the output file. That's right - pyBN can write a Bayesian network object to one of numerous formats by inferring which one you want in the output file string!


In [31]:
write_bn(bn, 'data/write_test.bn')

To finish this tutorial, let's make sure our file-written BayesNet contains the correct information:


In [40]:
bn2 = read_bn('data/write_test.bn')
print bn.V == bn2.V
print bn.E == bn2.E
print bn.data == bn2.data


True
True
True