UAIReader#

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

Bases: object

Initialize an instance of UAI reader class

Parameters:
pathfile or str

Path of the file containing UAI information.

stringstr

String containing UAI information.

Examples

>>> from pgmpy.readwrite import UAIReader, UAIWriter
>>> from pgmpy.example_models import load_model
>>> model = load_model("bnlearn/asia")
>>> writer = UAIWriter(model)
>>> writer.write("asia.uai")
>>> reader = UAIReader("asia.uai")
>>> model = reader.get_model()
get_domain()[source]#

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

Returns:
dict: dictionary containing variables and their domains

Examples

>>> from pgmpy.readwrite import UAIReader, UAIWriter
>>> from pgmpy.example_models import load_model
>>> model = load_model("bnlearn/asia")
>>> writer = UAIWriter(model)
>>> writer.write("asia.uai")
>>> reader = UAIReader("asia.uai")
>>> reader.get_domain()
{'var_0': '2', 'var_1': '2', 'var_2': '2', 'var_3': '2',
'var_4': '2', 'var_5': '2', 'var_6': '2', 'var_7': '2'}
get_edges()[source]#

Returns the edges of the network.

Returns:
set: set containing the edges of the network

Examples

>>> from pgmpy.readwrite import UAIReader, UAIWriter
>>> from pgmpy.example_models import load_model
>>> model = load_model("bnlearn/asia")
>>> writer = UAIWriter(model)
>>> writer.write("asia.uai")
>>> reader = UAIReader("asia.uai")
>>> sorted(reader.get_edges())
[('var_0', 'var_6'), ('var_1', 'var_2'), ('var_3', 'var_2'), ('var_3', 'var_7'),
('var_4', 'var_3'), ('var_5', 'var_1'), ('var_5', 'var_4'), ('var_6', 'var_3')]
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.

Examples

>>> from pgmpy.readwrite import UAIReader, UAIWriter
>>> from pgmpy.example_models import load_model
>>> model = load_model("bnlearn/asia")
>>> writer = UAIWriter(model)
>>> writer.write("asia.uai")
>>> reader = UAIReader("asia.uai")
>>> reader.get_model()
<pgmpy.models.DiscreteBayesianNetwork.DiscreteBayesianNetwork object at 0x...>
get_network_type()[source]#

Returns the type of network defined by the file.

Returns:
stringstr

String containing network type.

Examples

>>> from pgmpy.readwrite import UAIReader, UAIWriter
>>> from pgmpy.example_models import load_model
>>> model = load_model("bnlearn/asia")
>>> writer = UAIWriter(model)
>>> writer.write("asia.uai")
>>> reader = UAIReader("asia.uai")
>>> reader.get_network_type()
'BAYES'
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:
listlist of tuples of child variable and values in Bayesian

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

Examples

>>> from pgmpy.readwrite import UAIReader, UAIWriter
>>> from pgmpy.example_models import load_model
>>> model = load_model("bnlearn/asia")
>>> writer = UAIWriter(model)
>>> writer.write("asia.uai")
>>> reader = UAIReader("asia.uai")
>>> reader.get_tables()
[('var_0', ['0.01', '0.99']), ('var_1', ['0.6', '0.3', '0.4', '0.7']),
('var_2', ['0.9', '0.8', '0.7', '0.1', '0.1', '0.2', '0.3', '0.9']),
('var_3', ['1.0', '1.0', '1.0', '0.0', '0.0', '0.0', '0.0', '1.0']),
('var_4', ['0.1', '0.01', '0.9', '0.99']), ('var_5', ['0.5', '0.5']),
('var_6', ['0.05', '0.01', '0.95', '0.99']),
('var_7', ['0.98', '0.05', '0.02', '0.95'])]
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: list of variables

Examples

>>> from pgmpy.readwrite import UAIReader, UAIWriter
>>> from pgmpy.example_models import load_model
>>> model = load_model("bnlearn/asia")
>>> writer = UAIWriter(model)
>>> writer.write("asia.uai")
>>> reader = UAIReader("asia.uai")
>>> reader.get_variables()
['var_0', 'var_1', 'var_2', 'var_3', 'var_4', 'var_5', 'var_6', 'var_7']