IMP logo
IMP Reference Guide  develop.7400db2aee,2024/11/23
The Integrative Modeling Platform
kernel/graph.py

A simple example showing how to use the graph interface in Python.

1 ## \example kernel/graph.py
2 # A simple example showing how to use the graph interface in Python.
3 
4 import IMP
5 import sys
6 
7 IMP.setup_from_argv(sys.argv, "Graph interface")
8 
9 m = IMP.Model()
10 # An undirected graph with an IMP::Object for each node
12 vs = []
13 ps = []
14 for i in range(0, 10):
15  ps.append(IMP.Particle(m))
16  vs.append(g.add_vertex(ps[-1]))
17 g.add_edge(vs[0], vs[1])
18 g.add_edge(vs[1], vs[2])
19 
20 # try to use the altgraph package to visualize
22 try:
24 except:
25  print("Oh well, no altgraph")
26 
27 try:
28  import matplotlib
29  # the engine to be used must be selected before pyplot is imported
30  matplotlib.use("macosx")
31  import matplotlib.pyplot
32 
33  # the method below requires the altgraph Python package
35 
36  # the networkx visualization tools suck, so skip them
37  # import networkx
38  # networkx.draw(xg)
39  # networkx.draw_shell(xg)
40  # matplotlib.pyplot.show()
41 except:
42  print("networkx not fully installed")
43 
44 g.remove_vertex(0)
45 
46 # we can also try another show method
47 try:
49 except:
50  print("oh well, something not working with graphviz")
51 
52 # finally, we can
53 # in and out neighbors are the same
54 for n in g.get_in_neighbors(8):
55  print(g.get_vertex_name(n).get_name())