This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
mondrian_l_diversity.py
395 lines (359 loc) · 11 KB
/
mondrian_l_diversity.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""
main module of mondrian_l_diversity
"""
#!/usr/bin/env python
# coding=utf-8
# @InProceedings{LeFevre2006a,
# Title = {Workload-aware Anonymization},
# Author = {LeFevre, Kristen and DeWitt, David J. and Ramakrishnan, Raghu},
# Booktitle = {Proceedings of the 12th ACM SIGKDD International Conference on Knowledge Discovery and Data Mining},
# Year = {2006},
# Address = {New York, NY, USA},
# Pages = {277--286},
# Publisher = {ACM},
# Series = {KDD '06},
# Acmid = {1150435},
# Doi = {10.1145/1150402.1150435},
# ISBN = {1-59593-339-5},
# Keywords = {anonymity, data recoding, predictive modeling, privacy},
# Location = {Philadelphia, PA, USA},
# Numpages = {10},
# Url = {http://doi.acm.org/10.1145/1150402.1150435}
# }
# 2014-10-12
import pdb
from models.numrange import NumRange
from models.gentree import GenTree
from utils.utility import list_to_str, cmp_str
__DEBUG = False
QI_LEN = 10
GL_L = 0
RESULT = []
ATT_TREES = []
QI_RANGE = []
IS_CAT = []
class Partition(object):
"""Class for Group, which is used to keep records
Store tree node in instances.
self.member: records in group
self.width: width of this partition on each domain
self.middle: save the generalization result of this partition
self.allow: 0 donate that not allow to split, 1 donate can be split
"""
def __init__(self, data, width, middle):
"""
initialize with data, width and middle
"""
self.member = data[:]
self.width = list(width)
self.middle = list(middle)
self.allow = [1] * QI_LEN
def __len__(self):
"""
return the number of records in partition
"""
return len(self.member)
def check_L_diversity(partition):
"""check if partition satisfy l-diversity
return True if satisfy, False if not.
"""
sa_dict = {}
if len(partition) < GL_L:
return False
if isinstance(partition, Partition):
records_set = partition.member
else:
records_set = partition
num_record = len(records_set)
for record in records_set:
sa_value = list_to_str(record[-1])
try:
sa_dict[sa_value] += 1
except KeyError:
sa_dict[sa_value] = 1
if len(sa_dict.keys()) < GL_L:
return False
for sa in sa_dict.keys():
# if any SA value appear more than |T|/l,
# the partition does not satisfy l-diversity
if sa_dict[sa] > 1.0 * num_record / GL_L:
return False
return True
def get_normalized_width(partition, index):
"""
return Normalized width of partition
similar to NCP
"""
if IS_CAT[index] is False:
low = partition.width[index][0]
high = partition.width[index][1]
width = float(ATT_TREES[index].sort_value[high]) - float(ATT_TREES[index].sort_value[low])
else:
width = partition.width[index]
return width * 1.0 / QI_RANGE[index]
def choose_dimension(partition):
"""chooss dim with largest normlized Width
return dim index.
"""
max_witdh = -1
max_dim = -1
for i in range(QI_LEN):
if partition.allow[i] == 0:
continue
normWidth = get_normalized_width(partition, i)
if normWidth > max_witdh:
max_witdh = normWidth
max_dim = i
if max_witdh > 1:
print "Error: max_witdh > 1"
pdb.set_trace()
if max_dim == -1:
print "cannot find the max dim"
pdb.set_trace()
return max_dim
def frequency_set(partition, dim):
"""get the frequency_set of partition on dim
return dict{key: str values, values: count}
"""
frequency = {}
for record in partition.member:
try:
frequency[record[dim]] += 1
except KeyError:
frequency[record[dim]] = 1
return frequency
def find_median(partition, dim):
"""find the middle of the partition
return splitVal
"""
frequency = frequency_set(partition, dim)
splitVal = ''
nextVal = ''
value_list = frequency.keys()
value_list.sort(cmp=cmp_str)
total = sum(frequency.values())
middle = total / 2
if middle < GL_L:
return '', ''
index = 0
split_index = 0
for i, t in enumerate(value_list):
index += frequency[t]
if index >= middle:
splitVal = t
split_index = i
break
else:
print "Error: cannot find splitVal"
try:
nextVal = value_list[split_index + 1]
except IndexError:
nextVal = splitVal
return (splitVal, nextVal)
def split_numerical_value(numeric_value, splitVal):
"""
split numeric value on splitVal
return sub ranges
"""
split_num = numeric_value.split(',')
if len(split_num) <= 1:
return split_num[0], split_num[0]
else:
low = split_num[0]
high = split_num[1]
# Fix 2,2 problem
if low == splitVal:
lvalue = low
else:
lvalue = low + ',' + splitVal
if high == splitVal:
rvalue = high
else:
rvalue = splitVal + ',' + high
return lvalue, rvalue
def split_numerical(partition, dim, pwidth, pmiddle):
"""
strict split numeric attribute by finding a median,
lhs = [low, means], rhs = (mean, high]
"""
sub_partitions = []
# numeric attributes
(splitVal, nextVal, low, high) = find_median(partition, dim)
p_low = ATT_TREES[dim].dict[low]
p_high = ATT_TREES[dim].dict[high]
# update middle
if low == high:
pmiddle[dim] = low
else:
pmiddle[dim] = low + ',' + high
pwidth[dim] = (p_low, p_high)
if splitVal == '' or splitVal == nextVal:
# update middle
return []
middle_pos = ATT_TREES[dim].dict[splitVal]
lmiddle = pmiddle[:]
rmiddle = pmiddle[:]
lmiddle[dim], rmiddle[dim] = split_numerical_value(pmiddle[dim], splitVal)
lhs = []
rhs = []
for temp in partition.member:
pos = ATT_TREES[dim].dict[temp[dim]]
if pos <= middle_pos:
# lhs = [low, means]
lhs.append(temp)
else:
# rhs = (mean, high]
rhs.append(temp)
lwidth = pwidth[:]
rwidth = pwidth[:]
lwidth[dim] = (pwidth[dim][0], middle_pos)
rwidth[dim] = (ATT_TREES[dim].dict[nextVal], pwidth[dim][1])
if check_L_diversity(lhs) is False or check_L_diversity(rhs) is False:
return []
sub_partitions.append(Partition(lhs, lwidth, lmiddle))
sub_partitions.append(Partition(rhs, rwidth, rmiddle))
return sub_partitions
def split_categorical(partition, dim, pwidth, pmiddle):
"""
split categorical attribute using generalization hierarchy
"""
sub_partitions = []
# categoric attributes
splitVal = ATT_TREES[dim][partition.middle[dim]]
sub_node = [t for t in splitVal.child]
sub_groups = []
for i in range(len(sub_node)):
sub_groups.append([])
if len(sub_groups) == 0:
# split is not necessary
return []
for temp in partition.member:
qid_value = temp[dim]
for i, node in enumerate(sub_node):
try:
node.cover[qid_value]
sub_groups[i].append(temp)
break
except KeyError:
continue
else:
print "Generalization hierarchy error!"
flag = True
for index, sub_group in enumerate(sub_groups):
if len(sub_group) == 0:
continue
if check_L_diversity(sub_group) is False:
flag = False
break
if flag:
for i, sub_group in enumerate(sub_groups):
if len(sub_group) == 0:
continue
wtemp = pwidth[:]
mtemp = pmiddle[:]
wtemp[dim] = len(sub_node[i])
mtemp[dim] = sub_node[i].value
sub_partitions.append(Partition(sub_group, wtemp, mtemp))
return sub_partitions
def split_partition(partition, dim):
"""
split partition and distribute records to different sub-partitions
"""
pwidth = partition.width
pmiddle = partition.middle
if IS_CAT[dim] is False:
return split_numerical(partition, dim, pwidth, pmiddle)
else:
return split_categorical(partition, dim, pwidth, pmiddle)
def anonymize(partition):
"""
Main procedure of Half_Partition.
recursively partition groups until not allowable.
"""
# print len(partition)
# print partition.allow
# pdb.set_trace()
if check_splitable(partition) is False:
RESULT.append(partition)
return
# Choose dim
dim = choose_dimension(partition)
if dim == -1:
print "Error: dim=-1"
pdb.set_trace()
sub_partitions = split_partition(partition, dim)
if len(sub_partitions) == 0:
partition.allow[dim] = 0
anonymize(partition)
else:
for sub_p in sub_partitions:
anonymize(sub_p)
def check_splitable(partition):
"""
Check if the partition can be further splited while satisfying k-anonymity.
"""
temp = sum(partition.allow)
if temp == 0:
return False
return True
def init(att_trees, data, L):
"""
resset global variables
"""
global GL_L, RESULT, QI_LEN, ATT_TREES, QI_RANGE, IS_CAT
ATT_TREES = att_trees
for gen_tree in att_trees:
if isinstance(gen_tree, NumRange):
IS_CAT.append(False)
else:
IS_CAT.append(True)
QI_LEN = len(data[0]) - 1
GL_L = L
RESULT = []
QI_RANGE = []
def mondrian_l_diversity(att_trees, data, L):
"""
Mondrian for l-diversity.
This fuction support both numeric values and categoric values.
For numeric values, each iterator is a mean split.
For categoric values, each iterator is a split on GH.
The final result is returned in 2-dimensional list.
"""
init(att_trees, data, L)
middle = []
result = []
wtemp = []
for i in range(QI_LEN):
if IS_CAT[i] is False:
QI_RANGE.append(ATT_TREES[i].range)
wtemp.append((0, len(ATT_TREES[i].sort_value) - 1))
middle.append(ATT_TREES[i].value)
else:
QI_RANGE.append(len(ATT_TREES[i]['*']))
wtemp.append(len(ATT_TREES[i]['*']))
middle.append('*')
whole_partition = Partition(data, wtemp, middle)
anonymize(whole_partition)
ncp = 0.0
dp = 0.0
for partition in RESULT:
rncp = 0.0
dp += len(partition) ** 2
for i in range(QI_LEN):
rncp += get_normalized_width(partition, i)
for i in range(len(partition)):
gen_result = partition.middle + [partition.member[i][-1]]
result.append(gen_result[:])
rncp *= len(partition)
ncp += rncp
ncp /= QI_LEN
ncp /= len(data)
ncp *= 100
if __DEBUG:
from decimal import Decimal
print "Discernability Penalty=%.2E" % Decimal(str(dp))
print "size of partitions"
print len(RESULT)
# print [len(t) for t in RESULT]
print "NCP = %.2f %%" % ncp
return result, ncp