-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathsummary.py
More file actions
375 lines (315 loc) · 14 KB
/
summary.py
File metadata and controls
375 lines (315 loc) · 14 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# vim:ts=4:sw=4:sts=4:et
# -*- coding: utf-8 -*-
"""Summary representation of a graph."""
import sys
from igraph.statistics import median
from itertools import islice
from math import ceil
from texttable import Texttable
from textwrap import TextWrapper
__all__ = ("GraphSummary", "summary")
class FakeWrapper:
"""Object whose interface is compatible with C{textwrap.TextWrapper}
but does no wrapping."""
def __init__(self, *args, **kwds):
pass
def fill(self, text):
return [text]
def wrap(self, text):
return [text]
def _get_wrapper_for_width(width, *args, **kwds):
"""Returns a text wrapper that wraps text for the given width.
@param width: the maximal width of each line that the text wrapper
produces. C{None} means that no wrapping will be performed.
"""
if width is None:
return FakeWrapper(*args, **kwds)
return TextWrapper(width, *args, **kwds)
class GraphSummary:
"""Summary representation of a graph.
The summary representation includes a header line and the list of
edges. The header line consists of C{IGRAPH}, followed by a
four-character long code, the number of vertices, the number of
edges, two dashes (C{--}) and the name of the graph (i.e.
the contents of the C{name} attribute, if any). For instance,
a header line may look like this::
IGRAPH U--- 4 5 --
The four-character code describes some basic properties of the
graph. The first character is C{U} if the graph is undirected,
C{D} if it is directed. The second letter is C{N} if the graph
has a vertex attribute called C{name}, or a dash otherwise. The
third letter is C{W} if the graph is weighted (i.e. it has an
edge attribute called C{weight}), or a dash otherwise. The
fourth letter is C{B} if the graph has a vertex attribute called
C{type}; this is usually used for bipartite graphs.
Edges may be presented as an ordinary edge list or an adjacency
list. By default, this depends on the number of edges; however,
you can control it with the appropriate constructor arguments.
"""
def __init__(
self,
graph,
verbosity=0,
width=78,
edge_list_format="auto",
max_rows=99999,
print_graph_attributes=False,
print_vertex_attributes=False,
print_edge_attributes=False,
full=False,
):
"""Constructs a summary representation of a graph.
@param verbosity: the verbosity of the summary. If zero, only
the header line will be returned. If one, the header line
and the list of edges will both be returned.
@param width: the maximal width of each line in the summary.
C{None} means that no limit will be enforced.
@param max_rows: the maximal number of rows to print in a single
table (e.g., vertex attribute table or edge attribute table)
@param edge_list_format: format of the edge list in the summary.
Supported formats are: C{compressed}, C{adjlist}, C{edgelist},
C{auto}, which selects automatically from the other three based
on some simple criteria.
@param print_graph_attributes: whether to print graph attributes
if there are any.
@param print_vertex_attributes: whether to print vertex attributes
if there are any.
@param print_edge_attributes: whether to print edge attributes
if there are any.
@param full: False has no effect; True turns on the attribute
printing for graph, vertex and edge attributes with verbosity 1.
"""
if full:
print_graph_attributes = True
print_vertex_attributes = True
print_edge_attributes = True
verbosity = max(verbosity, 1)
self._graph = graph
self.edge_list_format = edge_list_format.lower()
self.max_rows = int(max_rows)
self.print_graph_attributes = print_graph_attributes
self.print_vertex_attributes = print_vertex_attributes
self.print_edge_attributes = print_edge_attributes
self.verbosity = verbosity
self.width = width
self.wrapper = _get_wrapper_for_width(self.width, break_on_hyphens=False)
if self._graph.is_named():
self._edges_header = "+ edges (vertex names):"
else:
self._edges_header = "+ edges:"
self._arrow = ["--", "->"][self._graph.is_directed()]
self._arrow_format = "%%s%s%%s" % self._arrow
def _construct_edgelist_adjlist(self):
"""Constructs the part in the summary that prints the edge list in an
adjacency list format."""
result = [self._edges_header]
if self._graph.vcount() == 0:
return
if self._graph.is_named():
names = self._graph.vs["name"]
maxlen = max(len(str(name)) for name in names)
format_str = "%%%ds %s %%s" % (maxlen, self._arrow)
for v1, name in enumerate(names):
neis = self._graph.successors(v1)
neis = ", ".join(str(names[v2]) for v2 in neis)
result.append(format_str % (name, neis))
else:
maxlen = len(str(self._graph.vcount()))
num_format = "%%%dd" % maxlen
format_str = "%s %s %%s" % (num_format, self._arrow)
for v1 in range(self._graph.vcount()):
neis = self._graph.successors(v1)
neis = " ".join(num_format % v2 for v2 in neis)
result.append(format_str % (v1, neis))
# Try to wrap into multiple columns if that works with the given width
if self.width is not None:
maxlen = max(len(line) for line in result[1:])
colcount = int(self.width + 3) / int(maxlen + 3)
if colcount > 1:
# Rewrap to multiple columns
nrows = len(result) - 1
colheight = int(ceil(nrows / float(colcount)))
newrows = [[] for _ in range(colheight)]
for i, row in enumerate(result[1:]):
newrows[i % colheight].append(row.ljust(maxlen))
result[1:] = [" ".join(row) for row in newrows]
return result
def _construct_edgelist_compressed(self):
"""Constructs the part in the summary that prints the edge list in a
compressed format suitable for graphs with mostly small degrees."""
result = [self._edges_header]
arrow = self._arrow_format
if self._graph.is_named():
names = self._graph.vs["name"]
edges = ", ".join(
arrow % (names[edge.source], names[edge.target])
for edge in self._graph.es
)
else:
edges = " ".join(arrow % edge.tuple for edge in self._graph.es)
result.append(edges)
return result
def _construct_edgelist_edgelist(self):
"""Constructs the part in the summary that prints the edge list in a
full edge list format."""
attrs = sorted(self._graph.edge_attributes())
table = self._new_table(headers=["", "edge"] + attrs)
table.add_rows(
islice(self._edge_attribute_iterator(attrs), 0, self.max_rows), header=False
)
table.set_cols_align(
["l", "l"] + self._infer_column_alignment(edge_attrs=attrs)
)
result = [self._edges_header]
result.extend(table.draw().split("\n"))
return result
def _construct_graph_attributes(self):
"""Constructs the part in the summary that lists the graph attributes."""
attrs = self._graph.attributes()
if not attrs:
return []
result = ["+ graph attributes:"]
attrs.sort()
for attr in attrs:
result.append("[[%s]]" % (attr,))
result.append(str(self._graph[attr]))
return result
def _construct_vertex_attributes(self):
"""Constructs the part in the summary that lists the vertex attributes."""
attrs = sorted(self._graph.vertex_attributes())
if not attrs or (len(attrs) == 1 and "name" in attrs):
return []
table = self._new_table(headers=[""] + attrs)
table.add_rows(
islice(self._vertex_attribute_iterator(attrs), 0, self.max_rows),
header=False,
)
table.set_cols_align(["l"] + self._infer_column_alignment(vertex_attrs=attrs))
result = ["+ vertex attributes:"]
result.extend(table.draw().split("\n"))
return result
def _construct_header(self):
"""Constructs the header part of the summary."""
graph = self._graph
params = {
"directed": "UD"[graph.is_directed()],
"named": "-N"[graph.is_named()],
"weighted": "-W"[graph.is_weighted()],
"typed": "-T"["type" in graph.vertex_attributes()],
"vcount": graph.vcount(),
"ecount": graph.ecount(),
}
if "name" in graph.attributes():
params["name"] = graph["name"]
else:
params["name"] = ""
result = [
"IGRAPH %(directed)s%(named)s%(weighted)s%(typed)s "
"%(vcount)d %(ecount)d -- %(name)s" % params
]
attrs = ["%s (g)" % (name,) for name in sorted(graph.attributes())]
attrs.extend("%s (v)" % (name,) for name in sorted(graph.vertex_attributes()))
attrs.extend("%s (e)" % (name,) for name in sorted(graph.edge_attributes()))
if attrs:
result.append("+ attr: %s" % ", ".join(attrs))
if self.wrapper is not None:
self.wrapper.subsequent_indent = " "
result[-1:] = self.wrapper.wrap(result[-1])
self.wrapper.subsequent_indent = ""
return result
def _edge_attribute_iterator(self, attribute_order):
"""Returns an iterator that yields the rows of the edge attribute table
in the summary. C{attribute_order} must be a list containing the names of
the attributes to be presented in this table."""
arrow = self._arrow_format
if self._graph.is_named():
names = self._graph.vs["name"]
for edge in self._graph.es:
formatted_edge = arrow % (names[edge.source], names[edge.target])
yield ["[%d]" % edge.index, formatted_edge] + [
edge[attr] for attr in attribute_order
]
else:
for edge in self._graph.es:
formatted_edge = arrow % edge.tuple
yield ["[%d]" % edge.index, formatted_edge] + [
edge[attr] for attr in attribute_order
]
def _infer_column_alignment(self, vertex_attrs=None, edge_attrs=None):
"""Infers the preferred alignment for the given vertex and edge attributes
in the tables by peeking into the attribute values of the first 100 vertices
or edges. Numeric attributes will be aligned right, everything else will be
aligned left."""
values = []
if vertex_attrs is not None:
vs = self._graph.vs[:100]
values.extend(vs[attr] for attr in vertex_attrs)
if edge_attrs is not None:
es = self._graph.es[:100]
values.extend(es[attr] for attr in edge_attrs)
result = []
for vs in values:
is_numeric = True
try:
[float(x) for x in vs]
except ValueError:
is_numeric = False
if is_numeric:
result.append("r")
else:
result.append("l")
return result
def _new_table(self, headers=None):
"""Constructs a new table to pretty-print vertex and edge attributes"""
table = Texttable(max_width=0)
table.set_deco(0)
if headers is not None:
table.header(headers)
return table
def _vertex_attribute_iterator(self, attribute_order):
"""Returns an iterator that yields the rows of the vertex attribute table
in the summary. C{attribute_order} must be a list containing the names of
the attributes to be presented in this table."""
for vertex in self._graph.vs:
yield ["[%d]" % vertex.index] + [vertex[attr] for attr in attribute_order]
def __str__(self):
"""Returns the summary representation as a string."""
output = self._construct_header()
if self.print_graph_attributes:
output.extend(self._construct_graph_attributes())
if self.print_vertex_attributes:
output.extend(self._construct_vertex_attributes())
if self.verbosity <= 0:
return "\n".join(output)
if self._graph.ecount() > 0:
# Add the edge list
if self.edge_list_format == "auto":
if self.print_edge_attributes and self._graph.edge_attributes():
format = "edgelist"
elif median(self._graph.degree(mode="out")) < 3:
format = "compressed"
else:
format = "adjlist"
else:
format = self.edge_list_format
method_name = "_construct_edgelist_%s" % format
if hasattr(self, method_name):
output.extend(getattr(self, method_name)())
if self.wrapper is not None:
return "\n".join("\n".join(self.wrapper.wrap(line)) for line in output)
return "\n".join(output)
def summary(obj, stream=None, *args, **kwds):
"""Prints a summary of object o to a given stream
Positional and keyword arguments not explicitly mentioned here are passed
on to the underlying C{summary()} method of the object if it has any.
@param obj: the object about which a human-readable summary is requested.
@param stream: the stream to be used. If C{None}, the standard output
will be used.
"""
if stream is None:
stream = sys.stdout
if hasattr(obj, "summary"):
stream.write(obj.summary(*args, **kwds))
else:
stream.write(str(obj))
stream.write("\n")