def __init__(self, name, lic=None, file=None): if file is None: self.name = name self.ntc = Netica() self.env = self.ntc.newenv(lic) self.ntc.initenv(self.env) self.net = self.ntc.newnet(name, self.env) self.nodes = [] else: self.name = name self.ntc = Netica() self.env = self.ntc.newenv(lic) self.ntc.initenv(self.env) self.net = self.ntc.opennet(self.env, file) nodelist_p = self.ntc.getnetnodes(self.net) numnode = self.ntc.lengthnodelist(nodelist_p) self.nodes = [] for i in range(numnode): nodei_p = self.ntc.nthnode(nodelist_p, i) nodename = self.ntc.getnodename(nodei_p) statename = 'init'; statenames = []; istate = 0 while statename!='error': statename = self.ntc.getnodestatename(nodei_p, istate) statenames.append(statename) istate += 1 statenames = statenames[:-1] # default: no parents and continuous: therefore not the original nodes nodei = Node(nodename, parents=[], rvname='continuous') nodei.set_node_ptr(nodei_p) nodei.set_node_state_name(statenames) self.nodes.append(nodei)
def __init__(self, name, lic=None, file=None): if file is None: self.name = name self.ntc = Netica() self.env = self.ntc.newenv(lic) self.ntc.initenv(self.env) self.net = self.ntc.newnet(name, self.env) self.nodes = [] else: self.name = name self.ntc = Netica() self.env = self.ntc.newenv(lic) self.ntc.initenv(self.env) self.net = self.ntc.opennet(self.env, file) nodelist_p = self.ntc.getnetnodes(self.net) numnode = self.ntc.lengthnodelist(nodelist_p) self.nodes = [] for i in range(numnode): nodei_p = self.ntc.nthnode(nodelist_p, i) nodename = self.ntc.getnodename(nodei_p) statename = 'init' statenames = [] istate = 0 while statename != 'error': statename = self.ntc.getnodestatename(nodei_p, istate) statenames.append(statename) istate += 1 statenames = statenames[:-1] # default: no parents and continuous: therefore not the original nodes nodei = Node(nodename, parents=[], rvname='continuous') nodei.set_node_ptr(nodei_p) nodei.set_node_state_name(statenames) self.nodes.append(nodei)
def setUp(self): from netica import Netica self.netica = Netica() self.netfilename = 'curvnet.dne' self.env = self.netica.newenv() self.netica.initenv(self.env) self.net = self.netica.opennet(self.env, self.netfilename) self.nl_p = self.netica.getnetnodes(self.net) self.netica.compilenet(self.net)
class Network(object): def __init__(self, name, lic=None, file=None): if file is None: self.name = name self.ntc = Netica() self.env = self.ntc.newenv(lic) self.ntc.initenv(self.env) self.net = self.ntc.newnet(name, self.env) self.nodes = [] else: self.name = name self.ntc = Netica() self.env = self.ntc.newenv(lic) self.ntc.initenv(self.env) self.net = self.ntc.opennet(self.env, file) nodelist_p = self.ntc.getnetnodes(self.net) numnode = self.ntc.lengthnodelist(nodelist_p) self.nodes = [] for i in range(numnode): nodei_p = self.ntc.nthnode(nodelist_p, i) nodename = self.ntc.getnodename(nodei_p) statename = 'init'; statenames = []; istate = 0 while statename!='error': statename = self.ntc.getnodestatename(nodei_p, istate) statenames.append(statename) istate += 1 statenames = statenames[:-1] # default: no parents and continuous: therefore not the original nodes nodei = Node(nodename, parents=[], rvname='continuous') nodei.set_node_ptr(nodei_p) nodei.set_node_state_name(statenames) self.nodes.append(nodei) def add_nodes(self, nodes): for node in nodes: if (node.nodekind==NATURE_NODE) and (node.cpt is None or (node.cpt.size == 0)): print "assign {} cpt first before add to network".format(node.name) #nodeptr = self.ntc.newnode(node.name, node.cpt.shape[0], self.net) #node.set_pointer(nodeptr) #node.set_net(self) node.add_to_net(self) self.nodes.append(node) def add_link(self): for node in self.nodes: if node.parents is not None: for parentNode in node.parents: self.ntc.addlink(parent=parentNode.ptr, child=node.ptr) def define_nodes(self): """define nodes can only be implemented after all links are added """ for node in self.nodes: node.define() def compile_net(self): self.ntc.compilenet(self.net) def set_autoupdate(self): self.ntc.setautoupdate(self.net) def get_node_beliefs(self, node): beliefs32 = self.ntc.getnodebeliefs(node.ptr) return beliefs32.astype('float') def get_node_expectedutils(self, node): utils32 = self.ntc.getnodeexpectedutils(node.ptr) return utils32.astype('float') def enter_finding(self, node, evidence): if isinstance(evidence, int): stateindx = evidence elif isinstance(evidence, basestring): lowercaseStates = np.array([name.lower() for name in node.statenames]) stateindx = np.where(lowercaseStates == evidence.lower())[0][0] stateindx = c_int(stateindx) self.ntc.enterfinding(node.ptr, stateindx) def retract_nodefindings(self, node): self.ntc.retractnodefindings(node.ptr) def retract_netfindings(self): self.ntc.retractnetfindings(self.net) def save_net(self, filename): self.ntc.savenet(self.env, self.net, filename) def set_node_kind(self, node, nodekind=NATURE_NODE): self.ntc.set_node_kind(node, nodekind) def get_node_funcstate(self, node, parentstate): return self.ntc.getnodefuncstate(node.ptr, parentstate) def find_nodenamed(self, nodename): """ find node by node name """ found = False for i,node in enumerate(self.nodes): if node.name == nodename: found =True break if found: return node else: return 'error'
from netica import Netica import numpy as np # initialize class ntc = Netica() # create new environment env = ntc.newenv() # initialize environment ntc.initenv(env) # create new net net_p = ntc.newnet('BNZeebrugge', env) # define nodes nodeBC1 = ntc.newnode('PeakWaterLevel', 5, net_p) ntc.setnodelevels(nodeBC1, 5, np.asarray([6.35, 7.1, 7.4, 7.8, 7.9], dtype='float64')) ntc.setnodetitle(nodeBC1, 'Peak Water Level (m)') nodeBC2 = ntc.newnode('MaxSignificantWaveHeight', 5, net_p) ntc.setnodelevels(nodeBC2, 5, np.asarray([5.2, 5.7, 5.9, 6.08, 6.2], dtype='float64')) ntc.setnodetitle(nodeBC2, 'Max. significant wave height (m)') nodeR1 = ntc.newnode('Location_Houses', 4, net_p) ntc.setnodetitle(nodeR1, 'Houses - Location') ntc.setnodestatenames(nodeR1, "Area1, Area2, Area3, Area4") ntc.setnodestatetitle(nodeR1, 0, 'Area 1 (has 283 houses)') ntc.setnodestatetitle(nodeR1, 1, 'Area 2 (has 759 houses)') ntc.setnodestatetitle(nodeR1, 2, 'Area 3 (has 383 houses)') ntc.setnodestatetitle(nodeR1, 3, 'Area 4 (has 273 houses)')
from netica import Netica import numpy as np # initialize class ntc = Netica() # create new environment env = ntc.newenv() # initialize environment ntc.initenv(env) # create new net net_p = ntc.newnet('ChestClinic', env) # define nodes VisitAsia = ntc.newnode("VisitAsia", 2, net_p) Tuberculosis = ntc.newnode ("Tuberculosis", 2, net_p) Smoking = ntc.newnode ("Smoking", 2, net_p) Cancer = ntc.newnode ("Cancer", 2, net_p) TbOrCa = ntc.newnode ("TbOrCa", 2, net_p) XRay = ntc.newnode ("XRay", 2, net_p) # set node names ntc.setnodestatenames(VisitAsia, "visit, no_visit"); ntc.setnodestatenames(Tuberculosis,"present, absent"); ntc.setnodestatenames(Smoking, "smoker, nonsmoker"); ntc.setnodestatenames(Cancer, "present, absent"); ntc.setnodestatenames(TbOrCa, "true, false"); ntc.setnodestatenames(XRay, "abnormal,normal"); # define links ntc.addlink(parent=VisitAsia, child=Tuberculosis)
class UnitTests(unittest.TestCase): def setUp(self): from netica import Netica self.netica = Netica() self.netfilename = 'curvnet.dne' self.env = self.netica.newenv() self.netica.initenv(self.env) self.net = self.netica.opennet(self.env, self.netfilename) self.nl_p = self.netica.getnetnodes(self.net) self.netica.compilenet(self.net) def test_enternodelikelyhood(self): self.netica.retractnetfindings(self.net) node_p = self.netica.getnodenamed('radius', self.net) self.netica.enternodelikelyhood(node_p, [1, 0, 0, 0, 0, 0]) # self.netica.savenet(env, net, 'test.dne') def test_equationtotable(self): node_p = self.netica.getnodenamed('radius', self.net) self.netica.equationtotable(node_p, 10, True, True) def test_revisecptsbycasefile(self): casefilename = 'wavedir.cas' file_p = self.netica._newstream(self.env, casefilename) self.netica.revisecptsbycasefile(file_p, self.nl_p, 0, 1) def test_getnodeequation(self): node_p = self.netica.getnodenamed('phi', self.net) eqn = self.netica.getnodeequation(node_p) print eqn # the radius node has no equation, thus should result in an empty string node_p = self.netica.getnodenamed('radius', self.net) eqn = self.netica.getnodeequation(node_p) self.assertEqual(eqn, '') def test_setnodeequation(self): node_p = self.netica.getnodenamed('phi', self.net) eqn_in = 'phi (theta, wavedir) = theta - wavedir' self.netica.setnodeequation(node_p, eqn_in) eqn_out = self.netica.getnodeequation(node_p) self.assertEqual(eqn_in, eqn_out) def test_newnode(self): nodename = 'test' node_p = self.netica.newnode(nodename, 3, self.net) self.assertEqual(self.netica.getnodename(node_p), nodename) def test_deletenode(self): nodename = 'test' node_p = self.netica.newnode(nodename, 3, self.net) self.netica.deletenode(node_p) def test_addlink(self): parent = self.netica.getnodenamed('wavedir', self.net) child = self.netica.getnodenamed('erosionfactor', self.net) link_index = self.netica.addlink(parent, child) print 'index', link_index def test_deletelink(self): child = self.netica.getnodenamed('erosionfactor', self.net) link_index = 1 self.netica.deletelink(link_index, child) def test_getnodelevels_continuous(self): node_p = self.netica.getnodenamed('phi', self.net) print self.netica.getnodelevels(node_p) def test_getnodelevels_discrete(self): node_p = self.netica.newnode('Time_horizon', 3, self.net) #self.netica.setnodestatenames(node_p, "one, five, ten") self.netica.getnodelevels(node_p) def test_getnodestatename(self): node_p = self.netica.newnode('Time_horizon', 3, self.net) self.netica.setnodestatenames(node_p, "one, five, ten") print 'STATE: "' + self.netica.getnodestatename(node_p, 0) + '"' def test_enterfinding(self): node_p = self.netica.getnodenamed('phi', self.net) self.netica.enterfinding(node_p, 0) def test_getnodeexpectedvalue(self): node_p = self.netica.getnodenamed('phi', self.net) expval = self.netica.getnodeexpectedvalue(node_p) print 'expval = ', expval
def test_opennet(self): from netica import Netica netica = Netica() netfilename = 'curvnet.dne' env = netica.newenv() netica.initenv(env) net = netica.opennet(env, netfilename) # this is another test... netica.compilenet(net) nl_p = netica.getnetnodes(net) print nl_p nnodes = netica.lengthnodelist(nl_p) print nnodes for index in range(nnodes): node_p = netica.nthnode(nl_p, index) print netica.getnodename(node_p) print netica.getnodebeliefs(node_p) print netica.getnodetype(node_p) self.assertTrue(netica)
class UnitTests(unittest.TestCase): def setUp(self): from netica import Netica self.netica = Netica() self.netfilename = 'curvnet.dne' self.env = self.netica.newenv() self.netica.initenv(self.env) self.net = self.netica.opennet(self.env, self.netfilename) self.nl_p = self.netica.getnetnodes(self.net) self.netica.compilenet(self.net) def test_enternodelikelyhood(self): self.netica.retractnetfindings(self.net) node_p = self.netica.getnodenamed('radius', self.net) self.netica.enternodelikelyhood(node_p, [1,0,0,0,0,0]) # self.netica.savenet(env, net, 'test.dne') def test_equationtotable(self): node_p = self.netica.getnodenamed('radius', self.net) self.netica.equationtotable(node_p, 10, True, True) def test_revisecptsbycasefile(self): casefilename = 'wavedir.cas' file_p = self.netica._newstream(self.env, casefilename) self.netica.revisecptsbycasefile(file_p, self.nl_p, 0, 1) def test_getnodeequation(self): node_p = self.netica.getnodenamed('phi', self.net) eqn = self.netica.getnodeequation(node_p) print eqn # the radius node has no equation, thus should result in an empty string node_p = self.netica.getnodenamed('radius', self.net) eqn = self.netica.getnodeequation(node_p) self.assertEqual(eqn, '') def test_setnodeequation(self): node_p = self.netica.getnodenamed('phi', self.net) eqn_in = 'phi (theta, wavedir) = theta - wavedir' self.netica.setnodeequation(node_p, eqn_in) eqn_out = self.netica.getnodeequation(node_p) self.assertEqual(eqn_in, eqn_out) def test_newnode(self): nodename = 'test' node_p = self.netica.newnode(nodename, 3, self.net) self.assertEqual(self.netica.getnodename(node_p), nodename) def test_deletenode(self): nodename = 'test' node_p = self.netica.newnode(nodename, 3, self.net) self.netica.deletenode(node_p) def test_addlink(self): parent = self.netica.getnodenamed('wavedir', self.net) child = self.netica.getnodenamed('erosionfactor', self.net) link_index = self.netica.addlink(parent, child) print 'index',link_index def test_deletelink(self): child = self.netica.getnodenamed('erosionfactor', self.net) link_index = 1 self.netica.deletelink(link_index, child) def test_getnodelevels_continuous(self): node_p = self.netica.getnodenamed('phi', self.net) print self.netica.getnodelevels(node_p) def test_getnodelevels_discrete(self): node_p = self.netica.newnode('Time_horizon', 3, self.net) #self.netica.setnodestatenames(node_p, "one, five, ten") self.netica.getnodelevels(node_p) def test_getnodestatename(self): node_p = self.netica.newnode('Time_horizon', 3, self.net) self.netica.setnodestatenames(node_p, "one, five, ten") print 'STATE: "' + self.netica.getnodestatename(node_p, 0) + '"' def test_enterfinding(self): node_p = self.netica.getnodenamed('phi', self.net) self.netica.enterfinding(node_p, 0) def test_getnodeexpectedvalue(self): node_p = self.netica.getnodenamed('phi', self.net) expval = self.netica.getnodeexpectedvalue(node_p) print 'expval = ',expval
class Network(object): def __init__(self, name, lic=None, file=None): if file is None: self.name = name self.ntc = Netica() self.env = self.ntc.newenv(lic) self.ntc.initenv(self.env) self.net = self.ntc.newnet(name, self.env) self.nodes = [] else: self.name = name self.ntc = Netica() self.env = self.ntc.newenv(lic) self.ntc.initenv(self.env) self.net = self.ntc.opennet(self.env, file) nodelist_p = self.ntc.getnetnodes(self.net) numnode = self.ntc.lengthnodelist(nodelist_p) self.nodes = [] for i in range(numnode): nodei_p = self.ntc.nthnode(nodelist_p, i) nodename = self.ntc.getnodename(nodei_p) statename = 'init' statenames = [] istate = 0 while statename != 'error': statename = self.ntc.getnodestatename(nodei_p, istate) statenames.append(statename) istate += 1 statenames = statenames[:-1] # default: no parents and continuous: therefore not the original nodes nodei = Node(nodename, parents=[], rvname='continuous') nodei.set_node_ptr(nodei_p) nodei.set_node_state_name(statenames) self.nodes.append(nodei) def add_nodes(self, nodes): for node in nodes: if (node.nodekind == NATURE_NODE) and (node.cpt is None or (node.cpt.size == 0)): print "assign {} cpt first before add to network".format( node.name) #nodeptr = self.ntc.newnode(node.name, node.cpt.shape[0], self.net) #node.set_pointer(nodeptr) #node.set_net(self) node.add_to_net(self) self.nodes.append(node) def add_link(self): for node in self.nodes: if node.parents is not None: for parentNode in node.parents: self.ntc.addlink(parent=parentNode.ptr, child=node.ptr) def define_nodes(self): """define nodes can only be implemented after all links are added """ for node in self.nodes: node.define() def compile_net(self): self.ntc.compilenet(self.net) def set_autoupdate(self): self.ntc.setautoupdate(self.net) def get_node_beliefs(self, node): beliefs32 = self.ntc.getnodebeliefs(node.ptr) return beliefs32.astype('float') def get_node_expectedutils(self, node): utils32 = self.ntc.getnodeexpectedutils(node.ptr) return utils32.astype('float') def enter_finding(self, node, evidence): if isinstance(evidence, int): stateindx = evidence elif isinstance(evidence, basestring): lowercaseStates = np.array( [name.lower() for name in node.statenames]) stateindx = np.where(lowercaseStates == evidence.lower())[0][0] stateindx = c_int(stateindx) self.ntc.enterfinding(node.ptr, stateindx) def retract_nodefindings(self, node): self.ntc.retractnodefindings(node.ptr) def retract_netfindings(self): self.ntc.retractnetfindings(self.net) def save_net(self, filename): self.ntc.savenet(self.env, self.net, filename) def set_node_kind(self, node, nodekind=NATURE_NODE): self.ntc.set_node_kind(node, nodekind) def get_node_funcstate(self, node, parentstate): return self.ntc.getnodefuncstate(node.ptr, parentstate) def find_nodenamed(self, nodename): """ find node by node name """ found = False for i, node in enumerate(self.nodes): if node.name == nodename: found = True break if found: return node else: return 'error'
from netica import Netica import numpy as np # initialize class ntc = Netica() # create new environment env = ntc.newenv() # initialize environment ntc.initenv(env) # create new net net_p = ntc.newnet('ChestClinic', env) # define nodes VisitAsia = ntc.newnode("VisitAsia", 2, net_p) Tuberculosis = ntc.newnode("Tuberculosis", 2, net_p) Smoking = ntc.newnode("Smoking", 2, net_p) Cancer = ntc.newnode("Cancer", 2, net_p) TbOrCa = ntc.newnode("TbOrCa", 2, net_p) XRay = ntc.newnode("XRay", 2, net_p) # set node names ntc.setnodestatenames(VisitAsia, "visit, no_visit") ntc.setnodestatenames(Tuberculosis, "present, absent") ntc.setnodestatenames(Smoking, "smoker, nonsmoker") ntc.setnodestatenames(Cancer, "present, absent") ntc.setnodestatenames(TbOrCa, "true, false") ntc.setnodestatenames(XRay, "abnormal,normal") # define links ntc.addlink(parent=VisitAsia, child=Tuberculosis)
from netica import Netica import numpy as np # initialize class ntc = Netica() # create new environment env = ntc.newenv() # initialize environment ntc.initenv(env) # create new net net_p = ntc.newnet('BNZeebrugge', env) # define nodes nodeBC1 = ntc.newnode('PeakWaterLevel', 5, net_p) ntc.setnodelevels(nodeBC1, 5, np.asarray([6.35, 7.1, 7.4, 7.8, 7.9], dtype='float64')) ntc.setnodetitle(nodeBC1,'Peak Water Level (m)') nodeBC2 = ntc.newnode('MaxSignificantWaveHeight', 5, net_p) ntc.setnodelevels(nodeBC2, 5, np.asarray([5.2, 5.7, 5.9, 6.08, 6.2], dtype='float64')) ntc.setnodetitle(nodeBC2,'Max. significant wave height (m)') nodeR1 = ntc.newnode('Location_Houses', 4, net_p) ntc.setnodetitle(nodeR1,'Houses - Location') ntc.setnodestatenames(nodeR1, "Area1, Area2, Area3, Area4") ntc.setnodestatetitle(nodeR1, 0, 'Area 1 (has 283 houses)') ntc.setnodestatetitle(nodeR1, 1, 'Area 2 (has 759 houses)') ntc.setnodestatetitle(nodeR1, 2, 'Area 3 (has 383 houses)') ntc.setnodestatetitle(nodeR1, 3, 'Area 4 (has 273 houses)') nodeH1_R1 = ntc.newnode('MaxInundation_Houses', 0, net_p)