-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerateNeg.py
363 lines (314 loc) · 13.8 KB
/
generateNeg.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
import json
import numpy as np
import pandas as pd
import argparse
from tqdm import tqdm
import sys
import os
def get_one_hot(valid_len, tot_len):
return np.concatenate((np.eye(valid_len), np.zeros((valid_len, tot_len-valid_len))), axis=-1)
def preprocess(data_name):
u_list, i_list, ts_list, label_list = [], [], [], []
feat_l = []
idx_list = []
with open(data_name) as f:
s = next(f)
for idx, line in tqdm(enumerate(f)):
e = line.strip().split(',')
u = int(e[0])
i = int(e[1])
ts = float(e[2])
label = int(e[3])
feat = np.array([float(x) for x in e[4:]])
u_list.append(u)
i_list.append(i)
ts_list.append(ts)
label_list.append(label)
idx_list.append(idx)
feat_l.append(feat)
print(ts.max(), ts.min())
exit()
return pd.DataFrame({'u': u_list,
'i':i_list,
'ts':ts_list,
'label':label_list,
'idx':idx_list}), np.array(feat_l)
def reindex(df, jodie_data):
if jodie_data:
upper_u = df.u.max() + 1
new_i = df.i + upper_u
new_df = df.copy()
new_df.i = new_i
new_df.u += 1
new_df.i += 1
new_df.idx += 1
else:
new_df = df.copy()
new_df.u += 1
new_df.i += 1
new_df.idx += 1
return new_df
from numpy.random import default_rng
def add(users, g_df, radio, e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start):
# add N noise interactions (interaction from other user)
for idx,uid in enumerate(users):
N = int(len(g_df[g_df.u == uid])/radio)
or_ids = g_df[g_df.u == uid].idx.values
e_id_u = np.random.randint(g_df.idx.min(), g_df.idx.max(), size=N)
for eid in or_ids:
tid = g_df.loc[g_df.idx == eid,'i'].values[0]
ts= g_df[g_df.idx == eid].ts.values[0]
label = g_df[g_df.idx == eid].label.values[0]
src_l.append(int(u_idx_start))
dst_l.append(int(tid))
ts_l.append(ts)
e_idx_l.append(e_idx_start)
label_l.append(label)
edic[e_idx_start] = eid
e_idx_start += 1
for eid in e_id_u:
tid = g_df.loc[g_df.idx == eid,'i'].values[0]
ts_c = g_df[g_df.u == uid].ts.values
if len(ts_c)==0:
continue
ts = np.random.randint(ts_c.min(), ts_c.max()-1)
src_l.append(int(u_idx_start))
dst_l.append(int(tid))
ts_l.append(ts)
e_idx_l.append(e_idx_start)
label_l.append(0)
edic[e_idx_start] = eid
e_idx_start += 1
u_idx_start += 1
return e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start
def cut(users, g_df, radio, e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start):
# cut off N interactions
for idx,uid in enumerate(users):
ulen = len(g_df[g_df.u == uid].idx.values)
N = int(ulen/radio)
e_id_u = np.random.choice(list(g_df[g_df.u == uid].idx.values), size=N)
g_df_new = g_df[g_df.u == uid].copy()
g_df_new = g_df_new[g_df_new.idx.isin(e_id_u)== False]
tmax = g_df_new.ts.max()
index = g_df_new[g_df_new.ts==tmax].index
g_df_new.loc[index,'label'] = 1
e_id_u = list(g_df_new.idx.values)
for eid in e_id_u:
tid = g_df_new.loc[g_df_new.idx == eid, 'i'].values[0]
ts = g_df_new[g_df_new.idx == eid].ts.values[0]
label = g_df_new[g_df_new.idx == eid].label.values[0]
src_l.append(int(u_idx_start))
dst_l.append(int(tid))
ts_l.append(ts)
e_idx_l.append(e_idx_start)
label_l.append(label)
edic[e_idx_start] = eid
e_idx_start += 1
u_idx_start += 1
return e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start
def replicate(users, g_df, radio, e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic, u_idx_start):
# replicate some links in interaction history
for idx,uid in enumerate(users):
or_ids = g_df[g_df.u == uid].idx.values
for eid in or_ids:
tid = g_df.loc[g_df.idx == eid,'i'].values[0]
ts= g_df[g_df.idx == eid].ts.values[0]
label = g_df[g_df.idx == eid].label.values[0]
src_l.append(int(u_idx_start))
dst_l.append(int(tid))
ts_l.append(ts)
e_idx_l.append(e_idx_start)
label_l.append(label)
edic[e_idx_start] = eid
e_idx_start += 1
N = int(len(g_df[g_df.u == uid])/radio)
e_id_u = np.random.randint(g_df[g_df.u == uid].idx.min(), g_df[g_df.u == uid].idx.max(), size=N)
for eid in e_id_u:
tid = g_df.loc[g_df.idx == eid, 'i'].values[0]
label = g_df[g_df.idx == eid].label.values[0]
ts_c = g_df[g_df.u == uid].ts.values
if len(ts_c)==0:
continue
ts = np.random.randint(ts_c.min(), ts_c.max())
src_l.append(int(u_idx_start))
dst_l.append(int(tid))
ts_l.append(ts)
e_idx_l.append(e_idx_start)
label_l.append(label)
edic[e_idx_start] = eid
e_idx_start += 1
u_idx_start += 1
return e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic, u_idx_start
def reorder(users,g_df, radio, e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start):
# exchange the time of some interactions
for idx,uid in enumerate(users):
N = int(len(g_df[g_df.u == uid])/radio)
g_df_new = g_df[g_df.u == uid].copy()
for i in range(N):
e_id_u = np.random.choice(list(g_df_new.index), size=2)
t0 = g_df_new.loc[e_id_u[0],'ts']
t1 = g_df_new.loc[e_id_u[1],'ts']
g_df_new.loc[e_id_u[0],'ts'] = t1
g_df_new.loc[e_id_u[1],'ts'] = t0
tmax = g_df_new.ts.max()
index = g_df_new[g_df_new.ts==tmax].index
g_df_new.loc[index,'label'] = 1
e_id_u = list(g_df_new.idx.values)
for eid in e_id_u:
tid = g_df_new.loc[g_df_new.idx == eid,'i'].values[0]
ts = g_df_new[g_df_new.idx == eid].ts.values[0]
label = g_df_new[g_df_new.idx == eid].label.values[0]
src_l.append(int(u_idx_start))
dst_l.append(int(tid))
ts_l.append(ts)
e_idx_l.append(e_idx_start)
label_l.append(label)
edic[e_idx_start] = eid
e_idx_start += 1
u_idx_start += 1
return e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start
def replicate_cut(users,g_df, radio, e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start):
#replicate N links and cut N links in interaction history
for idx,uid in enumerate(users):
N = int(len(g_df[g_df.u == uid])/radio)
e_id_u = np.random.randint(g_df[g_df.u == uid].idx.min(), g_df[g_df.u == uid].idx.max(), size=N)
for eid in e_id_u:
tid = g_df.loc[g_df.idx == eid, 'i'].values[0]
label = g_df[g_df.idx == eid].label.values[0]
ts_c = g_df[g_df.u == uid].ts.values
if len(ts_c)==0:
continue
ts = np.random.randint(ts_c.min(), ts_c.max())
src_l.append(int(u_idx_start))
dst_l.append(int(tid))
ts_l.append(ts)
e_idx_l.append(e_idx_start)
label_l.append(label)
edic[e_idx_start] = eid
e_idx_start += 1
g_df_new = g_df[g_df.u == uid].copy()
e_id_u = np.random.choice(list(g_df_new.idx.values), size=N)
g_df_new = g_df_new[g_df_new.idx.isin(e_id_u)== False]
tmax = g_df_new.ts.max()
index = g_df_new[g_df_new.ts==tmax].index
g_df_new.loc[index,'label'] = 1
e_id_u = list(g_df_new.idx.values)
for eid in e_id_u:
tid = g_df_new.loc[g_df_new.idx == eid, 'i'].values[0]
ts = g_df_new[g_df_new.idx == eid].ts.values[0]
label = g_df_new[g_df_new.idx == eid].label.values[0]
src_l.append(int(u_idx_start))
dst_l.append(int(tid))
ts_l.append(ts)
e_idx_l.append(e_idx_start)
label_l.append(label)
edic[e_idx_start] = eid
e_idx_start += 1
u_idx_start += 1
return e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic, u_idx_start
def add_cut(users,g_df, radio, e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start):
#add N links and cut N links in interaction history
for idx,uid in enumerate(users):
N = int(len(g_df[g_df.u == uid])/radio)
e_id_u = np.random.randint(g_df.idx.min(), g_df.idx.max(), size=N)
for eid in e_id_u:
tid = g_df.loc[g_df.idx == eid,'i'].values[0]
ts_c = g_df[g_df.u == uid].ts.values
if len(ts_c)==0:
continue
ts = np.random.randint(ts_c.min(), ts_c.max()-1)
src_l.append(int(u_idx_start))
dst_l.append(int(tid))
ts_l.append(ts)
e_idx_l.append(e_idx_start)
label_l.append(0)
edic[e_idx_start] = eid
e_idx_start += 1
g_df_new = g_df[g_df.u == uid].copy()
e_id_u = np.random.choice(list(g_df_new.idx.values), size=N)
g_df_new = g_df_new[g_df_new.idx.isin(e_id_u)== False]
tmax = g_df_new.ts.max()
index = g_df_new[g_df_new.ts==tmax].index
g_df_new.loc[index,'label'] = 1
e_id_u = list(g_df_new.idx.values)
for eid in e_id_u:
tid = g_df_new.loc[g_df_new.idx == eid, 'i'].values[0]
ts = g_df_new[g_df_new.idx == eid].ts.values[0]
label = g_df_new[g_df_new.idx == eid].label.values[0]
src_l.append(int(u_idx_start))
dst_l.append(int(tid))
ts_l.append(ts)
e_idx_l.append(e_idx_start)
label_l.append(label)
edic[e_idx_start] = eid
e_idx_start += 1
u_idx_start += 1
return e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic, u_idx_start
import random
def run(args):
data_name = args.dataset
# g_df = pd.read_csv('./data/{}.csv'.format(data_name))
g_df = pd.read_csv('./data/ml_{}.csv'.format(data_name))
if os.path.isfile('./{}_split.npy'.format(data_name)):
print('load ./{}_split.npy'.format(data_name))
src_data = np.load('./{}_split.npy'.format(data_name))
val_idx, test_idx = int(len(src_data)*0.6),int(len(src_data)*0.75)
src_train, src_val, src_test = src_data[:val_idx], src_data[val_idx: test_idx], src_data[test_idx:]
else:
print('Random spliting data')
src_data = list(set(g_df.u.values))
print(len(src_data))
random.shuffle(src_data)
src_data = np.array(src_data)
np.save( './{}_split.npy'.format(data_name),src_data)
print('Please run again')
exit()
src_l = []
dst_l = []
e_idx_l =[]
label_l = []
ts_l = []
print('g_df', len(g_df))
e_idx_start = max(list(g_df.idx.values))+1
u_idx_start = max(g_df.u.values.max(), g_df.i.values.max())+1
print(u_idx_start)
users = set(g_df[g_df.label==1].u.values) & set(src_train)
rnum = len(src_train)-len(users)
print('normal user {}, abnormal user {}'.format(rnum, len(users)))
gnum = rnum-len(users)
if gnum > 6*len(users):
gnum = 6*len(users)
if gnum<len(users)*0.1:
print('no need to generate data')
exit()
print('generate {} users'.format(gnum))
edic = dict()
e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start = add(users, g_df, 1, e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start)
e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start = cut(users, g_df, 1, e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start)
e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start = replicate(users, g_df, 1, e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start)
e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start = reorder(users, g_df, 1, e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start)
e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start = replicate_cut(users, g_df, 2, e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start)
e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start = add_cut(users, g_df, 2, e_idx_start,src_l,dst_l,ts_l,e_idx_l,label_l,edic,u_idx_start)
new_df = pd.DataFrame({'u': src_l, 'i':dst_l, 'ts':ts_l, 'label':label_l,'idx':e_idx_l})
users = set(new_df[new_df.label == 1].u.values)
print('user number ', len(users))
if os.path.isfile('./data/ml_{}.npy'.format(data_name)):
e_feat = np.load('./data/ml_{}.npy'.format(data_name))
feat_new = np.zeros((e_idx_start+1, len(e_feat[0])))
for i in range(len(e_feat)):
feat_new[i]= e_feat[i]
for u,i in edic.items():
feat_new[u] = e_feat[i]
np.save('./data/ml_{}_cont_full.npy'.format(data_name), feat_new)
new_df.to_csv('./data/{}_cont_full.csv'.format(data_name), index=False)
parser = argparse.ArgumentParser('Interface for propressing csv source data for TGAT framework')
parser.add_argument('--dataset', help='specify one dataset')
parser.add_argument('--node_edge_feat_dim', default=172, help='number of dimensions for 0-padded node and edge features')
parser.add_argument('--one-hot-node', type=bool, default=False,
help='using one hot embedding for node (which means inductive learning is impossible though)')
try:
args = parser.parse_args()
except:
parser.print_help()
sys.exit(0)
run(args)