IMP logo
IMP Reference Guide  2.17.0
The Integrative Modeling Platform
kernel/graph.py

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

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