forked from muhanzhang/IGMC
-
Notifications
You must be signed in to change notification settings - Fork 2
/
util_functions.py
323 lines (287 loc) · 12.4 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
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
from __future__ import print_function
import numpy as np
import random
from tqdm import tqdm
import os, sys, pdb, math, time
from copy import deepcopy
import multiprocessing as mp
import networkx as nx
import argparse
import scipy.io as sio
import scipy.sparse as ssp
import torch
from torch_geometric.data import Data, Dataset, InMemoryDataset
import warnings
warnings.simplefilter('ignore', ssp.SparseEfficiencyWarning)
cur_dir = os.path.dirname(os.path.realpath(__file__))
import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy('file_system')
class SparseRowIndexer:
def __init__(self, csr_matrix):
data = []
indices = []
indptr = []
for row_start, row_end in zip(csr_matrix.indptr[:-1], csr_matrix.indptr[1:]):
data.append(csr_matrix.data[row_start:row_end])
indices.append(csr_matrix.indices[row_start:row_end])
indptr.append(row_end - row_start) # nnz of the row
self.data = np.array(data, dtype=object)
self.indices = np.array(indices, dtype=object)
self.indptr = np.array(indptr, dtype=object)
self.shape = csr_matrix.shape
def __getitem__(self, row_selector):
indices = np.concatenate(self.indices[row_selector])
data = np.concatenate(self.data[row_selector])
indptr = np.append(0, np.cumsum(self.indptr[row_selector]))
shape = [indptr.shape[0] - 1, self.shape[1]]
return ssp.csr_matrix((data, indices, indptr), shape=shape)
class SparseColIndexer:
def __init__(self, csc_matrix):
data = []
indices = []
indptr = []
for col_start, col_end in zip(csc_matrix.indptr[:-1], csc_matrix.indptr[1:]):
data.append(csc_matrix.data[col_start:col_end])
indices.append(csc_matrix.indices[col_start:col_end])
indptr.append(col_end - col_start)
self.data = np.array(data, dtype=object)
self.indices = np.array(indices, dtype=object)
self.indptr = np.array(indptr, dtype=object)
self.shape = csc_matrix.shape
def __getitem__(self, col_selector):
indices = np.concatenate(self.indices[col_selector])
data = np.concatenate(self.data[col_selector])
indptr = np.append(0, np.cumsum(self.indptr[col_selector]))
shape = [self.shape[0], indptr.shape[0] - 1]
return ssp.csc_matrix((data, indices, indptr), shape=shape)
class MyDataset(InMemoryDataset):
def __init__(self, root, A, links, labels, h, sample_ratio, max_nodes_per_hop,
u_features, v_features, class_values, max_num=None, parallel=True):
self.Arow = SparseRowIndexer(A)
self.Acol = SparseColIndexer(A.tocsc())
self.links = links
self.labels = labels
self.h = h
self.sample_ratio = sample_ratio
self.max_nodes_per_hop = max_nodes_per_hop
self.u_features = u_features
self.v_features = v_features
self.class_values = class_values
self.parallel = parallel
self.max_num = max_num
if max_num is not None:
np.random.seed(123)
num_links = len(links[0])
perm = np.random.permutation(num_links)
perm = perm[:max_num]
self.links = (links[0][perm], links[1][perm])
self.labels = labels[perm]
super(MyDataset, self).__init__(root)
self.data, self.slices = torch.load(self.processed_paths[0])
@property
def processed_file_names(self):
name = 'data.pt'
if self.max_num is not None:
name = 'data_{}.pt'.format(self.max_num)
return [name]
def process(self):
# Extract enclosing subgraphs and save to disk
data_list = links2subgraphs(self.Arow, self.Acol, self.links, self.labels, self.h,
self.sample_ratio, self.max_nodes_per_hop,
self.u_features, self.v_features,
self.class_values, self.parallel)
data, slices = self.collate(data_list)
torch.save((data, slices), self.processed_paths[0])
del data_list
class MyDynamicDataset(Dataset):
def __init__(self, root, A, links, labels, h, sample_ratio, max_nodes_per_hop,
u_features, v_features, class_values, max_num=None):
super(MyDynamicDataset, self).__init__(root)
self.Arow = SparseRowIndexer(A)
self.Acol = SparseColIndexer(A.tocsc())
self.links = links
self.labels = labels
self.h = h
self.sample_ratio = sample_ratio
self.max_nodes_per_hop = max_nodes_per_hop
self.u_features = u_features
self.v_features = v_features
self.class_values = class_values
if max_num is not None:
np.random.seed(123)
num_links = len(links[0])
perm = np.random.permutation(num_links)
perm = perm[:max_num]
self.links = (links[0][perm], links[1][perm])
self.labels = labels[perm]
def __len__(self):
return len(self.links[0])
def get(self, idx):
i, j = self.links[0][idx], self.links[1][idx]
g_label = self.labels[idx]
tmp = subgraph_extraction_labeling(
(i, j), self.Arow, self.Acol, self.h, self.sample_ratio, self.max_nodes_per_hop,
self.u_features, self.v_features, self.class_values, g_label
)
return construct_pyg_graph(*tmp)
def links2subgraphs(Arow,
Acol,
links,
labels,
h=1,
sample_ratio=1.0,
max_nodes_per_hop=None,
u_features=None,
v_features=None,
class_values=None,
parallel=True):
# extract enclosing subgraphs
print('Enclosing subgraph extraction begins...')
g_list = []
if not parallel:
with tqdm(total=len(links[0])) as pbar:
for i, j, g_label in zip(links[0], links[1], labels):
tmp = subgraph_extraction_labeling(
(i, j), Arow, Acol, h, sample_ratio, max_nodes_per_hop, u_features,
v_features, class_values, g_label
)
data = construct_pyg_graph(*tmp)
g_list.append(data)
pbar.update(1)
else:
start = time.time()
pool = mp.Pool(mp.cpu_count())
results = pool.starmap_async(
subgraph_extraction_labeling,
[
((i, j), Arow, Acol, h, sample_ratio, max_nodes_per_hop, u_features,
v_features, class_values, g_label)
for i, j, g_label in zip(links[0], links[1], labels)
]
)
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()
end = time.time()
print("Time elapsed for subgraph extraction: {}s".format(end-start))
print("Transforming to pytorch_geometric graphs...")
g_list = []
pbar = tqdm(total=len(results))
while results:
tmp = results.pop()
g_list.append(construct_pyg_graph(*tmp))
pbar.update(1)
pbar.close()
end2 = time.time()
print("Time elapsed for transforming to pytorch_geometric graphs: {}s".format(end2-end))
return g_list
def subgraph_extraction_labeling(ind, Arow, Acol, h=1, sample_ratio=1.0, max_nodes_per_hop=None,
u_features=None, v_features=None, class_values=None,
y=1):
# extract the h-hop enclosing subgraph around link 'ind'
u_nodes, v_nodes = [ind[0]], [ind[1]]
u_dist, v_dist = [0], [0]
u_visited, v_visited = set([ind[0]]), set([ind[1]])
u_fringe, v_fringe = set([ind[0]]), set([ind[1]])
for dist in range(1, h+1):
v_fringe, u_fringe = neighbors(u_fringe, Arow), neighbors(v_fringe, Acol)
u_fringe = u_fringe - u_visited
v_fringe = v_fringe - v_visited
u_visited = u_visited.union(u_fringe)
v_visited = v_visited.union(v_fringe)
if sample_ratio < 1.0:
u_fringe = random.sample(u_fringe, int(sample_ratio*len(u_fringe)))
v_fringe = random.sample(v_fringe, int(sample_ratio*len(v_fringe)))
if max_nodes_per_hop is not None:
if max_nodes_per_hop < len(u_fringe):
u_fringe = random.sample(u_fringe, max_nodes_per_hop)
if max_nodes_per_hop < len(v_fringe):
v_fringe = random.sample(v_fringe, max_nodes_per_hop)
if len(u_fringe) == 0 and len(v_fringe) == 0:
break
u_nodes = u_nodes + list(u_fringe)
v_nodes = v_nodes + list(v_fringe)
u_dist = u_dist + [dist] * len(u_fringe)
v_dist = v_dist + [dist] * len(v_fringe)
subgraph = Arow[u_nodes][:, v_nodes]
# remove link between target nodes
subgraph[0, 0] = 0
# prepare pyg graph constructor input
u, v, r = ssp.find(subgraph) # r is 1, 2... (rating labels + 1)
v += len(u_nodes)
r = r - 1 # transform r back to rating label
num_nodes = len(u_nodes) + len(v_nodes)
node_labels = [x*2 for x in u_dist] + [x*2+1 for x in v_dist]
max_node_label = 2*h + 1
y = class_values[y]
# get node features
if u_features is not None:
u_features = u_features[u_nodes]
if v_features is not None:
v_features = v_features[v_nodes]
node_features = None
if False:
# directly use padded node features
if u_features is not None and v_features is not None:
u_extended = np.concatenate(
[u_features, np.zeros([u_features.shape[0], v_features.shape[1]])], 1
)
v_extended = np.concatenate(
[np.zeros([v_features.shape[0], u_features.shape[1]]), v_features], 1
)
node_features = np.concatenate([u_extended, v_extended], 0)
if False:
# use identity features (one-hot encodings of node idxes)
u_ids = one_hot(u_nodes, Arow.shape[0] + Arow.shape[1])
v_ids = one_hot([x+Arow.shape[0] for x in v_nodes], Arow.shape[0] + Arow.shape[1])
node_ids = np.concatenate([u_ids, v_ids], 0)
#node_features = np.concatenate([node_features, node_ids], 1)
node_features = node_ids
if True:
# only output node features for the target user and item
if u_features is not None and v_features is not None:
node_features = [u_features[0], v_features[0]]
return u, v, r, node_labels, max_node_label, y, node_features
def construct_pyg_graph(u, v, r, node_labels, max_node_label, y, node_features):
u, v = torch.LongTensor(u), torch.LongTensor(v)
r = torch.LongTensor(r)
edge_index = torch.stack([torch.cat([u, v]), torch.cat([v, u])], 0)
edge_type = torch.cat([r, r])
x = torch.FloatTensor(one_hot(node_labels, max_node_label+1))
y = torch.FloatTensor([y])
data = Data(x, edge_index, edge_type=edge_type, y=y)
if node_features is not None:
if type(node_features) == list: # a list of u_feature and v_feature
u_feature, v_feature = node_features
data.u_feature = torch.FloatTensor(u_feature).unsqueeze(0)
data.v_feature = torch.FloatTensor(v_feature).unsqueeze(0)
else:
x2 = torch.FloatTensor(node_features)
data.x = torch.cat([data.x, x2], 1)
return data
def neighbors(fringe, A):
# find all 1-hop neighbors of nodes in fringe from A
return set(A[list(fringe)].indices)
def one_hot(idx, length):
idx = np.array(idx)
x = np.zeros([len(idx), length])
x[np.arange(len(idx)), idx] = 1.0
return x
def PyGGraph_to_nx(data):
edges = list(zip(data.edge_index[0, :].tolist(), data.edge_index[1, :].tolist()))
g = nx.from_edgelist(edges)
g.add_nodes_from(range(len(data.x))) # in case some nodes are isolated
# transform r back to rating label
edge_types = {(u, v): data.edge_type[i].item() for i, (u, v) in enumerate(edges)}
nx.set_edge_attributes(g, name='type', values=edge_types)
node_types = dict(zip(range(data.num_nodes), torch.argmax(data.x, 1).tolist()))
nx.set_node_attributes(g, name='type', values=node_types)
g.graph['rating'] = data.y.item()
return g