-
Notifications
You must be signed in to change notification settings - Fork 0
/
workloads.py
356 lines (324 loc) · 14.2 KB
/
workloads.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
from itertools import combinations
import numpy as np
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score
import measures
class Workload:
# encoding is a list of vectors. len(encoding) = # of entities pairs
# each vector is an encoding for that particular entity pair
def __init__(
self,
df,
sens_att_left,
sens_att_right,
prediction,
label_column="label",
multiple_sens_attr=False,
delimiter=",",
single_fairness=True,
k_combinations=1,
):
self.df = df
self.label_column = label_column
self.prediction = prediction
self.sens_att_left = sens_att_left
self.sens_att_right = sens_att_right
self.multiple_sens_attr = multiple_sens_attr
self.delimiter = delimiter
self.single_fairness = single_fairness
self.sens_attr_vals = self.find_all_sens_attr()
self.sens_att_to_index = self.create_sens_att_to_index()
self.name_to_encode = {}
self.encoding = self.encode()
self.TP = 0
self.FP = 1
self.TN = 2
self.FN = 3
self.workload_conf_matrix = self.calculate_workload_conf_matrix()
self.entitites_to_count = self.create_entities_to_count()
self.k_combs = self.create_k_combs(k_combinations)
self.k_combs_to_attr_names = self.k_combs_to_attribute_names()
def find_all_sens_attr(self):
sens_att = set()
for it_index, row in self.df.iterrows():
left = row[self.sens_att_left]
right = row[self.sens_att_right]
if self.multiple_sens_attr:
for item in left.split(self.delimiter):
sens_att.add(item.strip())
for item in right.split(self.delimiter):
sens_att.add(item.strip())
else:
sens_att.add(left.strip())
sens_att.add(right.strip())
sens_attr_vals = list(sens_att)
sens_attr_vals.sort()
return sens_attr_vals
def encode(self):
encoding = []
for it_index, row in self.df.iterrows():
left_att = row[self.sens_att_left]
right_att = row[self.sens_att_right]
left_vector = np.zeros(len(self.sens_attr_vals))
right_vector = np.zeros(len(self.sens_attr_vals))
if self.multiple_sens_attr:
for item in left_att.split(self.delimiter):
curr_item = item.strip()
idx = self.sens_attr_vals.index(curr_item)
left_vector[idx] = 1
for item in right_att.split(self.delimiter):
curr_item = item.strip()
idx = self.sens_attr_vals.index(curr_item)
right_vector[idx] = 1
else:
curr_item = left_att.strip()
idx = self.sens_attr_vals.index(curr_item)
left_vector[idx] = 1
curr_item = right_att.strip()
idx = self.sens_attr_vals.index(curr_item)
right_vector[idx] = 1
encoding.append(np.concatenate([left_vector, right_vector]))
return encoding
def create_sens_att_to_index(self):
sens_att_to_index = {}
for i in range(len(self.sens_attr_vals)):
att = self.sens_attr_vals[i]
sens_att_to_index[att] = i
return sens_att_to_index
def create_hashmap_key(self, row):
left_att = row[self.sens_att_left]
right_att = row[self.sens_att_right]
key_left = []
key_right = []
if self.multiple_sens_attr:
for item in left_att.split(self.delimiter):
curr_item = item.strip()
key_left.append(self.sens_att_to_index[curr_item])
for item in right_att.split(self.delimiter):
curr_item = item.strip()
key_right.append(self.sens_att_to_index[curr_item])
key_left.sort()
key_right.sort()
else:
curr_item = left_att.strip()
key_left.append(self.sens_att_to_index[curr_item])
curr_item = right_att.strip()
key_right.append(self.sens_att_to_index[curr_item])
# -1 added as a delimiter between the left and right keys
res = (
key_left + [-1] + key_right
if key_left[0] <= key_right[0]
else key_right + [-1] + key_left
)
return tuple(res)
def create_entities_to_count(self):
entitites_to_count = {}
for ind, row in self.df.iterrows():
key = self.create_hashmap_key(row)
entitites_to_count[key] = [0, 0, 0, 0]
for ind, row in self.df.iterrows():
key = self.create_hashmap_key(row)
self.fill_entities_to_count(
entitites_to_count,
key,
ind,
self.prediction[ind],
row[self.label_column],
)
return entitites_to_count
def fill_entities_to_count(self, entitites_to_count, key, ind, pred, ground_truth):
if pred[0]:
if ground_truth:
entitites_to_count[key][self.TP] += 1
else:
entitites_to_count[key][self.FP] += 1
else:
if ground_truth:
entitites_to_count[key][self.FN] += 1
else:
entitites_to_count[key][self.TN] += 1
def find_border_in_key(self, key):
return key.index(-1)
def create_k_combs(self, k):
k_combs = {}
if self.single_fairness:
for entity in self.entitites_to_count:
number_of_pairs_with_entity = (
sum(self.entitites_to_count[entity]) + 1
) # because of -1 for the border
border = self.find_border_in_key(entity)
for comb in combinations(entity[:border], k):
if comb not in k_combs:
k_combs[comb] = number_of_pairs_with_entity
else:
k_combs[comb] = k_combs[comb] + number_of_pairs_with_entity
for comb in combinations(entity[border + 1 :], k):
if comb not in k_combs:
k_combs[comb] = number_of_pairs_with_entity
else:
k_combs[comb] = k_combs[comb] + number_of_pairs_with_entity
else:
for entity in self.entitites_to_count:
number_of_pairs_with_entity = (
sum(self.entitites_to_count[entity]) + 1
) # because of -1 for the border
border = self.find_border_in_key(entity)
for comb1 in combinations(entity[:border], k):
for comb2 in combinations(entity[border + 1 :], k):
k_comb = comb1 + comb2
if k_comb not in k_combs:
k_combs[k_comb] = number_of_pairs_with_entity
else:
k_combs[k_comb] = (
k_combs[k_comb] + number_of_pairs_with_entity
)
return k_combs
def k_combs_to_attribute_names(self):
comb_to_attribute_names = {}
if self.single_fairness:
for comb in self.k_combs:
names = []
for elem in comb:
names.append(elem)
names.sort() # sort by index
name = ""
for elem in names:
name += self.sens_attr_vals[elem] + "~"
name = name[:-1] # delete the last ~
comb_to_attribute_names[comb] = name
else:
for comb in self.k_combs:
comb_list = list(comb)
names_left = []
names_right = []
for i in range(int(len(comb_list) / 2)):
names_left.append(comb_list[i])
names_right.append(comb_list[i + int(len(comb_list) / 2)])
name_left = ""
name_right = ""
for elem in names_left:
name_left += self.sens_attr_vals[elem] + "~"
name_left = name_left[:-1]
for elem in names_right:
name_right += self.sens_attr_vals[elem] + "~"
name_right = name_right[:-1]
comb_to_attribute_names[comb] = name_left + "|" + name_right
return comb_to_attribute_names
def create_subgroup_encoding_from_subgroup_single(self, subgroup):
subgroup_encoding = [0] * len(self.sens_attr_vals)
for group in subgroup:
subgroup_encoding[group] = 1
return subgroup_encoding
def create_subgroup_encodings_from_subgroup_pairwise(self, subgroup):
subgroup_encoding = [0] * 2 * len(self.sens_attr_vals)
subgroup = list(subgroup)
border = int(len(subgroup) / 2)
left = subgroup[:border]
right = subgroup[border:]
left_encoding = [0] * len(self.sens_attr_vals)
right_encoding = [0] * len(self.sens_attr_vals)
for k in left:
left_encoding[k] = 1
for k in right:
right_encoding[k] = 1
return left_encoding + right_encoding, right_encoding + left_encoding
def calculate_fairness_single(self, subgroup, measure):
if measure == "accuracy_parity":
return measures.accuracy_parity_single(self, subgroup)
elif measure == "statistical_parity":
return measures.statistical_parity_single(self, subgroup)
elif measure == "true_positive_rate_parity":
return measures.true_positive_rate_parity_single(self, subgroup)
elif measure == "false_positive_rate_parity":
return measures.false_positive_rate_parity_single(self, subgroup)
elif measure == "negative_predictive_value_parity":
return measures.negative_predictive_value_parity_single(self, subgroup)
elif measure == "positive_predictive_value_parity":
return measures.positive_predictive_value_parity_single(self, subgroup)
def calculate_fairness_pairwise(self, subgroup, measure):
if measure == "accuracy_parity":
return measures.accuracy_parity_pairwise(self, subgroup)
elif measure == "statistical_parity":
return measures.statistical_parity_pairwise(self, subgroup)
elif measure == "true_positive_rate_parity":
return measures.true_positive_rate_parity_pairwise(self, subgroup)
elif measure == "false_positive_rate_parity":
return measures.false_positive_rate_parity_pairwise(self, subgroup)
elif measure == "negative_predictive_value_parity":
return measures.negative_predictive_value_parity_pairwise(self, subgroup)
elif measure == "positive_predictive_value_parity":
return measures.positive_predictive_value_parity_pairwise(self, subgroup)
def calculate_workload_conf_matrix(self):
conf_matr = [0] * 4
for ind, row in self.df.iterrows():
pred = self.prediction[ind][0]
ground_truth = row[self.label_column]
if pred:
if ground_truth:
conf_matr[self.TP] += 1
else:
conf_matr[self.FP] += 1
else:
if ground_truth:
conf_matr[self.FN] += 1
else:
conf_matr[self.TN] += 1
return tuple(conf_matr)
def calculate_workload_fairness(self, measure):
TP, FP, TN, FN = self.workload_conf_matrix
if measure == "accuracy_parity":
return measures.AP(TP, FP, TN, FN)
elif measure == "statistical_parity":
return measures.SP(TP, FP, TN, FN)
elif measure == "true_positive_rate_parity":
return measures.TPR(TP, FP, TN, FN)
elif measure == "false_positive_rate_parity":
return measures.FPR(TP, FP, TN, FN)
elif measure == "negative_predictive_value_parity":
return measures.NPV(TP, FP, TN, FN)
elif measure == "positive_predictive_value_parity":
return measures.PPV(TP, FP, TN, FN)
def fairness(self, subgroups, measure, aggregate="distribution"):
counts = []
if self.single_fairness:
values = [
self.calculate_fairness_single(subgroup, measure)
for subgroup in subgroups
]
for subgroup in subgroups:
counts.append(
measures.get_confusion_matrix_single(self, subgroup)[0]
+ measures.get_confusion_matrix_single(self, subgroup)[1]
+ measures.get_confusion_matrix_single(self, subgroup)[2]
+ measures.get_confusion_matrix_single(self, subgroup)[3]
)
else:
values = [
self.calculate_fairness_pairwise(subgroup, measure)
for subgroup in subgroups
]
for subgroup in subgroups:
counts.append(
measures.get_confusion_matrix_pairwise(self, subgroup)[0]
+ measures.get_confusion_matrix_pairwise(self, subgroup)[1]
+ measures.get_confusion_matrix_pairwise(self, subgroup)[2]
+ measures.get_confusion_matrix_pairwise(self, subgroup)[3]
)
# make the measure a parity by subtracting the model performance
workload_fairness = self.calculate_workload_fairness(measure)
# values = [x - workload_fairness for x in values]
if aggregate == "distribution-subtraction":
values = [x - workload_fairness for x in values]
elif aggregate == "distribution-division":
values = [
(max(x, workload_fairness) / min(x, workload_fairness)) - 1 if min(x, workload_fairness)!=0 else 0
for x in values
]
elif aggregate == "max":
return max(values)
elif aggregate == "min":
return min(values)
elif aggregate == "max_minus_min":
return max(values) - min(values)
elif aggregate == "average":
return np.mean(values)
return values, counts