Пример #1
0
    def testCycleAvoidance(self):
        # should orient X->Y
        partiallyDirectedGraph = nx.DiGraph()
        partiallyDirectedGraph.add_nodes_from(['X', 'Y', 'Z'])
        edgePairs = [('X', 'Y'), ('Y', 'X'), ('X', 'Z'), ('Z', 'Y')]
        partiallyDirectedGraph.add_edges_from(edgePairs)
        didOrient = EdgeOrientation.applyCycleAvoidance(partiallyDirectedGraph)
        self.assertTrue(didOrient)
        self.assertEqual(3, len(partiallyDirectedGraph.edges()))

        # should orient X<-Y
        partiallyDirectedGraph = nx.DiGraph()
        partiallyDirectedGraph.add_nodes_from(['X', 'Y', 'Z'])
        edgePairs = [('X', 'Y'), ('Y', 'X'), ('Z', 'X'), ('Y', 'Z')]
        partiallyDirectedGraph.add_edges_from(edgePairs)
        didOrient = EdgeOrientation.applyCycleAvoidance(partiallyDirectedGraph)
        self.assertTrue(didOrient)
        self.assertEqual(3, len(partiallyDirectedGraph.edges()))

        # should orient no edges if two are unoriented in triple
        partiallyDirectedGraph = nx.DiGraph()
        partiallyDirectedGraph.add_nodes_from(['X', 'Y', 'Z'])
        edgePairs = [('X', 'Y'), ('Y', 'X'), ('X', 'Z'), ('Z', 'X'), ('Y', 'Z')]
        partiallyDirectedGraph.add_edges_from(edgePairs)
        didOrient = EdgeOrientation.applyCycleAvoidance(partiallyDirectedGraph)
        self.assertFalse(didOrient)
        self.assertEqual(5, len(partiallyDirectedGraph.edges()))

        # should orient no edges if X and Y are not adjacent (unshieled triple)
        partiallyDirectedGraph = nx.DiGraph()
        partiallyDirectedGraph.add_nodes_from(['X', 'Y', 'Z'])
        edgePairs = [('X', 'Z'), ('Z', 'X'), ('Y', 'Z')]
        partiallyDirectedGraph.add_edges_from(edgePairs)
        didOrient = EdgeOrientation.applyCycleAvoidance(partiallyDirectedGraph)
        self.assertFalse(didOrient)
        self.assertEqual(3, len(partiallyDirectedGraph.edges()))

        # should orient no edges because X already oriented to Y
        partiallyDirectedGraph = nx.DiGraph()
        partiallyDirectedGraph.add_nodes_from(['X', 'Y', 'Z'])
        edgePairs = [('X', 'Y'), ('X', 'Z'), ('Z', 'Y')]
        partiallyDirectedGraph.add_edges_from(edgePairs)
        didOrient = EdgeOrientation.applyCycleAvoidance(partiallyDirectedGraph)
        self.assertFalse(didOrient)
        self.assertEqual(3, len(partiallyDirectedGraph.edges()))

        # should orient no edges because common cause (no cycle to avoid for a shielded triple)
        partiallyDirectedGraph = nx.DiGraph()
        partiallyDirectedGraph.add_nodes_from(['X', 'Y', 'Z'])
        edgePairs = [('X', 'Y'), ('Y', 'X'), ('Z', 'X'), ('Z', 'Y')]
        partiallyDirectedGraph.add_edges_from(edgePairs)
        didOrient = EdgeOrientation.applyCycleAvoidance(partiallyDirectedGraph)
        self.assertFalse(didOrient)
        self.assertEqual(4, len(partiallyDirectedGraph.edges()))

        # should orient no edges because common effect (no cycle to avoid for a shielded triple)
        partiallyDirectedGraph = nx.DiGraph()
        partiallyDirectedGraph.add_nodes_from(['X', 'Y', 'Z'])
        edgePairs = [('X', 'Y'), ('Y', 'X'), ('X', 'Z'), ('Y', 'Z')]
        partiallyDirectedGraph.add_edges_from(edgePairs)
        didOrient = EdgeOrientation.applyCycleAvoidance(partiallyDirectedGraph)
        self.assertFalse(didOrient)
        self.assertEqual(4, len(partiallyDirectedGraph.edges()))