Reading and Writing from files

BIF

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

Base class for reading network file in bif format

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.BayesianModel.BayesianModel 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)[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

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

PomdpX

class pgmpy.readwrite.PomdpX.PomdpXReader(path=None, string=None)[source]

Class for reading PomdpX file format from files or strings

get_description()[source]

Return the problem description

Examples

>>> reader = PomdpXReader('Test_Pomdpx.xml')
>>> reader.get_description()
'RockSample problem for map size 1 x 3.
Rock is at 0, Rover’s initial position is at 1.
Exit is at 2.'
>>> reader = PomdpXReader('Test_PomdpX.xml')
>>> reader.get_description()
'RockSample problem for map size 1 x 3.
 Rock is at 0, Rover’s initial position is at 1.
 Exit is at 2.'
get_discount()[source]

Returns the discount factor for the problem

Example

>>> reader = PomdpXReader('Test_PomdpX.xml')
>>> reader.get_discount()
0.95
get_initial_beliefs()[source]

Returns the state, action and observation variables as a dictionary in the case of table type parameter and a nested structure in case of decision diagram parameter

Examples

>>> reader = PomdpXReader('Test_PomdpX.xml')
>>> reader.get_initial_beliefs()
[{'Var': 'rover_0',
  'Parent': ['null'],
  'Type': 'TBL',
  'Parameter': [{'Instance': ['-'],
  'ProbTable': ['0.0', '1.0', '0.0']}]
 },
 {'Var': '',
  '...': ...,'
  '...': '...',
  }]
get_obs_function()[source]

Returns the observation function as nested dict in the case of table- type parameter and a nested structure in case of decision diagram parameter

Example

>>> reader = PomdpXReader('Test_PomdpX.xml')
>>> reader.get_obs_function()
[{'Var': 'obs_sensor',
      'Parent': ['action_rover', 'rover_1', 'rock_1'],
      'Type': 'TBL',
      'Parameter': [{'Instance': ['amw', '*', '*', '-'],
                     'ProbTable': ['1.0', '0.0']},
                 ...
                ]
}]
get_parameter(var)[source]

This method supports the functional tags by providing the actual values in the function as list of dict in case of table type parameter or as nested dict in case of decision diagram

get_parameter_dd(parameter)[source]

This method returns parameters as nested dicts in case of decision diagram parameter.

get_parameter_tbl(parameter)[source]

This method returns parameters as list of dict in case of table type parameter

get_reward_function()[source]

Returns the reward function as nested dict in the case of table- type parameter and a nested structure in case of decision diagram parameter

Example

>>> reader = PomdpXReader('Test_PomdpX.xml')
>>> reader.get_reward_function()
[{'Var': 'reward_rover',
      'Parent': ['action_rover', 'rover_0', 'rock_0'],
      'Type': 'TBL',
      'Parameter': [{'Instance': ['ame', 's1', '*'],
                     'ValueTable': ['10']},
                 ...
                ]
}]
get_state_transition_function()[source]

Returns the transition of the state variables as nested dict in the case of table type parameter and a nested structure in case of decision diagram parameter

Example

>>> reader = PomdpXReader('Test_PomdpX.xml')
>>> reader.get_state_transition_function()
[{'Var': 'rover_1',
  'Parent': ['action_rover', 'rover_0'],
  'Type': 'TBL',
  'Parameter': [{'Instance': ['amw', 's0', 's2'],
                 'ProbTable': ['1.0']},
                 {'Instance': ['amw', 's1', 's0'],
                 'ProbTable': ['1.0']},
                 ...
                ]
}]
get_variables()[source]

Returns list of variables of the network

Example

>>> reader = PomdpXReader("pomdpx.xml")
>>> reader.get_variables()
{'StateVar': [
                {'vnamePrev': 'rover_0',
                 'vnameCurr': 'rover_1',
                 'ValueEnum': ['s0', 's1', 's2'],
                 'fullyObs': True},
                {'vnamePrev': 'rock_0',
                 'vnameCurr': 'rock_1',
                 'fullyObs': False,
                 'ValueEnum': ['good', 'bad']}],
                'ObsVar': [{'vname': 'obs_sensor',
                            'ValueEnum': ['ogood', 'obad']}],
                'RewardVar': [{'vname': 'reward_rover'}],
                'ActionVar': [{'vname': 'action_rover',
                               'ValueEnum': ['amw', 'ame',
                                             'ac', 'as']}]
                }
class pgmpy.readwrite.PomdpX.PomdpXWriter(model_data, encoding='utf-8', prettyprint=True)[source]

Class for writing models in PomdpX

add_conditions(condition, condprob)[source]

helper function for adding probability conditions for model :param condition: contains and element of conditions list :type condition: dictionary :param condprob: the tag to which condition is added :type condprob: etree SubElement

Returns

Return type

None

add_initial_belief()[source]

add initial belief tag to pomdpx model

Returns

Return type

string containing the xml for initial belief tag

add_obs_function()[source]

add observation function tag to pomdpx model

Returns

Return type

string containing the xml for observation function tag

add_parameter_dd(dag_tag, node_dict)[source]

helper function for adding parameters in condition

Parameters
  • dag_tag (etree SubElement) – the DAG tag is contained in this subelement

  • node_dict (dictionary) – the decision diagram dictionary

Returns

Return type

None

add_reward_function()[source]

add reward function tag to pomdpx model

Returns

Return type

string containing the xml for reward function tag

add_state_transition_function()[source]

add state transition function tag to pomdpx model

Returns

Return type

string containing the xml for state transition tag

get_variables()[source]

Add variables to PomdpX

Returns

Return type

xml containing variables tag

indent(elem, level=0)[source]

Inplace prettyprint formatter.

UAI

class pgmpy.readwrite.UAI.UAIReader(path=None, string=None)[source]

Class for reading UAI file format from files or strings.

get_domain()[source]

Returns the dictionary of variables with keys as variable name and values as domain of the variables.

Returns

dict

Return type

dictionary containing variables and their domains

Examples

>>> from pgmpy.readwrite import UAIReader
>>> reader = UAIReader('TestUAI.uai')
>>> reader.get_domain()
{'var_0': '2', 'var_1': '2', 'var_2': '3'}
get_edges()[source]

Returns the edges of the network.

Returns

set

Return type

set containing the edges of the network

Examples

>>> from pgmpy.readwrite import UAIReader
>>> reader = UAIReader('TestUAI.uai')
>>> reader.get_edges()
{('var_0', 'var_1'), ('var_0', 'var_2'), ('var_1', 'var_2')}
get_grammar()[source]

Returns the grammar of the UAI file.

get_model()[source]

Returns an instance of Bayesian Model or Markov Model. Variables are in the pattern var_0, var_1, var_2 where var_0 is 0th index variable, var_1 is 1st index variable.

Returns

model

Return type

an instance of Bayesian or Markov Model.

Examples

>>> from pgmpy.readwrite import UAIReader
>>> reader = UAIReader('TestUAI.uai')
>>> reader.get_model()
get_network_type()[source]

Returns the type of network defined by the file.

Returns

string – String containing network type.

Return type

str

Examples

>>> from pgmpy.readwrite import UAIReader
>>> reader = UAIReader('TestUAI.uai')
>>> reader.get_network_type()
'MARKOV'
get_tables()[source]

Returns list of tuple of child variable and CPD in case of Bayesian and list of tuple of scope of variables and values in case of Markov.

Returns

list – list of tuples of scope of variables and values in case of Markov.

Return type

list of tuples of child variable and values in Bayesian

Examples

>>> from pgmpy.readwrite import UAIReader
>>> reader = UAIReader('TestUAI.uai')
>>> reader.get_tables()
[(['var_0', 'var_1'], ['4.000', '2.400', '1.000', '0.000']),
 (['var_0', 'var_1', 'var_2'],
  ['2.2500', '3.2500', '3.7500', '0.0000', '0.0000', '10.0000',
   '1.8750', '4.0000', '3.3330', '2.0000', '2.0000', '3.4000'])]
get_variables()[source]

Returns a list of variables. Each variable is represented by an index of list. For example if the no of variables are 4 then the list will be [var_0, var_1, var_2, var_3]

Returns

list

Return type

list of variables

Examples

>>> from pgmpy.readwrite import UAIReader
>>> reader = UAIReader('TestUAI.uai')
>>> reader.get_variables()
['var_0', 'var_1', 'var_2']
class pgmpy.readwrite.UAI.UAIWriter(model)[source]

Class for writing models in UAI.

get_domain()[source]

Adds domain of each variable to the network.

Examples

>>> from pgmpy.readwrite import UAIWriter
>>> writer = UAIWriter(model)
>>> writer.get_domain()
get_functions()[source]

Adds functions to the network.

Examples

>>> from pgmpy.readwrite import UAIWriter
>>> writer = UAIWriter(model)
>>> writer.get_functions()
get_nodes()[source]

Adds variables to the network.

Examples

>>> from pgmpy.readwrite import UAIWriter
>>> writer = UAIWriter(model)
>>> writer.get_nodes()
get_tables()[source]

Adds tables to the network.

Examples

>>> from pgmpy.readwrite import UAIWriter
>>> writer = UAIWriter(model)
>>> writer.get_tables()
write_uai(filename)[source]

Write the xml data into the file.

Parameters

filename (Name of the file.) –

Examples

>>> from pgmpy.readwrite import UAIWriter
>>> from pgmpy.utils import get_example_model
>>> model = get_example_model('asia')
>>> writer = UAIWriter(asia)
>>> writer.write_uai('asia.uai')

XMLBeliefNetwork

class pgmpy.readwrite.XMLBeliefNetwork.XBNReader(path=None, string=None)[source]

Base class for reading XML Belief Network File Format.

get_analysisnotebook_values()[source]

Returns a dictionary of the attributes of ANALYSISNOTEBOOK tag

Examples

>>> reader = XBNReader('xbn_test.xml')
>>> reader.get_analysisnotebook_values()
{'NAME': "Notebook.Cancer Example From Neapolitan",
 'ROOT': "Cancer"}
get_bnmodel_name()[source]

Returns the name of the BNMODEL.

Examples

>>> reader = XBNReader('xbn_test.xml')
>>> reader.get_bnmodel_name()
'Cancer'
get_distributions()[source]

Returns a dictionary of name and its distribution. Distribution is a ndarray.

The ndarray is stored in the standard way such that the rightmost variable changes most often. Consider a CPD of variable ‘d’ which has parents ‘b’ and ‘c’ (distribution[‘CONDSET’] = [‘b’, ‘c’])

d_0 d_1

b_0, c_0 | 0.8 0.2 b_0, c_1 | 0.9 0.1 b_1, c_0 | 0.7 0.3 b_1, c_1 | 0.05 0.95

The value of distribution[‘d’][‘DPIS’] for the above example will be: array([[ 0.8 , 0.2 ], [ 0.9 , 0.1 ], [ 0.7 , 0.3 ], [ 0.05, 0.95]])

Examples

>>> reader = XBNReader('xbn_test.xml')
>>> reader.get_distributions()
{'a': {'TYPE': 'discrete', 'DPIS': array([[ 0.2,  0.8]])},
 'e': {'TYPE': 'discrete', 'DPIS': array([[ 0.8,  0.2],
         [ 0.6,  0.4]]), 'CONDSET': ['c'], 'CARDINALITY': [2]},
 'b': {'TYPE': 'discrete', 'DPIS': array([[ 0.8,  0.2],
         [ 0.2,  0.8]]), 'CONDSET': ['a'], 'CARDINALITY': [2]},
 'c': {'TYPE': 'discrete', 'DPIS': array([[ 0.2 ,  0.8 ],
         [ 0.05,  0.95]]), 'CONDSET': ['a'], 'CARDINALITY': [2]},
 'd': {'TYPE': 'discrete', 'DPIS': array([[ 0.8 ,  0.2 ],
         [ 0.9 ,  0.1 ],
         [ 0.7 ,  0.3 ],
         [ 0.05,  0.95]]), 'CONDSET': ['b', 'c']}, 'CARDINALITY': [2, 2]}
get_edges()[source]

Returns a list of tuples. Each tuple contains two elements (parent, child) for each edge.

Examples

>>> reader = XBNReader('xbn_test.xml')
>>> reader.get_edges()
[('a', 'b'), ('a', 'c'), ('b', 'd'), ('c', 'd'), ('c', 'e')]
get_model()[source]

Returns an instance of Bayesian Model.

get_static_properties()[source]

Returns a dictionary of STATICPROPERTIES

Examples

>>> reader = XBNReader('xbn_test.xml')
>>> reader.get_static_properties()
{'FORMAT': 'MSR DTAS XML', 'VERSION': '0.2', 'CREATOR': 'Microsoft Research DTAS'}
get_variables()[source]

Returns a list of variables.

Examples

>>> reader = XBNReader('xbn_test.xml')
>>> reader.get_variables()
{'a': {'TYPE': 'discrete', 'XPOS': '13495',
       'YPOS': '10465', 'DESCRIPTION': '(a) Metastatic Cancer',
       'STATES': ['Present', 'Absent']}
'b': {'TYPE': 'discrete', 'XPOS': '11290',
       'YPOS': '11965', 'DESCRIPTION': '(b) Serum Calcium Increase',
       'STATES': ['Present', 'Absent']},
'c': {....},
'd': {....},
'e': {....}
}
class pgmpy.readwrite.XMLBeliefNetwork.XBNWriter(model, encoding='utf-8', prettyprint=True)[source]

Base class for writing XML Belief Network file format.

indent(elem, level=0)[source]

Inplace prettyprint formatter.

set_analysisnotebook(**data)[source]

Set attributes for ANALYSISNOTEBOOK tag

Parameters

**data (dict) – {name: value} for the attributes to be set.

Examples

>>> from pgmpy.readwrite.XMLBeliefNetwork import XBNWriter
>>> writer = XBNWriter()
>>> writer.set_analysisnotebook(NAME="Notebook.Cancer Example From Neapolitan",
...                             ROOT='Cancer')
set_bnmodel_name(name)[source]

Set the name of the BNMODEL.

Parameters

name (str) – Name of the BNModel.

Examples

>>> from pgmpy.readwrite.XMLBeliefNetwork import XBNWriter
>>> writer = XBNWriter()
>>> writer.set_bnmodel_name("Cancer")
set_distributions()[source]

Set distributions in the network.

Examples

>>> from pgmpy.readwrite.XMLBeliefNetwork import XBNWriter
>>> writer =XBNWriter()
>>> writer.set_distributions()
set_edges(edge_list)[source]

Set edges/arc in the network.

Parameters

edge_list (array_like) – list, tuple, dict or set whose each elements has two values (parent, child).

Examples

>>> from pgmpy.readwrite.XMLBeliefNetwork import XBNWriter
>>> writer = XBNWriter()
>>> writer.set_edges([('a', 'b'), ('a', 'c'), ('b', 'd'), ('c', 'd'), ('c', 'e')])
set_static_properties(**data)[source]

Set STATICPROPERTIES tag for the network

Parameters

**data (dict) – {name: value} for name and value of the property.

Examples

>>> from pgmpy.readwrite.XMLBeliefNetwork import XBNWriter
>>> writer = XBNWriter()
>>> writer.set_static_properties(FORMAT="MSR DTAS XML", VERSION="0.2", CREATOR="Microsoft Research DTAS")
set_variables(data)[source]

Set variables for the network.

Parameters

data (dict) – dict for variable in the form of example as shown.

Examples

>>> from pgmpy.readwrite.XMLBeliefNetwork import XBNWriter
>>> writer = XBNWriter()
>>> writer.set_variables({'a': {'TYPE': 'discrete', 'XPOS': '13495',
...                             'YPOS': '10465', 'DESCRIPTION': '(a) Metastatic Cancer',
...                             'STATES': ['Present', 'Absent']},
...                       'b': {'TYPE': 'discrete', 'XPOS': '11290',
...                             'YPOS': '11965', 'DESCRIPTION': '(b) Serum Calcium Increase',
...                             'STATES': ['Present', 'Absent']}})

XMLBIF

class pgmpy.readwrite.XMLBIF.XMLBIFReader(path=None, string=None)[source]

Base class for reading network file in XMLBIF format.

get_edges()[source]

Returns the edges of the network

Examples

>>> reader = XMLBIF.XMLBIFReader("xmlbif_test.xml")
>>> 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 a Bayesian Model instance from the file/string.

Parameters

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

Returns

BayesianModel instance

Return type

The read model.

Examples

>>> from pgmpy.readwrite import XMLBIFReader
>>> reader = XMLBIFReader("xmlbif_test.xml")
>>> model = reader.get_model()
get_parents()[source]

Returns the parents of the variables present in the network

Examples

>>> reader = XMLBIF.XMLBIFReader("xmlbif_test.xml")
>>> reader.get_parents()
{'bowel-problem': [],
 'dog-out': ['family-out', 'bowel-problem'],
 'family-out': [],
 'hear-bark': ['dog-out'],
 'light-on': ['family-out']}
get_property()[source]

Returns the property of the variable

Examples

>>> reader = XMLBIF.XMLBIFReader("xmlbif_test.xml")
>>> reader.get_property()
{'bowel-problem': ['position = (190, 69)'],
 'dog-out': ['position = (155, 165)'],
 'family-out': ['position = (112, 69)'],
 'hear-bark': ['position = (154, 241)'],
 'light-on': ['position = (73, 165)']}
get_states()[source]

Returns the states of variables present in the network

Examples

>>> reader = XMLBIF.XMLBIFReader("xmlbif_test.xml")
>>> 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

Examples

>>> reader = XMLBIF.XMLBIFReader("xmlbif_test.xml")
>>> reader.get_values()
{'bowel-problem': array([[ 0.01],
                         [ 0.99]]),
 'dog-out': array([[ 0.99,  0.01,  0.97,  0.03],
                   [ 0.9 ,  0.1 ,  0.3 ,  0.7 ]]),
 'family-out': array([[ 0.15],
                      [ 0.85]]),
 'hear-bark': array([[ 0.7 ,  0.3 ],
                     [ 0.01,  0.99]]),
 'light-on': array([[ 0.6 ,  0.4 ],
                    [ 0.05,  0.95]])}
get_variables()[source]

Returns list of variables of the network

Examples

>>> reader = XMLBIF.XMLBIFReader("xmlbif_test.xml")
>>> reader.get_variables()
['light-on', 'bowel-problem', 'dog-out', 'hear-bark', 'family-out']
class pgmpy.readwrite.XMLBIF.XMLBIFWriter(model, encoding='utf-8', prettyprint=True)[source]

Base class for writing XMLBIF network file format.

get_definition()[source]

Add Definition to XMLBIF

Returns

dict

Return type

dict of type {variable: definition tag}

Examples

>>> writer = XMLBIFWriter(model)
>>> writer.get_definition()
{'hear-bark': <Element DEFINITION at 0x7f1d48977408>,
 'family-out': <Element DEFINITION at 0x7f1d489773c8>,
 'dog-out': <Element DEFINITION at 0x7f1d48977388>,
 'bowel-problem': <Element DEFINITION at 0x7f1d48977348>,
 'light-on': <Element DEFINITION at 0x7f1d48977448>}
get_properties()[source]

Add property to variables in XMLBIF

Returns

dict

Return type

dict of type {variable: property tag}

Examples

>>> writer = XMLBIFWriter(model)
>>> writer.get_property()
{'light-on': <Element PROPERTY at 0x7f7a2ffac1c8>,
 'family-out': <Element PROPERTY at 0x7f7a2ffac148>,
 'hear-bark': <Element PROPERTY at 0x7f7a2ffac188>,
 'bowel-problem': <Element PROPERTY at 0x7f7a2ffac0c8>,
 'dog-out': <Element PROPERTY at 0x7f7a2ffac108>}
get_states()[source]

Add outcome to variables of XMLBIF

Returns

dict

Return type

dict of type {variable: outcome tags}

Examples

>>> writer = XMLBIFWriter(model)
>>> writer.get_states()
{'dog-out': [<Element OUTCOME at 0x7ffbabfcdec8>, <Element OUTCOME at 0x7ffbabfcdf08>],
 'family-out': [<Element OUTCOME at 0x7ffbabfd4108>, <Element OUTCOME at 0x7ffbabfd4148>],
 'bowel-problem': [<Element OUTCOME at 0x7ffbabfd4088>, <Element OUTCOME at 0x7ffbabfd40c8>],
 'hear-bark': [<Element OUTCOME at 0x7ffbabfcdf48>, <Element OUTCOME at 0x7ffbabfcdf88>],
 'light-on': [<Element OUTCOME at 0x7ffbabfcdfc8>, <Element OUTCOME at 0x7ffbabfd4048>]}
get_values()[source]

Add Table to XMLBIF.

Returns

dict

Return type

dict of type {variable: table tag}

Examples

>>> writer = XMLBIFWriter(model)
>>> writer.get_values()
{'dog-out': <Element TABLE at 0x7f240726f3c8>,
 'light-on': <Element TABLE at 0x7f240726f488>,
 'bowel-problem': <Element TABLE at 0x7f240726f388>,
 'family-out': <Element TABLE at 0x7f240726f408>,
 'hear-bark': <Element TABLE at 0x7f240726f448>}
get_variables()[source]

Add variables to XMLBIF

Returns

dict

Return type

dict of type {variable: variable tags}

Examples

>>> writer = XMLBIFWriter(model)
>>> writer.get_variables()
{'bowel-problem': <Element VARIABLE at 0x7fe28607dd88>,
 'family-out': <Element VARIABLE at 0x7fe28607de08>,
 'hear-bark': <Element VARIABLE at 0x7fe28607de48>,
 'dog-out': <Element VARIABLE at 0x7fe28607ddc8>,
 'light-on': <Element VARIABLE at 0x7fe28607de88>}
indent(elem, level=0)[source]

Inplace prettyprint formatter.

write_xmlbif(filename)[source]

Write the xml data into the file.

Parameters

filename (Name of the file.) –

Examples

>>> from pgmpy.readwrite import XMLBIFWriter
>>> from pgmpy.utils import get_example_model
>>> model = get_example_model('asia')
>>> writer = XMLBIFWriter(model)
>>> writer.write_xmlbif('asia.xml')