-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary_preprocessing.py
More file actions
214 lines (178 loc) · 8 KB
/
library_preprocessing.py
File metadata and controls
214 lines (178 loc) · 8 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
from apaa.data.manipulation import (
prepare_dataset,
prepare_internal_cv_dataset,
get_theorems_and_other,
)
import pickle
from apaa.data.structures import AgdaForest, KnowledgeGraph, AgdaDefinition
from apaa.other.helpers import Locations, NodeType
from apaa.preprocessing import create_library_definitions
from main_learner import LOGGER
import os
def load_and_dump_trees(library: str, library_dir: str):
tree_dump_file = Locations.forest_dump(library)
if not os.path.exists(tree_dump_file):
LOGGER.info(f"Constructing forest from files in '{library_dir}'")
forest = AgdaForest.create_from_files(library_dir)
LOGGER.info(f"Dumping to '{tree_dump_file}'")
forest.dump(tree_dump_file)
else:
LOGGER.info(f"Loading forest from '{tree_dump_file}'")
forest = AgdaForest.load(tree_dump_file)
LOGGER.info("Forest available.")
return forest
def load_and_dump_tree_unimath():
return load_and_dump_trees(Locations.NAME_UNIMATH, Locations.SEXP_DIR_UNIMATH)
def load_and_dump_tree_agda_test():
return load_and_dump_trees(Locations.NAME_AGDA_TEST, Locations.SEXP_DIR_AGDA_TEST)
def load_and_dump_tree_stdlib():
return load_and_dump_trees(Locations.NAME_STDLIB, Locations.SEXP_DIR_STDLIB)
def do_all_for_library(library: str, library_dir: str, stage: int):
LOGGER.info(f"Starting {library} from stage {stage}")
if stage <= 0:
load_and_dump_trees(library, library_dir)
if stage <= 1:
create_library_definitions(library)
if stage <= 2:
kg_loc = Locations.knowledge_graph(library)
if not os.path.exists(kg_loc):
kg = KnowledgeGraph.create_from_definitions_file(
library, Locations.definitions_pickled(library)
)
kg.dump(kg_loc)
g_loc = Locations.knowledge_graph_pure(library)
if not os.path.exists(g_loc):
kg = KnowledgeGraph.load(kg_loc)
kg.dump_pure(g_loc)
def do_all_agda_test(stage: int = 0):
do_all_for_library(Locations.NAME_AGDA_TEST, Locations.SEXP_DIR_AGDA_TEST, stage)
def do_all_lean_test(stage: int = 0):
do_all_for_library(Locations.NAME_LEAN_TEST, Locations.SEXP_DIR_LEAN_TEST, stage)
def do_all_stdlib(stage: int = 0):
do_all_for_library(Locations.NAME_STDLIB, Locations.SEXP_DIR_STDLIB, stage)
def do_all_unimath(stage: int = 0):
do_all_for_library(Locations.NAME_UNIMATH, Locations.SEXP_DIR_UNIMATH, stage)
def create_datasets(libraries: list[str], p_test: float, ps_body_to_keep: list[float]):
for library in libraries:
for p_body_to_keep in ps_body_to_keep:
dataset_file = Locations.dataset(library, p_test, p_body_to_keep)
if os.path.exists(dataset_file):
LOGGER.info(
f"Dataset for {library} (p_test = {p_test}, "
f"p_body_to_keep = {p_body_to_keep}) already exists."
)
continue
LOGGER.info(f"Preparing dataset {dataset_file}")
# this must be loaded every single time
kg = KnowledgeGraph.load(Locations.knowledge_graph(library))
dataset = prepare_dataset(
kg.graph,
kg.id_to_definition,
p_test=p_test,
p_def_to_keep=p_body_to_keep,
)
with open(dataset_file, "wb") as f:
pickle.dump(dataset, f)
def check_internal_datasets(internal_files: list[str], n_defs: int):
test_defs_set = set()
n_warnings = 0
for internal_file in internal_files:
with open(internal_file, "rb") as f:
_, (_, test_defs), _ = pickle.load(f)
for def_id in test_defs:
if def_id in test_defs_set:
n_warnings += 1
if n_warnings < 10:
LOGGER.warning(f"Definition {def_id} is in multiple folds.")
test_defs_set.add(def_id)
if len(test_defs_set) != n_defs:
LOGGER.warning(f"Missing {n_defs - len(test_defs_set)} test defs")
LOGGER.info(f"Tests passed with {n_warnings} warnings.")
def create_internal_cv_dataset(
libraries: list[str], n_folds: int, ps_body_to_keep: list[float]
):
import itertools
internal_files = []
n_defs = -1
for lib, p_body, fold in itertools.product(
libraries, ps_body_to_keep, range(n_folds)
):
lib_internal = Locations.library_name_to_internal(lib, n_folds, fold)
dataset_file = Locations.dataset(lib, 0.2, p_body)
internal_dataset_file = Locations.dataset(lib_internal, 0.2, p_body)
internal_pure_kg = Locations.knowledge_graph_pure(lib_internal)
if os.path.exists(internal_dataset_file) and os.path.exists(internal_pure_kg):
LOGGER.info(f"Dataset '{internal_dataset_file}' already exists.")
continue
elif os.path.exists(internal_dataset_file):
LOGGER.info("Dataset exists, but not pure KG. Creating it.")
with open(dataset_file, "rb") as f:
graph, _, _ = pickle.load(f)
with open(internal_pure_kg, "wb") as f:
pickle.dump(graph, f)
continue
LOGGER.info(f"Preparing dataset {internal_dataset_file}")
# this must be loaded every single time
dataset_file = Locations.dataset(lib, 0.2, p_body)
if not os.path.exists(dataset_file):
raise ValueError(f"Dataset {dataset_file} does not exist.")
with open(dataset_file, "rb") as f:
train_graph, (train_defs, external_test_defs), _ = pickle.load(f)
theorem_like_tag = NodeType.get_theorem_like_tag(train_graph)
ids_in_order = sorted(train_defs)
definitions_ids, _ = get_theorems_and_other(
ids_in_order, train_defs, theorem_like_tag
)
if n_defs == -1:
n_defs = len(definitions_ids)
else:
assert n_defs == len(definitions_ids)
internal_dataset = prepare_internal_cv_dataset(
train_graph, definitions_ids, train_defs, fold, n_folds, p_body, 12345
)
internal_dataset[1][0].update(external_test_defs) # add them to train ...
# dump the dataset
with open(internal_dataset_file, "wb") as f:
pickle.dump(internal_dataset, f)
internal_files.append(internal_dataset_file)
# dump the graph: this is the same graph for every fold,
# but makes life much easier later
with open(dataset_file, "rb") as f:
graph, _, _ = pickle.load(f)
with open(internal_pure_kg, "wb") as f:
pickle.dump(graph, f)
check_internal_datasets(internal_files, n_defs)
def do_all_and_datasets():
do_all_agda_test(0)
do_all_unimath(0)
do_all_stdlib(0)
create_datasets(
[
Locations.NAME_AGDA_TEST,
Locations.NAME_LEAN_TEST,
Locations.NAME_STDLIB,
Locations.NAME_UNIMATH,
][:1],
0.2,
[0.0, 0.1, 0.25, 0.5, 0.75, 0.9],
)
def do_efficient_for_lean_test():
AgdaDefinition.create_from_files(
Locations.SEXP_DIR_LEAN_TEST, Locations.dag_dir(Locations.NAME_LEAN_TEST)
)
def do_efficient_all(libs: list[str]):
pairs = [
(Locations.SEXP_DIR_AGDA_TEST, Locations.NAME_AGDA_TEST),
(Locations.SEXP_DIR_LEAN_TEST, Locations.NAME_LEAN_TEST),
(Locations.SEXP_DIR_STDLIB, Locations.NAME_STDLIB),
(Locations.SEXP_DIR_UNIMATH, Locations.NAME_UNIMATH),
(Locations.SEXP_DIR_TYPE_TOPOLOGY, Locations.NAME_TYPE_TOPOLOGY),
(Locations.SEXP_DIR_MATHLIB, Locations.NAME_MATHLIB),
]
for sexp_dir, lib in pairs:
if lib not in libs:
continue
LOGGER.info(f"############################################### Starting {lib}")
AgdaDefinition.create_from_files(sexp_dir, Locations.dag_dir(lib))
if __name__ == "__main__":
do_all_agda_test()