BIF (Bayesian Interchange Format)

class pgmpy.readwrite.BIF.BIFReader(path=None, string=None, include_properties=False, n_jobs=-1)[source]

Initializes a BIFReader object.

Parameters:
  • path (file or str) – File of bif data

  • string (str) – String of bif data

  • include_properties (boolean) – If True, gets the properties tag from the file and stores in graph properties.

  • n_jobs (int (default: -1)) – Number of jobs to run in parallel. -1 means use all processors.

Examples

# dog-problem.bif file is present at # http://www.cs.cmu.edu/~javabayes/Examples/DogProblem/dog-problem.bif >>> from pgmpy.readwrite import BIFReader >>> reader = BIFReader(“bif_test.bif”) <pgmpy.readwrite.BIF.BIFReader object at 0x7f2375621cf8> >>> model = reader.get_model()

Reference

[1] Geoff Hulten and Pedro Domingos. The interchange format for bayesian networks.

http://www.cs.washington.edu/dm/vfml/appendixes/bif.htm, 2003.

get_edges()[source]

Returns the edges of the network

Example

>>> from pgmpy.readwrite import BIFReader
>>> reader = BIFReader("bif_test.bif")
>>> reader.get_edges()
[['family-out', 'light-on'],
 ['family-out', 'dog-out'],
 ['bowel-problem', 'dog-out'],
 ['dog-out', 'hear-bark']]
get_model(state_name_type=<class 'str'>)[source]

Returns the Bayesian Model read from the file/str.

Parameters:

state_name_type (int, str or bool (default: str)) – The data type to which to convert the state names of the variables.

Example

>>> from pgmpy.readwrite import BIFReader
>>> reader = BIFReader("bif_test.bif")
>>> reader.get_model()
<pgmpy.models.BayesianNetwork.BayesianNetwork object at 0x7f20af154320>
get_network_name()[source]

Returns the name of the network

Example

>>> from pgmpy.readwrite import BIFReader
>>> reader = BIF.BifReader("bif_test.bif")
>>> reader.network_name()
'Dog-Problem'
get_parents()[source]

Returns the parents of the variables present in the network

Example

>>> from pgmpy.readwrite import BIFReader
>>> reader = BIFReader("bif_test.bif")
>>> reader.get_parents()
{'bowel-problem': [],
'dog-out': ['family-out', 'bowel-problem'],
'family-out': [],
'hear-bark': ['dog-out'],
'light-on': ['family-out']}
get_probability_grammar()[source]

A method that returns probability grammar

get_property()[source]

Returns the property of the variable

Example

>>> from pgmpy.readwrite import BIFReader
>>> reader = BIFReader("bif_test.bif")
>>> reader.get_property()
{'bowel-problem': ['position = (335, 99)'],
'dog-out': ['position = (300, 195)'],
'family-out': ['position = (257, 99)'],
'hear-bark': ['position = (296, 268)'],
'light-on': ['position = (218, 195)']}
get_states()[source]

Returns the states of variables present in the network

Example

>>> from pgmpy.readwrite import BIFReader
>>> reader = BIFReader("bif_test.bif")
>>> reader.get_states()
{'bowel-problem': ['true','false'],
'dog-out': ['true','false'],
'family-out': ['true','false'],
'hear-bark': ['true','false'],
'light-on': ['true','false']}
get_values()[source]

Returns the CPD of the variables present in the network

Example

>>> from pgmpy.readwrite import BIFReader
>>> reader = BIFReader("bif_test.bif")
>>> reader.get_values()
{'bowel-problem': np.array([[0.01],
                            [0.99]]),
'dog-out': np.array([[0.99, 0.97, 0.9, 0.3],
                    [0.01, 0.03, 0.1, 0.7]]),
'family-out': np.array([[0.15],
                        [0.85]]),
'hear-bark': np.array([[0.7, 0.01],
                        [0.3, 0.99]]),
'light-on': np.array([[0.6, 0.05],
                    [0.4, 0.95]])}
get_variable_grammar()[source]

A method that returns variable grammar

get_variables()[source]

Returns list of variables of the network

Example

