IMP logo
IMP Reference Guide  develop.7482b7c7af, 2026/12/05
The Integrative Modeling Platform
graph.py
1## \example kernel/graph.py
2# A simple example showing how to use the graph interface in Python.
3
4import IMP
5import sys
6
7IMP.setup_from_argv(sys.argv, "Graph interface")
8
9m = IMP.Model()
10# An undirected graph with an IMP::Object for each node
12vs = []
13ps = []
14for i in range(0, 10):
15 ps.append(IMP.Particle(m))
16 vs.append(g.add_vertex(ps[-1]))
17g.add_edge(vs[0], vs[1])
18g.add_edge(vs[1], vs[2])
19
20# try to use the altgraph package to visualize
22try:
24except:
25 print("Oh well, no altgraph")
26
27try:
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()
41except:
42 print("networkx not fully installed")
43
44g.remove_vertex(0)
45
46# we can also try another show method
47try:
49except:
50 print("oh well, something not working with graphviz")
51
52# finally, we can
53# in and out neighbors are the same
54for n in g.get_in_neighbors(8):
55 print(g.get_vertex_name(n).get_name())
Class for storing model, its restraints, constraints, and particles.
Definition Model.h:111
Class to handle individual particles of a Model object.
Definition Particle.h:45
void setup_from_argv(int argc, char **argv, std::string description)
Parse the command line flags and return the positional arguments.
std::string show_graphviz(Graph g)
boost::graph DependencyGraph
Directed graph on the interactions between the various objects in the model.
networkx::DiGraph get_networkx_graph(Graph g)