Esempio n. 1
0
    def test_copy_and_map(self):
        """
        Test the returned dictionary points toward equivaalent vertices and edges
        """

        vertices = [Vertex() for _ in range(6)]
        edges = [
            Edge(vertices[0], vertices[1]),
            Edge(vertices[1], vertices[2]),
            Edge(vertices[2], vertices[3]),
            Edge(vertices[3], vertices[4]),
            Edge(vertices[4], vertices[5]),
        ]

        graph = Graph()
        for vertex in vertices:
            graph.add_vertex(vertex)
        for edge in edges:
            graph.add_edge(edge)

        graph_dict = graph.copy_and_map()
        graph2 = Graph(vertices=list(graph_dict.values()))

        for vertex in graph.vertices:
            self.assertTrue(graph2.has_vertex(graph_dict[vertex]))
        for v1 in graph.vertices:
            for v2 in v1.edges:
                self.assertTrue(graph2.has_edge(graph_dict[v1],
                                                graph_dict[v2]))
                self.assertTrue(graph2.has_edge(graph_dict[v2],
                                                graph_dict[v1]))
        self.assertTrue(graph2.is_isomorphic(graph))
        self.assertTrue(graph.is_isomorphic(graph2))
Esempio n. 2
0
    def test_pickle(self):
        """
        Test that a Graph object can be successfully pickled and unpickled
        with no loss of information.
        """

        vertices = [Vertex() for i in range(6)]
        edges = [
            Edge(vertices[0], vertices[1]),
            Edge(vertices[1], vertices[2]),
            Edge(vertices[2], vertices[3]),
            Edge(vertices[3], vertices[4]),
            Edge(vertices[4], vertices[5]),
        ]

        graph0 = Graph()
        for vertex in vertices:
            graph0.addVertex(vertex)
        for edge in edges:
            graph0.addEdge(edge)
        graph0.updateConnectivityValues()

        import cPickle
        graph = cPickle.loads(cPickle.dumps(graph0))

        self.assertEqual(len(graph0.vertices), len(graph.vertices))
        for v1, v2 in zip(graph0.vertices, graph.vertices):
            self.assertEqual(v1.connectivity1, v2.connectivity1)
            self.assertEqual(v1.connectivity2, v2.connectivity2)
            self.assertEqual(v1.connectivity3, v2.connectivity3)
            self.assertEqual(v1.sortingLabel, v2.sortingLabel)
            self.assertEqual(len(v1.edges), len(v2.edges))
        self.assertTrue(graph0.isIsomorphic(graph))
        self.assertTrue(graph.isIsomorphic(graph0))
Esempio n. 3
0
    def test_split(self):
        """
        Test the graph split function to ensure a proper splitting of the graph
        is being done.
        """

        vertices = [Vertex() for i in range(6)]
        edges = [
            Edge(vertices[0], vertices[1]),
            Edge(vertices[1], vertices[2]),
            Edge(vertices[2], vertices[3]),
            Edge(vertices[4], vertices[5]),
        ]

        graph = Graph()
        for vertex in vertices:
            graph.addVertex(vertex)
        for edge in edges:
            graph.addEdge(edge)

        graphs = graph.split()

        self.assertTrue(len(graphs) == 2)
        self.assertTrue(
            len(graphs[0].vertices) == 4 or len(graphs[0].vertices) == 2)
        self.assertTrue(
            len(graphs[0].vertices) +
            len(graphs[1].vertices) == len(graph.vertices))
Esempio n. 4
0
    def test_copy(self):
        """
        Test the graph copy function to ensure a complete copy of the graph is
        made while preserving vertices and edges.
        """

        vertices = [Vertex() for i in range(6)]
        edges = [
            Edge(vertices[0], vertices[1]),
            Edge(vertices[1], vertices[2]),
            Edge(vertices[2], vertices[3]),
            Edge(vertices[3], vertices[4]),
            Edge(vertices[4], vertices[5]),
        ]

        graph = Graph()
        for vertex in vertices:
            graph.addVertex(vertex)
        for edge in edges:
            graph.addEdge(edge)

        graph2 = graph.copy()
        for vertex in graph.vertices:
            self.assertTrue(graph2.hasVertex(vertex))
        for v1 in graph.vertices:
            for v2 in v1.edges:
                self.assertTrue(graph2.hasEdge(v1, v2))
                self.assertTrue(graph2.hasEdge(v2, v1))
        self.assertTrue(graph2.isIsomorphic(graph))
        self.assertTrue(graph.isIsomorphic(graph2))
