This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
anonymizer.py
200 lines (184 loc) · 6.13 KB
/
anonymizer.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
"""
run mondrian with given parameters
"""
# !/usr/bin/env python
# coding=utf-8
from mondrian import mondrian
from utils.read_adult_data import read_data as read_adult
from utils.read_informs_data import read_data as read_informs
import sys, copy, random
DATA_SELECT = 'a'
RELAX = False
INTUITIVE_ORDER = None
def write_to_file(result):
"""
write the anonymized result to anonymized.data
"""
with open("data/anonymized.data", "w") as output:
for r in result:
output.write(';'.join(r) + '\n')
def get_result_one(data, k=10):
"""
run mondrian for one time, with k=10
"""
print("K=%d" % k)
data_back = copy.deepcopy(data)
result, eval_result = mondrian(data, k, RELAX)
# Convert numerical values back to categorical values if necessary
if DATA_SELECT == 'a':
result = covert_to_raw(result)
else:
for r in result:
r[-1] = ','.join(r[-1])
# write to anonymized.out
write_to_file(result)
data = copy.deepcopy(data_back)
print("NCP %0.2f" % eval_result[0] + "%")
print("Running time %0.2f" % eval_result[1] + " seconds")
def get_result_k(data):
"""
change k, while fixing QD and size of data set
"""
data_back = copy.deepcopy(data)
for k in range(5, 105, 5):
print('#' * 30)
print("K=%d" % k)
result, eval_result = mondrian(data, k, RELAX)
if DATA_SELECT == 'a':
result = covert_to_raw(result)
data = copy.deepcopy(data_back)
print("NCP %0.2f" % eval_result[0] + "%")
print("Running time %0.2f" % eval_result[1] + " seconds")
def get_result_dataset(data, k=10, num_test=10):
"""
fix k and QI, while changing size of data set
num_test is the test number.
"""
data_back = copy.deepcopy(data)
length = len(data_back)
joint = 5000
datasets = []
check_time = length / joint
if length % joint == 0:
check_time -= 1
for i in range(check_time):
datasets.append(joint * (i + 1))
datasets.append(length)
ncp = 0
rtime = 0
for pos in datasets:
print('#' * 30)
print("size of dataset %d" % pos)
for j in range(num_test):
temp = random.sample(data, pos)
result, eval_result = mondrian(temp, k, RELAX)
if DATA_SELECT == 'a':
result = covert_to_raw(result)
ncp += eval_result[0]
rtime += eval_result[1]
data = copy.deepcopy(data_back)
ncp /= num_test
rtime /= num_test
print("Average NCP %0.2f" % ncp + "%")
print("Running time %0.2f" % rtime + " seconds")
print('#' * 30)
def get_result_qi(data, k=10):
"""
change number of QI, while fixing k and size of data set
"""
data_back = copy.deepcopy(data)
num_data = len(data[0])
for i in reversed(list(range(1, num_data))):
print('#' * 30)
print("Number of QI=%d" % i)
result, eval_result = mondrian(data, k, RELAX, i)
if DATA_SELECT == 'a':
result = covert_to_raw(result)
data = copy.deepcopy(data_back)
print("NCP %0.2f" % eval_result[0] + "%")
print("Running time %0.2f" % eval_result[1] + " seconds")
def covert_to_raw(result, connect_str='~'):
"""
During preprocessing, categorical attributes are covert to
numeric attribute using intuitive order. This function will covert
these values back to they raw values. For example, Female and Male
may be converted to 0 and 1 during anonymizaiton. Then we need to transform
them back to original values after anonymization.
"""
covert_result = []
qi_len = len(INTUITIVE_ORDER)
for record in result:
covert_record = []
for i in range(qi_len):
if len(INTUITIVE_ORDER[i]) > 0:
vtemp = ''
if connect_str in record[i]:
temp = record[i].split(connect_str)
raw_list = []
for j in range(int(temp[0]), int(temp[1]) + 1):
raw_list.append(INTUITIVE_ORDER[i][j])
vtemp = connect_str.join(raw_list)
else:
vtemp = INTUITIVE_ORDER[i][int(record[i])]
covert_record.append(vtemp)
else:
covert_record.append(record[i])
if isinstance(record[-1], str):
covert_result.append(covert_record + [record[-1]])
else:
covert_result.append(covert_record + [connect_str.join(record[-1])])
return covert_result
if __name__ == '__main__':
FLAG = ''
LEN_ARGV = len(sys.argv)
try:
MODEL = sys.argv[1]
DATA_SELECT = sys.argv[2]
except IndexError:
MODEL = 's'
DATA_SELECT = 'a'
INPUT_K = 10
# read record
if MODEL == 's':
RELAX = False
else:
RELAX = True
if RELAX:
print("Relax Mondrian")
else:
print("Strict Mondrian")
if DATA_SELECT == 'i':
print("INFORMS data")
DATA = read_informs()
else:
print("Adult data")
# INTUITIVE_ORDER is an intuitive order for
# categorical attributes. This order is produced
# by the reading (from data set) order.
DATA, INTUITIVE_ORDER = read_adult()
print(INTUITIVE_ORDER)
if LEN_ARGV > 3:
FLAG = sys.argv[3]
if FLAG == 'k':
get_result_k(DATA)
elif FLAG == 'qi':
get_result_qi(DATA)
elif FLAG == 'data':
get_result_dataset(DATA)
elif FLAG == '':
get_result_one(DATA)
else:
try:
INPUT_K = int(FLAG)
get_result_one(DATA, INPUT_K)
except ValueError:
print("Usage: python anonymizer [r|s] [a | i] [k | qi | data]")
print("r: relax mondrian, s: strict mondrian")
print("a: adult dataset, i: INFORMS dataset")
print("k: varying k")
print("qi: varying qi numbers")
print("data: varying size of dataset")
print("example: python anonymizer s a 10")
print("example: python anonymizer s a k")
# anonymized dataset is stored in result
print("Finish Mondrian!!")