IMP logo
IMP Reference Guide  develop.27926d84dc,2024/04/18
The Integrative Modeling Platform
python_only.h
Go to the documentation of this file.
1 /**
2  * \file IMP/python_only.h
3  * \brief Helper functions for Python.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_PYTHON_ONLY_H
10 #define IMPKERNEL_PYTHON_ONLY_H
11 
12 #include <IMP/kernel_config.h>
13 #include "Model.h"
14 #include <boost/graph/adjacency_list.hpp>
15 
16 IMPKERNEL_BEGIN_NAMESPACE
17 
18 #ifdef IMP_DOXYGEN
19 /** \name Python Only
20  The following functions are only available in Python.
21  @{
22 */
23 /** The boost graphs used on the C++ side in \imp cannot easily be
24  exported to python. Instead we provide a simple graph wrapper and
25  a set of helper functions.
26 
27  Each node of the graph has an associated vertex name, the exact type
28  of which depends on the graph.
29 
30  \pythononlyclass
31 */
33  public:
34  class VertexDescriptor;
35  class VertexDescriptors;
36  class VertexName;
37  Vertices get_vertices() const;
38  VertexName get_vertex_name(VertexDescriptor v) const;
39  VertexDescriptors get_in_neighbors(VertexDescriptor) const;
40  VertexDescriptors get_out_neighbors(VertexDescriptor) const;
41  void add_edge(VertexDescriptor, VertexDescriptor);
42  VertexDescriptor add_vertex(VertexName);
43  void remove_vertex(VertexDescriptor);
44  //! Show the graph in the graphviz format.
45  /** \note The vertices are not output in order, but have
46  their indices shown.
47  */
48  void show(std::ostream &out = std::cout) const;
49 };
50 
51 /** Use the Python
52  \external{https://pypi.python.org/pypi/altgraph/,altgraph}
53  package to graphically display a graph.
54  Note that is requires X11 to be running (you can find it in
55  your Utilities folder in MacOS) and the window produced is shockingly
56  retro.
57 
58  An alternative is to write the graph to a \c dot file
59  \code
60  g.show_graphviz(open("graph.dot", "w"));
61  \endcode
62  then turn it to pdf with \c dot
63  \command{dot -Tpdf graph.dot > graph.pdf}
64  and finally view the pdf.
65 */
66 void show_altgraph(Graph g);
67 /** Export an IMP graph to a \external{https://networkx.github.io/,networkx}
68 graph.
69  The resulting graph can be analyzed and displayed, although the graph
70  layout algorithms in networkx seem to be quite poor compared to
71  graphviz.
72 
73  On a Mac, such graphs can be displayed by
74 \code
75 import matplotlib
76 # the engine to be used must be selected before pyplot is imported
77 matplotlib.use("macosx")
78 import matplotlib.pyplot as plt
79 
80 import networkx
81 networkx.draw_spring(xg)
82 plt.show()
83 \endcode
84  */
85 networkx::DiGraph get_networkx_graph(Graph g);
86 
87 /** Generate a nice pdf of the graph and attempt to open the pdf. The name
88  of the pdf will be returned in case the attempt to open it fails.
89 */
90 std::string show_graphviz(Graph g);
91 /** @} */
92 #endif
93 
94 IMPKERNEL_END_NAMESPACE
95 
96 #endif /* IMPKERNEL_PYTHON_ONLY_H */
Vector< VectorD< D > > get_vertices(const BoundingBoxD< D > &bb)
Return a list of the 2^D bounding points for the bounding box.
Definition: BoundingBoxD.h:281
Storage of a model, its restraints, constraints and particles.
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator to display each node.
std::string show_graphviz(Graph g)
networkx::DiGraph get_networkx_graph(Graph g)
void show_altgraph(Graph g)