def setUp(self): reader = XMLBIFReader(string=TEST_FILE) self.expected_model = reader.get_model() self.writer = XMLBIFWriter(self.expected_model) self.model_stateless = BayesianModel([('D', 'G'), ('I', 'G'), ('G', 'L'), ('I', 'S')]) self.cpd_d = TabularCPD(variable='D', variable_card=2, values=[[0.6, 0.4]]) self.cpd_i = TabularCPD(variable='I', variable_card=2, values=[[0.7, 0.3]]) self.cpd_g = TabularCPD(variable='G', variable_card=3, values=[[0.3, 0.05, 0.9, 0.5], [0.4, 0.25, 0.08, 0.3], [0.3, 0.7, 0.02, 0.2]], evidence=['I', 'D'], evidence_card=[2, 2]) self.cpd_l = TabularCPD(variable='L', variable_card=2, values=[[0.1, 0.4, 0.99], [0.9, 0.6, 0.01]], evidence=['G'], evidence_card=[3]) self.cpd_s = TabularCPD(variable='S', variable_card=2, values=[[0.95, 0.2], [0.05, 0.8]], evidence=['I'], evidence_card=[2]) self.model_stateless.add_cpds(self.cpd_d, self.cpd_i, self.cpd_g, self.cpd_l, self.cpd_s) self.writer_stateless = XMLBIFWriter(self.model_stateless)
def test_write_xmlbif_statefull(self): self.writer.write_xmlbif("dog_problem_output.xbif") with open("dog_problem_output.xbif", "r") as f: file_text = f.read() reader = XMLBIFReader(string=file_text) model = reader.get_model() self.assert_models_equivelent(self.expected_model, model) os.remove("dog_problem_output.xbif")
def loadXMLBIF(path): reader = XMLBIFReader(path) model = reader.get_model() print("LOADMODEL: "+str(path)) """ for cpd in model.get_cpds(): #if(cpd.variable == "SENSOR1"): print("CPD of {variable}:".format(variable=cpd.variable)) print(cpd) """ return model
def test_write_xmlbif_stateless(self): self.writer_stateless.write_xmlbif("grade_problem_output.xbif") with open("grade_problem_output.xbif", 'r') as f: reader = XMLBIFReader(f) model = reader.get_model() self.assert_models_equivelent(self.model_stateless, model) self.assertDictEqual({'G': ['state0', 'state1', 'state2'], 'I': ['state0', 'state1'], 'D': ['state0', 'state1'], 'S': ['state0', 'state1'], 'L': ['state0', 'state1']}, model.get_cpds('D').state_names) os.remove("grade_problem_output.xbif")
def test_write_xmlbif_stateless(self): self.writer_stateless.write_xmlbif("grade_problem_output.xbif") with open("grade_problem_output.xbif", "r") as f: reader = XMLBIFReader(f) model = reader.get_model() self.assert_models_equivelent(self.model_stateless, model) self.assertDictEqual( { "G": ["state0", "state1", "state2"], "I": ["state0", "state1"], "D": ["state0", "state1"], "S": ["state0", "state1"], "L": ["state0", "state1"], }, model.get_cpds("D").state_names, ) os.remove("grade_problem_output.xbif")
def setUp(self): reader = XMLBIFReader(string=TEST_FILE) self.expected_model = reader.get_model() self.writer = XMLBIFWriter(self.expected_model) self.model_stateless = BayesianModel([("D", "G"), ("I", "G"), ("G", "L"), ("I", "S")]) self.cpd_d = TabularCPD(variable="D", variable_card=2, values=[[0.6, 0.4]]) self.cpd_i = TabularCPD(variable="I", variable_card=2, values=[[0.7, 0.3]]) self.cpd_g = TabularCPD( variable="G", variable_card=3, values=[ [0.3, 0.05, 0.9, 0.5], [0.4, 0.25, 0.08, 0.3], [0.3, 0.7, 0.02, 0.2], ], evidence=["I", "D"], evidence_card=[2, 2], ) self.cpd_l = TabularCPD( variable="L", variable_card=2, values=[[0.1, 0.4, 0.99], [0.9, 0.6, 0.01]], evidence=["G"], evidence_card=[3], ) self.cpd_s = TabularCPD( variable="S", variable_card=2, values=[[0.95, 0.2], [0.05, 0.8]], evidence=["I"], evidence_card=[2], ) self.model_stateless.add_cpds(self.cpd_d, self.cpd_i, self.cpd_g, self.cpd_l, self.cpd_s) self.writer_stateless = XMLBIFWriter(self.model_stateless)
def setUp(self): with open("dog_problem.xml", 'w') as fout: fout.write(TEST_FILE) self.reader = XMLBIFReader("dog_problem.xml")
def setUp(self): self.reader = XMLBIFReader(string=TEST_FILE)
def setUp(self): self.reader = XMLBIFReader(string=""" <BIF VERSION="0.3"> <NETWORK> <NAME>Dog-Problem</NAME> <VARIABLE TYPE="nature"> <NAME>light-on</NAME> <OUTCOME>true</OUTCOME> <OUTCOME>false</OUTCOME> <PROPERTY>position = (73, 165)</PROPERTY> </VARIABLE> <VARIABLE TYPE="nature"> <NAME>bowel-problem</NAME> <OUTCOME>true</OUTCOME> <OUTCOME>false</OUTCOME> <PROPERTY>position = (190, 69)</PROPERTY> </VARIABLE> <VARIABLE TYPE="nature"> <NAME>dog-out</NAME> <OUTCOME>true</OUTCOME> <OUTCOME>false</OUTCOME> <PROPERTY>position = (155, 165)</PROPERTY> </VARIABLE> <VARIABLE TYPE="nature"> <NAME>hear-bark</NAME> <OUTCOME>true</OUTCOME> <OUTCOME>false</OUTCOME> <PROPERTY>position = (154, 241)</PROPERTY> </VARIABLE> <VARIABLE TYPE="nature"> <NAME>family-out</NAME> <OUTCOME>true</OUTCOME> <OUTCOME>false</OUTCOME> <PROPERTY>position = (112, 69)</PROPERTY> </VARIABLE> <DEFINITION> <FOR>light-on</FOR> <GIVEN>family-out</GIVEN> <TABLE>0.6 0.4 0.05 0.95 </TABLE> </DEFINITION> <DEFINITION> <FOR>bowel-problem</FOR> <TABLE>0.01 0.99 </TABLE> </DEFINITION> <DEFINITION> <FOR>dog-out</FOR> <GIVEN>bowel-problem</GIVEN> <GIVEN>family-out</GIVEN> <TABLE>0.99 0.01 0.97 0.03 0.9 0.1 0.3 0.7 </TABLE> </DEFINITION> <DEFINITION> <FOR>hear-bark</FOR> <GIVEN>dog-out</GIVEN> <TABLE>0.7 0.3 0.01 0.99 </TABLE> </DEFINITION> <DEFINITION> <FOR>family-out</FOR> <TABLE>0.15 0.85 </TABLE> </DEFINITION> </NETWORK> </BIF> """)
#!/usr/bin/env python import sys sys.path.insert(0, 'lib') # this line is necessary for the rest import os # of the imports to work! import web import sqlitedb from jinja2 import Environment, FileSystemLoader import json from pgmpy.models import BayesianModel from pgmpy.inference import VariableElimination from pgmpy.readwrite import XMLBIFReader MODEL_FILE = 'bayes453.bif.xml' model = XMLBIFReader(MODEL_FILE).get_model() infer = VariableElimination(model) baseline_result = {} for node in model.nodes(): baseline_result[node] = infer.query([node])[node].values[1] ########################################################################################### ##########################DO NOT CHANGE ANYTHING ABOVE THIS LINE!########################## ########################################################################################### ######################BEGIN HELPER METHODS###################### # helper method to render a template in the templates/ directory # # `template_name': name of template file to render #