-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawing.py
192 lines (158 loc) · 5.19 KB
/
drawing.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
import anonygraph
import pygraphviz as pgv
import networkx as nx
import matplotlib.pyplot as plt
users_data = {
"age": {
21: ["Ken", "Henry"],
19: ["Mary", "Jane"],
30: ["Tom"],
},
"job": {
"Student": ["Ken", "Mary", "Jane"],
"Engineer": ["Henry", "Tom"],
},
"follow": {
"Henry": ["Ken"],
},
"classmate of": {
"Jane": ["Mary"]
}
}
name2k_dict = {
"Ken": 2,
"Henry": 2,
"Tom": 1,
"Mary": 2,
"Jane": 4,
}
user_names = ["Ken", "Mary", "Henry", "Tom", "Jane"]
anony_users_data = {
"age": {
21: user_names,
19: user_names,
30: user_names,
},
"job": {
"Student": user_names,
"Engineer": user_names,
},
"follow": {
"Henry": ["Ken"],
"Tom": ["Henry"],
"Mary": ["Tom"],
"Jane": ["Mary"],
"Ken": ["Jane"],
},
"classmate of": {
"Henry": ["Ken"],
"Tom": ["Henry"],
"Mary": ["Tom"],
"Jane": ["Mary"],
"Ken": ["Jane"],
}
}
new_anony_users_data = {
"age": {
21: ["Mary", "Ken", "Henry", "Tom"],
19: ["Mary", "Ken"],
30: ["Henry", "Tom"],
},
"job": {
"Student": ["Ken", "Mary"],
"Engineer": ["Henry", "Tom"],
},
"follow": {
"Henry": ["Ken"],
"Tom": ["Mary"],
},
}
new_users_data = {
"age": {
21: ["Ken", "Henry"],
19: ["Mary"],
30: ["Tom"],
},
"job": {
"Student": ["Ken", "Mary"],
"Engineer": ["Henry", "Tom"],
},
"follow": {
"Henry": ["Ken"],
},
}
attr_relations = ["age", "job"]
def is_attr_relation(relation_name):
return relation_name in attr_relations
def add_edge(graph, start_node, end_node, relation, anony_mode, line_style, lib):
start_node_name = get_node_name(start_node, False, anony_mode)
end_node_name = get_node_name(end_node, is_attr_relation(relation), anony_mode)
print(start_node_name, relation, end_node_name)
if lib == "pygraphviz":
graph.add_edge(start_node_name, end_node_name, key=relation, label = relation, style=line_style)
elif lib == "networkx":
graph.add_edge(start_node_name, end_node_name, relation, label=relation)
else:
raise Exception()
def get_node_name(node_name, is_value, anony_mode):
if is_value:
return node_name
if anony_mode == "raw":
return "{}-{}".format(node_name, name2k_dict[node_name])
elif anony_mode == "anony":
return "user:{}".format(user_names.index(node_name))
else:
raise Exception()
def generate_graph(g, users_data, line_style, lib="pygraphviz"):
for relation, relation_data in users_data.items():
for end_node, start_nodes in relation_data.items():
for start_node in start_nodes:
add_edge(g, start_node, end_node, relation, "raw", line_style, lib)
# g.add_edge(start_node, end_node, relation, label = relation, style=line_style)
def generate_anony_graph(g, anony_users_data, line_style, lib="pygraphviz"):
for relation, relation_data in anony_users_data.items():
for end_node, start_nodes in relation_data.items():
for start_node in start_nodes:
if not g.has_edge(start_node, end_node, relation):
add_edge(g, start_node, end_node, relation, "anony", line_style, lib)
# g.add_edge(start_node, end_node, relation, label = relation, style=line_style)
def visualize_with_pygraphviz(users_data, anony_users_data):
graph = pgv.AGraph(directed=True)
generate_graph(graph, users_data, "solid")
# generate_anony_graph(graph, anony_users_data, "dashed")
print(graph.string()) # print to screen
graph.layout("circo")
# graph.layout("dot")
graph.draw("simple.png")
# A.write("simple.dot") # write to simple.dot
# B = pgv.AGraph("simple.dot") # create a new graph from file
# B.layout() # layout with default (neato)
# B.draw("simple.png") # draw png
def visualize_with_networkx(users_data, anony_users_data):
graph = nx.MultiDiGraph()
generate_graph(graph, users_data, "solid", "networkx")
# for user_name, user_data in users_data.items():
# for relation, val in user_data.items():
# graph.add_edge(user_name, val, label=relation)
pos = nx.layout.planar_layout(graph)
# pos = nx.bipartite_layout(G, top)
nx.draw_networkx(graph, with_label = True, pos=pos)
plt.show()
def visualize_raw_graph(users_data):
graph = pgv.AGraph(directed=True)
generate_graph(graph, users_data, "solid")
print(graph.string()) # print to screen
graph.layout("dot")
# graph.layout("dot")
graph.draw("graph_raw.pdf")
def visualize_anony_graph(users_data, anony_users_data):
graph = pgv.AGraph(directed=True)
generate_anony_graph(graph, users_data, "solid")
generate_anony_graph(graph, anony_users_data, "dashed")
print(graph.string()) # print to screen
# graph.layout("circo")
graph.layout("dot")
graph.draw("graph_anony.pdf")
visualize_raw_graph(users_data)
visualize_anony_graph(new_users_data, new_anony_users_data)
# visualize_with_networkx(users_data, anony_users_data)