XMLBeliefNetwork

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

Initializer for XBNReader class.

Parameters:
  • path (str or file) – Path of the file containing XBN data.

  • string (str) – String of XBN data

Examples

>>> reader = XBNReader('test_XBN.xml')

Reference

[1] Microsoft Research. XML belief network file format.

http://xml.coverpages.org/xbn-MSdefault19990414.html, 1999.

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

Initializer for XBNWriter class

Parameters:
  • model (BayesianNetwork Instance) – Model to write

  • encoding (str(optional)) – Encoding for test data

  • prettyprint (Bool(optional)) – Indentation in output XML if true

  • Reference

  • ---------

  • http (//xml.coverpages.org/xbn-MSdefault19990414.html) –

Examples

>>> writer = XBNWriter(model)
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 element 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']}})