-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.py
302 lines (181 loc) · 9.81 KB
/
sample.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
import numpy as np
from collections import defaultdict
import json
import networkx as nx
from graph_matcher import pattern_to_query, query_search_answer, \
triple2text_aser, interpretateQueryDirectly, count_edges, extend_occurential_constraints, extend_temporal_constraints
import pandas as pd
from tqdm import tqdm
from random import choices
import time
MAX_CONSTRAINTS = 10
def sample_general_train(train_graph, query_type, _triple2text, constraint_type):
while True:
query, _ = pattern_to_query(query_type, train_graph)
shared={"counter": -1}
query_interpretation, _, _ = interpretateQueryDirectly(query, shared_var=shared, triple2text=_triple2text)
answers, explanations, explanation_tuples = query_search_answer(train_graph, query)
if len(answers) == 0:
continue
if constraint_type == "occurential":
occurential_tuples = extend_occurential_constraints(train_graph, explanation_tuples)
if len(occurential_tuples) == 0:
continue
occurential_tuples = choices(occurential_tuples, k=MAX_CONSTRAINTS)
occurential_tuples = list(set(occurential_tuples))
else:
occurential_tuples = []
if constraint_type == "temporal":
temporal_tuples = extend_temporal_constraints(train_graph, explanation_tuples)
if len(temporal_tuples) == 0:
continue
temporal_tuples = choices(temporal_tuples, k=MAX_CONSTRAINTS)
temporal_tuples = list(set(temporal_tuples))
else:
temporal_tuples = []
if len(answers) > 0:
# background_tuples = extend_query(train_graph, query)
this_query_result = {
"query": query,
"nl_query": query_interpretation + " What is V0?",
"train_answers": answers,
"train_explanations": explanations,
"train_explanation_tuples": explanation_tuples,
"occurential_tuples": occurential_tuples,
"temporal_tuples": temporal_tuples
}
return this_query_result
def sample_general_valid(train_graph, valid_graph, query_type, _triple2text, constraint_type):
while True:
query, _ = pattern_to_query(query_type, valid_graph)
shared={"counter": -1}
query_interpretation, _, _ = interpretateQueryDirectly(query, shared_var=shared, triple2text=_triple2text)
train_answers, train_explanations, train_explanation_tuples = query_search_answer(train_graph, query)
valid_answers, valid_explanations, valid_explanation_tuples = query_search_answer(valid_graph, query)
if len(valid_answers) == 0 or len(train_answers) == len(valid_answers):
continue
if constraint_type == "occurential":
occurential_tuples = extend_occurential_constraints(valid_graph, valid_explanation_tuples)
if len(occurential_tuples) == 0:
continue
occurential_tuples = choices(occurential_tuples, k=MAX_CONSTRAINTS)
occurential_tuples = list(set(occurential_tuples))
else:
occurential_tuples = []
if constraint_type == "temporal":
temporal_tuples = extend_temporal_constraints(valid_graph, valid_explanation_tuples)
if len(temporal_tuples) == 0:
continue
temporal_tuples = choices(temporal_tuples, k=MAX_CONSTRAINTS)
temporal_tuples = list(set(temporal_tuples))
else:
temporal_tuples = []
this_query_result = {
"query": query,
"nl_query": query_interpretation + " What is V0?",
"train_answers": train_answers,
"train_explanations": train_explanations,
"train_explanation_tuples": train_explanation_tuples,
"valid_answers": valid_answers,
"valid_explanations": valid_explanations,
"valid_explanation_tuples": valid_explanation_tuples,
"occurential_tuples": occurential_tuples,
"temporal_tuples": temporal_tuples
}
return this_query_result
def sample_general_test(train_graph, valid_graph, test_graph, query_type, _triple2text, constraint_type):
while True:
query, _ = pattern_to_query(query_type, test_graph)
shared={"counter": -1}
query_interpretation, _, _ = interpretateQueryDirectly(query, shared_var=shared, triple2text=_triple2text)
train_answers, train_explanations, train_explanation_tuples = query_search_answer(train_graph, query)
valid_answers, valid_explanations, valid_explanation_tuples = query_search_answer(valid_graph, query)
test_answers, test_explanations, test_explanation_tuples = query_search_answer(test_graph, query)
if len(test_answers) == 0:
continue
if len(valid_answers) == len(test_answers):
continue
if constraint_type == "occurential":
occurential_tuples = extend_occurential_constraints(test_graph, test_explanation_tuples)
if len(occurential_tuples) == 0:
continue
occurential_tuples = choices(occurential_tuples, k=MAX_CONSTRAINTS)
occurential_tuples = list(set(occurential_tuples))
else:
occurential_tuples = []
if constraint_type == "temporal":
temporal_tuples = extend_temporal_constraints(test_graph, test_explanation_tuples)
if len(temporal_tuples) == 0:
continue
temporal_tuples = choices(temporal_tuples, k=MAX_CONSTRAINTS)
temporal_tuples = list(set(temporal_tuples))
else:
temporal_tuples = []
# background_tuples = extend_query(test_graph, query)
this_query_result = {
"query": query,
"nl_query": query_interpretation + " What is V0?",
"train_answers": train_answers,
"train_explanations": train_explanations,
"train_explanation_tuples": train_explanation_tuples,
"valid_answers": valid_answers,
"valid_explanations": valid_explanations,
"valid_explanation_tuples": valid_explanation_tuples,
"test_answers": test_answers,
"test_explanations": test_explanations,
"test_explanation_tuples": test_explanation_tuples,
"occurential_tuples": occurential_tuples,
"temporal_tuples": temporal_tuples
}
return this_query_result
if __name__ == "__main__":
all_query_types = pd.read_csv("./test_generated_formula_anchor_node=3.csv").reset_index(
drop=True) # debug
original_query_types = {}
for i in range(all_query_types.shape[0]):
fid = all_query_types.formula_id[i]
query = all_query_types.original[i]
original_depth = int(all_query_types.original_depth[i])
if original_depth > 2:
continue
if "u" in query or "n" in query:
continue
original_query_types[fid] = query
input_graph_names = [["./aser50k_train.pickle",
"./aser50k_valid.pickle", "./aser50k_test.pickle"]]
start_time = time.time()
for train_graph_name, valid_graph_name, test_graph_name in input_graph_names:
train_graph = nx.read_gpickle(train_graph_name)
valid_graph = nx.read_gpickle(valid_graph_name)
test_graph = nx.read_gpickle(test_graph_name)
for query_name, query_type in original_query_types.items():
print(test_graph_name, query_name)
number_samples = 1
this_query_type_list = []
for _ in tqdm(range(number_samples)):
this_query_result = sample_general_train(train_graph, query_type, triple2text_aser, "occurential")
this_query_type_list.append(this_query_result)
this_query_result = sample_general_train(train_graph, query_type, triple2text_aser, "temporal")
this_query_type_list.append(this_query_result)
with open("./query_data_dev/" + "aser" + "_train_" + query_name + ".json", "w") as f:
for query in this_query_type_list:
f.write(json.dumps(query) + "\n")
this_query_type_list = []
for _ in tqdm(range(number_samples)):
this_query_result = sample_general_valid(train_graph, valid_graph, query_type, triple2text_aser, "occurential")
this_query_type_list.append(this_query_result)
this_query_result = sample_general_valid(train_graph, valid_graph, query_type, triple2text_aser, "temporal")
this_query_type_list.append(this_query_result)
with open("./query_data_dev/" + "aser" + "_valid_" + query_name + ".json", "w") as f:
for query in this_query_type_list:
f.write(json.dumps(query) + "\n")
this_query_type_list = []
for _ in tqdm(range(number_samples)):
this_query_result = sample_general_test(train_graph, valid_graph, test_graph, query_type, triple2text_aser, "occurential")
this_query_type_list.append(this_query_result)
this_query_result = sample_general_test(train_graph, valid_graph, test_graph, query_type, triple2text_aser, "temporal")
this_query_type_list.append(this_query_result)
with open("./query_data_dev/" + "aser" + "_test_" + query_name + ".json", "w") as f:
for query in this_query_type_list:
f.write(json.dumps(query) + "\n")
print("time elapsed: ", time.time() - start_time)