/
visualize.py
145 lines (116 loc) · 4.92 KB
/
visualize.py
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
from datetime import date
from multiprocessing import Queue
from multiprocessing.pool import Pool
import networkx as nx
import yaml
from networkx import MultiDiGraph
from pygraphviz import AGraph
from tqdm import tqdm
from datings import BiblSource, add_timeline_edges
from uris import Reference
from faust_logging import logging
logger = logging.getLogger()
_render_queue = []
def simplify_graph(original_graph: nx.MultiDiGraph) -> nx.MultiDiGraph:
"""
Creates a copy of the graph that contains only simple types, so it can be serialized to, e.g., GEXF
"""
graph = original_graph.copy()
translation = {}
for node, attrs in graph.nodes.data():
if isinstance(node, date):
attrs['kind'] = 'date'
translation[node] = node.isoformat()
elif isinstance(node, Reference):
attrs['kind'] = node.__class__.__name__
attrs['label'] = node.label
translation[node] = node.uri
_simplify_attrs(attrs)
nx.relabel_nodes(graph, translation, copy=False)
for u, v, attrs in graph.edges(data=True):
if 'source' in attrs and not 'label' in attrs:
attrs['label'] = str(attrs['source'])
_simplify_attrs(attrs)
return graph
def _simplify_attrs(attrs):
for key, value in list(attrs.items()):
if isinstance(value, BiblSource):
attrs[key] = value.uri
if value.detail is not None:
attrs[key + '_detail'] = value.detail
elif value is None:
del attrs[key]
elif type(value) not in {str, int, float, bool}:
attrs[key] = str(value)
def _load_style(filename):
with open(filename, encoding='utf-8') as f:
return yaml.load(f)
def write_dot(graph: nx.MultiDiGraph, target='base_graph.dot', style=_load_style('styles.yaml'), highlight=None, record='auto'):
logger.info('Writing %s ...', target)
if record == 'auto':
record = len(graph.edges) < 1000
vis = graph.copy()
add_timeline_edges(vis)
for node in vis:
if isinstance(node, Reference):
vis.nodes[node]['URL'] = node.filename.stem
vis.nodes[node]['target'] = '_top'
if highlight is not None and 'highlight' in style['node']:
for key, value in style['node']['highlight'].items():
try:
vis.nodes[highlight][key] = value
except KeyError:
logger.warning('Highlight key %s not found while writing %s', highlight, target)
simplified: MultiDiGraph = simplify_graph(vis)
# now style by kind:
if 'edge' in style:
for u, v, k, attr in simplified.edges(data=True, keys=True):
kind = attr.get('kind', None)
if kind in style['edge']:
for key, value in style['edge'][kind].items():
simplified.edges[u, v, k][key] = value
if attr.get('delete', False) and 'delete' in style['edge']:
for key, value in style['edge']['delete'].items():
simplified.edges[u, v, k][key] = value
if 'node' in style:
for node, attr in simplified.nodes(data=True):
kind = attr.get('kind', None)
if kind in style['node']:
for key, value in style['node'][kind].items():
simplified.nodes[node][key] = value
agraph: AGraph = nx.nx_agraph.to_agraph(simplified)
agraph.edge_attr['fontname'] = 'Ubuntu derivative Faust'
agraph.edge_attr['fontsize'] = 8
agraph.node_attr['fontname'] = 'Ubuntu derivative Faust'
agraph.node_attr['fontsize'] = 12
agraph.graph_attr['rankdir'] = 'LR'
# extract the timeline
timeline = agraph.add_subgraph([node for node in agraph.nodes() if node.attr['kind'] == 'date'],
name='cluster_timeline')
if 'timeline' in style:
timeline_style = style['timeline']
for t in ('graph', 'edge', 'node'):
if t in timeline_style:
getattr(timeline, t + '_attr', {}).update(timeline_style[t])
logger.debug('timeline style: %s = %s', t, getattr(timeline, t + '_attr').items()) ## Doesn’t work
dotfilename = str(target)
agraph.write(dotfilename)
if record:
_render_queue.append(dotfilename)
else:
logger.warning('%s has not been queued for rendering', dotfilename)
def render_file(filename):
graph = AGraph(filename=filename)
try:
resultfn = filename[:-3] + 'svg'
graph.draw(resultfn, format='svg', prog='dot')
return resultfn
except:
logger.exception('Failed to render %s', filename)
def render_all():
with Pool() as pool:
global _render_queue
dots, _render_queue = _render_queue, []
result = list(tqdm(pool.imap_unordered(render_file, dots), desc='Rendering', total=len(dots), unit=' SVGs'))
failcount = result.count(None)
logger.info('Rendered %d SVGs, %d failed', len(result) - failcount, failcount)