Repository navigation
Expand file tree
/
Copy pathplot_graph.py
More file actions
executable file
·256 lines (239 loc) · 11 KB
/
Copy pathplot_graph.py
File metadata and controls
executable file
·256 lines (239 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python3
import sys
import os
import argparse
import logging
from src.file_handler import file_handler
import src.graph_tool_functions as gt
from src.version import repo_version
__version__ = repo_version()
DESCRIPTION = '''
Load and plot input_graph - color using hex values in specified vertex property and names in specified vertex property.
NOTE: Assumes that you have computed a layout first.
See https://graph-tool.skewed.de/static/doc/autosummary/graph_tool.draw.graph_draw.html#graph_tool.draw.graph_draw
'''
## Pass arguments.
def main():
## Pass command line arguments.
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=DESCRIPTION)
parser.add_argument('-i', '--input_graph', metavar='graph.gt.xz',
required=False, default='/dev/stdin', type=lambda x: file_handler(x, 'rs'),
help='Input graph file (default: stdin)'
)
parser.add_argument('-o', '--output_plot', metavar='graph.pdf',
required=True, type=lambda x: file_handler(x, 'ws'),
help='Output plot file (pdf, png) - NOTE: use png for large netowrks'
)
parser.add_argument('--px',
required=False, default=None, type=int,
help='Width and heigth of plot in pixels (default: auto)'
)
# If True, the layout will be scaled to fit the entire clip region. If a float value is given, it will be interpreted as True, and in addition the viewport will be scaled out by that factor. If a tuple value is given, it should have four values (x, y, w, h) that specify the view in user coordinates.
fit_view=(500, 500, 200, 200)
##
## Vertex arguments
##
parser.add_argument('--vertex_shape',
required=False, default=None, type=str,
help='The vertex shape. Can be one of the following strings: “circle”, “triangle”, “square”, “pentagon”, “hexagon”, “heptagon”, “octagon” “double_circle”, “double_triangle”, “double_square”, “double_pentagon”, “double_hexagon”, “double_heptagon”, “double_octagon”, “pie”, “none”. Optionally, this might take a numeric value corresponding to position in the list above. (default: circle)'
)
parser.add_argument('--vertex_color',
required=False, default=None, type=str,
help='Vertex property to use for coloring the stroke of the vertex (default: Black)'
)
parser.add_argument('--vertex_fill_color',
required=False, default=None, type=str,
help='Vertex property to use for coloring the interior fill of the vertex (default: Black)'
)
parser.add_argument('--vertex_size',
required=False, default=5.0, type=float,
help='The size of the vertex, in the default units of the output format (normally either pixels or points) (default: %(default)s)'
)
parser.add_argument('--vertex_pen_width',
required=False, default=0.8, type=float,
help='Width of the lines used to draw the vertex, in the default units of the output format (normally either pixels or points) (default: %(default)s)'
)
parser.add_argument('--vertex_halo_color',
required=False, default=None, type=str,
help='Vertex property to use for coloring the halo around the vertex (default: no halo)'
)
parser.add_argument('--vertex_halo_size',
required=False, default=1.5, type=float,
help='Relative size of the halo. (default: %(default)s)'
)
parser.add_argument('--vertex_text',
required=False, default=None, type=str,
help='Vertex property to use for text to draw together with the vertex (default: no text)'
)
parser.add_argument('--vertex_text_color',
required=False, default="#000000", type=str,
help='Vertex property to use for coloring the text drawn with the vertex. If the value is "auto", it will be computed based on fill_color to maximize contrast (default: %(default)s)'
)
parser.add_argument('--vertex_text_position',
required=False, default=-1, type=float,
help='Position of the text relative to the vertex. If the passed value is positive, it will correspond to an angle in radians, which will determine where the text will be placed outside the vertex. If the value is negative, the text will be placed inside the vertex. If the value is -1, the vertex size will be automatically increased to accommodate the text. (default: %(default)s)'
)
parser.add_argument('--vertex_text_offset_x',
required=False, default=0.0, type=float,
help='Text position offset x-asis (default: %(default)s)'
)
parser.add_argument('--vertex_text_offset_y',
required=False, default=0.0, type=float,
help='Text position offset y-asis (default: %(default)s)'
)
parser.add_argument('--vertex_text_rotation',
required=False, default=0.0, type=float,
help='Angle of rotation (in radians) for the text. The center of rotation is the position of the vertex (default: %(default)s)'
)
parser.add_argument('--vertex_text_out_color',
required=False, default=None, type=str,
help='Vertex property to use for coloring the text outline (default: Black)'
)
parser.add_argument('--vertex_text_out_width',
required=False, default=1.0, type=float,
help='Width of the text outline (default: %(default)s)'
)
parser.add_argument('--vertex_font_family',
required=False, default="serif", type=str,
help='Font family used to draw the text (default: %(default)s)'
)
parser.add_argument('--vertex_font_size',
required=False, default=12, type=str,
help='Font size used to draw the text (default: %(default)s)'
)
parser.add_argument('--vertex_pie_fractions',
required=False, default=None, type=str,
help='Vertex property to use for fractions of the pie sections for the vertices if --vertex_shape=="pie". Vertex property needs to be a list of int or floats.'
)
parser.add_argument('--vertex_pie_colors',
required=False, default=None, type=str,
help='Vertex property to use for coloring the pie sections for the vertices if --vertex_shape=="pie"'
)
##
## Edge arguments
##
parser.add_argument('--edge_color',
required=False, default=None, type=str,
help='Edge property to use for coloring the stroke of the edge lines (default: Black)'
)
parser.add_argument('--edge_pen_width',
required=False, default=1.0, type=float,
help='Width of the line used to draw the edge, in the default units of the output format (normally either pixels or points) (default: %(default)s)'
)
parser.add_argument('--edge_start_marker',
required=False, default="none", type=str,
help='Edge markers. Can be one of “none”, “arrow”, “circle”, “square”, “diamond”, or “bar”. (default: %(default)s)'
)
parser.add_argument('--edge_mid_marker',
required=False, default="none", type=str,
help='Edge markers. Can be one of “none”, “arrow”, “circle”, “square”, “diamond”, or “bar”. (default: %(default)s)'
)
parser.add_argument('--edge_end_marker',
required=False, default="none", type=str,
help='Edge markers. Can be one of “none”, “arrow”, “circle”, “square”, “diamond”, or “bar”. (default: %(default)s)'
)
parser.add_argument('--edge_mid_marker_pos',
required=False, default=0.5, type=float,
help='Relative position of the middle marker (default: %(default)s)'
)
parser.add_argument('--edge_marker_size',
required=False, default=4.0, type=float,
help='Size of edge markers, in units appropriate to the output format (normally either pixels or points) (default: %(default)s)'
)
parser.add_argument('--edge_text',
required=False, default=None, type=str,
help='Edge property to use for text to draw together with the edge (default: no text)'
)
parser.add_argument('--edge_text_color',
required=False, default=None, type=str,
help='Edge property to use for coloring the text to draw together with the edge (default: Black)'
)
parser.add_argument('--edge_text_distance',
required=False, default=4.0, type=float,
help='Distance from the edge and its text (default: %(default)s)'
)
parser.add_argument('--edge_text_parallel',
required=False, action='store_true',
help='If True the text will be drawn parallel to the edges (default: %(default)s)'
)
parser.add_argument('--edge_text_out_color',
required=False, default=None, type=str,
help='Edge property to use for coloring the outline of the text darwn together with the edge (default: Black)'
)
parser.add_argument('--edge_text_out_width',
required=False, default=1.0, type=float,
help='Edge property to use for the width of the text to draw together with the edge (default: %(default)s)'
)
parser.add_argument('--edge_font_family',
required=False, default="serif", type=str,
help='Font family used to draw the text (default: %(default)s)'
)
parser.add_argument('--edge_font_size',
required=False, default=12, type=float,
help='Font size used to draw the text (default: %(default)s)'
)
##
## Misc parameters
##
parser.add_argument('-n', '--ncpus',
required=False, default=6, type=int,
help='Number of CPUs to use (default: %(default)s)'
)
parser.add_argument('--version', action='version',
version=f"{__version__}",
help='Print repo version'
)
parser.add_argument('--debug',
required=False, action='store_true',
help='Print DEBUG info (default: %(default)s)'
)
args = parser.parse_args()
## Set up basic debugger
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
logging.debug('%s', args) ## DEBUG
logging.info('Version: %s', __version__) ##INFO
## Workflow
with args.input_graph as in_graph_file, args.output_plot as out_plot_file:
gt.set_ncpus(args.ncpus)
G = gt.load_graph(in_graph_file)
gt.plot_graph_vertex_colored(G, out_plot_file, args.px,
vertex_shape=args.vertex_shape,
vertex_color=args.vertex_color,
vertex_fill_color=args.vertex_fill_color,
vertex_size=args.vertex_size,
vertex_pen_width=args.vertex_pen_width,
vertex_halo_color=args.vertex_halo_color,
vertex_halo_size=args.vertex_halo_size,
vertex_text=args.vertex_text,
vertex_text_color=args.vertex_text_color,
vertex_text_position=args.vertex_text_position,
vertex_text_offset=[args.vertex_text_offset_x, args.vertex_text_offset_y],
vertex_text_rotation=args.vertex_text_rotation,
vertex_text_out_color=args.vertex_text_out_color,
vertex_text_out_width=args.vertex_text_out_width,
vertex_font_family=args.vertex_font_family,
vertex_font_size=args.vertex_font_size,
vertex_pie_fractions=args.vertex_pie_fractions,
vertex_pie_colors=args.vertex_pie_colors,
edge_color=args.edge_color,
edge_pen_width=args.edge_pen_width,
edge_start_marker=args.edge_start_marker,
edge_mid_marker=args.edge_mid_marker,
edge_end_marker=args.edge_end_marker,
edge_mid_marker_pos=args.edge_mid_marker_pos,
edge_marker_size=args.edge_marker_size,
edge_text=args.edge_text,
edge_text_color=args.edge_text_color,
edge_text_distance=args.edge_text_distance,
edge_text_parallel=args.edge_text_parallel,
edge_text_out_color=args.edge_text_out_color,
edge_text_out_width=args.edge_text_out_width,
edge_font_family=args.edge_font_family,
edge_font_size=args.edge_font_size,
fit_view=fit_view,
)
if __name__ == '__main__':
log_format = "%(asctime)s [%(levelname)s] "+os.path.basename(__file__)+": %(message)s"
logging.basicConfig(format=log_format, stream=sys.stderr, level=logging.INFO)
main()