-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
308 lines (236 loc) · 8.32 KB
/
test.py
File metadata and controls
308 lines (236 loc) · 8.32 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
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
#!/usr/bin/env python
import itertools
import math
import os
import random
import sys
from collections import Counter
from collections import OrderedDict
from datetime import datetime
from random import choice
import networkx as nx
import numpy as np
from numpy import genfromtxt
import pandas as pd
import helper
# 18728 words
# with open('dictionary', 'rb') as f:
# virtual_nodes = f.read().split(',')
percentage = 0.2
index = 0
timeslot = False
NUMBER_OF_ACCUSED = 5
NUMBER_OF_K = 2
def memoize(f):
cache = {}
return lambda *args: cache[args] if args in cache else cache.update({args: f(*args)}) or cache[args]
def getVnodes():
global virtual_nodes
return virtual_nodes.pop(0)
def saveGraph(G, filename, dir):
global index
index += 1
helper.saveToDotGraph(G, '{}/{:0>3d}_{}'.format(dir, index, filename))
def graphInfo(G):
helper.draw_table(G)
# helper.graph_info(G)
def method1(G):
num_of_necessary_vnodes = (int)(math.ceil(G.number_of_nodes()*percentage))
num_of_necessary_vedges = (int)(math.ceil(G.number_of_edges()*percentage))
# Add confusing nodes
nodelist = G.nodes()
random.shuffle(nodelist)
confusing_nodes = []
for u in nodelist[:num_of_necessary_vnodes]:
v = getVnodes()
G.add_edge(u, v, color='green')
G.add_node(v, color='blue', style='filled')
confusing_nodes.append(v)
# Add confusing edges
confusing_edges = [(u, v) for u in G.nodes() for v in confusing_nodes if u != v]
random.shuffle(confusing_edges)
num_of_total_edges = G.number_of_edges() + num_of_necessary_vedges
for e in confusing_edges:
G.add_edge(*e, color='red')
if G.number_of_edges() == num_of_total_edges:
break
return G
def K_anonymize(G):
#######
graphInfo(G)
degrees = zip(G.in_degree().values(), G.out_degree().values())
df = pd.DataFrame(data=zip(G.nodes(), degrees), columns=['id', 'deg'])
d = dict(df['deg'].value_counts())
#######
print d
depressed_nodes = []
processed_nodes = []
for k, v in d.iteritems():
if v == 1:
node_id = df[df['deg'] == k].id.values[0]
# print node_id
ix = G.nodes().index(node_id)
in_deg_num = G.in_degree().values()[ix]
if in_deg_num <= NUMBER_OF_ACCUSED:
processed_nodes.append(ix)
else:
depressed_nodes.append(G.nodes()[ix])
#######
print "--" * 20
print depressed_nodes
print processed_nodes
for nix in processed_nodes:
vertex1 = getVnodes()
in_deg_num = G.in_degree().values()[nix]
out_deg_num = G.out_degree().values()[nix]
for in_deg in range(in_deg_num):
vertex2 = getVnodes()
G.add_node(vertex1, color='blue', style='filled')
G.add_node(vertex2, color='blue', style='filled')
G.add_edge(vertex2, vertex1, color='green')
#######
print "{} ---> {}".format(vertex2, vertex1)
for out_deg in range(out_deg_num):
try:
vertex2 = depressed_nodes.pop()
except:
vertex2 =getVnodes()
G.add_node(vertex1, color='blue', style='filled')
G.add_node(vertex2, color='blue', style='filled')
G.add_edge(vertex1, vertex2, color='green')
#######
print "{} ---> {}".format(vertex1, vertex2)
graphInfo(G)
return G
def anonymize(G):
#######
graphInfo(G)
degrees = zip(G.in_degree().values(), G.out_degree().values())
nodes = G.nodes()
degree_table = dict(zip(nodes, degrees))
# Sorting degree_table by degree
sorted_table = OrderedDict()
for k in sorted(degree_table, key=degree_table.get):
sorted_table[k] = degree_table[k]
print '{:3} -> {:6}'.format(k, degree_table[k])
"""
Nodes need to be anonymized
unique_degree & in degree is larger than a particular threshold
"""
unique_degree = [k for k, v in Counter(sorted_table.values()).iteritems() if v == 1]
non_anonymize_nodes = []
conviction_nodes = []
for deg in reversed(unique_degree):
for k, v in sorted_table.iteritems():
if deg == v:
if deg[0] <= NUMBER_OF_ACCUSED:
non_anonymize_nodes.append(k)
else:
conviction_nodes.append(k)
#######
print 'conviction', conviction_nodes
for anony in non_anonymize_nodes:
vertex1 = anony
in_deg, out_deg = sorted_table[vertex1]
#######
#Determine case by case
#######
print 'non_anonymize', anony
print '[IN, OUT] = [{}, {}]'.format(in_deg, out_deg)
# Search weather there is same in_degree within existing nodes
best_case = []
# second_case = []
worst_case = []
for deg in sorted_table.values():
# best case
if deg[0] == in_deg and deg[1] < out_deg:
best_case.append(deg)
# second case
# elif deg[0] < in_deg and deg[1] < out_deg:
# second_case.append(deg)
# worst case
elif in_deg == 0 and deg[0] == 0 and deg[1] <= out_deg:
worst_case.append(deg)
b_checked = [k for k, v in Counter(best_case).iteritems() if v > NUMBER_OF_K]
w_checked = [k for k, v in Counter(worst_case).iteritems() if v > NUMBER_OF_K]
if best_case and b_checked:
print 'Best'
print Counter(best_case)
b_node = b_checked[-1]
print in_deg-b_node[0], out_deg-b_node[1]
add_noise(G, vertex1, in_deg-b_node[0], out_deg-b_node[1], conviction_nodes)
print "create best case"
continue
# if second_case:
# print 'Second'
# print Counter(second_case)
elif worst_case and w_checked:
print 'worst'
print Counter(worst_case)
w_node = w_checked[-1]
print in_deg-w_node[0], out_deg-w_node[1]
add_noise(G, vertex1, in_deg-w_node[0], out_deg-w_node[1], conviction_nodes)
print "create worst case"
continue
else:
vertex1 = getVnodes()
add_noise(G, vertex1, in_deg, out_deg, conviction_nodes)
print "create new one"
#######
graphInfo(G)
degrees = zip(G.in_degree().values(), G.out_degree().values())
nodes = G.nodes()
degree_table = dict(zip(nodes, degrees))
# Sorting degree_table by degree
sorted_table = OrderedDict()
for k in sorted(degree_table, key=degree_table.get):
sorted_table[k] = degree_table[k]
# print '{:3} -> {:6}'.format(k, degree_table[k])
"""
Nodes need to be anonymized
unique_degree & in degree is larger than a particular threshold
"""
unique_degree = [k for k, v in Counter(sorted_table.values()).iteritems() if v == 1]
non_anonymize_nodes = []
conviction_nodes = []
for deg in unique_degree:
for k, v in sorted_table.iteritems():
if deg == v:
if deg[0] <= NUMBER_OF_ACCUSED:
non_anonymize_nodes.append(k)
else:
conviction_nodes.append(k)
#######
a = [anony for anony in non_anonymize_nodes]
if conviction_nodes and a:
"==anothere round=="
anonymize(G)
return G
def add_noise(G, vertex1, number_of_in_degs_need, number_of_out_degs_need, conviction_nodes):
for i in range(number_of_in_degs_need):
vertex2 = getVnodes()
# print vertex1, vertex2
G.add_node(vertex1, color='blue', style='filled')
G.add_node(vertex2, color='blue', style='filled')
G.add_edge(vertex2, vertex1, color='green')
#######
print "{} ---> {}".format(vertex2, vertex1)
for i in range(number_of_out_degs_need):
try:
vertex2 = conviction_nodes.pop()
except:
vertex2 = getVnodes()
# print vertex1, vertex2
G.add_node(vertex1, color='blue', style='filled')
G.add_node(vertex2, color='blue', style='filled')
G.add_edge(vertex1, vertex2, color='green')
#######
print "{} ---> {}".format(vertex1, vertex2)
return G
def main():
G = nx.DiGraph()
G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'D'), ('B', 'E')])
G.add_edge(1, 'A')
graphInfo(G)
if __name__ == '__main__':
main()