/
util_functions.py
231 lines (211 loc) · 8.64 KB
/
util_functions.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
from __future__ import print_function
import numpy as np
import random
from tqdm import tqdm
import os, sys, pdb, math, time
import cPickle as cp
#import _pickle as cp # python3 compatability
import networkx as nx
import argparse
import scipy.io as sio
import scipy.sparse as ssp
from sklearn import metrics
from gensim.models import Word2Vec
import warnings
warnings.simplefilter('ignore', ssp.SparseEfficiencyWarning)
cur_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.append('%s/../../pytorch_DGCNN' % cur_dir)
sys.path.append('%s/software/node2vec/src' % cur_dir)
from util import GNNGraph
import node2vec
import multiprocessing as mp
def sample_neg(net, test_ratio=0.1, train_pos=None, test_pos=None, max_train_num=None):
# get upper triangular matrix
net_triu = ssp.triu(net, k=1)
# sample positive links for train/test
row, col, _ = ssp.find(net_triu)
# sample positive links if not specified
if train_pos is None or test_pos is None:
perm = random.sample(range(len(row)), len(row))
row, col = row[perm], col[perm]
split = int(math.ceil(len(row) * (1 - test_ratio)))
train_pos = (row[:split], col[:split])
test_pos = (row[split:], col[split:])
# if max_train_num is set, randomly sample train links
if max_train_num is not None:
perm = np.random.permutation(len(train_pos[0]))[:max_train_num]
train_pos = (train_pos[0][perm], train_pos[1][perm])
# sample negative links for train/test
train_num, test_num = len(train_pos[0]), len(test_pos[0])
neg = ([], [])
n = net.shape[0]
print('sampling negative links for train and test')
while len(neg[0]) < train_num + test_num:
i, j = random.randint(0, n-1), random.randint(0, n-1)
if i < j and net[i, j] == 0:
neg[0].append(i)
neg[1].append(j)
else:
continue
train_neg = (neg[0][:train_num], neg[1][:train_num])
test_neg = (neg[0][train_num:], neg[1][train_num:])
return train_pos, train_neg, test_pos, test_neg
def links2subgraphs(A, train_pos, train_neg, test_pos, test_neg, h=1, max_nodes_per_hop=None, node_information=None):
# automatically select h from {1, 2}
if h == 'auto':
# split train into val_train and val_test
_, _, val_test_pos, val_test_neg = sample_neg(A, 0.1)
val_A = A.copy()
val_A[val_test_pos[0], val_test_pos[1]] = 0
val_A[val_test_pos[1], val_test_pos[0]] = 0
val_auc_CN = CN(val_A, val_test_pos, val_test_neg)
val_auc_AA = AA(val_A, val_test_pos, val_test_neg)
print('\033[91mValidation AUC of AA is {}, CN is {}\033[0m'.format(val_auc_AA, val_auc_CN))
if val_auc_AA >= val_auc_CN:
h = 2
print('\033[91mChoose h=2\033[0m')
else:
h = 1
print('\033[91mChoose h=1\033[0m')
# extract enclosing subgraphs
max_n_label = {'value': 0}
def helper(A, links, g_label):
'''
g_list = []
for i, j in tqdm(zip(links[0], links[1])):
g, n_labels, n_features = subgraph_extraction_labeling((i, j), A, h, max_nodes_per_hop, node_information)
max_n_label['value'] = max(max(n_labels), max_n_label['value'])
g_list.append(GNNGraph(g, g_label, n_labels, n_features))
return g_list
'''
# the new parallel extraction code
start = time.time()
pool = mp.Pool(mp.cpu_count())
results = pool.map_async(parallel_worker, [((i, j), A, h, max_nodes_per_hop, node_information) for i, j in zip(links[0], links[1])])
remaining = results._number_left
pbar = tqdm(total=remaining)
while True:
pbar.update(remaining - results._number_left)
if results.ready(): break
remaining = results._number_left
time.sleep(1)
results = results.get()
pool.close()
pbar.close()
g_list = [GNNGraph(g, g_label, n_labels, n_features) for g, n_labels, n_features in results]
max_n_label['value'] = max(max([max(n_labels) for _, n_labels, _ in results]), max_n_label['value'])
end = time.time()
print("Time eplased for subgraph extraction: {}s".format(end-start))
return g_list
print('Enclosing subgraph extraction begins...')
train_graphs = helper(A, train_pos, 1) + helper(A, train_neg, 0)
test_graphs = helper(A, test_pos, 1) + helper(A, test_neg, 0)
print(max_n_label)
return train_graphs, test_graphs, max_n_label['value']
def parallel_worker(x):
return subgraph_extraction_labeling(*x)
def subgraph_extraction_labeling(ind, A, h=1, max_nodes_per_hop=None, node_information=None):
# extract the h-hop enclosing subgraph around link 'ind'
dist = 0
nodes = set([ind[0], ind[1]])
visited = set([ind[0], ind[1]])
fringe = set([ind[0], ind[1]])
nodes_dist = [0, 0]
for dist in range(1, h+1):
fringe = neighbors(fringe, A)
fringe = fringe - visited
visited = visited.union(fringe)
if max_nodes_per_hop is not None:
if max_nodes_per_hop < len(fringe):
fringe = random.sample(fringe, max_nodes_per_hop)
if len(fringe) == 0:
break
nodes = nodes.union(fringe)
nodes_dist += [dist] * len(fringe)
# move target nodes to top
nodes.remove(ind[0])
nodes.remove(ind[1])
nodes = [ind[0], ind[1]] + list(nodes)
subgraph = A[nodes, :][:, nodes]
# apply node-labeling
labels = node_label(subgraph)
# get node features
features = None
if node_information is not None:
features = node_information[nodes]
# construct nx graph
g = nx.from_scipy_sparse_matrix(subgraph)
# remove link between target nodes
if g.has_edge(0, 1):
g.remove_edge(0, 1)
return g, labels.tolist(), features
def neighbors(fringe, A):
# find all 1-hop neighbors of nodes in fringe from A
res = set()
for node in fringe:
nei, _, _ = ssp.find(A[:, node])
nei = set(nei)
res = res.union(nei)
return res
def node_label(subgraph):
# an implementation of the proposed double-radius node labeling (DRNL)
K = subgraph.shape[0]
subgraph_wo0 = subgraph[1:, 1:]
subgraph_wo1 = subgraph[[0]+range(2, K), :][:, [0]+range(2, K)]
dist_to_0 = ssp.csgraph.shortest_path(subgraph_wo0, directed=False, unweighted=True)
dist_to_0 = dist_to_0[1:, 0]
dist_to_1 = ssp.csgraph.shortest_path(subgraph_wo1, directed=False, unweighted=True)
dist_to_1 = dist_to_1[1:, 0]
d = (dist_to_0 + dist_to_1).astype(int)
d_over_2, d_mod_2 = np.divmod(d, 2)
labels = 1 + np.minimum(dist_to_0, dist_to_1).astype(int) + d_over_2 * (d_over_2 + d_mod_2 - 1)
labels = np.concatenate((np.array([1, 1]), labels))
labels[np.isinf(labels)] = 0
labels[labels>1e6] = 0 # set inf labels to 0
labels[labels<-1e6] = 0 # set -inf labels to 0
return labels
def generate_node2vec_embeddings(A, emd_size=128, negative_injection=False, train_neg=None):
if negative_injection:
row, col = train_neg
A = A.copy()
A[row, col] = 1 # inject negative train
A[col, row] = 1 # inject negative train
nx_G = nx.from_scipy_sparse_matrix(A)
G = node2vec.Graph(nx_G, is_directed=False, p=1, q=1)
G.preprocess_transition_probs()
walks = G.simulate_walks(num_walks=10, walk_length=80)
walks = [map(str, walk) for walk in walks]
model = Word2Vec(walks, size=emd_size, window=10, min_count=0, sg=1,
workers=8, iter=1)
wv = model.wv
embeddings = np.zeros([A.shape[0], emd_size], dtype='float32')
sum_embeddings = 0
empty_list = []
for i in range(A.shape[0]):
if str(i) in wv:
embeddings[i] = wv.word_vec(str(i))
sum_embeddings += embeddings[i]
else:
empty_list.append(i)
mean_embedding = sum_embeddings / (A.shape[0] - len(empty_list))
embeddings[empty_list] = mean_embedding
return embeddings
def AA(A, test_pos, test_neg):
# Adamic-Adar score
A_ = A / np.log(A.sum(axis=1))
A_[np.isnan(A_)] = 0
A_[np.isinf(A_)] = 0
sim = A.dot(A_)
return CalcAUC(sim, test_pos, test_neg)
def CN(A, test_pos, test_neg):
# Common Neighbor score
sim = A.dot(A)
return CalcAUC(sim, test_pos, test_neg)
def CalcAUC(sim, test_pos, test_neg):
pos_scores = np.asarray(sim[test_pos[0], test_pos[1]]).squeeze()
neg_scores = np.asarray(sim[test_neg[0], test_neg[1]]).squeeze()
scores = np.concatenate([pos_scores, neg_scores])
labels = np.hstack([np.ones(len(pos_scores)), np.zeros(len(neg_scores))])
fpr, tpr, _ = metrics.roc_curve(labels, scores, pos_label=1)
auc = metrics.auc(fpr, tpr)
return auc