Reading and Writing from files

BIF

class pgmpy.readwrite.BIF.BIFReader(path=None, string=None)[source]

Base class for reading network file in bif format

get_cpd()[source]

Returns the CPD of the variables present in the network

>>> from pgmpy.readwrite import BIFReader
>>> reader = BIFReader("bif_test.bif")
>>> reader.get_cpd()
{'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_edges()[source]

Returns the edges of the network

>>> 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()[source]

Returns the fitted bayesian model

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

Retruns the name of the network

>>> 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

>>> 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

>>> 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

>>> 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_variable_grammar()[source]

A method that returns variable grammar

get_variables()[source]

Returns list of variables of the network

>>> 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)[source]

Base class for writing BIF network file format

BIF_templates()[source]

Create template for writing in BIF format

get_cpds()[source]

Adds tables to BIF

dict: dict of type {variable: array}

>>> 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

dict: dict of type {variable: a list of parents}

>>> 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

dict: dict of type {variable: list of properties }

>>> 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

dict: dict of type {variable: a list of states}

>>> 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

list: a list containing names of variable

>>> 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

filename : Name of the file

>>> from pgmpy.readwrite import BIFReader, BIFWriter
>>> model = BIFReader('dog-problem.bif').get_model()
>>> writer = BIFWriter(model)
>>> writer.write_bif(filname='test_file.bif')

PomdpX

ProbModelXML

For the student example the ProbModelXML file should be:

<?xml version=“1.0” encoding=“UTF-8”?> <ProbModelXML formatVersion=“1.0”>

<ProbNet type=”BayesianNetwork”>

<AdditionalConstraints /> <Comment>

Student example model from Probabilistic Graphical Models: Principles and Techniques by Daphne Koller

</Comment> <Language>

English

</Language> <AdditionalProperties /> <Variables>

<Variable name=”intelligence” type=”FiniteState” role=”Chance”>

<Comment /> <Coordinates /> <AdditionalProperties /> <States>

<State name=”smart”><AdditionalProperties /></State> <State name=”dumb”><AdditionalProperties /></State>

</States>

</Variable> <Variable name=”difficulty” type=”FiniteState” role=”Chance”>

<Comment /> <Coordinates /> <AdditionalProperties /> <States>

<State name=”difficult”><AdditionalProperties /></State> <State name=”easy”><AdditionalProperties /></State>

</States>

</Variable> <Variable name=”grade” type=”FiniteState” role=”Chance”>

<Comment /> <Coordinates /> <AdditionalProperties /> <States>

<State name=”grade_A”><AdditionalProperties /></State> <State name=”grade_B”><AdditionalProperties /></State> <State name=”grade_C”><AdditionalProperties /></State>

</States>

</Variable> <Variable name=”recommendation_letter” type=”FiniteState”

role=”Chance”>

<Comment /> <Coordinates /> <AdditionalProperties /> <States>

<State name=”good”><AdditionalProperties /></State> <State name=”bad”><AdditionalProperties /></State>

</States>

</Variable> <Variable name=”SAT” type=”FiniteState” role=”Chance”>

<Comment /> <Coordinates /> <AdditionalProperties /> <States>

<State name=”high”><AdditionalProperties /></State> <State name=”low”><AdditionalProperties /></State>

</States>

</Variable>

</Variables> <Links>

<Link var1=”difficulty” var2=”grade” directed=1>
<Comment>Directed Edge from difficulty to grade</Comment> <Label>diff_to_grad</Label> <AdditionalProperties />

</Link> <Link var1=”intelligence” var2=”grade” directed=1>

<Comment>Directed Edge from intelligence to grade</Comment> <Label>intel_to_grad</Label> <AdditionalProperties />

</Link> <Link var1=”intelligence” var2=”SAT” directed=1>

<Comment>Directed Edge from intelligence to SAT</Comment> <Label>intel_to_sat</Label> <AdditionalProperties />

</Link> <Link var1=”grade” var2=”recommendation_letter” directed=1>

<Comment>Directed Edge from grade to
recommendation_letter</Comment>

<Label>grad_to_reco</Label> <AdditionalProperties />

</Link>

</Links> <Potential type=”Table” role=”ConditionalProbability” label=string>

<Comment>CPDs in the form of table</Comment> <AdditionalProperties /> <!–

There is no specification in the paper about how the tables should be represented.

–>

</Potential>

</ProbNet> <Policies /> <InferenceOptions /> <Evidence>

<EvidenceCase>
<Finding variable=string state=string stateIndex=integer
numericValue=number/>

</EvidenceCase>

</Evidence>

</ProbModelXML>

UAI

XMLBeliefNetwork

XMLBIF