generated from DavidLeoni/jupman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoft.py
422 lines (319 loc) · 13.7 KB
/
soft.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#
# Library of utilities for
# SoftPython
# author: David Leoni
#
# DO NOT MODIFY THIS FILE !
#
#
#
import unittest
import sys
import random
# some attempt for avoiding random graphviz layout https://github.com/DavidLeoni/softpython-en/issues/2
random.seed(0)
def show_distances():
import networkx as nx
ret = nx.DiGraph()
ret.graph['dpi'] = 80
ret.add_nodes_from(['a 0','b 1', 'c 1', 'd 2', 'e 3', 'f -1', 'g -1'])
ret.add_edges_from([('a 0','b 1'),('a 0', 'c 1'), ('b 1', 'd 2'), ('c 1', 'd 2'), ('d 2', 'e 3')
, ('e 3', 'd 2'),
('f -1', 'g -1')])
return ret
def dig_to_nx(sciprog_digraph):
""" Convert a Sciprog DiGraph into a NetworkX graph and return it. """
import networkx as nx
ret = nx.DiGraph()
ret.graph['dpi'] = 80
ret.add_nodes_from(sciprog_digraph.verteces())
for sv in sciprog_digraph.verteces():
for tv in sciprog_digraph.adj(sv):
ret.add_edge(sv, tv)
return ret
def draw_dig(sciprog_digraph,legend_edges=None, label='', save_to='', options={}):
""" Draws a Sciprog DiGraph"""
draw_nx(dig_to_nx(sciprog_digraph),legend_edges, label=label, save_to=save_to, options=options)
def get_pydot_mod(obj):
""" RETURN the pydot module used by object.
Made this function because there are too many pydot versions around,
pydot, pydotplus and whatnot
If you need to get the string name, use .__package__ attribute
"""
import sys, inspect
mod = inspect.getmodule(obj)
base, _sep, _stem = mod.__name__.partition('.')
return sys.modules[base]
def draw_nx(G, legend_edges=None, label='', save_to='', options={}):
""" Draws a NetworkX graph object. By default, assumes it is a DiGraph.
- save_to: optional filepath where to save a .png image or .dot file
- to show clusters, set the cluster attribute in nodes
- options: Dictionary of GraphViz options
For required libraries, see
https://en.softpython.org/relational/relational1-intro-sol.html#Required-libraries
legend_edges example:
legend_edges = [
{'label':'ciao',
'color':'red'},
{'label':'mondo',
'color':'blue'}
]
"""
if G == None:
raise ValueError('Provided Graph is None !')
import networkx as nx
# fix graphviz path for anaconda in windows ...
try:
import os
if os.name == 'nt':
from os.path import expanduser
home = expanduser("~") # because in windows actual path can differ from user login !!!
graphviz_path = '"C:\\Users\\' + home + '\\Anaconda3\\Library\\bin\\graphviz"'
if os.path.exists(graphviz_path) and "PATH" in os.environ and (graphviz_path not in os.environ["PATH"]) :
os.environ["PATH"] += ';' + graphviz_path
except Exception as e:
print(e)
if not 'node' in G.graph:
G.graph['node'] = {}
if not 'edge' in G.graph:
G.graph['edge'] = {}
if not 'graph' in G.graph:
G.graph['graph'] = {}
def merge(d2, d1):
d2.update({k:v for k,v in d1.items() if not k in d2})
# add graphviz layout options (see https://stackoverflow.com/a/39662097)
if 'node' in options:
merge(G.graph['node'], options['node'])
if 'edge' in options:
merge(G.graph['edge'], options['edge'])
if 'graph' in options:
merge(G.graph['graph'], options['graph'])
merge(G.graph['node'], {'color':'blue', 'fontcolor':'blue'})
merge(G.graph['edge'], {'arrowsize': '0.6', 'splines': 'curved', 'fontcolor':'brown'})
merge(G.graph['graph'], {'scale': '3', 'style':'dotted, rounded',})
# adding attributes to edges in multigraphs is more complicated but see
# https://stackoverflow.com/a/26694158
#G[0][0]['color']='red'
pdot = nx.drawing.nx_pydot.to_pydot(G)
if G.name:
pdot.set_label(G.name)
pdot.set_labelloc('t')
pdot.set_labeljust('l')
def make_legend():
if legend_edges:
pydot_mod = get_pydot_mod(pdot)
glegend = pydot_mod.Cluster(graph_name = 'Legend', label = 'Legend', labeljust='c')
i = 0
for line in legend_edges:
n1 = pydot_mod.Node(name='legend%s' % i , label=line['label'], shape='none', fontcolor=line['color'])
n2 = pydot_mod.Node(name='legend%s' % (i+len(legend_edges)), label='', shape='none')
glegend.add_node(n1)
glegend.add_node(n2)
glegend.add_edge(pydot_mod.Edge(n1,n2, color=line['color'], penwidth=3))
i += 1
pdot.add_subgraph(glegend)
def make_clusters():
allowed_types = (int,float,str,tuple)
clus = {}
nodes = G.nodes(data=True)
if len(nodes) > 0:
for node_id, data in nodes:
if 'cluster' in data:
c = data['cluster']
if not type(c) in allowed_types:
raise ValueError('Cluster type must be one of %s, found insted: %s' \
% (type(c),allowed_types))
if c in clus:
clus[c].append(node_id)
else:
clus[c] = [node_id]
for c in clus:
if len(clus[c]) > 0:
pydot_mod = get_pydot_mod(pdot)
pydot_nodes = []
pydot_cluster = pydot_mod.Cluster(graph_name=str(c))
for node_id in clus[c]:
pydot_node = pydot_mod.Node(name=node_id)
pydot_cluster.add_node(pydot_node)
pdot.add_subgraph(pydot_cluster)
make_clusters()
make_legend()
fix_save_to = save_to.strip().lower()
if fix_save_to:
try:
# note for saving dot we don't require graph_viz installed
if fix_save_to.endswith('.dot'):
pdot.write_raw(save_to.strip())
print("Dot saved to file: ", save_to)
else:
if not fix_save_to.endswith('.png'):
raise ValueError("Provided filename should end with .png found instead save_to=%s" % save_to)
pdot.write_png(save_to)
print("Image saved to file: ", save_to)
except Exception as e:
print("ERROR: Could not save file to ", save_to)
print(e)
# if we are in jupyter ...
if not fix_save_to.endswith('.dot'):
# if we save the dot file it's probably because
# we don't have graphviz on our system
import importlib
ipython_spec = importlib.util.find_spec("IPython")
if ipython_spec:
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from IPython.display import Image, display
plt = Image(pdot.create_png())
display(plt)
def draw_mat(mat, legend_edges=None, label='', save_to='', options={}):
""" Draws a matrix as a DiGraph
- save_to: optional filepath where to save a .png image or .dot file
- to show clusters, set the cluster attribute in nodes
- options: Dictionary of GraphViz options
For required libraries, see
https://en.softpython.org/relational1-intro-sol.html#Required-libraries
For other options, see draw_nx
"""
if mat == None:
raise ValueError('Provided matrix is None !')
import numpy as np
import networkx as nx
m = np.matrix(mat)
G=nx.DiGraph(m)
if not isinstance(mat[0][0], bool):
for i in range(len(mat)):
for j in range(len(mat)):
if i in G and j in G[i]:
G[i][j]['label'] = G[i][j]['weight']
draw_nx(G,legend_edges, label=label, save_to=save_to, options=options)
def draw_adj(d,legend_edges=None, label='', save_to='', options={}):
"""
Draws a a graph represented as a dictionary of adjancency lists.
Node identifiers can be any immutable data structure, like numbers, strings, tuples ...
{
'c': ['a','d'], # node 'c' links to node 'a' and 'd'
'f': ['c'] # node 'f' links to node 'c'
}
- save_to: optional filepath where to save a .png image or .dot file
- to show clusters, set the cluster attribute in nodes
- options: Dictionary of GraphViz options
For required libraries, see
https://en.softpython.org/relational1-intro-sol.html#Required-libraries
For other options, see draw_nx
"""
if d == None:
raise ValueError('Provided dictionary is None !')
import networkx as nx
G=nx.DiGraph(d)
draw_nx(G,legend_edges, label=label, save_to=save_to, options=options)
def draw_proof(proof, db, step_id=None, only_ids=False):
""" Draw all statements reachable from given row_id
THIS FUNCTION IS ALREADY COMPLETE, *DO NOT* CHANGE IT !
"""
import networkx as nx
stmt_type_to_color = {
'$a' : 'blue',
'$f' : 'purple'
}
G=nx.DiGraph()
if step_id == None:
step_id = len(proof)
stack = [step_id]
while len(stack) > 0:
dep_id = stack.pop()
attrs = proof[dep_id-1]
if only_ids:
label = str(dep_id)
else:
label = '%s\n%s\n%s: %s' % (dep_id,
attrs['sequence'],
attrs['label'],
db[attrs['label']]['sequence'])
color = stmt_type_to_color[attrs['keyword']]
G.add_node( dep_id,
label=label,
color=color)
stack.extend(reversed(attrs['step_ids'])) # NOTE: IT IS REVERSED !
for key in G.nodes():
for target in proof[key-1]['step_ids']:
G.add_edge(key, target)
draw_nx(G)
def viz_server(default_module='example.py'):
"""
Creates a Flask server to serve GraphViz by connecting to Gravizo online service
default_module: a module name to execute,
(can be either with or without the '.py')
WARNING: this is a prototype to use only when GraphViz is not installable, like in repl.it
See also https://github.com/DavidLeoni/softpython-en/issues/3
"""
from flask import Flask, render_template_string, request, redirect
web_site = Flask(__name__, static_url_path='/')
index_html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>softpython viz tool</title>
</head>
<body>
<form style="display:block; margin-bottom:15px;"
action="/"
method="post">
<input type="text"
name="module"
value="{{module}}"/>
<input type="submit"
value="Reload"/>
</form>
<img src='https://g.gravizo.com/svg?{{dot}}'/>
<pre> {{error}} </pre>
</body>
</html>
"""
def load_index(module):
default_data = "digraph G {}"
data = default_data
err = ''
try:
print("The module is '" + module + "'")
import importlib
smod = module.strip()
if smod.endswith('.py'):
smod = smod[:-3]
mod = importlib.import_module(smod)
importlib.reload(mod)
with open('output.dot', 'r') as file:
data = file.read()
i = data.index('{')
if i != -1:
# gravizo really wants a named graph
# [ strict ] (graph | digraph) [ ID ] '{' stmt_list '}'
j = data.index('graph')
sp = data[j:i].strip().split()
if len(sp) == 1:
data = data[:j] + 'graph G ' + data[i:]
# gravizo really does not want "" quotes
data = data[:i].replace('"','') + data[i:]
except Exception as e:
print('****** ERROR')
import traceback
traceback.print_exc()
data = default_data
err = e.__class__.__name__ + ': '+ str(e)
import urllib
from urllib.parse import quote
data=quote(data)
return render_template_string(index_html,
module=module,
dot=data,
error=err)
@web_site.route('/')
def index():
return load_index(default_module)
@web_site.route('/', methods = ['POST'])
def reload():
module = request.form['module']
return load_index(module)
web_site.run(host='0.0.0.0', port=8080)