def cond(n_1: Node, n_2: Node, e: Edge): "" if e.type() == EdgeType.DIRECTED: c1 = n_1 == e.start() and e.end() == n_2 return c1 else: c1 = n_1 == e.start() and e.end() == n_2 c2 = n_2 == e.start() and e.end() == n_1 return c1 or c2
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 moralize(self) -> MarkovNetwork: """! Moralize given chain graph: For any \f X,Y \in Pa_{K_i} \f add an edge between them if it does not exist. Then drop the direction of edges. """ edges = self.edges() enodes = set([frozenset([e.start(), e.end()]) for e in edges]) # add edges for cid in range(len(self.ccomponents)): pa_k_i: Set[NumCatRVariable] = self.parents_of_K(i=cid) pa_k_i_cp = set([p for p in pa_k_i]) while len(pa_k_i_cp) > 0: parent_node = pa_k_i_cp.pop() for pnode in pa_k_i: is_n_ind = self.is_node_independent_of(parent_node, pnode) if ( is_n_ind is True and frozenset([parent_node, pnode]).issubset(enodes) is False ): e = Edge( edge_id=str(uuid4()), start_node=parent_node, end_node=pnode, edge_type=EdgeType.UNDIRECTED, ) edges.add(e) # drop orientation nedges = set() for e in edges: if e.type() == EdgeType.DIRECTED: ne = Edge( edge_id=str(uuid4()), start_node=e.start(), end_node=e.end(), edge_type=EdgeType.UNDIRECTED, data=e.data(), ) nedges.add(ne) else: nedges.add(e) # return MarkovNetwork( gid=str(uuid4()), nodes=self.nodes(), edges=nedges, factors=self.factors() )