-
Notifications
You must be signed in to change notification settings - Fork 0
/
evals.py
111 lines (84 loc) · 4.09 KB
/
evals.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
##########################################################################
# @Author : Peizhao Li
# @Contact : [email protected]
#
# add fairness scores
##########################################################################
from sklearn.metrics import roc_auc_score
from sklearn.metrics import average_precision_score
from sklearn.metrics import accuracy_score
import numpy as np
from scipy import stats
import networkx as nx
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def get_scores(test_true_edges, test_false_edges, preds, G, sensitive, threshold=0.5):
if G.is_directed():
G = G.to_undirected()
G_ground = G.copy()
G_new = G.copy()
preds_pos_intra = []
preds_pos_inter = []
for e in test_true_edges:
G_ground.add_edge(e[0], e[1], weight=1)
if preds[e[0], e[1]] > threshold:
G_new.add_edge(e[0], e[1], weight=1)
if sensitive[e[0]] == sensitive[e[1]]:
preds_pos_intra.append(sigmoid(preds[e[0], e[1]]))
else:
preds_pos_inter.append(sigmoid(preds[e[0], e[1]]))
# if sensitive[e[0]] == sensitive[e[1]]:
# preds_pos_intra.append(preds[e[0], e[1]])
# else:
# preds_pos_inter.append(preds[e[0], e[1]])
preds_neg_intra = []
preds_neg_inter = []
for e in test_false_edges:
if preds[e[0], e[1]] > threshold:
G_new.add_edge(e[0], e[1], weight=1)
if sensitive[e[0]] == sensitive[e[1]]:
preds_neg_intra.append(sigmoid(preds[e[0], e[1]]))
else:
preds_neg_inter.append(sigmoid(preds[e[0], e[1]]))
# if sensitive[e[0]] == sensitive[e[1]]:
# preds_neg_intra.append(preds[e[0], e[1]])
# else:
# preds_neg_inter.append(preds[e[0], e[1]])
# homophily
cnt = 0
for edge in np.array(G_new.edges):
src_class = sensitive[edge[0]]
dst_class = sensitive[edge[1]]
if src_class == dst_class:
# print(f"src : {edge[0], src_class}\t dst : {edge[1], dst_class}")
cnt += 1
print(f"New network homophily : {cnt / len(G_new.edges) :.2f}")
res = {}
for preds_pos, preds_neg, type in zip((preds_pos_intra, preds_pos_inter, preds_pos_intra + preds_pos_inter),
(preds_neg_intra, preds_neg_inter, preds_neg_intra + preds_neg_inter),
("intra", "inter", "overall")):
preds_all = np.hstack([preds_pos, preds_neg])
labels_all = np.hstack([np.ones(len(preds_pos)), np.zeros(len(preds_neg))])
roc_score = roc_auc_score(labels_all, preds_all)
ap_score = average_precision_score(labels_all, preds_all)
err = (np.sum(list(map(lambda x: x >= threshold, preds_pos))) + np.sum(
list(map(lambda x: x < threshold, preds_neg)))) / (len(preds_pos) + len(preds_neg))
score_avg = (sum(preds_pos) + sum(preds_neg)) / (len(preds_pos) + len(preds_neg))
pos_avg, neg_avg = sum(preds_pos) / len(preds_pos), sum(preds_neg) / len(preds_neg)
res[type] = [roc_score, ap_score, err, score_avg, pos_avg, neg_avg]
ks_pos = stats.ks_2samp(preds_pos_intra, preds_pos_inter)[0]
ks_neg = stats.ks_2samp(preds_neg_intra, preds_neg_inter)[0]
# calculate modularity
communities = [set(np.where(sens == sensitive)[0].tolist()) for sens in np.unique(sensitive)]
modularity_new = nx.community.modularity(G_new, communities)
modularity_ground = nx.community.modularity(G_ground, communities)
modred = (modularity_ground - modularity_new) / np.abs(modularity_ground)
scores = res["overall"][0:2] + [modred] + [abs(res["intra"][i] - res["inter"][i]) for i in range(3, 6)] + [ks_pos, ks_neg]
return scores
def result_print(scores):
print("Evaluation Results " + '-'*50)
print('METRIC\t' + ' '.join(
'{0:>8}'.format(metric) for metric in ["auc", "ap", "modred", "dp", "true", "false", "fnr", "tnr"]))
print('VALUE\t' + ' '.join('{0:>8.4f}'.format(value) for value in scores))
print('-'*50)