-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_graph.py
More file actions
156 lines (139 loc) · 6.73 KB
/
create_graph.py
File metadata and controls
156 lines (139 loc) · 6.73 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
import networkx as nx
from matplotlib import pyplot as plt
from PIL import Image, ImageDraw, ImageFont
import sys
import parse_file
import math
'''
input: connections = list of CallData objects
'''
def draw(connections):
g = nx.DiGraph() # directed graph with self-loops
num_nodes = len(connections)
files = {}
locations = {}
stretch = {}
nodes = [f.func_name for f in connections]
font = ImageFont.truetype("arial.ttf", size=12)
for i in range(len(connections)):
if (connections[i].src_file in files):
files[connections[i].src_file].append(connections[i].func_name)
else:
files[connections[i].src_file] = [connections[i].func_name]
im_size = max(1000, len(files.keys()) * 100 , max([len(lst) for lst in files.values()]) * 80)
im = Image.new('RGB', (im_size, im_size), color="white")
draw = ImageDraw.Draw(im)
file_list = list(files.keys())
x_spacing = {}
for file in file_list:
if (len(files[file]) == 1):
x_spacing[file] = [im_size / 2]
else:
x_spacing[file] = list(range(100, im_size - 100, int( (im_size - 200) / (len(files[file]) - 1))))
x_spacing[file].append(im_size - 100)
# here we're listing the node-y coords, not the file divides
step = int(im_size / len(file_list))
y_spacing = list(range(int(step / 2), im_size, step))
# draw file divisions
for y in range(step, im_size, step):
for j in range(im_size):
if (j % 10 < 5):
draw.point((j, y), fill="blue")
# get locations of nodes in (x, y) coordinates spread out across image
for i in range(len(nodes)):
stretch[connections[i].func_name] = len(connections[i].func_name) * 1.4
node_index = files[connections[i].src_file].index(connections[i].func_name)
node_x = x_spacing[connections[i].src_file][node_index]
node_y = y_spacing[file_list.index(connections[i].src_file)]
locations[connections[i].func_name] = (node_x, node_y)
g.add_node(connections[i].func_name, pos=locations[connections[i].func_name])
# add the edges to the drawing based on saved (x,y) of each function
for i in range(len(nodes)):
for dest in connections[i].call_list:
start = connections[i].func_name
draw_edge(draw, locations[start][0], locations[start][1], locations[dest][0], locations[dest][1])
g.add_edge(start, dest)
# draw the arrows on the edges
for i in range(len(nodes)):
for dest in connections[i].call_list:
start = connections[i].func_name
if ((locations[dest][0] == locations[start][0]) or (locations[dest][1] == locations[start][1])):
draw_arrow_arc(draw, locations[dest][0], locations[dest][1], locations[start][0], locations[start][1])
else:
draw_arrow(draw, locations[dest][0], locations[dest][1], locations[start][0], locations[start][1], 35 + stretch[connections[i].func_name])
# draw the nodes after drawing edges
for i in range(len(nodes)):
node = locations[connections[i].func_name]
draw_circle(draw, node[0], node[1], 30, connections[i].func_name, connections[i].times_called, font, stretch[connections[i].func_name])
# function names last
for i, y in enumerate(range(step, im_size + 5, step)):
draw.text((im_size - 100, y - 20), file_list[i], font=font, fill="black")
#draw.text((im_size - 100, im_size - 20), file_list[len(file_list) - 1], font=font, fill="black")
nx.draw(g, with_labels=True, pos=locations, node_size=700)
# plt.show()
del draw
im.save("test.png", format="PNG")
return im
def draw_circle(im, x, y, rad, label, times_called, font, stretch):
im.ellipse((x - rad - stretch - 1, y - rad - 1, x + rad + stretch + 1, y + rad + 1), fill=(0, 0, 0))
intensity = max(255 - (15 * times_called), 0)
im.ellipse((x - rad - stretch, y - rad, x + rad + stretch, y + rad), fill=(intensity, intensity, 255))
len(label)
im.text((x - (len(label) * 2.75), y - 5), label, font=font, fill="black")
return stretch
def draw_edge(im, x_1, y_1, x_2, y_2):
if ((x_1 == x_2) and (y_1 == y_2)):
# recursive self-loop
im.arc((x_1 - 75, y_1 - 30, x_1 - 20, y_2 + 30), 30, 330, 'black')
im.arc((x_1 - 74, y_1 - 30, x_1 - 19, y_2 + 30), 30, 330, 'black')
#im.rectangle((x_1 - 75, y_1 - 30, x_1 - 20, y_2 + 30), outline="black")
elif (x_1 == x_2):
# same column
im.arc((x_1 - 80, min(y_1, y_2) + 20, x_1 + 20, max(y_1, y_2) - 20), 90, 270, 'black')
im.arc((x_1 - 79, min(y_1, y_2) + 20, x_1 + 19, max(y_1, y_2) - 20), 90, 270, 'black')
#im.rectangle((x_1 - 80, min(y_1, y_2) + 20, x_1 + 20, max(y_1, y_2) - 20), outline="black")
elif (y_1 == y_2):
# same row
im.arc((min(x_1, x_2) + 20, y_2 - 80, max(x_1, x_2) - 20, y_2 + 30), 180, 0, 'black')
im.arc((min(x_1, x_2) + 20, y_2 - 79, max(x_1, x_2) - 20, y_2 + 29), 180, 0, 'black')
# im.rectangle((x_1, y_1 - 10, x_2, y_2 - 60), outline="black")
else:
im.line(((x_1, y_1), (x_2, y_2)), fill="black", width=2)
def draw_arrow(im, x_1, y_1, x_2, y_2, r):
if ((x_1 == x_2) and (y_1 == y_2)):
return
dist = ((x_1 - x_2)**2 + (y_1 - y_2)**2)**.5
x_a = x_1 - (r * (x_1 - x_2) / dist)
y_a = y_1 - (r * (y_1 - y_2) / dist)
t = math.tan(math.pi/6)
target = 150
x_a1 = ((x_a-x_2)*math.cos(t) - (y_a-y_2)*math.sin(t))
y_a1 = ((y_a-y_2)*math.cos(t) + (x_a-x_2)*math.sin(t))
d1 = x_a1**2 + y_a1**2
x_a2 = ((x_a-x_2)*math.cos(-t) - (y_a-y_2)*math.sin(-t))
y_a2 = ((y_a-y_2)*math.cos(-t) + (x_a-x_2)*math.sin(-t))
d2 = x_a2**2 + y_a2**2
x_l = x_a - x_a1*((target/d1)**0.5)
y_l = y_a - y_a1*((target/d1)**0.5)
x_r = x_a - x_a2*((target/d2)**0.5)
y_r = y_a - y_a2*((target/d2)**0.5)
im.line(((x_l, y_l), (x_a, y_a)), fill="black", width=3)
im.line(((x_r, y_r), (x_a, y_a)), fill="black", width=3)
def draw_arrow_arc(im, x_1, y_1, x_2, y_2):
#same column, also includes recursive and should route to left side
if (x_1 == x_2 and y_1 == y_2):
draw_arrow(im, x_1 - 75, (y_1 + y_2)/2, x_1-75, y_2 + 30, 0)
elif (x_1 == x_2):
draw_arrow(im, x_1 - 80, (y_1 + y_2) / 2, x_1 - 80, max(y_1, y_2) - 20, 0)
else:
draw_arrow(im, (x_1 + x_2) / 2, y_2 - 80, max(x_1, x_2) - 20, y_2 - 80, 0)
def main():
t1 = parse_file.CallData("one", "f1", ["one", "two", "four"], 10)
t2 = parse_file.CallData("two", "f1", ["three", "four"], 7)
t5 = parse_file.CallData("five", "f1", ["two"], 4)
t3 = parse_file.CallData("three", "f2", ["one"], 2)
t4 = parse_file.CallData("four", "f2", ["one"], 1)
test = [t1, t2, t3, t4, t5]
draw(test)
if __name__ == "__main__":
main()