-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgen_interp.py
179 lines (135 loc) · 5.46 KB
/
gen_interp.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
# coding=utf-8
# Subterm statistics. Given a dataset, sensitive attribute, class
# attribute, and classifier parameters, trains the classifier to
# predict class attribute. Then for each sub-expression in the
# resulting classifier, provides normalized mutual information and
# influence metrics.
import sys
reload(sys)
sys.setdefaultencoding('utf8')
sys.tracebacklimit=2
from detect import all_stats,Decomposition
from util import *
from ml_util import *
import sklearn.cross_validation as cross_validation
from plot_util import *
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.unicode'] = True
from scipy.interpolate import spline
e = experiment_from_args()
metric = e.association
proj = nth(e.sensitive_index)
distData = lift(e.data.itertuples_noid())
eprint("len(data)=%d, len(data_test)=%d\n" % (len(list(e.data.itertuples_noid())), len(list(e.data_test.itertuples_noid()))))
distData_test = lift(e.data_test.itertuples_noid())
distX = lift(map(lambda s: State(s), e.dataX.itertuples_noid()))
utility = (lambda exp: distData .expectation(exp_accurate(exp,e.class_index)))
utility_test = (lambda exp: distData_test.expectation(exp_accurate(exp,e.class_index)))
max_utility = utility(e.expression)
max_utility_test = utility_test(e.expression)
print "(train) utility = %f" % max_utility
print "(test ) utility = %f" % max_utility_test
# Lists used in storing epsilon and delta values
epsilon_list = [] # epsilon
delta_list = [] # delta
size_list = [] # size
arrows_list = []
color_list = []
# list of parent nodes
plist = []
threshold = e.epsilon[0]
model = e.args.model
ident = e.args.label
print >>e.handle1, "\t".join(['label','epsilon','delta','size','height','va','ha','xtext','ytext','color','model','ident'])
e.handle1.flush()
print >>e.handle3, "\t".join(['label','epsilon','delta','size','height','va','ha','xtext','ytext','color','model','ident'])
e.handle2.flush()
print >>e.handle2, "\t".join(['parent_epsilon','parent_delta','child_epsilon','child_delta','color','model','ident'])
e.handle3.flush()
print >>e.handle3, "\t".join(['B','0.0','0.0','10.0','10.0','baseline','right','18.0','18.0',e.color,'nomodel','noident'])
e.handle3.flush()
counted = set()
considered = dict()
count_unique = 0
count_duplicates = 0
def count(d,parent):
global considered
global counted
global count_unique
global count_duplicates
h = d.subholed.str_()
if h in counted:
count_duplicates += 1
return
else:
count_unique += 1
counted.add(h)
for sd in d.get_immediate_subdecomps():
if not (type(sd.submodel) is ExpConst):
if not (type(sd.submodel) is ExpVar):
count(sd, d)
def consider(d,parent,i,t):
global considered
eprint(t)
h = d.subholed.str_()
if h in considered:
eprint("( seen) ")
eprint(type(d.submodel).__name__ + " ... ")
sys.stderr.flush()
index = len(epsilon_list) - 1
(epsilon, delta) = considered[h]
else:
eprint("(unseen) ")
eprint(type(d.submodel).__name__ + " ... ")
sys.stderr.flush()
epsilon = d.get_epsilon(distX,metric)
delta = d.get_delta()
epsilon_list.append(epsilon)
delta_list .append(delta)
size_list .append(d.submodel.size())
plist .append(i)
color_list .append(e.color)
index = len(epsilon_list) - 1
considered[h] = (epsilon,delta)
if parent is not None:
pepsilon = parent.get_epsilon(distX,metric)
pdelta = parent.get_delta()
arrows_list.append(((pdelta,pepsilon),(delta,epsilon),delta - pdelta,d,parent))
print >>e.handle2, "\t".join(map(str, [pepsilon,pdelta,epsilon,delta,e.color,model,ident]))
e.handle2.flush()
else:
print >>e.handle3, "\t".join(['A','1.0',str(d.delta),'10.0','10.0','top','right','-18.0','-18.0',e.color,'nomodel','noident'])
e.handle3.flush()
print >>e.handle1, "\t".join(map(str, [latexify(d.submodel.smallstr()).split("\n")[0],epsilon,delta,d.submodel.size(),d.submodel.height(),'baseline','right',-18.0,18.0,e.color,model,ident]))
e.handle1.flush()
eprint("\n")
for sd in d.get_immediate_subdecomps():
# Skip constants since they are all the same point in the picture at (0,0).
if not (type(sd.submodel) is ExpConst):
if not (type(sd.submodel) is ExpVar):
consider(sd, d, index, t + " ")
#print e.expression
e.expression.flow(distX, distX, 1.0, proj)
decomp = Decomposition(1, 1, proj, e.expression, ExpHole(0), e.expression, lens=lens_identity)
count(decomp, None)
print "unique = %d, duplicates = %d" % (count_unique, count_duplicates)
consider(decomp, None, 0, "")
e.close_handles()
# plot labels
plt.xlabel(r'$\delta$ / influence [probability]')
plt.ylabel(r'$\epsilon$ / association (%s)' % (metric))
plt.grid(b=True)
plt.autoscale(enable=True,tight=False,axis='both')
plt.scatter(delta_list, epsilon_list, s=size_list, color=color_list)
ax = plt.axes()
ax.set_yscale('log', basey=2)
ax.set_xscale('log', basex=2)
for (p1,p2,diff,d,dp) in arrows_list:
color = e.color
plt.quiver(p1[0],p1[1],p2[0]-p1[0],p2[1]-p1[1],units='dots',width=1,scale_units='xy',angles='xy',scale=1,color=color,alpha=0.25)
if e.save_figure is not None:
plt.savefig(e.save_figure)
if e.show_figure:
plt.show()