def setUp(self): """ """ self.n1 = Node("n1", {}) self.n2 = Node("n2", {}) self.n3 = Node("n3", {}) self.e1 = Edge( "e1", start_node=self.n1, end_node=self.n2, edge_type=EdgeType.DIRECTED, ) self.e2 = Edge( "e2", start_node=self.n2, end_node=self.n3, edge_type=EdgeType.DIRECTED, ) self.q = PriorityQueue(is_min=True) self.q.insert(2, self.n1) self.q.insert(5, self.n2) self.q.insert(1, self.n3) self.qm = PriorityQueue(is_min=False) self.qm.insert(2, self.n1) self.qm.insert(5, self.n2) self.qm.insert(1, self.n3)
def setUp(self): self.n1 = Node("n1", {}) self.n2 = Node("n2", {}) self.n3 = Node("n3", {}) self.n4 = Node("n4", {}) self.n5 = Node("n5", {}) self.e1 = Edge.undirected("e1", start_node=self.n1, end_node=self.n2) self.e2 = Edge.undirected("e2", start_node=self.n2, end_node=self.n3) self.e3 = Edge.undirected("e3", start_node=self.n3, end_node=self.n4) self.e4 = Edge.undirected("e4", start_node=self.n1, end_node=self.n4) self.graph = BaseGraph( "g1", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([self.n1, self.n2, self.n3, self.n4]), edges=set([self.e1, self.e2]), ) # intialize profiler self.prof = cProfile.Profile() self.prof.enable() self.verbose = False
def setUp(self): """""" self.a = Node("a") self.b = Node("b") self.c = Node("c") self.d = Node("d") self.e = Node("e") self.f = Node("f") self.g = Node("g") self.h = Node("h") self.j = Node("j") self.k = Node("k") self.m = Node("m") # # +--a --+ # | | # b c # | \ # +--+--+ g # | | | | # d e f h -- j # | # +--+---+ # | | # k m # # self.ab = Edge(edge_id="ab", start_node=self.a, end_node=self.b) self.ac = Edge(edge_id="ac", start_node=self.a, end_node=self.c) self.bd = Edge(edge_id="bd", start_node=self.b, end_node=self.d) self.be = Edge(edge_id="be", start_node=self.b, end_node=self.e) self.bf = Edge(edge_id="bf", start_node=self.b, end_node=self.f) self.fk = Edge(edge_id="fk", start_node=self.f, end_node=self.k) self.fm = Edge(edge_id="fm", start_node=self.f, end_node=self.m) self.cg = Edge(edge_id="cg", start_node=self.c, end_node=self.g) self.gh = Edge(edge_id="gh", start_node=self.g, end_node=self.h) self.hj = Edge(edge_id="hj", start_node=self.h, end_node=self.j) self.gtree = Tree( gid="t", edges=set( [ self.ab, self.ac, self.bd, self.be, self.bf, self.fk, self.fm, self.cg, self.gh, self.hj, ] ), )
def test_edges_by_end(self): """""" n1 = Node("n1", {}) n2 = Node("n2", {}) e1 = Edge( "e1", start_node=n1, end_node=n2, edge_type=EdgeType.UNDIRECTED ) e2 = Edge( "e2", start_node=n1, end_node=n1, edge_type=EdgeType.UNDIRECTED ) g = BaseGraph("g", nodes=set([n1, n2]), edges=set([e1, e2])) self.assertEqual(BaseGraphOps.edges_by_end(g, n2), set([e1]))
def test_is_node_incident(self): """""" n1 = Node("n1", {}) n2 = Node("n2", {}) e1 = Edge( "e1", start_node=n1, end_node=n2, edge_type=EdgeType.UNDIRECTED ) e2 = Edge( "e2", start_node=n1, end_node=n1, edge_type=EdgeType.UNDIRECTED ) self.assertTrue(Graph.is_node_incident(n1, e1)) self.assertFalse(Graph.is_node_incident(n2, e2))
def test_equal(self): n1 = Node("n1", {}) n2 = Node("n2", {}) n3 = Node("n3", {}) n4 = Node("n4", {}) e1 = Edge( "e1", start_node=n1, end_node=n2, edge_type=EdgeType.UNDIRECTED ) e2 = Edge( "e2", start_node=n2, end_node=n3, edge_type=EdgeType.UNDIRECTED ) graph = Graph( "g1", data={}, nodes=set([n1, n2, n3, n4]), edges=set([e1, e2]) ) self.assertEqual(graph, self.graph)
def test_union_e(self): n = Node("n646", {}) e = Edge( "e8", start_node=self.n1, end_node=n, edge_type=EdgeType.UNDIRECTED ) eset = BaseGraphSetOps.union(self.graph, set([e])) self.assertEqual(eset, set([e, self.e1, self.e2]))
def test_has_self_loop(self): """""" n1 = Node("n1", {}) n2 = Node("n2", {}) e1 = Edge("e1", start_node=n1, end_node=n2, edge_type=EdgeType.UNDIRECTED) e2 = Edge("e2", start_node=n1, end_node=n1, edge_type=EdgeType.UNDIRECTED) g1 = BaseGraph("graph", nodes=set([n1, n2]), edges=set([e1, e2])) g2 = BaseGraph("graph", nodes=set([n1, n2]), edges=set([e1])) self.assertTrue(BaseGraphAnalyzer.has_self_loop(g1)) self.assertFalse(BaseGraphAnalyzer.has_self_loop(g2))
def test__sub__e(self): """""" n = Node("n646", {}) e = Edge( "e8", start_node=self.n1, end_node=n, edge_type=EdgeType.UNDIRECTED ) g = self.graph - e self.assertEqual(BaseGraphOps.edges(g), set([self.e1, self.e2]))
def added_edge_between_if_none( g: AbstractGraph, n1: AbstractNode, n2: AbstractNode, is_directed: bool = False, ) -> BaseGraph: """! Add edge between nodes. If there are no edges in between. The flag is_directed specifies if the edge is directed or not """ if not BaseGraphOps.is_in(g, n1) or not BaseGraphOps.is_in(g, n2): raise ValueError("one of the nodes is not present in graph") n1id = n1.id() n2id = n2.id() gdata = BaseGraphOps.to_edgelist(g) first_eset = set(gdata[n1id]) second_eset = set(gdata[n2id]) common_edge_ids = first_eset.intersection(second_eset) if len(common_edge_ids) == 0: # there are no edges between the nodes if isinstance(g, AbstractUndiGraph): edge = Edge.undirected(eid=str(uuid4()), start_node=n1, end_node=n2) return BaseGraphAlgOps.add(g, edge) elif isinstance(g, AbstractDiGraph): edge = Edge.directed(eid=str(uuid4()), start_node=n1, end_node=n2) return BaseGraphAlgOps.add(g, edge) elif is_directed is True: edge = Edge.directed(eid=str(uuid4()), start_node=n1, end_node=n2) return BaseGraphAlgOps.add(g, edge) elif is_directed is False: edge = Edge.undirected(eid=str(uuid4()), start_node=n1, end_node=n2) return BaseGraphAlgOps.add(g, edge) else: raise ValueError("Must specify an edge type to be added") else: # there is already an edge in between return g
def setUp(self): """""" n1 = Node("m1", {}) n2 = Node("m2", {}) self.dedge = Edge( edge_id="medge", start_node=n1, end_node=n2, edge_type=EdgeType.DIRECTED, data={"my": "data"}, ) self.uedge = Edge( edge_id="uedge", start_node=n1, end_node=n2, edge_type=EdgeType.UNDIRECTED, data={"my": "data"}, )
def test_is_trivial_2(self): """""" n = Node("n646", {}) e = Edge("e8", start_node=n, end_node=n, edge_type=EdgeType.UNDIRECTED) check = False try: BaseGraph(gid="temp", data={}, nodes=set([n]), edges=set([e])) except ValueError: check = True self.assertTrue(check)
def is_adjacent_of(cls, e1: Edge, e2: Edge) -> bool: """! \brief Check if two edges are adjacent \param e1 an edge \param e2 an edge \code{.py} >>> n1 = Node("n1", {}) >>> n2 = Node("n2", {}) >>> n3 = Node("n3", {}) >>> n4 = Node("n4", {}) >>> e1 = Edge( >>> "e1", start_node=n1, end_node=n2, edge_type=EdgeType.UNDIRECTED >>> ) >>> e2 = Edge( >>> "e2", start_node=n2, end_node=n3, edge_type=EdgeType.UNDIRECTED >>> ) >>> e3 = Edge( >>> "e3", start_node=n3, end_node=n4, edge_type=EdgeType.UNDIRECTED >>> ) >>> graph_2 = Graph( >>> "g2", >>> data={"my": "graph", "data": "is", "very": "awesome"}, >>> nodes=set([n1, n2, n3, n4]), >>> edges=set([e1, e2, e3]), >>> ) >>> graph_2.is_adjacent_of(e2, e3) >>> True \endcode """ n1_ids = e1.node_ids() n2_ids = e2.node_ids() return len(n1_ids.intersection(n2_ids)) > 0
def from_node_tuples(cls, ntpls: Set[Tuple[Node, Node, EdgeType]]): """""" edges: Set[Edge] = set() for e in ntpls: child = e[0] parent = e[1] edge = Edge( edge_id=str(uuid4()), start_node=parent, end_node=child, edge_type=e[2], ) edges.add(edge) return Tree(gid=str(uuid4()), edges=edges)
def test__add__g(self): """""" n = Node("n646", {}) n1 = Node("n647", {}) n2 = Node("n648", {}) e = Edge( "e8", start_node=self.n1, end_node=n, edge_type=EdgeType.UNDIRECTED ) gg = Graph(gid="temp", data={}, nodes=set([n, n1, n2]), edges=set([e])) g = self.graph + gg self.assertEqual( BaseGraphOps.nodes(g), set([self.n1, self.n2, self.n3, self.n4, n, n1, n2]), ) self.assertEqual(BaseGraphOps.edges(g), set([e, self.e1, self.e2]))
def test_subtract_g(self): n = Node("n646", {}) n1 = Node("n647", {}) n2 = Node("n648", {}) e = Edge( "e8", start_node=self.n1, end_node=n, edge_type=EdgeType.UNDIRECTED ) gg = BaseGraph( gid="temp", data={}, nodes=set([n, n1, n2]), edges=set([e, self.e1]), ) g = BaseGraphAlgOps.subtract(self.graph, gg) self.assertEqual(BaseGraphOps.edges(g), set([])) self.assertEqual(BaseGraphOps.nodes(g), set([self.n3, self.n4]))
class EdgeTest(unittest.TestCase): """""" def setUp(self): """""" n1 = Node("m1", {}) n2 = Node("m2", {}) self.dedge = Edge( edge_id="medge", start_node=n1, end_node=n2, edge_type=EdgeType.DIRECTED, data={"my": "data"}, ) self.uedge = Edge( edge_id="uedge", start_node=n1, end_node=n2, edge_type=EdgeType.UNDIRECTED, data={"my": "data"}, ) def test_id(self): """""" self.assertEqual(self.uedge.id(), "uedge") def test_type(self): """""" self.assertEqual(self.uedge.type(), EdgeType.UNDIRECTED) def test_start(self): self.assertEqual(self.uedge.start(), Node("m1", {})) def test_end(self): self.assertEqual(self.uedge.end(), Node("m2", {})) def test_node_ids(self): self.assertEqual(self.uedge.node_ids(), set(["m1", "m2"])) def test_is_endvertice_true(self): """""" positive = self.uedge.is_endvertice(Node("m1", {})) self.assertEqual(positive, True) def test_is_endvertice_false(self): """""" negative = self.uedge.is_endvertice(Node("m3", {})) self.assertEqual(negative, False)
def is_node_incident(cls, n: Node, e: Edge) -> bool: """! \brief Check if a node is incident of an edge \param n node We check if this node is an endvertex of the edge. \param e The queried edge. \code{.py} >>> n1 = Node("n1", {}) >>> n2 = Node("n2", {}) >>> e1 = Edge("e1", start_node=n1, end_node=n2, edge_type=EdgeType.UNDIRECTED) >>> e2 = Edge("e2", start_node=n1, end_node=n1, edge_type=EdgeType.UNDIRECTED) >>> Graph.is_node_incident(n1, e1) >>> # True >>> Graph.is_node_incident(n2, e2) >>> # False \endcode """ return e.is_endvertice(n)
def setUp(self): """""" idata = { "A": {"outcome-values": [True, False]}, "B": {"outcome-values": [True, False]}, "C": {"outcome-values": [True, False]}, "D": {"outcome-values": [True, False]}, } # misconception example: Koller, Friedman, 2009 p. 104 self.A = NumCatRVariable( node_id="A", input_data=idata["A"], marginal_distribution=lambda x: 0.5, ) self.B = NumCatRVariable( node_id="B", input_data=idata["B"], marginal_distribution=lambda x: 0.5, ) self.C = NumCatRVariable( node_id="C", input_data=idata["C"], marginal_distribution=lambda x: 0.5, ) self.D = NumCatRVariable( node_id="D", input_data=idata["D"], marginal_distribution=lambda x: 0.5, ) self.AB = Edge( edge_id="AB", edge_type=EdgeType.UNDIRECTED, start_node=self.A, end_node=self.B, ) self.AD = Edge( edge_id="AD", edge_type=EdgeType.UNDIRECTED, start_node=self.A, end_node=self.D, ) self.DC = Edge( edge_id="DC", edge_type=EdgeType.UNDIRECTED, start_node=self.D, end_node=self.C, ) self.BC = Edge( edge_id="BC", edge_type=EdgeType.UNDIRECTED, start_node=self.B, end_node=self.C, ) def phi_AB(scope_product): """""" ss = frozenset(scope_product) if ss == frozenset([("A", False), ("B", False)]): return 30.0 elif ss == frozenset([("A", False), ("B", True)]): return 5.0 elif ss == frozenset([("A", True), ("B", False)]): return 1.0 elif ss == frozenset([("A", True), ("B", True)]): return 10.0 else: raise ValueError("product error") def phi_BC(scope_product): """""" ss = frozenset(scope_product) if ss == frozenset([("B", False), ("C", False)]): return 100.0 elif ss == frozenset([("B", False), ("C", True)]): return 1.0 elif ss == frozenset([("B", True), ("C", False)]): return 1.0 elif ss == frozenset([("B", True), ("C", True)]): return 100.0 else: raise ValueError("product error") def phi_CD(scope_product): """""" ss = frozenset(scope_product) if ss == frozenset([("C", False), ("D", False)]): return 1.0 elif ss == frozenset([("C", False), ("D", True)]): return 100.0 elif ss == frozenset([("C", True), ("D", False)]): return 100.0 elif ss == frozenset([("C", True), ("D", True)]): return 1.0 else: raise ValueError("product error") def phi_DA(scope_product): """""" ss = frozenset(scope_product) if ss == frozenset([("D", False), ("A", False)]): return 100.0 elif ss == frozenset([("D", False), ("A", True)]): return 1.0 elif ss == frozenset([("D", True), ("A", False)]): return 1.0 elif ss == frozenset([("D", True), ("A", True)]): return 100.0 else: raise ValueError("product error") self.AB_f = Factor( gid="ab_f", scope_vars=set([self.A, self.B]), factor_fn=phi_AB ) self.BC_f = Factor( gid="bc_f", scope_vars=set([self.B, self.C]), factor_fn=phi_BC ) self.CD_f = Factor( gid="cd_f", scope_vars=set([self.C, self.D]), factor_fn=phi_CD ) self.DA_f = Factor( gid="da_f", scope_vars=set([self.D, self.A]), factor_fn=phi_DA ) self.mnetwork = MarkovNetwork( gid="mnet", nodes=set([self.A, self.B, self.C, self.D]), edges=set([self.AB, self.AD, self.BC, self.DC]), factors=set([self.DA_f, self.CD_f, self.BC_f, self.AB_f]), ) # CH-Asia values from Cowell 2005, p. 116 table 6.9 self.a = NumCatRVariable( node_id="a", input_data=idata["A"], marginal_distribution=lambda x: 0.01 if x else 0.99, ) self.b = NumCatRVariable( node_id="b", input_data=idata["B"], marginal_distribution=lambda x: 0.5, ) self.d = NumCatRVariable( node_id="d", input_data=idata["A"], marginal_distribution=lambda x: 0.7468 if x else 0.2532, ) self.c = NumCatRVariable( node_id="c", input_data=idata["A"], marginal_distribution=lambda x: 0.7312 if x else 0.2688, ) self.ab = Edge( "ab", start_node=self.a, end_node=self.b, edge_type=EdgeType.UNDIRECTED, ) self.ad = Edge( "ad", start_node=self.a, end_node=self.d, edge_type=EdgeType.UNDIRECTED, ) self.bc = Edge( "bc", start_node=self.b, end_node=self.c, edge_type=EdgeType.UNDIRECTED, ) self.dc = Edge( "dc", start_node=self.d, end_node=self.c, edge_type=EdgeType.UNDIRECTED, ) self.ugraph = UndiGraph( "ug1", data={"m": "f"}, nodes=set([self.a, self.b, self.c, self.d]), edges=set([self.ab, self.ad, self.bc, self.dc]), ) # # Conditional Random Field test # from Koller, Friedman 2009, p. 144-145, example 4.20 self.X_1 = NumCatRVariable( node_id="X_1", input_data=idata["A"], marginal_distribution=lambda x: 0.5, ) self.X_2 = NumCatRVariable( node_id="X_2", input_data=idata["A"], marginal_distribution=lambda x: 0.5, ) self.X_3 = NumCatRVariable( node_id="X_3", input_data=idata["A"], marginal_distribution=lambda x: 0.5, ) self.Y_1 = NumCatRVariable( node_id="Y_1", input_data=idata["A"], marginal_distribution=lambda x: 0.5, ) self.X1_Y1 = Edge( edge_id="X1_Y1", edge_type=EdgeType.UNDIRECTED, start_node=self.X_1, end_node=self.Y_1, ) self.X2_Y1 = Edge( edge_id="X2_Y1", edge_type=EdgeType.UNDIRECTED, start_node=self.X_2, end_node=self.Y_1, ) self.X3_Y1 = Edge( edge_id="X3_Y1", edge_type=EdgeType.UNDIRECTED, start_node=self.X_3, end_node=self.Y_1, ) def phi_X1_Y1(scope_product): """""" w = 0.5 ss = frozenset(scope_product) if ss == frozenset([("X_1", True), ("Y_1", True)]): return math.exp(1.0 * w) else: return math.exp(0.0) def phi_X2_Y1(scope_product): """""" w = 5.0 ss = frozenset(scope_product) if ss == frozenset([("X_2", True), ("Y_1", True)]): return math.exp(1.0 * w) else: return math.exp(0.0) def phi_X3_Y1(scope_product): """""" w = 9.4 ss = frozenset(scope_product) if ss == frozenset([("X_3", True), ("Y_1", True)]): return math.exp(1.0 * w) else: return math.exp(0.0) def phi_Y1(scope_product): """""" w = 0.6 ss = frozenset(scope_product) if ss == frozenset([("Y_1", True)]): return math.exp(1.0 * w) else: return math.exp(0.0) self.X1_Y1_f = Factor( gid="x1_y1_f", scope_vars=set([self.X_1, self.Y_1]), factor_fn=phi_X1_Y1, ) self.X2_Y1_f = Factor( gid="x2_y1_f", scope_vars=set([self.X_2, self.Y_1]), factor_fn=phi_X2_Y1, ) self.X3_Y1_f = Factor( gid="x3_y1_f", scope_vars=set([self.X_3, self.Y_1]), factor_fn=phi_X3_Y1, ) self.Y1_f = Factor( gid="y1_f", scope_vars=set([self.Y_1]), factor_fn=phi_Y1 ) self.crf_koller = ConditionalRandomField( "crf", observed_vars=set([self.X_1, self.X_2, self.X_3]), target_vars=set([self.Y_1]), edges=set([self.X1_Y1, self.X2_Y1, self.X3_Y1]), factors=set([self.X1_Y1_f, self.X2_Y1_f, self.X3_Y1_f, self.Y1_f]), )
def setUp(self): self.verbose = False # # Alan Gibbons, Algorithmic graph theory 1985, p. 22, fig. 1.16 # depth first undirected graph # nodes self.n1 = Node("n1", data={}) self.n2 = Node("n2", data={}) self.n3 = Node("n3", data={}) self.n4 = Node("n4", data={}) self.n5 = Node("n5", data={}) self.n6 = Node("n6", data={}) self.n7 = Node("n7", data={}) self.n8 = Node("n8", data={}) self.n9 = Node("n9", data={}) self.n10 = Node("n10", data={}) self.n11 = Node("n11", data={}) self.n12 = Node("n12", data={}) self.n13 = Node("n13", data={}) # edges self.e1u = Edge.undirected( "n1n4", start_node=self.n1, end_node=self.n4, data={} ) self.e2u = Edge.undirected( "n1n3", start_node=self.n1, end_node=self.n3, data={} ) self.e3u = Edge.undirected( "n1n2", start_node=self.n1, end_node=self.n2, data={} ) self.e4u = Edge.undirected( "n1n5", start_node=self.n1, end_node=self.n5, data={} ) self.e5u = Edge.undirected( "n1n6", start_node=self.n1, end_node=self.n6, data={} ) self.e6u = Edge.undirected( "n1n7", start_node=self.n1, end_node=self.n7, data={} ) self.e7u = Edge.undirected( "n1n8", start_node=self.n1, end_node=self.n8, data={} ) self.e8u = Edge.undirected( "n8n2", start_node=self.n8, end_node=self.n2, data={} ) self.e9u = Edge.undirected( "n9n10", start_node=self.n9, end_node=self.n10, data={} ) self.e10u = Edge.undirected( "n9n13", start_node=self.n9, end_node=self.n13, data={} ) self.e11u = Edge.undirected( "n10n11", start_node=self.n10, end_node=self.n11, data={} ) self.e12u = Edge.undirected( "n10n12", start_node=self.n10, end_node=self.n12, data={} ) self.ugraph = BaseGraph( "ugraph", nodes=set( [ self.n1, self.n2, self.n3, self.n4, self.n5, self.n6, self.n7, self.n8, self.n9, self.n10, self.n11, self.n12, self.n13, ] ), edges=set( [ self.e1u, self.e2u, self.e3u, self.e4u, self.e5u, self.e6u, self.e7u, self.e8u, self.e9u, self.e10u, self.e11u, self.e12u, ] ), data={}, ) # tree self.a = Node("a") self.b = Node("b") self.c = Node("c") self.d = Node("d") self.e = Node("e") self.f = Node("f") self.g = Node("g") self.h = Node("h") self.j = Node("j") self.k = Node("k") self.m = Node("m") # # +--a --+ # | | # b c # | \ # +--+--+ g # | | | | # d e f h -- j # | # +--+---+ # | | # k m # # self.ab = Edge(edge_id="ab", start_node=self.a, end_node=self.b) self.ac = Edge(edge_id="ac", start_node=self.a, end_node=self.c) self.bd = Edge(edge_id="bd", start_node=self.b, end_node=self.d) self.be = Edge(edge_id="be", start_node=self.b, end_node=self.e) self.bf = Edge(edge_id="bf", start_node=self.b, end_node=self.f) self.fk = Edge(edge_id="fk", start_node=self.f, end_node=self.k) self.fm = Edge(edge_id="fm", start_node=self.f, end_node=self.m) self.cg = Edge(edge_id="cg", start_node=self.c, end_node=self.g) self.gh = Edge(edge_id="gh", start_node=self.g, end_node=self.h) self.hj = Edge(edge_id="hj", start_node=self.h, end_node=self.j) self.gtree = BaseGraph.from_edgeset( edges=set( [ self.ab, self.ac, self.bd, self.be, self.bf, self.fk, self.fm, self.cg, self.gh, self.hj, ] ), ) # initialize profiler self.prof = cProfile.Profile() self.prof.enable()
def setUp(self): # self.n1 = Node("n1", {}) self.n2 = Node("n2", {}) self.n3 = Node("n3", {}) self.n4 = Node("n4", {}) self.n5 = Node("n5", {}) self.e1 = Edge( "e1", start_node=self.n1, end_node=self.n2, edge_type=EdgeType.UNDIRECTED, ) self.e2 = Edge( "e2", start_node=self.n2, end_node=self.n3, edge_type=EdgeType.UNDIRECTED, ) self.e3 = Edge( "e3", start_node=self.n3, end_node=self.n4, edge_type=EdgeType.UNDIRECTED, ) self.e4 = Edge( "e4", start_node=self.n1, end_node=self.n4, edge_type=EdgeType.UNDIRECTED, ) self.graph_2 = UndiGraph( "g2", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([self.n1, self.n2, self.n3, self.n4]), edges=set([self.e1, self.e2, self.e3]), ) self.a = Node("a", {}) # b self.b = Node("b", {}) # c self.c = Node("c", {}) self.d = Node("d", {}) self.f = Node("f", {}) # d self.e = Node("e", {}) # e self.g = Node("g", {}) self.h = Node("h", {}) self.ae = Edge( "ae", start_node=self.a, end_node=self.e, edge_type=EdgeType.UNDIRECTED, ) self.ab = Edge( "ab", start_node=self.a, end_node=self.b, data={"w": 1}, edge_type=EdgeType.UNDIRECTED, ) self.af = Edge( "af", start_node=self.a, end_node=self.f, edge_type=EdgeType.UNDIRECTED, ) self.ah = Edge( "ah", start_node=self.a, end_node=self.h, edge_type=EdgeType.UNDIRECTED, ) self.bh = Edge( "bh", start_node=self.b, end_node=self.h, edge_type=EdgeType.UNDIRECTED, ) self.be = Edge( "be", start_node=self.b, end_node=self.e, edge_type=EdgeType.UNDIRECTED, ) self.ef = Edge( "ef", data={"w": 5}, start_node=self.e, end_node=self.f, edge_type=EdgeType.UNDIRECTED, ) self.de = Edge( "de", data={"w": 4}, start_node=self.d, end_node=self.e, edge_type=EdgeType.UNDIRECTED, ) self.df = Edge( "df", data={"w": 8}, start_node=self.d, end_node=self.f, edge_type=EdgeType.UNDIRECTED, ) self.cd = Edge( "cd", data={"w": 3}, start_node=self.c, end_node=self.d, edge_type=EdgeType.UNDIRECTED, ) self.cg = Edge( "cg", start_node=self.c, end_node=self.g, edge_type=EdgeType.UNDIRECTED, ) self.gd = Edge( "gd", data={"w": 7}, start_node=self.g, end_node=self.d, edge_type=EdgeType.UNDIRECTED, ) self.bg = Edge( "bg", data={"w": 6}, start_node=self.b, end_node=self.g, edge_type=EdgeType.UNDIRECTED, ) self.fg = Edge( "fg", start_node=self.f, end_node=self.g, edge_type=EdgeType.UNDIRECTED, ) self.bc = Edge( "bc", start_node=self.b, end_node=self.c, data={"w": 2}, edge_type=EdgeType.UNDIRECTED, ) # undirected graph self.ugraph1 = UndiGraph( "ug1", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([self.a, self.b, self.e, self.f]), edges=set([ self.ae, # self.ab, self.af, # self.be, self.ef, ]), ) # ugraph1: # +-----+ # / \ # a b e # \ / # +-----f self.ugraph2 = UndiGraph( "ug2", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([self.a, self.b, self.e, self.f]), edges=set([ self.ae, self.ab, self.af, self.be, self.ef, ]), ) # ugraph2 : # +-----+ # / \ # a -- b -- e # \ / # +-----f self.ugraph3 = UndiGraph( "ug3", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([self.a, self.b, self.e, self.f]), edges=set([ self.ab, self.af, self.be, ]), ) # ugraph3 : # # # a -- b -- e # \ # +-----f self.ugraph4 = UndiGraph( "ug4", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=BaseGraphOps.nodes(self.ugraph2).union( BaseGraphOps.nodes(self.graph_2)), edges=BaseGraphOps.edges(self.ugraph2).union( BaseGraphOps.edges(self.graph_2)), ) # ugraph 4 # +-----+ n1 -- n2 -- n3 -- n4 # / \ \ / # a -- b -- e +--------------+ # \ / # +-----f self.ugraph5 = UndiGraph( "ug5", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([self.a, self.b, self.c, self.d, self.e, self.f, self.g]), edges=set([ self.ab, self.bc, self.bg, self.cd, self.gd, self.df, self.de, self.ef, ]), ) # ugraph 5 # +----c---+ +--e # / 2 3 \ / 4 | # a --- b d | 5 # 1 \ 6 7 / \ 8 | # +---g---+ +--f self.ugraph6 = UndiGraph( "ug6", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([ self.a, self.b, self.c, self.d, self.e, self.f, self.g, self.h, ]), edges=set([ self.ab, self.ah, self.bc, self.bh, self.cd, self.de, self.df, self.cg, self.fg, ]), ) # ugraph 6 # a--+ e----d # | \ / \ # | b----c f # | / \ / # h--+ g self.ad = Edge( "ad", start_node=self.a, end_node=self.d, edge_type=EdgeType.UNDIRECTED, ) # self.ugraph7 = UndiGraph( "ug7", nodes=set([self.a, self.b, self.c, self.d]), edges=set([self.ab, self.bc, self.cd, self.ad]), )
def cls_nodes_2(cls): """""" odata = {"outcome-values": [True, False]} cls.J = NumCatRVariable(node_id="J", input_data=odata, marginal_distribution=lambda x: 0.5) cls.Irvar = NumCatRVariable(node_id="I", input_data=odata, marginal_distribution=lambda x: 0.5) cls.X = NumCatRVariable(node_id="X", input_data=odata, marginal_distribution=lambda x: 0.5) cls.Y = NumCatRVariable(node_id="Y", input_data=odata, marginal_distribution=lambda x: 0.5) cls.Orvar = NumCatRVariable(node_id="O", input_data=odata, marginal_distribution=lambda x: 0.5) cls.JX = Edge( edge_id="JX", edge_type=EdgeType.DIRECTED, start_node=cls.J, end_node=cls.X, ) cls.JY = Edge( edge_id="JY", edge_type=EdgeType.DIRECTED, start_node=cls.J, end_node=cls.Y, ) cls.IX = Edge( edge_id="IX", edge_type=EdgeType.DIRECTED, start_node=cls.Irvar, end_node=cls.X, ) cls.XO = Edge( edge_id="XO", edge_type=EdgeType.DIRECTED, start_node=cls.X, end_node=cls.Orvar, ) cls.YO = Edge( edge_id="YO", edge_type=EdgeType.DIRECTED, start_node=cls.Y, end_node=cls.Orvar, ) def phi_ij(scope_product, i: str): """""" ss = set(scope_product) if ss == set([(i, True)]): return 0.5 elif ss == set([(i, False)]): return 0.5 else: raise ValueError("unknown scope product") def phi_i(scope_product): """""" return phi_ij(scope_product, i="I") cls.I_f = Factor(gid="I_f", scope_vars=set([cls.Irvar]), factor_fn=phi_i) def phi_j(scope_product): """""" return phi_ij(scope_product, i="J") cls.J_f = Factor(gid="J_f", scope_vars=set([cls.J]), factor_fn=phi_j) def phi_jy(scope_product): """""" ss = set(scope_product) if ss == set([("J", True), ("Y", True)]): return 0.01 elif ss == set([("J", True), ("Y", False)]): return 0.99 elif ss == set([("J", False), ("Y", True)]): return 0.99 elif ss == set([("J", False), ("Y", False)]): return 0.01 else: raise ValueError("scope product unknown") cls.JY_f = Factor(gid="JY_f", scope_vars=set([cls.J, cls.Y]), factor_fn=phi_jy)
def setUp(self): self.n1 = Node("n1", {}) self.n2 = Node("n2", {}) self.n3 = Node("n3", {}) self.n4 = Node("n4", {}) self.n5 = Node("n5", {}) self.e1 = Edge( "e1", start_node=self.n1, end_node=self.n2, edge_type=EdgeType.UNDIRECTED, ) self.e2 = Edge( "e2", start_node=self.n2, end_node=self.n3, edge_type=EdgeType.UNDIRECTED, ) self.e3 = Edge( "e3", start_node=self.n3, end_node=self.n4, edge_type=EdgeType.UNDIRECTED, ) self.e4 = Edge( "e4", start_node=self.n1, end_node=self.n4, edge_type=EdgeType.UNDIRECTED, ) self.graph = BaseGraph( "g1", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set([self.n1, self.n2, self.n3, self.n4]), edges=set([self.e1, self.e2]), ) self.graph_2 = BaseGraph( "g2", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set([self.n1, self.n2, self.n3, self.n4]), edges=set([self.e1, self.e2, self.e3]), ) # self.a = Node("a", {}) # b self.b = Node("b", {}) # c self.f = Node("f", {}) # d self.e = Node("e", {}) # e self.ae = Edge( "ae", start_node=self.a, end_node=self.e, edge_type=EdgeType.UNDIRECTED, ) self.ab = Edge( "ab", start_node=self.a, end_node=self.b, edge_type=EdgeType.UNDIRECTED, ) self.af = Edge( "af", start_node=self.a, end_node=self.f, edge_type=EdgeType.UNDIRECTED, ) self.be = Edge( "be", start_node=self.b, end_node=self.e, edge_type=EdgeType.UNDIRECTED, ) self.ef = Edge( "ef", start_node=self.e, end_node=self.f, edge_type=EdgeType.UNDIRECTED, ) # undirected graph self.ugraph2 = BaseGraph( "ug2", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set([self.a, self.b, self.e, self.f]), edges=set( [ self.ae, self.ab, self.af, self.be, self.ef, ] ), ) # ugraph2 : # +-----+ # / \ # a -- b -- e # \ / # +-----f self.ugraph3 = BaseGraph( "ug3", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set([self.a, self.b, self.e, self.f]), edges=set( [ self.ab, # self.af, self.be, ] ), ) # ugraph3 : # # # a -- b -- e # \ # +-----f self.ugraph4 = BaseGraph( "ug4", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set(BaseGraphOps.nodes(self.ugraph2)).union( BaseGraphOps.nodes(self.graph_2) ), edges=BaseGraphOps.edges(self.ugraph2).union( BaseGraphOps.edges(self.graph_2) ), ) # ugraph 4 # +-----+ n1 -- n2 -- n3 -- n4 # / \ \ / # a -- b -- e +--------------+ # \ / # +-----f # make some directed edges self.bb = Node("bb", {}) self.cc = Node("cc", {}) self.dd = Node("dd", {}) self.ee = Node("ee", {}) self.bb_cc = Edge( "bb_cc", start_node=self.bb, end_node=self.cc, edge_type=EdgeType.DIRECTED, ) self.cc_dd = Edge( "cc_dd", start_node=self.cc, end_node=self.dd, edge_type=EdgeType.DIRECTED, ) self.dd_ee = Edge( "dd_ee", start_node=self.dd, end_node=self.ee, edge_type=EdgeType.DIRECTED, ) self.ee_bb = Edge( "ee_bb", start_node=self.ee, end_node=self.bb, edge_type=EdgeType.DIRECTED, ) self.bb_dd = Edge( "bb_dd", start_node=self.bb, end_node=self.dd, edge_type=EdgeType.DIRECTED, )
def cls_nodes_1(cls): """""" idata = { "a": { "outcome-values": [True, False] }, "b": { "outcome-values": [True, False] }, "c": { "outcome-values": [True, False] }, } cls.a = NumCatRVariable( node_id="a", input_data=idata["a"], marginal_distribution=lambda x: 0.6 if x else 0.4, ) cls.b = NumCatRVariable( node_id="b", input_data=idata["b"], marginal_distribution=lambda x: 0.5 if x else 0.5, ) cls.c = NumCatRVariable( node_id="c", input_data=idata["c"], marginal_distribution=lambda x: 0.5 if x else 0.5, ) cls.ab = Edge( edge_id="ab", edge_type=EdgeType.UNDIRECTED, start_node=cls.a, end_node=cls.b, ) cls.bc = Edge( edge_id="bc", edge_type=EdgeType.UNDIRECTED, start_node=cls.b, end_node=cls.c, ) def phi_ba(scope_product): """""" ss = set(scope_product) if ss == set([("a", True), ("b", True)]): return 0.9 elif ss == set([("a", True), ("b", False)]): return 0.1 elif ss == set([("a", False), ("b", True)]): return 0.2 elif ss == set([("a", False), ("b", False)]): return 0.8 else: raise ValueError("product error") def phi_cb(scope_product): """""" ss = set(scope_product) if ss == set([("c", True), ("b", True)]): return 0.3 elif ss == set([("c", True), ("b", False)]): return 0.5 elif ss == set([("c", False), ("b", True)]): return 0.7 elif ss == set([("c", False), ("b", False)]): return 0.5 else: raise ValueError("product error") def phi_a(scope_product): s = set(scope_product) if s == set([("a", True)]): return 0.6 elif s == set([("a", False)]): return 0.4 else: raise ValueError("product error") cls.ba_f = Factor(gid="ba", scope_vars=set([cls.b, cls.a]), factor_fn=phi_ba) cls.cb_f = Factor(gid="cb", scope_vars=set([cls.c, cls.b]), factor_fn=phi_cb) cls.a_f = Factor(gid="a", scope_vars=set([cls.a]), factor_fn=phi_a)
def data_1(self): """""" idata = {"outcome-values": [True, False]} self.A = NumCatRVariable(node_id="A", input_data=idata, marginal_distribution=lambda x: 0.5) self.B = NumCatRVariable(node_id="B", input_data=idata, marginal_distribution=lambda x: 0.5) self.C = NumCatRVariable(node_id="C", input_data=idata, marginal_distribution=lambda x: 0.5) self.D = NumCatRVariable(node_id="D", input_data=idata, marginal_distribution=lambda x: 0.5) self.E = NumCatRVariable(node_id="E", input_data=idata, marginal_distribution=lambda x: 0.5) self.F = NumCatRVariable(node_id="F", input_data=idata, marginal_distribution=lambda x: 0.5) self.G = NumCatRVariable(node_id="G", input_data=idata, marginal_distribution=lambda x: 0.5) self.H = NumCatRVariable(node_id="H", input_data=idata, marginal_distribution=lambda x: 0.5) self.Irvar = NumCatRVariable(node_id="I", input_data=idata, marginal_distribution=lambda x: 0.5) self.K = NumCatRVariable(node_id="K", input_data=idata, marginal_distribution=lambda x: 0.5) self.L = NumCatRVariable(node_id="L", input_data=idata, marginal_distribution=lambda x: 0.5) # # Cowell 2005, p. 110 # # A E---+ # | | # +----+ F <-+ # | | # B <--+---> C --> D <---+ # | | # +---> H <--------+----> G # | | # +---> I # self.AB_c = Edge( edge_id="AB", start_node=self.A, end_node=self.B, edge_type=EdgeType.DIRECTED, ) self.AC_c = Edge( edge_id="AC", start_node=self.A, end_node=self.C, edge_type=EdgeType.DIRECTED, ) self.CD_c = Edge( edge_id="CD", start_node=self.C, end_node=self.D, edge_type=EdgeType.DIRECTED, ) self.EF_c = Edge( edge_id="EF", start_node=self.E, end_node=self.F, edge_type=EdgeType.DIRECTED, ) self.FD_c = Edge( edge_id="FD", start_node=self.F, end_node=self.D, edge_type=EdgeType.DIRECTED, ) self.DG_c = Edge( edge_id="DG", start_node=self.D, end_node=self.G, edge_type=EdgeType.DIRECTED, ) self.DH_c = Edge( edge_id="DH", start_node=self.D, end_node=self.H, edge_type=EdgeType.DIRECTED, ) self.BH_c = Edge( edge_id="BH", start_node=self.B, end_node=self.H, edge_type=EdgeType.DIRECTED, ) self.BI_c = Edge( edge_id="BI", start_node=self.B, end_node=self.Irvar, edge_type=EdgeType.DIRECTED, ) self.HI_c = Edge( edge_id="HI", start_node=self.H, end_node=self.Irvar, edge_type=EdgeType.UNDIRECTED, )
def setUp(self): self.verbose = False self.n1 = Node("n1", {}) self.n2 = Node("n2", {}) self.n3 = Node("n3", {}) self.n4 = Node("n4", {}) self.n5 = Node("n5", {}) self.e1 = Edge( "e1", start_node=self.n1, end_node=self.n2, edge_type=EdgeType.UNDIRECTED, ) self.e2 = Edge( "e2", start_node=self.n2, end_node=self.n3, edge_type=EdgeType.UNDIRECTED, ) self.e3 = Edge( "e3", start_node=self.n3, end_node=self.n4, edge_type=EdgeType.UNDIRECTED, ) self.e4 = Edge( "e4", start_node=self.n1, end_node=self.n4, edge_type=EdgeType.UNDIRECTED, ) self.graph = Graph( "g1", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set([self.n1, self.n2, self.n3, self.n4]), edges=set([self.e1, self.e2]), ) self.graph_2 = Graph( "g2", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set([self.n1, self.n2, self.n3, self.n4]), edges=set([self.e1, self.e2, self.e3]), ) # self.a = Node("a", {}) # b self.b = Node("b", {}) # c self.f = Node("f", {}) # d self.e = Node("e", {}) # e self.ae = Edge( "ae", start_node=self.a, end_node=self.e, edge_type=EdgeType.UNDIRECTED, ) self.ab = Edge( "ab", start_node=self.a, end_node=self.b, edge_type=EdgeType.UNDIRECTED, ) self.af = Edge( "af", start_node=self.a, end_node=self.f, edge_type=EdgeType.UNDIRECTED, ) self.be = Edge( "be", start_node=self.b, end_node=self.e, edge_type=EdgeType.UNDIRECTED, ) self.ef = Edge( "ef", start_node=self.e, end_node=self.f, edge_type=EdgeType.UNDIRECTED, ) # undirected graph self.ugraph1 = Graph( "ug1", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set([self.a, self.b, self.e, self.f]), edges=set( [ self.ae, # self.ab, self.af, # self.be, self.ef, ] ), ) # ugraph1: # +-----+ # / \ # a b e # \ / # +-----f self.ugraph2 = Graph( "ug2", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set([self.a, self.b, self.e, self.f]), edges=set( [ self.ae, self.ab, self.af, self.be, self.ef, ] ), ) # ugraph2 : # +-----+ # / \ # a -- b -- e # \ / # +-----f self.ugraph3 = Graph( "ug3", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set([self.a, self.b, self.e, self.f]), edges=set( [ self.ab, # self.af, self.be, ] ), ) # ugraph3 : # # # a -- b -- e # \ # +-----f self.ugraph4 = Graph( "ug4", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set( [ self.a, self.b, self.e, self.f, self.n1, self.n2, self.n3, self.n4, ] ), edges=set( [ self.ab, self.af, self.ae, self.be, self.ef, self.e1, self.e2, self.e3, self.e4, ] ), ) # ugraph 4 # +-----+ n1 -- n2 -- n3 -- n4 # / \ \ / # a -- b -- e +--------------+ # \ / # +-----f # make some directed edges self.bb = Node("bb", {}) self.cc = Node("cc", {}) self.dd = Node("dd", {}) self.ee = Node("ee", {}) self.bb_cc = Edge( "bb_cc", start_node=self.bb, end_node=self.cc, edge_type=EdgeType.DIRECTED, ) self.cc_dd = Edge( "cc_dd", start_node=self.cc, end_node=self.dd, edge_type=EdgeType.DIRECTED, ) self.dd_ee = Edge( "dd_ee", start_node=self.dd, end_node=self.ee, edge_type=EdgeType.DIRECTED, ) self.ee_bb = Edge( "ee_bb", start_node=self.ee, end_node=self.bb, edge_type=EdgeType.DIRECTED, ) self.bb_dd = Edge( "bb_dd", start_node=self.bb, end_node=self.dd, edge_type=EdgeType.DIRECTED, ) self.dgraph = Graph( "g1", data={"my": "graph", "data": "is", "very": "awesome"}, nodes=set([self.bb, self.cc, self.dd, self.ee]), edges=set( [self.bb_cc, self.cc_dd, self.dd_ee, self.ee_bb, self.bb_dd] ), ) # initialize profiler self.prof = cProfile.Profile() self.prof.enable()
def setUp(self): """""" # From Diestel 2017, p. 7, fig. 1.3.1 self.n1 = Node("a", {}) # b self.n2 = Node("b", {}) # c self.n3 = Node("f", {}) # d self.n4 = Node("e", {}) # e self.n5 = Node("g", {}) # e self.n6 = Node("h", {}) # e self.n7 = Node("j", {}) # e self.e1 = Edge( "e1", start_node=self.n1, end_node=self.n2, edge_type=EdgeType.UNDIRECTED, ) self.e2 = Edge( "e2", start_node=self.n2, end_node=self.n3, edge_type=EdgeType.UNDIRECTED, ) self.e3 = Edge( "e3", start_node=self.n3, end_node=self.n4, edge_type=EdgeType.UNDIRECTED, ) self.e4 = Edge( "e4", start_node=self.n4, end_node=self.n5, edge_type=EdgeType.UNDIRECTED, ) self.e5 = Edge( "e5", start_node=self.n5, end_node=self.n6, edge_type=EdgeType.UNDIRECTED, ) self.e6 = Edge( "e6", start_node=self.n6, end_node=self.n7, edge_type=EdgeType.UNDIRECTED, ) # n1 - n2 - n3 - n4 - n5 - n6 - n7 # # # self.path = Path( gid="mpath", data={}, edges=[self.e1, self.e2, self.e3, self.e4, self.e5, self.e6], ) # tree self.a = Node("a") self.b = Node("b") self.c = Node("c") self.d = Node("d") self.e = Node("e") self.f = Node("f") self.g = Node("g") self.h = Node("h") self.j = Node("j") self.k = Node("k") self.m = Node("m") # # +--a --+ # | | # b c # | \ # +--+--+ g # | | | | # d e f h -- j # | # +--+---+ # | | # k m # # self.ab = Edge(edge_id="ab", start_node=self.a, end_node=self.b) self.ac = Edge(edge_id="ac", start_node=self.a, end_node=self.c) self.bd = Edge(edge_id="bd", start_node=self.b, end_node=self.d) self.be = Edge(edge_id="be", start_node=self.b, end_node=self.e) self.bf = Edge(edge_id="bf", start_node=self.b, end_node=self.f) self.fk = Edge(edge_id="fk", start_node=self.f, end_node=self.k) self.fm = Edge(edge_id="fm", start_node=self.f, end_node=self.m) self.cg = Edge(edge_id="cg", start_node=self.c, end_node=self.g) self.gh = Edge(edge_id="gh", start_node=self.g, end_node=self.h) self.hj = Edge(edge_id="hj", start_node=self.h, end_node=self.j) self.gtree = Graph.from_edgeset(edges=set([ self.ab, self.ac, self.bd, self.be, self.bf, self.fk, self.fm, self.cg, self.gh, self.hj, ]), )
def setUp(self): # self.n1 = Node("n1", {}) self.n2 = Node("n2", {}) self.n3 = Node("n3", {}) self.n4 = Node("n4", {}) self.n5 = Node("n5", {}) self.e1 = Edge.directed("e1", start_node=self.n1, end_node=self.n2) self.e2 = Edge.directed("e2", start_node=self.n2, end_node=self.n3) self.e3 = Edge.directed("e3", start_node=self.n3, end_node=self.n4) self.e4 = Edge.directed("e4", start_node=self.n1, end_node=self.n4) self.graph_2 = DiGraph( "g2", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([self.n1, self.n2, self.n3, self.n4]), edges=set([self.e1, self.e2, self.e3, self.e4]), ) # # n1 → n2 → n3 → n4 # | ↑ # +--------------+ self.a = Node("a", {}) # b self.b = Node("b", {}) # c self.c = Node("c", {}) self.d = Node("d", {}) self.f = Node("f", {}) # d self.e = Node("e", {}) # e self.g = Node("g", {}) self.h = Node("h", {}) self.ae = Edge.directed("ae", start_node=self.a, end_node=self.e) self.ab = Edge.directed("ab", start_node=self.a, end_node=self.b) self.af = Edge.directed("af", start_node=self.a, end_node=self.f) self.ah = Edge.directed("ah", start_node=self.a, end_node=self.h) self.bh = Edge.directed("bh", start_node=self.b, end_node=self.h) self.be = Edge.directed("be", start_node=self.b, end_node=self.e) self.ef = Edge.directed("ef", start_node=self.e, end_node=self.f) self.de = Edge.directed("de", start_node=self.d, end_node=self.e) self.df = Edge.directed("df", start_node=self.d, end_node=self.f) self.cd = Edge.directed("cd", start_node=self.c, end_node=self.d) self.cg = Edge.directed("cg", start_node=self.c, end_node=self.g) self.gd = Edge.directed("gd", start_node=self.g, end_node=self.d) self.bg = Edge.directed("bg", start_node=self.b, end_node=self.g) self.fg = Edge.directed("fg", start_node=self.f, end_node=self.g) self.bc = Edge.directed("bc", start_node=self.b, end_node=self.c) # directed graph self.dgraph1 = DiGraph( "dg1", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([self.a, self.b, self.e, self.f]), edges=set([ self.ae, # self.ab, self.af, # self.be, self.ef, ]), ) # dgraph1: # # # a --------> e b # | | # +---> f <---+ # self.dgraph2 = DiGraph( "dg2", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([self.a, self.b, self.e, self.f]), edges=set([ self.ae, self.ab, self.af, self.be, self.ef, ]), ) # dgraph2 : # # a -> b -> e -> f # | ↑ ↑ # +---------+----+ self.dgraph3 = DiGraph( "dg3", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([self.a, self.b, self.e, self.f]), edges=set([ self.ab, self.af, self.be, ]), ) # dgraph3 : # # a -> b -> e # \ # +---> f self.dgraph4 = DiGraph( "dg4", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=BaseGraphOps.nodes(self.dgraph2).union( BaseGraphOps.nodes(self.graph_2)), edges=BaseGraphOps.edges(self.dgraph2).union( BaseGraphOps.edges(self.graph_2)), ) # dgraph 4 # # a -> b -> e -> f n1 -> n2 -> n3 -> n4 # | ↑ ↑ | ↑ # +---------+----+ +------------------+ self.e_n = Edge.directed("en", start_node=self.e, end_node=self.n1) self.dgraph5 = DiGraph( "dg5", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([self.a, self.b, self.c, self.d, self.e, self.f, self.g]), edges=set([ self.ab, self.bc, self.bg, self.cd, self.gd, self.df, self.de, self.ef, ]), ) # dgraph 5 # +--> c +---> e # / \ / | # a -> b +--> d | # \ / \ v # +--> g +---> f self.dgraph6 = DiGraph( "dg6", data={ "my": "graph", "data": "is", "very": "awesome" }, nodes=set([ self.a, self.b, self.c, self.d, self.e, self.f, self.g, self.h, ]), edges=set([ self.ab, self.ah, self.bc, self.bh, self.cd, self.de, self.df, self.cg, self.fg, ]), )
def setUp(self): """""" self.data_1() self.data_2() self.data_3() self.data_4() # self.cowell = LWFChainGraph( gid="cowell", nodes=set([ self.A, self.B, self.C, self.D, self.E, self.F, self.G, self.H, self.Irvar, ]), edges=set([ self.AB_c, self.AC_c, self.CD_c, self.EF_c, self.FD_c, self.DG_c, self.DH_c, self.BH_c, self.BI_c, self.HI_c, ]), factors=set([ self.E_cf, self.EF_cf, self.DG_cf, self.A_cf, self.AB_cf, self.AC_cf, self.CDF_cf, self.IHB_cf, self.HBD_cf, self.BD_cf, ]), ) # Koller, Friedman 2009, p. 149 # # +--------------+ # | | # | A + B # | | | | # | +--> C -- D -- E <--+ # | | | | # | +--+ | v # | | +-------> I <---- H # | F <-+ # | | # | +----- G # | ^ # | | # +----------+ self.AC_k = Edge( edge_id="AC", start_node=self.A, end_node=self.C, edge_type=EdgeType.DIRECTED, ) self.FG_k = Edge( edge_id="FG", start_node=self.F, end_node=self.G, edge_type=EdgeType.UNDIRECTED, ) self.DG_k = Edge( edge_id="DG", start_node=self.D, end_node=self.G, edge_type=EdgeType.DIRECTED, ) self.CF_k = Edge( edge_id="CF", start_node=self.C, end_node=self.F, edge_type=EdgeType.UNDIRECTED, ) self.CD_k = Edge( edge_id="CD", start_node=self.C, end_node=self.D, edge_type=EdgeType.UNDIRECTED, ) self.CI_k = Edge( edge_id="CI", start_node=self.C, end_node=self.Irvar, edge_type=EdgeType.DIRECTED, ) self.DE_k = Edge( edge_id="DE", start_node=self.D, end_node=self.E, edge_type=EdgeType.UNDIRECTED, ) self.EI_k = Edge( edge_id="EI", start_node=self.E, end_node=self.Irvar, edge_type=EdgeType.DIRECTED, ) self.BE_k = Edge( edge_id="BE", start_node=self.B, end_node=self.E, edge_type=EdgeType.DIRECTED, ) self.HI_k = Edge( edge_id="HI", start_node=self.H, end_node=self.Irvar, edge_type=EdgeType.DIRECTED, ) self.koller = LWFChainGraph( gid="koller", nodes=set([ self.A, self.C, self.D, self.E, self.B, self.Irvar, self.F, self.G, ]), edges=set([ self.AC_k, self.FG_k, self.CF_k, self.HI_k, self.CD_k, self.CI_k, self.DE_k, self.DG_k, self.EI_k, self.BE_k, ]), factors=None, ) # evidence values taken from # Cowell 2005, p. 119, table 6.12 e_comp_val = [("E", True, 0.0096), ("E", False, 0.9904)] h_comp_val = [("H", True, 0.7635), ("H", False, 0.2365)] c_comp_val = [("C", True, 0.0025), ("C", False, 0.9975)] i_comp_val = [("I", True, 0.7939), ("I", False, 0.2061)] g_comp_val = [("G", True, 0.1849), ("G", False, 0.8151)] a_comp_val = [("A", True, 0.4767), ("A", False, 0.5233)] f_comp_val = [("F", True, 0.0012), ("F", False, 0.9988)] d_comp_val = [("D", True, 0.0036), ("D", False, 0.9964)] b_comp_val = [("B", True, 0.60), ("B", False, 0.40)] self.evidences = set([("E", True), ("A", True), ("G", False)]) self.q_tsts = { (self.E): e_comp_val, # asia (self.Irvar): i_comp_val, # dyspnoea (self.H): h_comp_val, # cough (self.A): a_comp_val, # smoke (self.B): b_comp_val, # bronchitis (self.C): c_comp_val, # lung (self.D): d_comp_val, # either (self.F): f_comp_val, # tuberculosis (self.G): g_comp_val, # x ray }
def test_from_digraph_with_factors(self): """! Values from Darwiche 2009, p. 132, figure 6.4 """ A = NumCatRVariable( "A", input_data={"outcome-values": [True, False]}, marginal_distribution=lambda x: 0.6 if x else 0.4, ) B = NumCatRVariable( "B", input_data={"outcome-values": [True, False]}, marginal_distribution=lambda x: 0.62 if x else 0.38, ) C = NumCatRVariable( "C", input_data={"outcome-values": [True, False]}, marginal_distribution=lambda x: 0.624 if x else 0.376, ) AB_Edge = Edge( edge_id="ab_edge", start_node=A, end_node=B, edge_type=EdgeType.DIRECTED, ) BC_Edge = Edge( edge_id="bc_edge", start_node=B, end_node=C, edge_type=EdgeType.DIRECTED, ) def phi_a(scope_product): """""" ss = set(scope_product) if ss == set([("A", True)]): return 0.6 elif ss == set([("A", False)]): return 0.4 else: raise ValueError("unknown argument") def phi_ab(scope_product): ss = set(scope_product) if ss == set([("A", True), ("B", True)]): return 0.9 elif ss == set([("A", True), ("B", False)]): return 0.1 elif ss == set([("A", False), ("B", True)]): return 0.2 elif ss == set([("A", False), ("B", False)]): return 0.8 else: raise ValueError("unknown argument") def phi_bc(scope_product): ss = set(scope_product) if ss == set([("C", True), ("B", True)]): return 0.3 elif ss == set([("C", True), ("B", False)]): return 0.5 elif ss == set([("C", False), ("B", True)]): return 0.7 elif ss == set([("C", False), ("B", False)]): return 0.5 else: raise ValueError("unknown argument") A_f = Factor(gid="A_f", scope_vars=set([A]), factor_fn=phi_a) AB_f = Factor(gid="AB_f", scope_vars=set([A, B]), factor_fn=phi_ab) BC_f = Factor(gid="BC_f", scope_vars=set([C, B]), factor_fn=phi_bc) dig = DiGraph(gid="temp", nodes=set([A, B, C]), edges=set([AB_Edge, BC_Edge])) factors = set([A_f, AB_f, BC_f]) bn = BayesianNetwork( gid="temp", nodes=set([A, B, C]), edges=set([AB_Edge, BC_Edge]), factors=set([A_f, AB_f, BC_f]), ) q = set([B]) evidence = set([]) foo, a = bn.cond_prod_by_variable_elimination(queries=q, evidences=evidence) bayes = BayesianNetwork.from_digraph(dig, factors) foo2, a2 = bayes.cond_prod_by_variable_elimination(queries=q, evidences=evidence) f1 = set([("B", False)]) self.assertEqual(foo.phi(f1), foo2.phi(f1)) f1 = set([("B", True)]) self.assertEqual(foo.phi(f1), foo2.phi(f1))