def test23(self):
     self.pag.add_edge(1, 2)
     self.pag.add_edge(2, 3)
     self.pag.add_edge(1, 3)
     self.pag.fully_direct_edge(3, 2)
     self.pag.fully_direct_edge(1, 2)
     FCIAlg.rule2(self.pag, 1, 2, 3)
     assert (not self.pag.has_directed_edge(1, 3))
 def test93(self):
     self.pag.add_edge(1, 3)
     self.pag.add_edge(1, 2)
     self.pag.add_edge(2, 4)
     self.pag.add_edge(4, 3)
     self.pag.direct_edge(1, 3)
     self.pag.direct_edge(2, 1)
     FCIAlg.rule9(self.pag, 1, 2, 3, 4)
     assert (not self.pag.has_fully_directed_edge(1, 3))
 def test82(self):
     self.pag.add_edge(1, 2)
     self.pag.add_edge(2, 3)
     self.pag.add_edge(1, 3)
     self.pag.direct_edge(1, 3)
     self.pag.setTag([1, 2], 1, '-')
     self.pag.fully_direct_edge(2, 3)
     FCIAlg.rule8(self.pag, 1, 2, 3)
     assert (self.pag.has_fully_directed_edge(1, 3))
 def test81(self):
     self.pag.add_edge(1, 2)
     self.pag.add_edge(2, 3)
     self.pag.add_edge(1, 3)
     self.pag.direct_edge(1, 3)
     self.pag.fully_direct_edge(1, 2)
     self.pag.fully_direct_edge(2, 3)
     FCIAlg.rule8(self.pag, 1, 2, 3)
     assert (self.pag.has_fully_directed_edge(1, 3))
 def test53(self):
     self.pag.add_edge(1, 2)
     self.pag.add_edge(1, 3)
     self.pag.add_edge(3, 4)
     FCIAlg.rule5(self.pag, 1, 2, 3, 4)
     assert (not (self.pag.has_fully_undirected_edge(1, 2)
                  and self.pag.has_fully_undirected_edge(1, 3)
                  and self.pag.has_fully_undirected_edge(3, 4)
                  and self.pag.has_fully_undirected_edge(4, 2)))
 def test31(self):
     self.pag.add_edge(1, 2)
     self.pag.add_edge(2, 3)
     self.pag.add_edge(1, 4)
     self.pag.add_edge(4, 3)
     self.pag.add_edge(4, 2)
     self.pag.direct_edge(1, 2)
     self.pag.direct_edge(3, 2)
     FCIAlg.rule3(self.pag, 1, 2, 3, 4)
     assert (self.pag.has_directed_edge(4, 2))
 def test103(self):
     self.pag.add_edge(1, 3)
     self.pag.add_edge(1, 2)
     self.pag.add_edge(2, 3)
     self.pag.add_edge(4, 3)
     self.pag.add_edge(1, 5)
     self.pag.direct_edge(1, 3)
     self.pag.direct_edge(1, 5)
     self.pag.fully_direct_edge(4, 3)
     FCIAlg.rule10(self.pag, 1, 2, 3, 4)
     assert (not (self.pag.has_fully_directed_edge(1, 3)))
 def setUp(self):
     self.data = PCAlg.prepare_data('data/asia_1000.data', ' ', True)
     self.pcalg = PCAlg(self.data, chi)
     self.fcialg = FCIAlg(self.data, chi)
     self.directed = nx.DiGraph()
     self.undirected = nx.Graph()
     self.directed.add_nodes_from([1, 2, 3, 4])
     self.undirected.add_nodes_from(self.directed)
     self.pag = PAG()
     self.pag.add_nodes_from(self.undirected)
     self.sepset = {(x, y): []
                    for x in [1, 2, 3, 4, 5] for y in [1, 2, 3, 4, 5]
                    if x != y}
