-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepair.py
458 lines (346 loc) · 17.5 KB
/
repair.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# coding=utf-8
import sys
sys.tracebacklimit=5
from util import *
from lang import *
from detect import *
from itertools import *
def repair_all_decomps(decomp, distX, projectSens, association, evalfun, utility, last_utility, iteration):
def get_params(d):
#d.decache()
#d.model.flow(distX, distX, 1.0, projectSens)
eprint(".")
if d.submodel.isbase: return (0.0, 0.0)
epsilon = d.get_epsilon(distX,association)
delta = d.get_delta()
#eprint("%f,%f\n" % (epsilon,delta))
return (epsilon,delta)
def apply_subdecomp(decomp, sub_decomp):
eprint(".")
(replacement, subutil) = sub_decomp.get_optimal_patch(utility)
model = sub_decomp.lens[1](decomp.model, replacement)
submodel = decomp.lens[0](model).simplify()
model = decomp.lens[1](model, submodel)
subholed = decomp.lens[1](model, ExpHole(0))
ret = Decomposition(decomp.holes, decomp.max_holes, decomp.proj,
model = model,
subholed = subholed,
submodel = submodel,
lens = decomp.lens)
ret.sub_replacement = replacement
ret.sub_utility = subutil
ret.sub_decomp = sub_decomp
return ret
if decomp.holes > 1:
eprint("sub repair cannot handle multi-hole decompositions presently\n")
traceback.print_stack()
exit(1)
sub_decomps = filter(lambda d: not d.submodel.isbase, decomp.get_local_subdecomps()) + [decomp]
eprint(" have %d local sub-expressions\n" % len(sub_decomps))
eprint(" constructing local decompositions ")
sub_decomps = map(lambda d: apply_subdecomp(decomp, d), sub_decomps)
eprint(" done\n")
eprint(" computing ε/δ ")
ret = map(lambda d: (d, get_params(d), d.sub_utility), sub_decomps)
eprint(" done\n")
return ret
def repair_decomp(decomp, distX, projectSens, epsilon, delta, association, evalfun, utility, last_utility, iteration):
def is_violation(d):
#d.decache()
#d.model.flow(distX, distX, 1.0, projectSens)
e = d.get_epsilon(distX,association)
if e < epsilon:
eprint("*")
return False
d = d.get_delta()
#eprint("ε = %f, δ = %f\n" % (e,d))
if d < delta:
eprint("*")
return False
eprint(".")
return True
def apply_subdecomp(decomp, sub_decomp):
eprint(".")
(replacement, subutil) = sub_decomp.get_optimal_patch(utility)
model = sub_decomp.lens[1](decomp.model, replacement)
submodel = decomp.lens[0](model).simplify()
model = decomp.lens[1](model, submodel)
subholed = decomp.lens[1](model, ExpHole(0))
ret = Decomposition(decomp.holes, decomp.max_holes, decomp.proj,
model = model,
subholed = subholed,
submodel = submodel,
lens = decomp.lens)
ret.sub_replacement = replacement
ret.sub_utility = subutil
ret.sub_decomp = sub_decomp
return ret
if decomp.holes > 1:
eprint("sub repair cannot handle multi-hole decompositions presently\n")
traceback.print_stack()
exit(1)
model = decomp.model
sub_epsilon = decomp.get_epsilon(distX,association)
sub_delta = decomp.get_delta()
smallstr = decomp.submodel.smallstr()
eprint("\n violation:\tε=%0.6f\tδ=%0.6f\n" % (sub_epsilon, sub_delta))
eprint(" in expression (%d instances):\n%s\n" % (decomp.holes, tab(tab(smallstr))))
eprint(" starting local repair\n")
sub_epsilon = decomp.get_epsilon(distX,association)
sub_delta = decomp.get_delta()
eprint(" currently:\tε=%0.6f\tδ=%0.6f\n" % (sub_epsilon, sub_delta))
#sub_decomps = itertools.islice(decomp.get_local_decomps(), 5)
#sub_decomps = filter(lambda d: not d.submodel.isbase, decomp.get_subdecomps()) + [decomp]
sub_decomps = filter(lambda d: not d.submodel.isbase, decomp.get_local_subdecomps()) + [decomp]
eprint(" have %d local sub-expressions\n" % len(sub_decomps))
eprint(" constructing local decompositions ")
sub_decomps = map(lambda d: apply_subdecomp(decomp, d), sub_decomps)
eprint(" done\n")
eprint(" checking if any will fix violation ")
sub_decomps = filter(lambda d: not is_violation(d), sub_decomps)
eprint(" done\n")
eprint(" %d decomps would remove violation, using one with best model accuracy\n" % len(sub_decomps))
utils = map(lambda d: (d, d.sub_utility), sub_decomps)
utils.sort(key = lambda (d,u): - u)
opt_util = utils[0]
# for (d,u) in utils:
# eprint("util=%f decomp_size=%d\n" % (u[1], d.submodel.size()))
sub_decomp = opt_util[0].sub_decomp
(replacement, patch_util) = (opt_util[0].sub_replacement, opt_util[0].sub_utility)
lost_utility = last_utility - patch_util
last_utility = patch_util
subreach = sub_decomp.submodel.probReach
eprint(" local expression will be replaced:\n%s\n"
% (tab(tab(tab(str(sub_decomp.submodel))))))
eprint(" replacement:\t%s (utility=%f, lost=%0.16f, reached by=%0.16f)\n"
% (str(replacement), patch_util, lost_utility, subreach))
if lost_utility == 0.0:
eprint(" **** LOST ZERO UTILITY ****\n")
#eprint("decomp model = \n%s\n" % decomp.model.str_())
#eprint("subdecomp model = \n%s\n" % sub_decomp.model.str_())
#eprint("model = \n%s\n" % model.str_())
#eprint("(pre) subholed = \n%s\n" % subholed.str_())
#eprint("(pre) submodel = \n%s\n" % submodel.str_())
decomp = opt_util[0]
model = decomp.model
if not decomp.submodel.isbase:
sub_epsilon = decomp.get_epsilon(distX,association)
sub_delta = decomp.get_delta()
eprint(" params after replacement: \tε=%0.6f\tδ=%0.6f\n"
% (sub_epsilon, sub_delta))
else:
eprint(" violation itself was replaced by constant\n")
#eprint("(post) subholed = \n%s\n" % subholed.str_())
#print "\t".join(map(str, [iteration,epsilon,delta,sub_epsilon,sub_delta,model.size(),patch_util]))
return (patch_util, sub_epsilon, sub_delta, model)
def sub_repair(distX, projectSens, model, epsilon, delta, association, evalfun, utility, order):
iteration = 0
last_utility = utility(model)
print "\t".join(map(str, [iteration,epsilon,delta,-1.0,-1.0,model.size(),last_utility]))
while True:
#eprint("current model:\n%s\n" % tab(model.str_()))
iteration += 1
model = model.simplify().copy_()
eprint("pushing dataset through model ... ")
model.flow(distX, distX, 1.0, projectSens)
eprint(" done\n")
eprint("currently, " + evalfun(model) + "\n")
#eprint(str(model) + "\n")
eprint("looking for violations\n")
decomps = list(violations(distX, projectSens, model, epsilon, delta, association, order))
decomps_actual = filter(lambda d: type(d) is not tuple, decomps)
decomps_single_hole = filter(lambda d: d.holes <= 1, decomps_actual)
if len(decomps_single_hole) == 0:
eprint(" no more violations\n")
return model
diff = len(decomps_actual) - len(decomps_single_hole)
if diff > 0:
eprint("WARNING; there were %d decompositions with multiple holes, sub_repair cannot handle them right now\n" % diff)
decomps_single_hole.sort(key = lambda d: d.submodel.size_())
#eprint("\n".join(map(lambda d: d.submodel.str_(), decomps_single_hole)))
#decomp = decomps.next()
decomp = decomps_single_hole[0]
#while type(decomp) is not tuple and decomp.holes > 1:
# eprint("sub repair cannot handle multi-hole decompositions presently, skipping\n")
# eprint(str(decomp.submodel) + "\n")
# decomp = decomps.next()
if type(decomp) is tuple:
print "\t".join(map(str, [iteration,epsilon,delta,decomp[0],decomp[1],model.size_(),last_utility]))
#eprint(" final model:\n%s\n" % tab(model.str_()))
return model
if decomp.holes > 1:
eprint("sub repair cannot handle multi-hole decompositions presently\n")
eprint(str(decomp.submodel) + "\n")
#traceback.print_stack()
#exit(1)
continue
(patch_util, sub_epsilon, sub_delta, model) = repair_decomp(decomp, distX, projectSens, epsilon, delta, association, evalfun, utility, last_utility, iteration)
lost_utility = last_utility - patch_util
last_utility = patch_util
print "\t".join(map(str, [iteration,epsilon,delta,sub_epsilon,sub_delta,model.size(),patch_util]))
#eprint("final model:\n%s\n" % tab(model.str_()))
return model
def sub_repair_greedy(distX, projectSens, model, epsilon, delta, association, evalfun, utility, order):
iteration = 0
last_utility = 0
print "\t".join(map(str, [iteration, epsilon,delta,-1.0,-1.0,model.size(),utility(model)]))
def is_violation(d):
d.decache()
#d.model.flow(distX, distX, 1.0, projectSens)
e = d.get_epsilon(distX,association)
d = d.get_delta()
#eprint("ε = %f, δ = %f\n" % (e,d))
return e > epsilon and d > delta
def apply_subdecomp(decomp, sub_decomp):
(replacement, _) = sub_decomp.get_optimal_patch(utility)
model = sub_decomp.lens[2](decomp.model, replacement)
submodel = decomp.lens[0](model).simplify()
model = decomp.lens[2](model, submodel)
subholed = decomp.lens[2](model, ExpHole(0))
ret = Decomposition(decomp.holes, decomp.max_holes, decomp.proj,
model = model,
subholed = subholed,
submodel = submodel,
lens = decomp.lens)
return ret
while True:
#eprint("current model:\n%s\n" % tab(model.str_()))
iteration += 1
eprint("pushing dataset through model ... ")
model.flow(distX, distX, 1.0, projectSens)
eprint("done\n")
eprint("currently, " + evalfun(model) + "\n")
#eprint(str(model) + "\n")
eprint("looking for violations\n")
try:
decomp = violations(distX, projectSens, model, epsilon, delta, association, order).next()
except StopIteration:
eprint(" no more violations\n")
eprint(" final model:\n%s\n" % tab(model.str_()))
return model
if type(decomp) is tuple:
print "\t".join(map(str, [iteration,epsilon,delta,decomp[0],decomp[1],model.size(),last_utility]))
eprint(" final model:\n%s\n" % tab(model.str_()))
return model
#submodel = decomp.submodel
#subholed = decomp.subholed
sub_epsilon = decomp.get_epsilon(distX,association)
sub_delta = decomp.get_delta()
smallstr = submodel.smallstr()
eprint(" found violation:\tε=%0.6f\tδ=%0.6f\n" % (sub_epsilon, sub_delta))
eprint(" in expression (%d instances):\n%s\n" % (decomp.holes, tab(tab(smallstr))))
#eprint("decomp hole type = %s\n" % type(decomp.submodel))
eprint(" starting local repair\n")
subiteration = 0
done = False
while not done:
#eprint(" current model:\n%s\n" % tab(model.str_()))
subiteration += 1
sub_epsilon = decomp.get_epsilon(distX,association)
sub_delta = decomp.get_delta()
eprint(" currently:\tε=%0.6f\tδ=%0.6f\n" % (sub_epsilon, sub_delta))
#sub_decomps = itertools.islice(decomp.get_local_decomps(), 5)
#sub_decomps = filter(lambda d: not d.submodel.isbase, decomp.get_subdecomps()) + [decomp]
sub_decomps = filter(lambda d: not d.submodel.isbase, decomp.get_local_subdecomps()) + [decomp]
eprint("have %d local sub-expressions\n" % len(sub_decomps))
sub_decomps = map(lambda d: apply_subdecomp(decomp, d), sub_decomps)
sub_decomps = filter(lambda d: not is_violation(d), sub_decomps)
eprint("of which %d would remove the violation\n" % len(sub_decomps))
#for d in sub_decomps:
# eprint(str(type(d.submodel)) + "\n")
def comp_util(d):
#eprint("computing optimal patch for:\n%s\n" % tab(d.submodel.str_()))
u = d.get_optimal_patch(utility)
return (d,u)
utils = map(comp_util,sub_decomps)
utils.sort(key = lambda (d,u): - u[1])
opt_util = utils[0]
# for (d,u) in utils:
# eprint("util=%f decomp_size=%d\n" % (u[1], d.submodel.size()))
sub_decomp = opt_util[0]
sub_submodel = sub_decomp.submodel
sub_subholed = sub_decomp.subholed
sub_sub_epsilon = decomp.get_epsilon(distX,association)
sub_sub_delta = decomp.get_delta()
# (replacement, rprob) = submodel.distReachOut.max()
# TODO: optimize this instead of getting argmax
# eprint(" finding optimal patch ... ")
(replacement, patch_util) = sub_decomp.get_optimal_patch(utility)
last_utility = patch_util
eprint(" local replacement for:\n%s\n" % (str(sub_submodel)))
eprint(" replacement:\t%s (utility=%f)\n" % (str(replacement), patch_util))
#eprint("decomp model = \n%s\n" % decomp.model.str_())
#eprint("subdecomp model = \n%s\n" % sub_decomp.model.str_())
#eprint("model = \n%s\n" % model.str_())
#eprint("(pre) subholed = \n%s\n" % subholed.str_())
#eprint("(pre) submodel = \n%s\n" % submodel.str_())
if (sub_submodel.str_() == submodel.str_()):
eprint(" replaced violation\n")
done = True
decomp = apply_subdecomp(decomp, sub_decomp, replacement)
model = decomp.model
submodel = decomp.submodel
subholed = decomp.subholed
#
#model = sub_decomp.lens[2](model, replacement)#.simplify()
#submodel = decomp.lens[0](model).simplify()
#model = decomp.lens[2](model, submodel)
#subholed = decomp.lens[2](model, ExpHole(0))
#
##subholed = sub_decomp.lens[2](subholed,replacement)#.simplify()
#
##eprint("(post) submodel = \n%s\n" % submodel.str_())
#
##model = sub_subholed.subst(ExpHole(0),replacement).simplify()
#decomp.model = model
#decomp.subholed = subholed
#decomp.submodel = submodel
if not done:
model.flow(distX, distX, 1.0, projectSens)
done = not is_violation(decomp)
#if model.str_() != sub_decomp.model.str_():
# eprint("models do not match")
# exit(1)
#eprint("(post) subholed = \n%s\n" % subholed.str_())
print "\t".join(map(str, [iteration, epsilon,delta,sub_epsilon,sub_delta,model.size(),patch_util]))
eprint("final model:\n%s\n" % tab(model.str_()))
return model
def repair(distX, projectSens, model, epsilon, delta, association, evalfun, utility, order):
iteration = 0
last_utility = 0
print "\t".join(map(str, [iteration, epsilon,delta,-1.0,-1.0,model.size(),utility(model)]))
while True:
iteration += 1
eprint("pushing dataset through model ... ")
model.flow(distX, distX, 1.0, projectSens)
eprint("done\n")
eprint("currently, " + evalfun(model) + "\n")
#eprint(str(model) + "\n")
eprint("looking for violations\n")
try:
decomp = violations(distX, projectSens, model, epsilon, delta, association, order).next()
except StopIteration:
eprint("no more violations\n")
return model
if type(decomp) is tuple:
print "\t".join(map(str, [iteration,epsilon,delta,decomp[0],decomp[1],model.size(),last_utility]))
return model
submodel = decomp.submodel
subholed = decomp.subholed
sub_epsilon = decomp.get_epsilon(distX,association)
sub_delta = decomp.get_delta()
#(replacement, rprob) = submodel.distReachOut.max()
# TODO: optimize this instead of getting argmax
smallstr = submodel.smallstr()
eprint(" violation:\tε=%0.6f\tδ=%0.6f\n" % (sub_epsilon, sub_delta))
eprint(" subexp (%d instances):\n%s\n" % (decomp.holes, tab(tab(smallstr))))
eprint(" finding optimal patch ... ")
(replacement, patch_util) = decomp.get_optimal_patch(utility)
last_utility = patch_util
eprint("done\n")
eprint(" replacement:\t%s (utility=%f)\n" % (str(replacement), patch_util))
model = decomp.lens[2](model,replacement).simplify()
#model = subholed.subst(ExpHole(0),replacement).simplify()
print "\t".join(map(str, [iteration, epsilon,delta,sub_epsilon,sub_delta,model.size(),patch_util]))
#print "%d\t%s\t%s\t%f\t%f\t%d\t%d\t%f\t%f\t%f" % (iteration,e.class_field,e.sensitive_field,epsilon,delta,e.expression.size(),exp_r.size(),error_base,error_exp,error_repaired)
return model