-
Notifications
You must be signed in to change notification settings - Fork 3
/
graph-visualization.lisp
142 lines (139 loc) · 6.62 KB
/
graph-visualization.lisp
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
(in-package #:graph-utils)
(defun which (program)
(cl-ppcre:regex-replace-all
"\\s+$"
(trivial-shell:shell-command (format nil "/usr/bin/which ~A" program))
""))
(defmethod visualize ((graph graph) &key (file "/var/tmp/graph.dot") render?
colors sizes (format "svg"))
"Save a dot file of this graph. Render can be one of (:heirarchical
:circular :radial :spring), which will render the graph using the appropriate
Graphviz tool."
(let ((memory (make-hash-table :test 'equalp))
(connector (if (directed? graph) "->" "--")))
(with-open-file (out file
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(format out
"~A graphutils~%{~% splines=true;~% node [ color = black, "
(if (directed? graph) "digraph" "graph"))
(format out "fillcolor = white, style = filled ];~%")
(map-nodes (lambda (name id)
(let ((neighbors (if (directed? graph)
(outbound-neighbors graph id)
(neighbors graph id))))
(dolist (n neighbors)
(unless (if (directed? graph)
(gethash (list id n) memory)
(or (gethash (list id n) memory)
(gethash (list n id) memory)))
(setf (gethash (list id n) memory) t)
(when (not (directed? graph))
(setf (gethash (list n id) memory) t))
(format out
" \"~A\" ~A \"~A\" [w=~A,label=~A];~%"
name
connector
(gethash n (ids graph))
(saref (matrix graph) id n)
(saref (matrix graph) id n))))
(format out " \"~A\" [fillcolor=\"~A\""
name
(if (hash-table-p colors)
(gethash name colors)
"#ffff00"))
(if (hash-table-p sizes)
(progn
(format out ",shape=box,width=~F,fontname=Helvetica,"
(gethash name sizes))
(format out "fixedsize=true,fontsize=~D"
(truncate (* 10 (gethash name sizes)))))
"")
(format out "];~%")))
graph)
(format out "}~%"))
(if render?
(let ((f (regex-replace "\.[a-z]+$" file (format nil "\.~A" format)))
(program (case render?
(:hierarchical (which "dot"))
(:circular (which "circo"))
(:radial (which "twopi"))
(:spring (or (which "fdp") (which "neato")))
(otherwise (or (which "fdp") (which "dot"))))))
(if program
(multiple-value-bind (output error-output exit-status)
(trivial-shell:shell-command
(format nil "~A -T~A -o ~A ~A" program format f file))
(unless (= 0 exit-status)
(error "~A exited with status ~A: ~A ~A~%" program exit-status output error-output)))
(format t "Unable to create PNG of graph ~A. Graphviz not in your path.~%" graph))
f)
file)))
(defmethod visualize ((graph typed-graph) &key (file "/var/tmp/graph.dot") render?
colors sizes (format "svg"))
"Save a dot file of this graph. Render can be one of (:heirarchical
:circular :radial :spring), which will render the graph using the appropriate
Graphviz tool."
(let ((memory (make-hash-table :test 'equalp))
(connector (if (directed? graph) "->" "--")))
(with-open-file (out file
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(format out
"~A graphutils~%{~% splines=true;~% node [ color = black, "
(if (directed? graph) "digraph" "graph"))
(format out "fillcolor = white, style = filled ];~%")
(map-nodes (lambda (name id)
(let ((neighbors (if (directed? graph)
(outbound-neighbors graph id)
(neighbors graph id))))
(dolist (n neighbors)
(let ((edge-type (car n)))
(unless (if (directed? graph)
(gethash (list id n) memory)
(or (gethash (list id n) memory)
(gethash (list n id) memory)))
(setf (gethash (list id n) memory) t)
(when (not (directed? graph))
(setf (gethash (list n id) memory) t))
(format out
" \"~A\" ~A \"~A\" [w=~A,label=\"~A\"];~%"
name
connector
(gethash (cdr n) (ids graph))
(saref (gethash edge-type (matrix graph)) id (cdr n))
edge-type))))
(format out " \"~A\" [fillcolor=\"~A\""
name
(if (hash-table-p colors)
(gethash name colors)
"#ffff00"))
(if (hash-table-p sizes)
(progn
(format out ",shape=box,width=~F,fontname=Helvetica,"
(gethash name sizes))
(format out "fixedsize=true,fontsize=~D"
(truncate (* 10 (gethash name sizes)))))
"")
(format out "];~%")))
graph)
(format out "}~%"))
(if render?
(let ((f (regex-replace "\.[a-z]+$" file (format nil "\.~A" format)))
(program (case render?
(:hierarchical (which "dot"))
(:circular (which "circo"))
(:radial (which "twopi"))
(:spring (or (which "fdp") (which "neato")))
(otherwise (or (which "fdp") (which "dot"))))))
(if program
(multiple-value-bind (output error-output exit-status)
(trivial-shell:shell-command
(format nil "~A -T~A -o ~A ~A" program format f file))
(unless (= 0 exit-status)
(error "~A exited with status ~A: ~A ~A~%" program exit-status output error-output)))
(format t "Unable to create PNG of graph ~A. Graphviz not in your path.~%" graph))
f)
file)))