-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
308 lines (203 loc) · 8.96 KB
/
utils.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
"""
Author: Shadi Zabad
Date: April 2020
Note: Some of the operations here are copied from the examples/utilities code of torch_geometric.
"""
from multiprocessing import Pool
import networkx as nx
import numpy as np
import torch
import torch_geometric as tg
import math
import random
from parallel_betweenness import betweenness_centrality_parallel
import _pickle as cPickle
import bz2
import glob
import os
import errno
def add_negative_anchor_edge_index(tg_dataset):
for i, g1 in enumerate(tg_dataset):
for j, g2 in enumerate(tg_dataset):
if i != j:
g1.anchor_data[g2.gidx]['negative_anchor_edge_index'] = obtain_negative_anchors(
g1.x.size(0),
g2.x.size(0),
g1.anchor_data[g2.gidx]['anchor_edge_index']
)
def pickle_data(data, output_file):
with bz2.BZ2File(output_file, 'w') as f:
cPickle.dump(data, f)
def unpickle_data(input_file):
with bz2.BZ2File(input_file, 'rb') as inpf:
return cPickle.load(inpf)
def save_tg_dataset(dataset, output_dir):
for g1 in dataset:
fname = os.path.join(output_dir, g1.graph_name + '.pbz2')
print("Writing data file:", fname)
pickle_data(g1, fname)
def load_tg_dataset(dataset_dir):
tg_data = []
for fname in glob.glob(os.path.join(dataset_dir, '*.pbz2')):
tg_data.append(unpickle_data(fname))
return tg_data
def make_dirs(new_dir):
try:
os.makedirs(new_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
def select_top_k_anchors(tg_data, k=3, normalize_dist=True):
for g1 in tg_data:
for g2 in g1.anchor_data.keys():
ei_nodes = [] # edge index nodes
ei_anchors = [] # edge index anchors
sp_dist = [] # short path distance
for i, (eian, sp) in g1.anchor_data[g2]['closest_anchor_data'].items():
ei_nodes += [i]*len(eian[:k])
ei_anchors += eian[:k]
dist = sp[:k]
if normalize_dist:
dist = list(np.array(dist) / (sum(dist)))
sp_dist += dist
g1.anchor_data[g2]['closest_anchors'] = torch.stack([torch.Tensor(ei_nodes),
torch.Tensor(ei_anchors)], dim=0).to(torch.long)
g1.anchor_data[g2]['sp_dist'] = torch.Tensor(sp_dist).reshape(-1, 1)
return tg_data
def get_anchor_link_labels(pos_anchor_idx, neg_anchor_idx, neg_label=0):
link_labels = torch.ones(pos_anchor_idx.size(1) +
neg_anchor_idx.size(1)).float()
link_labels *= neg_label
link_labels[:pos_anchor_idx.size(1)] = 1.
return link_labels
def get_central_nodes_in_neighborhood(g, n, q_hops=3):
q_order_neighb = nx.single_source_shortest_path_length(g, n, cutoff=q_hops)
sorted_nodes = sorted(q_order_neighb.items(),
key=lambda x: g.graph['centrality'][x[0]],
reverse=True)
return [(list(g.nodes()).index(an), d) for an, d in sorted_nodes]
def get_closest_anchors_node(g, n, anchors, shuffle=True):
anchor_dist = []
if len(anchors) > 0:
for an in anchors:
try:
dist = nx.shortest_path_length(g, source=n, target=an)
except nx.exception.NetworkXNoPath:
dist = np.inf
anchor_dist.append((list(g.nodes()).index(an), dist))
if shuffle:
# Shuffle randomly to account for collisions
np.random.shuffle(anchor_dist)
anchor_dist = sorted(anchor_dist, key=lambda x: x[1])
if all([np.isinf(d) for _, d in anchor_dist]):
anchor_dist = get_central_nodes_in_neighborhood(g, n)
else:
anchor_dist = get_central_nodes_in_neighborhood(g, n)
anchor_idx, dist = [list(t) for t in zip(*anchor_dist)]
dist = list(1. / (np.array(dist) + 1))
return {
list(g.nodes()).index(n): (anchor_idx, dist)
}
def get_closest_anchors(g, anchors, shuffle=True, n_proc=5):
pool = Pool(n_proc)
res = pool.starmap(get_closest_anchors_node, [(g, n, anchors, shuffle) for n in g.nodes()])
pool.close()
pool.join()
return {k: v for d in res for k, v in d.items()}
def sample_negative_anchors(neg_anchor_edge_index, num_negative):
perm = random.sample(range(neg_anchor_edge_index.size(1)),
min(num_negative, neg_anchor_edge_index.size(1)))
perm = torch.tensor(perm).to(torch.long)
return neg_anchor_edge_index[:, perm]
def obtain_negative_anchors(g1_n_nodes, g2_n_nodes, positive_anchors):
row, col = positive_anchors
neg_adj_mask = torch.ones(g1_n_nodes, g2_n_nodes, dtype=torch.uint8)
neg_adj_mask = neg_adj_mask.triu(diagonal=1).to(torch.bool)
neg_adj_mask[row, col] = 0
return neg_adj_mask.nonzero().t().to(torch.long)
def get_positive_anchors(g1, g2, test_ratio, shuffle=True):
"""
:param g1: networkx graph 1
:param g2: networkx graph 2
:param test_ratio: test split ratio
:param shuffle: flag to shuffle the anchor nodes
"""
positive_anchors = list(set(g1.nodes()).intersection(set(g2.nodes())))
g1_ei = [list(g1.nodes()).index(n) for n in positive_anchors]
g2_ei = [list(g2.nodes()).index(n) for n in positive_anchors]
anchor_ei = torch.stack([torch.Tensor(g1_ei), torch.Tensor(g2_ei)], dim=0).to(torch.long)
if shuffle:
perm = torch.tensor(random.sample(range(anchor_ei.size(1)), anchor_ei.size(1))).to(torch.long)
positive_anchors = list(np.array(positive_anchors)[perm.numpy()])
anchor_ei = anchor_ei[:, perm]
if test_ratio is None or test_ratio == 0.0:
return {
'positive_anchors': positive_anchors,
'train_positive_anchors': positive_anchors,
'test_positive_anchors': positive_anchors,
'anchor_edge_index': anchor_ei,
'train_anchor_edge_index': anchor_ei,
'test_anchor_edge_index': anchor_ei
}
else:
n_t = int(math.floor(test_ratio*len(positive_anchors)))
train_positive_anchors, test_positive_anchors = positive_anchors[n_t:], positive_anchors[:n_t]
train_ei, test_ei = anchor_ei[:, n_t:], anchor_ei[:, :n_t]
return {
'positive_anchors': positive_anchors,
'train_positive_anchors': train_positive_anchors,
'test_positive_anchors': test_positive_anchors,
'anchor_edge_index': anchor_ei,
'train_anchor_edge_index': train_ei,
'test_anchor_edge_index': test_ei
}
def from_nx_to_tg_graphs(graphs, attributes=None, test_ratio=0.8, ntp_ratio=2, normalize_dist=True):
print("> Transforming the data...")
tg_graphs = []
for i, g1 in enumerate(graphs):
if g1.graph['centrality'] is None:
g1.graph['centrality'] = betweenness_centrality_parallel(
g1, processes=min(5, max(2, g1.number_of_nodes() // 1000))
)
pickle_data(g1.graph['centrality'], g1.graph['centrality_file'])
tg_g1 = tg.utils.from_networkx(g1)
try:
tg_g1['graph_name'] = g1.graph['name']
except KeyError:
tg_g1['graph_name'] = 'g' + str(i)
tg_g1['gidx'] = 'g' + str(i)
tg_g1['anchor_data'] = {}
# If the graph is not attributed, add the node degrees as attributes:
if attributes is None:
tg_g1.x = torch.Tensor(list(dict(g1.degree()).values())).reshape(-1, 1)
else:
tg_g1.x = torch.Tensor(attributes[i])
for j, g2 in enumerate(graphs):
print(i, j)
if j == i:
continue
if j < i:
tg_g1['anchor_data']['g' + str(j)] = {}
for k, v in tg_graphs[j]['anchor_data']['g' + str(i)].items():
if 'positive_anchors' in k:
tg_g1['anchor_data']['g' + str(j)][k] = v
elif 'anchor_edge_index' in k:
tg_g1['anchor_data']['g' + str(j)][k] = v[[-1, 0], :]
if j > i:
tg_g1['anchor_data']['g' + str(j)] = get_positive_anchors(g1, g2, test_ratio)
negative_anchor_edge_index = obtain_negative_anchors(
g1.number_of_nodes(),
g2.number_of_nodes(),
tg_g1['anchor_data']['g' + str(j)]['anchor_edge_index']
)
tg_g1['anchor_data']['g' + str(j)]['test_negative_anchor_edge_index'] = sample_negative_anchors(
negative_anchor_edge_index,
tg_g1['anchor_data']['g' + str(j)]['test_anchor_edge_index'].size(1)*ntp_ratio
)
tg_g1['anchor_data']['g' + str(j)]['closest_anchor_data'] = get_closest_anchors(
g1,
tg_g1['anchor_data']['g' + str(j)]['train_positive_anchors'],
normalize_dist
)
tg_graphs.append(tg_g1)
return tg_graphs