本文整理匯總了Python中rmgpy.molecule.graph.Graph類的典型用法代碼示例。如果您正苦於以下問題:Python Graph類的具體用法?Python Graph怎麽用?Python Graph使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Graph類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: make_graph
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.addEdge(Edge(vertices[idx1], vertices[idx2]))
return graph
示例2: test_getLargestRing
def test_getLargestRing(self):
"""
Test that the Graph.getPolycyclicRings() method returns only polycyclic rings.
"""
vertices = [Vertex() for i 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.addVertex(vertex)
for edge in edges: graph.addEdge(edge)
graph.updateConnectivityValues()
rings = graph.getPolycyclicRings()
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.getLargestRing(rings[0][0])
long_ring2 = graph.getLargestRing(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)
示例3: test_pickle
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))
示例4: test_vertex_connectivity_values
def test_vertex_connectivity_values(self):
"""
Tests the vertex connectivity values as introduced by Morgan (1965).
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
# = 3 # = 4 # = 4
*selected*
"""
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([11, 15, 18, 10, 7, 11]):
cv = vertices[i].connectivity
self.assertEqual(cv, cv_, "On vertex {0:d} got connectivity[0]={1:d} but expected {2:d}".format(i, cv, cv_))
示例5: test_copy
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))
示例6: test_split
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))
示例7: setUp
def setUp(self):
"""
A function run before each unit test in this class.
"""
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]),
]
self.graph = Graph(vertices)
for edge in edges: self.graph.addEdge(edge)
示例8: test_vertex_connectivity_values
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_))
示例9: test_subgraphIsomorphism
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) )
示例10: test_isomorphism
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))
示例11: test_merge
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)
示例12: test_copyAndMap
def test_copyAndMap(self):
"""
Test the returned dictionary points toward equivaalent 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)
graphDict = graph.copyAndMap()
graph2 = Graph(vertices = graphDict.values())
for vertex in graph.vertices:
self.assertTrue(graph2.hasVertex(graphDict[vertex]))
for v1 in graph.vertices:
for v2 in v1.edges:
self.assertTrue(graph2.hasEdge(graphDict[v1], graphDict[v2]))
self.assertTrue(graph2.hasEdge(graphDict[v2], graphDict[v1]))
self.assertTrue(graph2.isIsomorphic(graph))
self.assertTrue(graph.isIsomorphic(graph2))
示例13: test_isomorphism_disconnected
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 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))
self.assertTrue(len(graph1.findSubgraphIsomorphisms(graph2)) > 0)
示例14: TestGraph
class TestGraph(unittest.TestCase):
"""
Contains unit tests of the Vertex, Edge, and Graph classes. Most of the
functionality of Vertex and Edge is only meaningful when part of a graph,
so we test them all together instead of having separate unit test classes
for each.
"""
def setUp(self):
"""
A function run before each unit test in this class.
"""
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]),
]
self.graph = Graph()
for vertex in vertices:
self.graph.addVertex(vertex)
for edge in edges:
self.graph.addEdge(edge)
def test_addVertex(self):
"""
Test the Graph.addVertex() method.
"""
vertex = Vertex()
self.graph.addVertex(vertex)
self.assertTrue(vertex in self.graph.vertices)
self.assertTrue(vertex.edges == {})
def test_addEdge(self):
"""
Test the Graph.addEdge() method.
"""
vertex1 = Vertex()
vertex2 = Vertex()
edge = Edge(vertex1, vertex2)
try:
self.graph.addEdge(edge)
self.fail("Added edge between vertices not in graph to graph.")
except ValueError:
pass
self.graph.addVertex(vertex1)
self.graph.addVertex(vertex2)
self.graph.addEdge(edge)
self.assertTrue(vertex1 in self.graph.vertices)
self.assertTrue(vertex1 in vertex2.edges)
self.assertTrue(vertex2 in self.graph.vertices)
self.assertTrue(vertex2 in vertex1.edges)
self.assertTrue(vertex1.edges[vertex2] is edge)
self.assertTrue(vertex2.edges[vertex1] is edge)
def test_getEdge(self):
"""
Test the Graph.getEdge() method.
"""
vertex1 = self.graph.vertices[2]
vertex2 = self.graph.vertices[4]
try:
edge = self.graph.getEdge(vertex1, vertex2)
self.fail("Returned an edge between vertices that should not be connected in graph.")
except ValueError:
pass
vertex1 = self.graph.vertices[2]
vertex2 = self.graph.vertices[3]
edge = self.graph.getEdge(vertex1, vertex2)
self.assertNotEqual(edge, None)
self.assertTrue(isinstance(edge, Edge))
self.assertTrue(vertex1.edges[vertex2] is edge)
self.assertTrue(vertex2.edges[vertex1] is edge)
def test_getEdges(self):
"""
Test the Graph.getEdges() method.
"""
vertex1 = self.graph.vertices[2]
edges = self.graph.getEdges(vertex1)
self.assertTrue(isinstance(edges, dict))
self.assertEqual(len(edges), 2)
self.assertTrue(self.graph.vertices[1] in edges)
self.assertTrue(self.graph.vertices[3] in edges)
def test_hasVertex(self):
"""
Test the Graph.hasVertex() method.
"""
vertex = Vertex()
self.assertFalse(self.graph.hasVertex(vertex))
for v in self.graph.vertices:
self.assertTrue(self.graph.hasVertex(v))
def test_hasEdge(self):
"""
Test the Graph.hasEdge() method.
#.........這裏部分代碼省略.........
示例15: test_getPolycyclicRings
def test_getPolycyclicRings(self):
"""
Test that the Graph.getPolycyclicRings() method returns only polycyclic rings.
"""
vertices = [Vertex() for i 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.addVertex(vertex)
for edge in edges: graph.addEdge(edge)
graph.updateConnectivityValues()
SSSR = graph.getSmallestSetOfSmallestRings()
self.assertEqual(len(SSSR),6)
polycyclicVertices = set(graph.getAllPolycyclicVertices())
expectedPolycyclicVertices = set([vertices[index] for index in [3,23,4,22,12]])
self.assertEqual(polycyclicVertices, expectedPolycyclicVertices)
continuousRings = graph.getPolycyclicRings()
expectedContinuousRings = [[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
continuousRings = [set(ring) for ring in continuousRings]
expectedContinuousRings = [set(ring) for ring in expectedContinuousRings]
for ring in expectedContinuousRings:
self.assertTrue(ring in continuousRings)