>>> from pgmpy.readwrite import BIFReader
>>> reader = BIFReader("bif_test.bif")
>>> reader.get_variables()
['light-on','bowel_problem','dog-out','hear-bark','family-out']
class pgmpy.readwrite.BIF.BIFWriter(model, round_values=None)[source]

Initialise a BIFWriter Object

Parameters:
  • model (BayesianNetwork Instance) –

  • round_values (int (default: None)) – Round the probability values to round_values decimals. If None, keeps all decimal points.

Examples

>>> from pgmpy.readwrite import BIFWriter
>>> from pgmpy.utils import get_example_model
>>> asia = get_example_model('asia')
>>> writer = BIFWriter(asia)
>>> writer
<writer_BIF.BIFWriter at 0x7f05e5ea27b8>
>>> writer.write_bif('asia.bif')
BIF_templates()[source]

Create template for writing in BIF format

get_cpds()[source]

Adds tables to BIF

Returns:

dict

Return type:

dict of type {variable: array}

Example

>>> from pgmpy.readwrite import BIFReader, BIFWriter
>>> model = BIFReader('dog-problem.bif').get_model()
>>> writer = BIFWriter(model)
>>> writer.get_cpds()
{'bowel-problem': array([ 0.01,  0.99]),
 'dog-out': array([ 0.99,  0.97,  0.9 ,  0.3 ,  0.01,  0.03,  0.1 ,  0.7 ]),
 'family-out': array([ 0.15,  0.85]),
 'hear-bark': array([ 0.7 ,  0.01,  0.3 ,  0.99]),
 'light-on': array([ 0.6 ,  0.05,  0.4 ,  0.95])}
get_parents()[source]

Add the parents to BIF

Returns:

dict

Return type:

dict of type {variable: a list of parents}

Example

>>> from pgmpy.readwrite import BIFReader, BIFWriter
>>> model = BIFReader('dog-problem.bif').get_model()
>>> writer = BIFWriter(model)
>>> writer.get_parents()
{'bowel-problem': [],
 'dog-out': ['bowel-problem', 'family-out'],
 'family-out': [],
 'hear-bark': ['dog-out'],
 'light-on': ['family-out']}
get_properties()[source]

Add property to variables in BIF

Returns:

dict

Return type:

dict of type {variable: list of properties }

Example

>>> from pgmpy.readwrite import BIFReader, BIFWriter
>>> model = BIFReader('dog-problem.bif').get_model()
>>> writer = BIFWriter(model)
>>> writer.get_properties()
{'bowel-problem': ['position = (335, 99)'],
 'dog-out': ['position = (300, 195)'],
 'family-out': ['position = (257, 99)'],
 'hear-bark': ['position = (296, 268)'],
 'light-on': ['position = (218, 195)']}
get_states()[source]

Add states to variable of BIF

Returns:

dict

Return type:

dict of type {variable: a list of states}

Example

>>> from pgmpy.readwrite import BIFReader, BIFWriter
>>> model = BIFReader('dog-problem.bif').get_model()
>>> writer = BIFWriter(model)
>>> writer.get_states()
{'bowel-problem': ['bowel-problem_0', 'bowel-problem_1'],
 'dog-out': ['dog-out_0', 'dog-out_1'],
 'family-out': ['family-out_0', 'family-out_1'],
 'hear-bark': ['hear-bark_0', 'hear-bark_1'],
 'light-on': ['light-on_0', 'light-on_1']}
get_variables()[source]

Add variables to BIF

Returns:

list

Return type:

a list containing names of variable

Example

>>> from pgmpy.readwrite import BIFReader, BIFWriter
>>> model = BIFReader('dog-problem.bif').get_model()
>>> writer = BIFWriter(model)
>>> writer.get_variables()
['bowel-problem', 'family-out', 'hear-bark', 'light-on', 'dog-out']
write_bif(filename)[source]

Writes the BIF data into a file

Parameters:

filename (Name of the file) –

Example

>>> from pgmpy.utils import get_example_model
>>> from pgmpy.readwrite import BIFReader, BIFWriter
>>> asia = get_example_model('asia')
>>> writer = BIFWriter(asia)
>>> writer.write_bif(filename='asia.bif')