-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_processor.py
More file actions
469 lines (404 loc) · 17.7 KB
/
data_processor.py
File metadata and controls
469 lines (404 loc) · 17.7 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import numpy as np
import random
import time
def binary_representation_from_int(number, max_size=16):
""" Returns a binary number from an integer with bits set to max size """
return format(number,'b').zfill(max_size)
def numpy_binary(number, max_size = 16):
"""
Returns a binary representation of a given number
:param number: needs conversion to binary representation
:param max_size: max number of bits of the binary representation
:return: binary representation
"""
binary_str = binary_representation_from_int(number, max_size)
arr = list(map(int, binary_str))
return np.array(arr)
def read_star_graph_pattern(d, b, n, e, file_name, train_tuples = 10000000, matrix_mode = 0, star_mode = 0):
"""
Creates encoding from queries in the file
:param d: int, the number of distinct nodes (subjects + objects) in KG
:param b: int, the number of distinct edges (predicates) in KG
:param n: int, the number of nodes in the subgraph
:param e: int, the number of edges in the subgraph
:param file_name: str, the input file name of the queries used for training or testing
:param train_tuples: int, limits the training sample size, in default we train on all of them
:param matrix_mode: 0, 1 being two matrices 2 and 3 being with 3 matrices as reported in paper, one-hot and binary encoding
:param star_mode:
:return: encoded queries
"""
print("started reading and creating star patterns")
star_node_id = 0
star_predicate_id = 0
# we need to add one node for star
X=[]
A=[]
E=[]
y=[]
total_predicate_star = 0
with open(file_name) as fp:
line_nb = 0
lines = fp.readlines()
# We need the shuffle in cases where we want to take a subset of the data
# random.shuffle(lines)
# for line in lines:
time_start = time.time()
for line in lines:
if line_nb == train_tuples:
print("We are going out due to training size limitation")
break
patterns, cardinality = line.split(":")
cardinality = int(cardinality)
if "*-" in patterns:
total_predicate_star += 1
if (line_nb % 100000) == 0:
print(line_nb)
line_nb += 1
patterns = patterns.split(",")
if matrix_mode == 2:
x = np.zeros((n, d), dtype='uint8')
ep = np.zeros((e, b), dtype='uint8')
a = np.zeros((e, n, n), dtype='uint8')
elif matrix_mode == 3:
bits_d = int(np.ceil(np.log2(d))) + 1
bits_b = int(np.ceil(np.log2(b))) + 1
x = np.zeros((n, bits_d), dtype='uint8')
ep = np.zeros((e, bits_b), dtype='uint8')
a = np.zeros((e, n, n), dtype='uint8')
else:
print("Matrix mode unspecified")
exit(1)
nodes = set()
# Only the subject will be added as a star node
nodes.add("*")
total_zero = 0
predicates = set()
pattern_zero_flag = False
predicate_node_counter = 1
for pattern in patterns:
predicate, object = pattern.split("-")
if object != "*":
if int(object) == 0:
pattern_zero_flag = True
else:
nodes.add(object)
else:
nodes.add("*n"+str(predicate))
star_node_id += 1
if pattern_zero_flag:
break
if predicate != "*":
if int(predicate) == 0:
pattern_zero_flag = True
else:
predicates.add(predicate)
else:
predicates.add("*p"+str(object))
predicate_node_counter += 1
if pattern_zero_flag:
break
if pattern_zero_flag:
continue
y.append(cardinality)
if pattern_zero_flag:
continue
if total_zero > 0:
print("Total zero "+str(total_zero))
exit(1)
nodes=list(nodes)
nodes.sort()
id = 0
nodes_dict = dict()
for node in nodes:
nodes_dict[node]=id
id += 1
predicates = list(predicates)
predicates.sort()
id = 0
predicates_dict = dict()
for predicate in predicates:
predicates_dict[predicate] = id
id += 1
# creating the encodings
for pattern in patterns:
predicate, object = pattern.split("-")
subject_id = "*"
object_id = ""
predicate_id = ""
if object != "*":
object_id = object
else:
object_id = "*n" + str(predicate)
if predicate != "*":
predicate_id = predicate
else:
predicate_id = "*p" + str(object)
if matrix_mode == 2:
subject_subgraph_id = nodes_dict[subject_id]
predicate_subgraph_id = predicates_dict[predicate_id]
object_subgraph_id = nodes_dict[object_id]
a[predicate_subgraph_id, subject_subgraph_id, object_subgraph_id] = 1
if predicate_id == star_predicate_id and star_mode == 1:
for pred_id in range(b):
ep[predicate_subgraph_id, predicate_id] = 1
else:
ep[predicate_subgraph_id, predicate_id] = 1
if subject_id == star_node_id and star_mode == 1:
for subj_id in range(d):
x[subject_subgraph_id, subj_id] = 1
else:
x[subject_subgraph_id, subject_id] = 1
if object_id == star_node_id and star_mode == 1:
for obj_id in range(d):
x[object_subgraph_id, obj_id] = 1
else:
x[object_subgraph_id, object_id] = 1
elif matrix_mode == 3:
subject_subgraph_id = nodes_dict[subject_id]
predicate_subgraph_id = predicates_dict[predicate_id]
object_subgraph_id = nodes_dict[object_id]
a[predicate_subgraph_id, subject_subgraph_id, object_subgraph_id] = 1
predicate_nb = 0
if "*" not in predicate_id:
predicate_nb = int(predicate_id)
arr = numpy_binary(predicate_nb, bits_b)
for i in range(len(arr)):
ep[predicate_subgraph_id][i] = arr[i]
subject_nb = 0
if "*" not in subject_id:
subject_nb = int(subject_id)
arr = numpy_binary(subject_nb, bits_d)
for i in range(len(arr)):
x[subject_subgraph_id][i] = arr[i]
object_nb = 0
if "*" not in object_id:
object_nb = int(object_id)
arr = numpy_binary(object_nb, bits_d)
for i in range(len(arr)):
x[object_subgraph_id][i] = arr[i]
else:
print("Matrix mode unspecified")
exit(1)
X.append(x)
E.append(ep)
A.append(a)
time_end = time.time() - time_start
return np.array(X),np.array(A),np.array(E), np.array(y), time_end
def read_chain_graph_pattern(d, b, n, e, file_name, train_tuples = 10000, matrix_mode = 0, star_mode = 0):
"""
Creates encoding from queries in the file
:param d: int, the number of distinct nodes (subjects + objects) in KG
:param b: int, the number of distinct edges (predicates) in KG
:param n: int, the number of nodes in the subgraph
:param e: int, the number of edges in the subgraph
:param file_name: str, the input file name of the queries used for training or testing
:param train_tuples: int, limits the training sample size, in default we train on all of them
:param matrix_mode: 0, 1 being two matrices 2 and 3 being with 3 matrices as reported in paper, one-hot and binary encoding
:param star_mode:
:return: encoded queries
"""
print("started reading and creating chain patterns")
X=[]
A=[]
E=[]
y=[]
total_predicate_star = 0
with open(file_name) as fp:
line_nb = 0
lines = fp.readlines()
random.shuffle(lines)
time_start = time.time()
for line in lines:
if line_nb == train_tuples:
print("We are going out due to training size limitation")
break
patterns, cardinality = line.split(",")
cardinality = int(cardinality)
if (line_nb % 100000) == 0:
print(line_nb)
line_nb += 1
if matrix_mode == 2:
x = np.zeros((n, d), dtype='uint8')
ep = np.zeros((e, b), dtype='uint8')
a = np.zeros((e, n, n), dtype='uint8')
elif matrix_mode == 3:
bits_d = int(np.ceil(np.log2(d))) + 1
bits_b = int(np.ceil(np.log2(b))) + 1
x = np.zeros((n, bits_d), dtype='uint8')
ep = np.zeros((e, bits_b), dtype='uint8')
a = np.zeros((e, n, n), dtype='uint8')
else:
print("Matrix mode unspecified")
exit(1)
nodes = list()
total_zero = 0
predicates = list()
pattern_zero_flag = False
star_node_counter = 1
predicate_node_counter = 1
patterns1 = patterns.split("-")
first_time = True
patterns = []
i = 0
nb_predicate_star = 0
while i < len(patterns1) - 1:
if first_time:
s = patterns1[i]
p = patterns1[i + 1]
o = patterns1[i + 2]
else:
s = o
p = patterns1[i + 1]
o = patterns1[i + 2]
if first_time:
if "*" not in s:
nodes.append(s)
else:
s = s + "00n" + str(p)
nodes.append(s)
if "*" not in o:
nodes.append(o)
else:
if i == len(patterns1) - 3:
o = o + "xn" + str(p)
else:
o = o + str(nb_predicate_star) + "n" + str(p)
nb_predicate_star += 1
nodes.append(o)
if "*" not in p:
predicates.append(p)
else:
# omitting predicates star
total_predicate_star += 1
p = p + "p" + str(s)
predicates.append(p)
patterns.append(s + "-" + p + "-" + o)
i += 2
first_time = False
y.append(cardinality)
if total_zero > 0:
print("Total zero "+str(total_zero))
exit(1)
nodes_list = []
[nodes_list.append(x) for x in nodes if x not in nodes_list]
# nodes.sort()
nodes = list(set(nodes))
id = 0
nodes_dict = dict()
for node in nodes_list:
nodes_dict[node]=id
id += 1
predicates_list = []
[predicates_list.append(x) for x in predicates if x not in predicates_list]
# predicates.sort()
id = 0
predicates_dict = dict()
for predicate in predicates_list:
predicates_dict[predicate] = id
id += 1
# creating the encodings
for pattern in patterns:
subject, predicate, object = pattern.split("-")
subject_id = subject
object_id = object
predicate_id = predicate
if matrix_mode == 2:
subject_subgraph_id = nodes_dict[subject_id]
predicate_subgraph_id = predicates_dict[predicate_id]
object_subgraph_id = nodes_dict[object_id]
a[predicate_subgraph_id, subject_subgraph_id, object_subgraph_id] = 1
ep[predicate_subgraph_id, predicate_id] = 1
x[subject_subgraph_id, subject_id] = 1
x[object_subgraph_id, object_id] = 1
elif matrix_mode == 3:
subject_subgraph_id = nodes_dict[subject_id]
predicate_subgraph_id = predicates_dict[predicate_id]
object_subgraph_id = nodes_dict[object_id]
a[predicate_subgraph_id, subject_subgraph_id, object_subgraph_id] = 1
predicate_nb = 0
if "*" not in predicate_id:
predicate_nb = int(predicate_id)
arr = numpy_binary(predicate_nb, bits_b)
for i in range(len(arr)):
ep[predicate_subgraph_id][i] = arr[i]
subject_nb = 0
if "*" not in subject_id:
subject_nb = int(subject_id)
arr = numpy_binary(subject_nb, bits_d)
for i in range(len(arr)):
x[subject_subgraph_id][i] = arr[i]
object_nb = 0
if "*" not in object_id:
object_nb = int(object_id)
arr = numpy_binary(object_nb, bits_d)
for i in range(len(arr)):
x[object_subgraph_id][i] = arr[i]
else:
print("Matrix mode unspecified")
exit(1)
X.append(x)
E.append(ep)
A.append(a)
time_end = time.time() - time_start
return np.array(X),np.array(A),np.array(E), np.array(y), time_end
def read_combined(d, b, n, e, file_name_star, file_name_chain, train_tuples = 10000, matrix_mode = 0, star_mode = 0, test_mode = "star"):
"""
Creates encoding from queries in the file
:param d: int, the number of distinct nodes (subjects + objects) in KG
:param b: int, the number of distinct edges (predicates) in KG
:param n: int, the number of nodes in the subgraph
:param e: int, the number of edges in the subgraph
:param file_name_star: str, the input file name of the queries used for training or testing
:param file_name_chain: str, the input file name of the queries used for training or testing
:param train_tuples: int, limits the training sample size, in default we train on all of them
:param matrix_mode: 0, 1 being two matrices 2 and 3 being with 3 matrices as reported in paper, one-hot and binary encoding
:param star_mode:
:param test_mode: str, it can be star or chain
:return: encoded queries
"""
if "star" in test_mode:
return read_star_graph_pattern(d, b, n, e, file_name_star, train_tuples, matrix_mode)
if "chain" in test_mode:
return read_chain_graph_pattern(d, b, n, e , file_name_chain, train_tuples, matrix_mode)
X_s, A_s, E_s, y_s, time_start_star = read_star_graph_pattern(d, b, n, e, file_name_star, train_tuples, matrix_mode)
X_c, A_c, E_c, y_c, time_start_chain = read_chain_graph_pattern(d, b, n, e , file_name_chain, train_tuples, matrix_mode)
X = np.concatenate([X_s, X_c], axis = 0)
A = np.concatenate([A_s, A_c], axis = 0)
E = np.concatenate([E_s, E_c], axis = 0)
y = np.concatenate([y_s, y_c], axis = 0)
return X, A, E, y, (time_start_star + time_start_chain)
def read_combined_all_sizes_star_or_chain(d, b, n, e, file_names, train_tuples = 10000, matrix_mode = 0, star_mode = 0, test_mode = "star"):
"""
Creates encoding from queries in the file
:param d: int, the number of distinct nodes (subjects + objects) in KG
:param b: int, the number of distinct edges (predicates) in KG
:param n: int, the number of nodes in the subgraph
:param e: int, the number of edges in the subgraph
:param file_names: str, the input file name of the queries used for training or testing
:param train_tuples: int, limits the training sample size, in default we train on all of them
:param matrix_mode: 0, 1 being two matrices 2 and 3 being with 3 matrices as reported in paper, one-hot and binary encoding
:param star_mode:
:param test_mode: str, it can be star or chain
:return: encoded queries
"""
all_X = []
all_A = []
all_E = []
all_y = []
time = 0
for i in range(len(file_names)):
if 'star' in file_names[i]:
X_i, A_i, E_i, y_i, time_start_i = read_star_graph_pattern(d, b, n, e, file_names[i], train_tuples, matrix_mode)
else:
X_i, A_i, E_i, y_i, time_start_i = read_chain_graph_pattern(d, b, n, e , file_names[i], train_tuples, matrix_mode)
all_X.append(X_i)
all_A.append(A_i)
all_E.append(E_i)
all_y.append(y_i)
time += time_start_i
print("Read " + file_names[i])
X = np.concatenate(all_X, axis = 0)
A = np.concatenate(all_A, axis = 0)
E = np.concatenate(all_E, axis = 0)
y = np.concatenate(all_y, axis = 0)
return X, A, E, y, time