-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdata_tlp_cite.py
425 lines (352 loc) · 18.1 KB
/
data_tlp_cite.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# coding: utf-8
# author: lu yf
# create date: 2018/11/12
# We can debug at this file
from __future__ import division
from torch.utils.data import Dataset
import numpy as np
import sys
import random
import copy
import torch
from sklearn import preprocessing
import seaborn as sns
import matplotlib.pyplot as plt
# np.random.seed(1)
class DataHelper_t(Dataset):
def __init__(self, file_path, node_feature_path, neg_size, hist_len, directed=False, transform=None, tlp_flag=False):
self.node2hist = dict()
self.neg_size = neg_size # 5
self.hist_len = hist_len # 5
self.directed = directed
self.transform = transform
# self.max_d_time = -sys.maxint # Time interval [0, T]
self.max_d_time = -sys.maxsize # 1.0
self.NEG_SAMPLING_POWER = 0.75
self.neg_table_size = int(1e8) # 10^8
self.all_edge_index = []
self.test_edge_index = []
self.node_time_nodes = dict()
self.node_set = set()
self.degrees = dict()
self.edge_list = []
self.node_rate = {}
self.edge_rate = {}
self.node_sum = {}
self.edge_sum = {}
self.time_stamp = []
self.time_edges_dict = {}
self.time_nodes_dict = {}
print('loading data...')
infile = torch.load(file_path)
for i in range(infile.size(0)):
s_node = int(infile[i][0].item()) # source node
t_node = int(infile[i][1].item()) # target node
d_time = float(infile[i][2].item()) # time slot, delta t
self.node_set.update([s_node, t_node])
if s_node not in self.degrees:
self.degrees[s_node] = 0
if t_node not in self.degrees:
self.degrees[t_node] = 0
self.all_edge_index.append([s_node, t_node])
if s_node not in self.node2hist: # node2hist {node: [(historical neighbor, time)], ……}
self.node2hist[s_node] = list()
if not directed: # undirected
if t_node not in self.node2hist:
self.node2hist[t_node] = list()
if s_node not in self.node_time_nodes:
self.node_time_nodes[s_node] = dict() # for the new added s_node, create a dict for it
if d_time not in self.node_time_nodes[s_node]:
self.node_time_nodes[s_node][d_time] = list() # for the new time,
self.node_time_nodes[s_node][d_time].append(t_node)
if not directed: # undirected
if t_node not in self.node_time_nodes:
self.node_time_nodes[t_node] = dict()
if d_time not in self.node_time_nodes[t_node]:
self.node_time_nodes[t_node][d_time] = list()
self.node_time_nodes[t_node][d_time].append(s_node)
if d_time > self.max_d_time:
self.max_d_time = d_time # record the max time
self.degrees[s_node] += 1 # node degree
self.degrees[t_node] += 1
self.time_stamp.append(d_time)
self.edge_list.append((s_node, t_node, d_time)) # edge list
if not directed:
self.edge_list.append((t_node, s_node, d_time))
if tlp_flag:
if d_time >= 1.0:
self.test_edge_index.append([s_node, t_node])
continue
self.node2hist[s_node].append((t_node, d_time))
if not directed:
self.node2hist[t_node].append((s_node, d_time)) # because undirected, so add the inverse version
nodes_list = list(self.node_set)
self.max_node = np.max(nodes_list) + 1
# self.degree_features = np.eye(self.max_node)[nodes_list]
# print("degree_features", degree_features[0:5])
self.test_edge_index = np.array(self.test_edge_index).T
self.time_stamp = sorted(list(set(self.time_stamp))) # !!! time from 0 to 1
self.node_dim = len(self.node_set) # number of nodes 28085
self.data_size = 0
for s in self.node2hist:
hist = self.node2hist[s]
hist = sorted(hist,
key=lambda x: x[1]) # from past(0) to now(1). This supports the events ranked in time order.
self.node2hist[s] = hist
self.data_size += len(self.node2hist[s])
self.max_nei_len = max(map(lambda x: len(x), self.node2hist.values()))
# print('#nodes: {}, #edges: {}, #all time_stamp: {}'.
# format(self.node_dim, len(self.edge_list), len(self.time_stamp)))
# print('avg. degree: {}'.format(sum(self.degrees.values()) / len(self.degrees)))
# print('max neighbors length: {}'.format(self.max_nei_len))
self.idx2source_id = np.zeros((self.data_size,), dtype=np.int32)
self.idx2target_id = np.zeros((self.data_size,), dtype=np.int32)
idx = 0
for s_node in self.node2hist:
for t_idx in range(len(self.node2hist[
s_node])): # Note the range here, which means from 0 to the number of historical neighbors
self.idx2source_id[
idx] = s_node
self.idx2target_id[
idx] = t_idx
idx += 1
print('init. neg_table...')
self.neg_table = np.zeros((self.neg_table_size,))
self.init_neg_table()
self.node_features = torch.load(node_feature_path).numpy()
self.node_features = preprocessing.StandardScaler().fit_transform(self.node_features)
def get_node_dim(self):
return self.node_dim
def get_max_d_time(self):
return self.max_d_time
def init_neg_table(self):
tot_sum, cur_sum, por = 0., 0., 0.
n_id = 0
for k in range(self.node_dim):
tot_sum += np.power(self.degrees[k],
self.NEG_SAMPLING_POWER) # total number of negtive sampling += (node degree of node i)^0.75
for k in range(self.neg_table_size):
if (k + 1.) / self.neg_table_size > por:
cur_sum += np.power(self.degrees[n_id], self.NEG_SAMPLING_POWER)
por = cur_sum / tot_sum
n_id += 1
self.neg_table[k] = n_id - 1 # negtive table size = 1e8, element inside range from 1~number of total nodes
def __len__(self):
return len(self.test_edge_index.T)
def __getitem__(self, idx):
# sampling via htne
s_node = self.test_edge_index[:, idx][0]
t_node = self.test_edge_index[:, idx][1]
e_time = 1.0
if len(self.node2hist[s_node]) == 0:
s_his_nodes = np.array(s_node).repeat(self.hist_len)
s_his_times = np.array(e_time).repeat(self.hist_len)
else:
if len(self.node2hist[s_node]) < self.hist_len:
s_nei_idx = np.random.choice(len(self.node2hist[s_node]), self.hist_len, replace=True)
else:
s_nei_idx = np.random.choice(len(self.node2hist[s_node]), self.hist_len, replace=False)
s_his = [(self.node2hist[s_node])[i] for i in s_nei_idx]
s_his_nodes = [h[0] for h in s_his]
s_his_times = np.array([h[1] for h in s_his])
if len(self.node2hist[t_node]) == 0:
t_his_nodes = np.array(t_node).repeat(self.hist_len)
t_his_times = np.array(e_time).repeat(self.hist_len)
else:
if len(self.node2hist[t_node]) < self.hist_len:
t_nei_idx = np.random.choice(len(self.node2hist[t_node]), self.hist_len, replace=True)
else:
t_nei_idx = np.random.choice(len(self.node2hist[t_node]), self.hist_len, replace=False)
t_his = [(self.node2hist[t_node])[i] for i in t_nei_idx]
t_his_nodes = [h[0] for h in t_his]
t_his_times = np.array([h[1] for h in t_his])
s_his_tidx_list = []
for i in range(len(s_his_nodes)):
s_node_his = self.node2hist[s_his_nodes[i]]
if s_node_his == []:
s_his_tidx_list.append(0)
elif np.array([list(i) for i in s_node_his])[:, 1][0] > s_his_times[i]:
s_his_tidx_list.append(0)
else:
his_tidx = np.argwhere(np.array([list(i) for i in s_node_his])[:, 1] <= s_his_times[i])[-1].item()
s_his_tidx_list.append(his_tidx)
s_his_his_nodes_list = []
s_his_his_times_list = []
for i in range(len(s_his_nodes)):
if len(self.node2hist[s_his_nodes[i]][:s_his_tidx_list[i]]) == 0:
s_his_his_nodes = np.array(s_his_nodes[i]).repeat(self.hist_len)
s_his_his_times = np.array(s_his_times[i]).repeat(self.hist_len)
else:
if s_his_tidx_list[i] < self.hist_len:
s_his_nei_idx = np.random.choice(np.arange(
len(self.node2hist[s_his_nodes[i]][:s_his_tidx_list[i]])), self.hist_len, replace=True)
else:
s_his_nei_idx = np.random.choice(np.arange(len(self.node2hist[s_his_nodes[i]][:s_his_tidx_list[i]])), self.hist_len, replace=False)
s_his_his = [(self.node2hist[s_his_nodes[i]][:s_his_tidx_list[i]])[a] for a in s_his_nei_idx]
s_his_his_nodes = [h[0] for h in s_his_his]
s_his_his_times = [h[1] for h in s_his_his]
s_his_his_times_list.append(s_his_his_times)
s_his_his_nodes_list.append(s_his_his_nodes)
s_his_his_times_list = np.array(s_his_his_times_list)
s_his_his_nodes_list = np.array(s_his_his_nodes_list).astype(int)
t_his_tidx_list = []
for i in range(len(t_his_nodes)):
t_node_his = self.node2hist[t_his_nodes[i]]
if t_node_his == []:
t_his_tidx_list.append(0)
elif np.array([list(i) for i in t_node_his])[:, 1][0] > t_his_times[i]:
t_his_tidx_list.append(0)
else:
his_tidx = np.argwhere(np.array([list(i) for i in t_node_his])[:, 1] <= t_his_times[i])[-1].item()
t_his_tidx_list.append(his_tidx+1)
t_his_his_nodes_list = []
t_his_his_times_list = []
for i in range(len(t_his_nodes)):
if len(self.node2hist[t_his_nodes[i]][:t_his_tidx_list[i]]) == 0:
t_his_his_nodes = np.array(t_his_nodes[i]).repeat(self.hist_len)
t_his_his_times = np.array(t_his_times[i]).repeat(self.hist_len)
else:
if t_his_tidx_list[i] < self.hist_len:
t_his_nei_idx = np.random.choice(np.arange(
len(self.node2hist[t_his_nodes[i]][:t_his_tidx_list[i]])), self.hist_len, replace=True)
else:
t_his_nei_idx = np.random.choice(np.arange(
len(self.node2hist[t_his_nodes[i]][:t_his_tidx_list[i]])), self.hist_len, replace=False)
t_his_his = [(self.node2hist[t_his_nodes[i]][:t_his_tidx_list[i]])[a] for a in t_his_nei_idx]
t_his_his_nodes = [h[0] for h in t_his_his]
t_his_his_times = [h[1] for h in t_his_his]
t_his_his_times_list.append(t_his_his_times)
t_his_his_nodes_list.append(t_his_his_nodes)
t_his_his_times_list = np.array(t_his_his_times_list)
t_his_his_nodes_list = np.array(t_his_his_nodes_list).astype(int)
# negtive_sampling part
neg_s_nodes = self.negative_sampling().astype(int)
neg_tidx_list = []
for i in range(self.neg_size):
neg_node = self.node2hist[neg_s_nodes[i]]
if neg_node == []:
neg_tidx_list.append(0)
elif np.array([list(i) for i in neg_node])[:, 1][0] > e_time:
neg_tidx_list.append(0)
else:
his_tidx = np.argwhere(np.array([list(i) for i in neg_node])[:, 1] <= e_time)[-1].item()
neg_tidx_list.append(his_tidx+1)
neg_his_nodes_list = []
neg_his_times_list = []
# neg_his_len = []
for i in range(self.neg_size):
if len(self.node2hist[neg_s_nodes[i]][:neg_tidx_list[i]]) == 0:
neg_his_nodes = np.array(neg_s_nodes[i]).repeat(self.hist_len)
neg_his_times = np.array(e_time).repeat(self.hist_len)
else:
if neg_tidx_list[i] < self.hist_len:
neg_nei_idx = np.random.choice(len(self.node2hist[neg_s_nodes[i]][:neg_tidx_list[i]]), self.hist_len, replace=True)
else:
neg_nei_idx = np.random.choice(len(self.node2hist[neg_s_nodes[i]][:neg_tidx_list[i]]), self.hist_len, replace=False)
neg_his = [(self.node2hist[neg_s_nodes[i]][:neg_tidx_list[i]])[a] for a in neg_nei_idx]
neg_his_nodes = [h[0] for h in neg_his]
neg_his_times = [h[1] for h in neg_his]
neg_his_nodes_list.append(neg_his_nodes)
neg_his_times_list.append(neg_his_times)
# neg_his_len.append(len(neg_his))
neg_his_nodes_list = np.array(neg_his_nodes_list).astype(int)
neg_his_times_list = np.array(neg_his_times_list)
neg_his_idx_list = []
for i in range(self.neg_size):
his_idx_list = []
for j in range(self.hist_len):
neg_node_his = self.node2hist[neg_his_nodes_list[i][j]]
if neg_node_his ==[]:
his_idx = 0
elif np.array([list(i) for i in neg_node_his])[:, 1][0] > e_time:
his_idx = 0
else:
his_idx = np.argwhere(np.array([list(i) for i in neg_node_his])[:, 1] <= neg_his_times_list[i][j])[-1].item()
his_idx_list.append(his_idx+1)
neg_his_idx_list.append(his_idx_list)
neg_his_idx_list = np.array(neg_his_idx_list)
neg_his_his_nodes_list = []
neg_his_his_times_list = []
for i in range(self.neg_size):
neg_h_h_nodes_list = []
neg_h_h_times_list = []
for j in range(self.hist_len):
if len(self.node2hist[neg_his_nodes_list[i][j]][0:neg_his_idx_list[i][j]]) == 0:
neg_his_his_nodes = np.array(neg_his_nodes_list[i][j]).repeat(self.hist_len)
neg_his_his_times = np.array(e_time).repeat(self.hist_len)
else:
if neg_his_idx_list[i][j] < self.hist_len:
neg_his_his_idx = np.random.choice(len(self.node2hist[neg_his_nodes_list[i][j]][0:neg_his_idx_list[i][j]]), self.hist_len, replace=True)
else:
neg_his_his_idx = np.random.choice(len(self.node2hist[neg_his_nodes_list[i][j]][0:neg_his_idx_list[i][j]]), self.hist_len, replace=False)
neg_his_his = [(self.node2hist[neg_his_nodes_list[i][j]][0:neg_his_idx_list[i][j]])[a] for a in neg_his_his_idx]
neg_his_his_nodes = [h[0] for h in neg_his_his]
neg_his_his_times = [h[1] for h in neg_his_his]
neg_h_h_nodes_list.append(neg_his_his_nodes)
neg_h_h_times_list.append(neg_his_his_times)
neg_h_h_nodes_list = np.array(neg_h_h_nodes_list)
neg_h_h_times_list = np.array(neg_h_h_times_list)
neg_his_his_nodes_list.append(neg_h_h_nodes_list)
neg_his_his_times_list.append(neg_h_h_times_list)
neg_his_his_nodes_list = np.array(neg_his_his_nodes_list).astype(int)
neg_his_his_times_list = np.array(neg_his_his_times_list)
s_self_feat = self.node_features[s_node]
s_one_hop_feat = self.node_features[s_his_nodes]
s_two_hop_feat = []
for i in range(self.hist_len):
s_two_feat = self.node_features[s_his_his_nodes_list[i]]
s_two_hop_feat.append(s_two_feat)
s_two_hop_feat = np.array(s_two_hop_feat)
t_self_feat = self.node_features[t_node]
t_one_hop_feat = self.node_features[t_his_nodes]
t_two_hop_feat = []
for i in range(self.hist_len):
t_two_feat = self.node_features[t_his_his_nodes_list[i]]
t_two_hop_feat.append(t_two_feat)
t_two_hop_feat = np.array(t_two_hop_feat)
neg_self_feat = self.node_features[neg_s_nodes]
neg_one_hop_feat = []
for i in range(self.neg_size):
neg_one_feat = self.node_features[neg_his_nodes_list[i]]
neg_one_hop_feat.append(neg_one_feat)
neg_one_hop_feat = np.array(neg_one_hop_feat)
neg_two_hop_feat = []
for i in range(self.neg_size):
neg_two_h_feat = []
for j in range(self.hist_len):
neg_t_h_f = self.node_features[neg_his_his_nodes_list[i][j]]
neg_two_h_feat.append(neg_t_h_f)
neg_two_h_feat = np.array(neg_two_h_feat)
neg_two_hop_feat.append(neg_two_h_feat)
neg_two_hop_feat = np.array(neg_two_hop_feat)
s_edge_rate = len(self.node_time_nodes[s_node][e_time])
sample = {
's_node': s_node, # e.g., 5424
't_node': t_node, # e.g., 5427
'neg_s_nodes':neg_s_nodes,
'event_time': e_time,
's_history_times': s_his_times, # e.g., [0.88462, 0.88462, 0.92308, 0.92308, 0.92308]
't_history_times': t_his_times,
's_his_his_times_list': s_his_his_times_list,
't_his_his_nodes_list': t_his_his_nodes_list,
't_his_his_times_list': t_his_his_times_list,
's_self_feat': s_self_feat,
's_one_hop_feat': s_one_hop_feat,
's_two_hop_feat': s_two_hop_feat,
't_self_feat': t_self_feat,
't_one_hop_feat': t_one_hop_feat,
't_two_hop_feat': t_two_hop_feat,
'neg_his_times_list': neg_his_times_list,
'neg_his_his_times_list': neg_his_his_times_list,
'neg_self_feat': neg_self_feat,
'neg_one_hop_feat': neg_one_hop_feat,
'neg_two_hop_feat': neg_two_hop_feat,
's_edge_rate': s_edge_rate,
}
if self.transform:
sample = self.transform(sample)
return sample
def negative_sampling(self):
rand_idx = np.random.randint(0, self.neg_table_size, (self.neg_size,))
sampled_nodes = self.neg_table[rand_idx]
return sampled_nodes