-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
295 lines (231 loc) · 11 KB
/
evaluate.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
import networkx as nx
import scipy
import numpy as np
from scipy.sparse import diags
from sklearn.metrics import roc_auc_score
from sklearn.metrics import average_precision_score
from munkres import Munkres
from sklearn import metrics
class clustering_metrics():
"from https://github.com/Ruiqi-Hu/ARGA"
def __init__(self, true_label, predict_label):
self.true_label = true_label
self.pred_label = predict_label
def clusteringAcc(self):
# best mapping between true_label and predict label
l1 = list(set(self.true_label))
numclass1 = len(l1)
l2 = list(set(self.pred_label))
numclass2 = len(l2)
if numclass1 != numclass2:
print('Class Not equal, Error!!!!')
return 0
cost = np.zeros((numclass1, numclass2), dtype=int)
for i, c1 in enumerate(l1):
mps = [i1 for i1, e1 in enumerate(self.true_label) if e1 == c1]
for j, c2 in enumerate(l2):
mps_d = [i1 for i1 in mps if self.pred_label[i1] == c2]
cost[i][j] = len(mps_d)
# match two clustering results by Munkres algorithm
m = Munkres()
cost = cost.__neg__().tolist()
indexes = m.compute(cost)
# get the match results
new_predict = np.zeros(len(self.pred_label))
for i, c in enumerate(l1):
# correponding label in l2:
c2 = l2[indexes[i][1]]
# ai is the index with label==c2 in the pred_label list
ai = [ind for ind, elm in enumerate(self.pred_label) if elm == c2]
new_predict[ai] = c
acc = metrics.accuracy_score(self.true_label, new_predict)
f1_macro = metrics.f1_score(self.true_label, new_predict, average='macro')
precision_macro = metrics.precision_score(self.true_label, new_predict, average='macro')
recall_macro = metrics.recall_score(self.true_label, new_predict, average='macro')
f1_micro = metrics.f1_score(self.true_label, new_predict, average='micro')
precision_micro = metrics.precision_score(self.true_label, new_predict, average='micro')
recall_micro = metrics.recall_score(self.true_label, new_predict, average='micro')
return acc, f1_macro, precision_macro, recall_macro, f1_micro, precision_micro, recall_micro
def evaluationClusterModelFromLabel(self):
nmi = metrics.normalized_mutual_info_score(self.true_label, self.pred_label)
adjscore = metrics.adjusted_rand_score(self.true_label, self.pred_label)
acc, f1_macro, precision_macro, recall_macro, f1_micro, precision_micro, recall_micro = self.clusteringAcc()
return acc, nmi, adjscore
def get_roc_score(edges_pos, edges_neg, embeddings, adj_sparse):
"from https://github.com/tkipf/gae"
score_matrix = np.dot(embeddings, embeddings.T)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Store positive edge predictions, actual values
preds_pos = []
pos = []
for edge in edges_pos:
preds_pos.append(sigmoid(score_matrix[edge[0], edge[1]])) # predicted score
pos.append(adj_sparse[edge[0], edge[1]]) # actual value (1 for positive)
# Store negative edge predictions, actual values
preds_neg = []
neg = []
for edge in edges_neg:
preds_neg.append(sigmoid(score_matrix[edge[0], edge[1]])) # predicted score
neg.append(adj_sparse[edge[0], edge[1]]) # actual value (0 for negative)
# Calculate scores
preds_all = np.hstack([preds_pos, preds_neg])
labels_all = np.hstack([np.ones(len(preds_pos)), np.zeros(len(preds_neg))])
# print(preds_all, labels_all )
roc_score = roc_auc_score(labels_all, preds_all)
ap_score = average_precision_score(labels_all, preds_all)
return roc_score, ap_score
def sparse_to_tuple(sparse_mx, insert_batch=False):
"""Convert sparse matrix to tuple representation."""
"""Set insert_batch=True if you want to insert a batch dimension."""
def to_tuple(mx):
if not scipy.sparse.isspmatrix_coo(mx):
mx = mx.tocoo()
if insert_batch:
coords = np.vstack((np.zeros(mx.row.shape[0]), mx.row, mx.col)).transpose()
values = mx.data
shape = (1,) + mx.shape
else:
coords = np.vstack((mx.row, mx.col)).transpose()
values = mx.data
shape = mx.shape
return coords, values, shape
if isinstance(sparse_mx, list):
for i in range(len(sparse_mx)):
sparse_mx[i] = to_tuple(sparse_mx[i])
else:
sparse_mx = to_tuple(sparse_mx)
return sparse_mx
def mask_test_edges(adj, test_frac=.1, val_frac=.05, prevent_disconnect=True, verbose=False):
# NOTE: Splits are randomized and results might slightly deviate from reported numbers in the paper.
"from https://github.com/tkipf/gae"
if verbose == True:
print('preprocessing...')
# Remove diagonal elements
adj = adj - scipy.sparse.dia_matrix((adj.diagonal()[np.newaxis, :], [0]), shape=adj.shape)
adj.eliminate_zeros()
# Check that diag is zero:
assert np.diag(adj.todense()).sum() == 0
g = nx.from_scipy_sparse_matrix(adj)
orig_num_cc = nx.number_connected_components(g)
adj_triu = scipy.sparse.triu(adj) # upper triangular portion of adj matrix
adj_tuple = sparse_to_tuple(adj_triu) # (coords, values, shape), edges only 1 way
edges = adj_tuple[0] # all edges, listed only once (not 2 ways)
# edges_all = sparse_to_tuple(adj)[0] # ALL edges (includes both ways)
num_test = int(np.floor(edges.shape[0] * test_frac)) # controls how large the test set should be
num_val = int(np.floor(edges.shape[0] * val_frac)) # controls how alrge the validation set should be
# Store edges in list of ordered tuples (node1, node2) where node1 < node2
edge_tuples = [(min(edge[0], edge[1]), max(edge[0], edge[1])) for edge in edges]
all_edge_tuples = set(edge_tuples)
train_edges = set(edge_tuples) # initialize train_edges to have all edges
test_edges = set()
val_edges = set()
if verbose == True:
print('generating test/val sets...')
# Iterate over shuffled edges, add to train/val sets
np.random.shuffle(edge_tuples)
for edge in edge_tuples:
# print edge
node1 = edge[0]
node2 = edge[1]
# If removing edge would disconnect a connected component, backtrack and move on
g.remove_edge(node1, node2)
if prevent_disconnect == True:
if nx.number_connected_components(g) > orig_num_cc:
g.add_edge(node1, node2)
continue
# Fill test_edges first
if len(test_edges) < num_test:
test_edges.add(edge)
train_edges.remove(edge)
# Then, fill val_edges
elif len(val_edges) < num_val:
val_edges.add(edge)
train_edges.remove(edge)
# Both edge lists full --> break loop
elif len(test_edges) == num_test and len(val_edges) == num_val:
break
if (len(val_edges) < num_val or len(test_edges) < num_test):
print("WARNING: not enough removable edges to perform full train-test split!")
print("Num. (test, val) edges requested: (", num_test, ", ", num_val, ")")
print("Num. (test, val) edges returned: (", len(test_edges), ", ", len(val_edges), ")")
if prevent_disconnect == True:
assert nx.number_connected_components(g) == orig_num_cc
if verbose == True:
print('creating false test edges...')
test_edges_false = set()
while len(test_edges_false) < num_test:
idx_i = np.random.randint(0, adj.shape[0])
idx_j = np.random.randint(0, adj.shape[0])
if idx_i == idx_j:
continue
false_edge = (min(idx_i, idx_j), max(idx_i, idx_j))
# Make sure false_edge not an actual edge, and not a repeat
if false_edge in all_edge_tuples:
continue
if false_edge in test_edges_false:
continue
test_edges_false.add(false_edge)
if verbose == True:
print('creating false val edges...')
val_edges_false = set()
while len(val_edges_false) < num_val:
idx_i = np.random.randint(0, adj.shape[0])
idx_j = np.random.randint(0, adj.shape[0])
if idx_i == idx_j:
continue
false_edge = (min(idx_i, idx_j), max(idx_i, idx_j))
# Make sure false_edge in not an actual edge, not in test_edges_false, not a repeat
if false_edge in all_edge_tuples or \
false_edge in test_edges_false or \
false_edge in val_edges_false:
continue
val_edges_false.add(false_edge)
if verbose == True:
print('creating false train edges...')
train_edges_false = set()
while len(train_edges_false) < len(train_edges):
idx_i = np.random.randint(0, adj.shape[0])
idx_j = np.random.randint(0, adj.shape[0])
if idx_i == idx_j:
continue
false_edge = (min(idx_i, idx_j), max(idx_i, idx_j))
# Make sure false_edge in not an actual edge, not in test_edges_false,
# not in val_edges_false, not a repeat
if false_edge in all_edge_tuples or \
false_edge in test_edges_false or \
false_edge in val_edges_false or \
false_edge in train_edges_false:
continue
train_edges_false.add(false_edge)
if verbose == True:
print('final checks for disjointness...')
# assert: false_edges are actually false (not in all_edge_tuples)
assert test_edges_false.isdisjoint(all_edge_tuples)
assert val_edges_false.isdisjoint(all_edge_tuples)
assert train_edges_false.isdisjoint(all_edge_tuples)
# assert: test, val, train false edges disjoint
assert test_edges_false.isdisjoint(val_edges_false)
assert test_edges_false.isdisjoint(train_edges_false)
assert val_edges_false.isdisjoint(train_edges_false)
# assert: test, val, train positive edges disjoint
assert val_edges.isdisjoint(train_edges)
assert test_edges.isdisjoint(train_edges)
assert val_edges.isdisjoint(test_edges)
if verbose == True:
print('creating adj_train...')
# Re-build adj matrix using remaining graph
adj_train = nx.adjacency_matrix(g)
# Convert edge-lists to numpy arrays
train_edges = np.array([list(edge_tuple) for edge_tuple in train_edges])
train_edges_false = np.array([list(edge_tuple) for edge_tuple in train_edges_false])
val_edges = np.array([list(edge_tuple) for edge_tuple in val_edges])
val_edges_false = np.array([list(edge_tuple) for edge_tuple in val_edges_false])
test_edges = np.array([list(edge_tuple) for edge_tuple in test_edges])
test_edges_false = np.array([list(edge_tuple) for edge_tuple in test_edges_false])
if verbose == True:
print('Done with train-test split!')
print('')
# NOTE: these edge lists only contain single direction of edge!
return adj_train, train_edges, train_edges_false, \
val_edges, val_edges_false, test_edges, test_edges_false