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 SkeletonTests(unittest.TestCase):
    # Tests for the skeleton estimation
    def setUp(self):
        self.data = PCAlg.prepare_data('data/asia_1000.data', ' ', True)
        self.pcalg = PCAlg(self.data, chi)
        self.skeleton = nx.Graph()

    def test1(self):
        self.skeleton.add_nodes_from([1, 2, 3, 4, 5])
        sepset = {
            (1, 2): (),
            (1, 3): (2, ),
            (1, 4): (),
            (1, 5): (2, ),
            (2, 1): (),
            (2, 3): (4, 5),
            (2, 4): (),
            (2, 5): (),
            (3, 1): (2, ),
            (3, 2): (4, 5),
            (3, 4): (),
            (3, 5): (),
            (4, 1): (),
            (4, 2): (),
            (4, 3): (),
            (4, 5): (3, ),
            (5, 1): (2, ),
            (5, 2): (),
            (5, 3): (),
            (5, 4): (3, ),
        }
        self.skeleton.add_edges_from([[1, 2], [3, 4], [3, 5], [4, 2], [5, 2]])
        directed = self.pcalg.orientEdges(self.skeleton, sepset)
        assert (list(directed.edges) == [(1, 2), (4, 2), (4, 3), (5, 2),
                                         (5, 3), (3, 4), (3, 5)])
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 setUp(self):
     self.data = PCAlg.prepare_data('data/asia_1000.data', ' ', True)
     self.pcalg = PCAlg(self.data, chi)
     self.skeleton = nx.Graph()
 def test13(self):
     self.data = PCAlg.prepare_data('data/insurance_1000.data', ' ', False)
     p, h = chi(self.data, '0', '1', ['2', '3'])
     assert (round(h, 3) == 35.531)
 def test12(self):
     self.data = PCAlg.prepare_data('data/alarm_1000.data', ' ', False)
     p, h = chi(self.data, '1', '2', ['3', '4'])
     assert (round(h, 3) == 3.115)
 def test11(self):
     self.data = PCAlg.prepare_data('data/asia_10000.data', ' ', True)
     p, h = chi(self.data, 'either', 'tub', ['bronc', 'xray'])
     assert (round(h, 3) == 9884.0)
 def test9(self):
     self.data = PCAlg.prepare_data('data/asia_1000.data', ' ', True)
     p, h = chi(self.data, 'asia', 'tub', ['smoke', 'lung'])
     assert (round(h, 3) == 36.997)
 def test7(self):
     self.data = PCAlg.prepare_data('data/asia_1000.data', ' ', True)
     p, h = chi(self.data, 'asia', 'tub', [])
     assert (round(h, 3) == 34.496)
 def test6(self):
     self.directed.add_edges_from([[1, 3]])
     self.directed.add_edges_from([[1, 2], [3, 4], [3, 5]])
     assert (not PCAlg.findPath(2, 5, self.directed, []))
 def test4(self):
     self.directed.add_edges_from([[1, 2], [2, 3]])
     self.directed.add_edges_from([[3, 4]])
     assert (PCAlg.findPath(1, 4, self.directed, []))
 def setUp(self):
     self.data = PCAlg.prepare_data('data/alarm_10000.dat', ' ', True)
 def test2(self):
     self.directed.add_edges_from([[1, 2], [2, 3]])
     assert (not PCAlg.findPath(1, 5, self.directed, []))