def setUp(self): from gPy.Variables import Domain self.bnm = BN(domain=Domain()) # don't use default domain self.bnm.from_dnet(read_dnet('Asia.dnet')) self.cptdict = self.bnm.factors # taken directly from Netica output self.marginals = [ Factor((('VisitAsia'), ), [0.99, 0.01]), Factor((('Tuberculosis'), ), [0.9896, 0.0104]), Factor((('Smoking'), ), [0.5, 0.5]), Factor((('Cancer'), ), [0.945, 0.055]), Factor((('TbOrCa'), ), [0.93517, 0.064828]), Factor((('XRay'), ), [0.11029, 0.88971]), Factor((('Bronchitis'), ), [0.55, 0.45]), Factor((('Dyspnea'), ), [0.56403, 0.43597]) ] # taken directly from Netica output self.cond_marginals = [ Factor((('VisitAsia'), ), [0.95192, 0.048077]), Factor((('Tuberculosis'), ), [0, 1]), Factor((('Smoking'), ), [0.52381, 0.47619]), #other marginals are conditional on these values #Factor((('Cancer'),), # [1,0]), #Factor((('TbOrCa'),), # [0,1]), Factor((('XRay'), ), [0.98, 0.02]), Factor((('Bronchitis'), ), [0.55714, 0.44286]), Factor((('Dyspnea'), ), [0.21143, 0.78857]) ]
def testdnet(self): from gPy.IO import read_dnet from gPy.Models import BN from gPy.Variables import Domain bnm = BN(domain=Domain()) bnm.from_dnet(read_dnet('Asia.dnet')) self.samegraph(bnm.adg(), self.asia_adg) for name, cpt_in_file in self.asia_cpts.items(): cpt = bnm[name] self.samecpt(cpt, cpt_in_file, cpt.child())
def testdnet(self): from gPy.IO import read_dnet from gPy.Models import BN from gPy.Variables import Domain bnm = BN(domain=Domain()) bnm.from_dnet(read_dnet('Asia.dnet')) self.samegraph(bnm.adg(),self.asia_adg) for name, cpt_in_file in self.asia_cpts.items(): cpt = bnm[name] self.samecpt(cpt,cpt_in_file,cpt.child())
def setUp(self): from gPy.Variables import Domain self.domain = Domain() self.bnm = BN(domain=self.domain) self.bnm.from_dnet(read_dnet('Asia.dnet')) self.cptdict = {} # taken directly from Netica output self.marginals = [ Factor((('VisitAsia'),), [0.99,0.01]), Factor((('Tuberculosis'),), [0.9896,0.0104]), Factor((('Smoking'),), [0.5,0.5]), Factor((('Cancer'),), [0.945,0.055]), Factor((('TbOrCa'),), [0.93517, 0.064828]), Factor((('XRay'),), [0.11029, 0.88971]), Factor((('Bronchitis'),), [0.55,0.45]), Factor((('Dyspnea'),), [0.56403,0.43597]) ] # taken directly from Netica output self.cond_marginals = [ Factor((('VisitAsia'),), [0.95192,0.048077]), Factor((('Tuberculosis'),), [0,1]), Factor((('Smoking'),), [0.52381,0.47619]), #other marginals are conditional on these values #Factor((('Cancer'),), # [1,0]), #Factor((('TbOrCa'),), # [0,1]), Factor((('XRay'),), [0.98, 0.02]), Factor((('Bronchitis'),), [0.55714,0.44286]), Factor((('Dyspnea'),), [0.21143,0.78857]) ] for cpt in self.bnm: self.cptdict[cpt.child()] = cpt self.rawdata = read_csv(open('alarm_1K.dat'))
def setUp(self): from gPy.Variables import Domain bnm = BN(domain=Domain()) bnm.from_dnet(read_dnet('Asia.dnet')) self.hypergraph = bnm._hypergraph self.adg = bnm._adg self.tarjan = UGraph(range(1,11), ((1,2),(1,3),(2,3),(2,10),(3,10),(4,5), (4,7),(5,6),(5,9),(5,7),(6,7),(6,9), (7,8),(7,9),(8,9),(8,10),(9,10))) self.tarjan2 = UGraph(range(1,10), ((1,4),(1,3),(2,3),(2,7),(3,5),(3,6), (4,5),(4,8),(5,6),(5,8),(6,7),(6,9), (7,9),(8,9))) self.tarjan3 = UGraph(range(1,10), ((1,4),(1,3),(2,3),(2,7),(3,5),(3,6), (4,5),(4,8),(5,6),(5,8),(6,7),(6,9), (7,9),(8,9), (3,4),(3,7),(4,6),(4,7),(5,7),(6,8),(7,8))) self.tarjanh1 = Hypergraph([[3,4],[2,4],[1,2,3]]) self.tarjanh2 = Hypergraph([[3,4],[2,4],[1,2,3],[2,3,4]]) self.graph1 = UGraph('ABCDEF',('AB','AC','BD','CE','EF')) self.graph2 = UGraph('ABCDEF',('AB','AC','BD','CE','EF','BC','CD','DE'))
def bnfromfile(filename): x= BN(domain=self.domain); x.from_dnet(read_dnet(filename)); return x self.assertRaises(CPTError,bnfromfile,open('Asia_wrong.dnet'))
from gPy.IO import read_dnet from gPy.Models import BNM from gPy.Demos import prod_gui asia = BNM() asia.from_dnet(read_dnet(open('Asia_lower.dnet'))) cancer = asia['Smoking'] * asia['Cancer'] cancer = cancer.sumout(['Smoking']) visit = asia['VisitAsia'] * 1 prod_gui(visit,cancer)
from gPy.IO import read_dnet from gPy.Models import BNM from gPy.Demos import marginalise_gui asia = BNM() asia.from_dnet(read_dnet(open('Asia_lower.dnet'))) smoking = asia['Smoking'] * 1 cancer = asia['Cancer'] * 1 bronchitis = asia['Bronchitis'] * 1 smoking._data = [0.4,0.6] # gives better example print cancer print smoking print bronchitis foo = bronchitis.demo_mult(cancer) joint = foo.demo_mult(smoking) print joint #show joint as a single factor with strings not numbers #then marginalise joint, again showing numbers
def bnfromfile(filename): x = BN(domain=self.domain) x.from_dnet(read_dnet(filename)) return x