class VOrientTests(unittest.TestCase):
    # tests for v structure orientation
    def setUp(self):
        self.data = PCAlg.prepare_data('data/asia_1000.data', ' ', True)
        self.pcalg = PCAlg(self.data, chi)
        self.fcialg = FCIAlg(self.data, chi)
        self.directed = nx.DiGraph()
        self.undirected = nx.Graph()
        self.directed.add_nodes_from([1, 2, 3, 4])
        self.undirected.add_nodes_from(self.directed)
        self.pag = PAG()
        self.pag.add_nodes_from(self.undirected)
        self.sepset = {(x, y): []
                       for x in [1, 2, 3, 4, 5] for y in [1, 2, 3, 4, 5]
                       if x != y}

    def test1(self):
        self.undirected.add_edges_from([(1, 2), (2, 3)])
        self.pcalg.orient_V(self.undirected, self.directed, self.sepset)
        part1 = not self.undirected.has_edge(
            1, 2) and not self.undirected.has_edge(3, 2)
        part2 = self.directed.has_edge(1, 2) and self.directed.has_edge(3, 2)
        assert (part1 and part2)

    def test2(self):
        self.undirected.add_edges_from([(1, 2), (2, 3)])
        self.sepset[(1, 3)].append(2)
        self.sepset[(3, 1)].append(2)
        self.pcalg.orient_V(self.undirected, self.directed, self.sepset)
        part1 = not self.undirected.has_edge(
            1, 2) and not self.undirected.has_edge(3, 2)
        part2 = self.directed.has_edge(1, 2) and self.directed.has_edge(3, 2)
        assert (not (part1 and part2))

    def test3(self):
        self.pag.add_edges_from([(1, 2), (2, 3)])
        self.fcialg.orient_V(self.pag, self.sepset)
        assert (self.pag.has_directed_edge(1, 2)
                and self.pag.has_directed_edge(3, 2))

    def test4(self):
        self.pag.add_edges_from([(1, 2), (2, 3)])
        self.sepset[(1, 3)].append(2)
        self.sepset[(3, 1)].append(2)
        self.fcialg.orient_V(self.pag, self.sepset)
        assert (not self.pag.has_directed_edge(1, 2)
                and not self.pag.has_directed_edge(3, 2))
 def test41(self):
     self.pag.add_edge(1, 2)
     self.pag.add_edge(2, 3)
     self.pag.add_edge(3, 4)
     self.pag.add_edge(4, 5)
     self.pag.add_edge(2, 5)
     self.pag.add_edge(3, 5)
     self.pag.direct_edge(1, 2)
     self.pag.direct_edge(2, 3)
     self.pag.direct_edge(2, 5)
     self.pag.direct_edge(3, 2)
     self.pag.direct_edge(3, 4)
     self.pag.direct_edge(3, 5)
     self.pag.direct_edge(4, 3)
     self.pag.direct_edge(3, 2)
     sepset = {(1, 5): [4], (5, 1): [4]}
     FCIAlg.rule4(self.pag, 3, 4, 5, 1, sepset)
     assert (self.pag.has_directed_edge(4, 5))
 def test4(self):
     self.pag.add_edge(1, 2)
     self.pag.add_edge(3, 2)
     self.pag.add_edge(3, 1)
     dseps = FCIAlg.possible_d_seps(self.pag)
     assert (dseps == {
         1: [2, 3],
         2: [1, 3],
         3: [1, 2],
         4: [],
         5: [],
         6: []
     })
 def test2(self):
     self.pag.add_edge(1, 2)
     self.pag.add_edge(2, 3)
     self.pag.fully_direct_edge(1, 2)
     self.pag.fully_direct_edge(3, 2)
     dseps = FCIAlg.possible_d_seps(self.pag)
     assert (dseps == {
         1: [2, 3],
         2: [1, 3],
         3: [1, 2],
         4: [],
         5: [],
         6: []
     })
 def test72(self):
     self.pag.add_edge(1, 2)
     self.pag.add_edge(2, 3)
     FCIAlg.rule67(self.pag, 1, 2, 3)
     assert (not (self.pag.get_edge_data(2, 3)[2] == '-'))
 def test71(self):
     self.pag.add_edge(1, 2)
     self.pag.add_edge(2, 3)
     self.pag.setTag([1, 2], 1, '-')
     FCIAlg.rule67(self.pag, 1, 2, 3)
     assert (self.pag.get_edge_data(2, 3)[2] == '-')
 def test61(self):
     self.pag.add_edge(1, 2)
     self.pag.add_edge(2, 3)
     self.pag.undirect_edge(1, 2)
     FCIAlg.rule67(self.pag, 1, 2, 3)
     assert (self.pag.get_edge_data(2, 3)[2] == '-')
 def test12(self):
     self.pag.add_edge(1, 2)
     self.pag.add_edge(2, 3)
     FCIAlg.rule1(self.pag, 1, 2, 3)
     assert (not self.pag.has_fully_directed_edge(2, 3))