Esempio n. 5
0
    def test_get_largest_ring(self):
        """
        Test that the Graph.get_polycycles() method returns only polycyclic rings.
        """
        vertices = [Vertex() for _ in range(27)]
        bonds = [(0, 1), (1, 2),
                 (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (9, 10), (10, 11),
                 (11, 12), (12, 13), (13, 14), (14, 15), (12, 16), (10, 17),
                 (17, 18), (18, 19), (9, 20), (20, 21), (6, 22), (22, 23),
                 (22, 8), (8, 4), (23, 3), (23, 24), (24, 25), (25, 1)]
        edges = []
        for bond in bonds:
            edges.append(Edge(vertices[bond[0]], vertices[bond[1]]))

        graph = Graph()
        for vertex in vertices:
            graph.add_vertex(vertex)
        for edge in edges:
            graph.add_edge(edge)
        graph.update_connectivity_values()

        rings = graph.get_polycycles()
        self.assertEqual(len(rings), 1)

        # ensure the last ring doesn't include vertex 8, since it isn't in the
        # longest ring. Try two different items since one might contain the vertex 8
        long_ring = graph.get_largest_ring(rings[0][0])
        long_ring2 = graph.get_largest_ring(rings[0][1])

        if len(long_ring) > len(long_ring2):
            longest_ring = long_ring
        else:
            longest_ring = long_ring2

        self.assertEqual(len(longest_ring), len(rings[0]) - 1)
Esempio n. 6
0
 def make_graph(edge_inds):
     nvert = max(max(inds) for inds in edge_inds) + 1
     vertices = [Vertex() for _ in range(nvert)]
     graph = Graph(vertices)
     for idx1, idx2 in edge_inds:
         graph.add_edge(Edge(vertices[idx1], vertices[idx2]))
     return graph
Esempio n. 7
0
    def test_merge(self):
        """
        Test the graph merge function to ensure a proper merging of the graph
        is being done.
        """

        vertices1 = [Vertex() for i in range(4)]
        edges1 = [
            Edge(vertices1[0], vertices1[1]),
            Edge(vertices1[1], vertices1[2]),
            Edge(vertices1[2], vertices1[3]),
        ]

        vertices2 = [Vertex() for i in range(3)]
        edges2 = [
            Edge(vertices2[0], vertices2[1]),
            Edge(vertices2[1], vertices2[2]),
        ]

        graph1 = Graph()
        for vertex in vertices1:
            graph1.addVertex(vertex)
        for edge in edges1:
            graph1.addEdge(edge)

        graph2 = Graph()
        for vertex in vertices2:
            graph2.addVertex(vertex)
        for edge in edges2:
            graph2.addEdge(edge)

        graph = graph1.merge(graph2)

        self.assertTrue(
            len(graph1.vertices) + len(graph2.vertices) == len(graph.vertices))
        for vertex1 in vertices1:
            self.assertTrue(vertex1 in graph.vertices)
            for vertex2 in vertex1.edges:
                self.assertTrue(vertex2 in graph.vertices)
        for vertex2 in vertices2:
            self.assertTrue(vertex2 in graph.vertices)
            for vertex1 in vertex2.edges:
                self.assertTrue(vertex1 in vertex2.edges)
Esempio n. 8
0
    def test_isomorphism_disconnected(self):
        """
        Check the graph isomorphism for broken graphs.
        
        This tries to match graphs with a missing bond, 
        eg. [ 0-1-2-3-4  5 ] should match [ 0-1-2-3-4  5 ]
        """

        vertices1 = [Vertex() for _ in range(6)]
        edges1 = [
            Edge(vertices1[0], vertices1[1]),
            Edge(vertices1[1], vertices1[2]),
            Edge(vertices1[2], vertices1[3]),
            Edge(vertices1[3], vertices1[4]),
            # Edge(vertices1[4], vertices1[5]),
        ]

        vertices2 = [Vertex() for _ in range(6)]
        edges2 = [
            Edge(vertices2[0], vertices2[1]),
            Edge(vertices2[1], vertices2[2]),
            Edge(vertices2[2], vertices2[3]),
            Edge(vertices2[3], vertices2[4]),
            # Edge(vertices2[4], vertices2[5]),
        ]

        graph1 = Graph()
        for vertex in vertices1:
            graph1.add_vertex(vertex)
        for edge in edges1:
            graph1.add_edge(edge)

        graph2 = Graph()
        for vertex in vertices2:
            graph2.add_vertex(vertex)
        for edge in edges2:
            graph2.add_edge(edge)

        self.assertTrue(graph1.is_isomorphic(graph2))
        self.assertTrue(graph1.is_subgraph_isomorphic(graph2))
        self.assertTrue(graph2.is_isomorphic(graph1))
        self.assertTrue(graph2.is_subgraph_isomorphic(graph1))
        self.assertTrue(len(graph1.find_subgraph_isomorphisms(graph2)) > 0)
Esempio n. 9
0
    def test_subgraphIsomorphism(self):
        """
        Check the subgraph isomorphism functions.
        """

        vertices1 = [Vertex() for i in range(6)]
        edges1 = [
            Edge(vertices1[0], vertices1[1]),
            Edge(vertices1[1], vertices1[2]),
            Edge(vertices1[2], vertices1[3]),
            Edge(vertices1[3], vertices1[4]),
            Edge(vertices1[4], vertices1[5]),
        ]
        vertices2 = [Vertex() for i in range(2)]
        edges2 = [
            Edge(vertices2[0], vertices2[1]),
        ]

        graph1 = Graph()
        for vertex in vertices1:
            graph1.addVertex(vertex)
        for edge in edges1:
            graph1.addEdge(edge)

        graph2 = Graph()
        for vertex in vertices2:
            graph2.addVertex(vertex)
        for edge in edges2:
            graph2.addEdge(edge)

        self.assertFalse(graph1.isIsomorphic(graph2))
        self.assertFalse(graph2.isIsomorphic(graph1))
        self.assertTrue(graph1.isSubgraphIsomorphic(graph2))

        mapList = graph1.findSubgraphIsomorphisms(graph2)
        self.assertTrue(len(mapList) == 10)

        for mapping in mapList:
            self.assertTrue(graph1.isMappingValid(graph2, mapping))
            self.assertTrue(graph1.isMappingValid(graph2, mapping))
Esempio n. 10
0
    def test_isomorphism(self):
        """
        Check the graph isomorphism functions.
        """

        vertices1 = [Vertex() for i in range(6)]
        edges1 = [
            Edge(vertices1[0], vertices1[1]),
            Edge(vertices1[1], vertices1[2]),
            Edge(vertices1[2], vertices1[3]),
            Edge(vertices1[3], vertices1[4]),
            Edge(vertices1[4], vertices1[5]),
        ]

        vertices2 = [Vertex() for i in range(6)]
        edges2 = [
            Edge(vertices2[0], vertices2[1]),
            Edge(vertices2[1], vertices2[2]),
            Edge(vertices2[2], vertices2[3]),
            Edge(vertices2[3], vertices2[4]),
            Edge(vertices2[4], vertices2[5]),
        ]

        graph1 = Graph()
        for vertex in vertices1:
            graph1.addVertex(vertex)
        for edge in edges1:
            graph1.addEdge(edge)

        graph2 = Graph()
        for vertex in vertices2:
            graph2.addVertex(vertex)
        for edge in edges2:
            graph2.addEdge(edge)

        self.assertTrue(graph1.isIsomorphic(graph2))
        self.assertTrue(graph1.isSubgraphIsomorphic(graph2))
        self.assertTrue(graph2.isIsomorphic(graph1))
        self.assertTrue(graph2.isSubgraphIsomorphic(graph1))
Esempio n. 11
0
    def test_vertex_connectivity_values(self):
        """
        Tests the vertex connectivity values as introduced by Morgan (1965).
        
        First CV1 is the number of neighbours
        CV2 is the sum of neighbouring CV1 values
        CV3 is the sum of neighbouring CV2 values
        
        Graph:     Expected (and tested) values:
        
        0-1-2-3-4            1-3-2-2-1   3-4-5-3-2    4-11-7-7-3
        |                    |           |             |
        5                    1           3             4
        
        """
        vertices = [Vertex() for i in range(6)]
        edges = [
            Edge(vertices[0], vertices[1]),
            Edge(vertices[1], vertices[2]),
            Edge(vertices[2], vertices[3]),
            Edge(vertices[3], vertices[4]),
            Edge(vertices[1], vertices[5]),
        ]

        graph = Graph()
        for vertex in vertices:
            graph.addVertex(vertex)
        for edge in edges:
            graph.addEdge(edge)

        graph.updateConnectivityValues()

        for i, cv_ in enumerate([1, 3, 2, 2, 1, 1]):
            cv = vertices[i].connectivity1
            self.assertEqual(
                cv, cv_,
                "On vertex {0:d} got connectivity[0]={1:d} but expected {2:d}".
                format(i, cv, cv_))
        for i, cv_ in enumerate([3, 4, 5, 3, 2, 3]):
            cv = vertices[i].connectivity2
            self.assertEqual(
                cv, cv_,
                "On vertex {0:d} got connectivity[0]={1:d} but expected {2:d}".
                format(i, cv, cv_))
        for i, cv_ in enumerate([4, 11, 7, 7, 3, 4]):
            cv = vertices[i].connectivity3
            self.assertEqual(
                cv, cv_,
                "On vertex {0:d} got connectivity[0]={1:d} but expected {2:d}".
                format(i, cv, cv_))
Esempio n. 12
0
    def setUp(self):
        """
        A function run before each unit test in this class.
        """
        vertices = [Vertex() for _ in range(6)]
        edges = [
            Edge(vertices[0], vertices[1]),
            Edge(vertices[1], vertices[2]),
            Edge(vertices[2], vertices[3]),
            Edge(vertices[3], vertices[4]),
            Edge(vertices[4], vertices[5]),
        ]

        self.graph = Graph(vertices)
        for edge in edges:
            self.graph.add_edge(edge)
Esempio n. 13
0
    def test_get_polycyclic_rings(self):
        """
        Test that the Graph.get_polycycles() method returns only polycyclic rings.
        """
        vertices = [Vertex() for _ in range(27)]
        bonds = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6),
                 (6, 7), (7, 8), (8, 9), (9, 10), (10, 11), (11, 12), (12, 13),
                 (13, 14), (14, 15), (14, 12), (12, 16), (16, 10), (10, 17),
                 (17, 18), (18, 19), (9, 20), (20, 21), (21, 7), (6, 22),
                 (22, 23), (22, 4), (23, 3), (23, 24), (24, 25), (25, 1)]
        edges = []
        for bond in bonds:
            edges.append(Edge(vertices[bond[0]], vertices[bond[1]]))

        graph = Graph()
        for vertex in vertices:
            graph.add_vertex(vertex)
        for edge in edges:
            graph.add_edge(edge)
        graph.update_connectivity_values()

        sssr = graph.get_smallest_set_of_smallest_rings()
        self.assertEqual(len(sssr), 6)
        polycyclic_vertices = set(graph.get_all_polycyclic_vertices())
        expected_polycyclic_vertices = set(
            [vertices[index] for index in [3, 23, 4, 22, 12]])

        self.assertEqual(polycyclic_vertices, expected_polycyclic_vertices)

        continuous_rings = graph.get_polycycles()
        expected_continuous_rings = [
            [vertices[index] for index in [1, 2, 3, 4, 5, 6, 22, 23, 24, 25]],
            # [vertices[index] for index in [7,8,9,21,20]], # This is a nonpolycyclic ring
            [vertices[index] for index in [10, 11, 12, 13, 14, 16]],
        ]

        # Convert to sets for comparison purposes
        continuous_rings = [set(ring) for ring in continuous_rings]
        expected_continuous_rings = [
            set(ring) for ring in expected_continuous_rings
        ]
        for ring in expected_continuous_rings:
            self.assertTrue(ring in continuous_rings)