forked from jacobvsdanniel/pubmedkb_core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
361 lines (301 loc) · 11.4 KB
/
main.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
"""
CUDA 10.1;
Optimized for 1 GPU + 13 CPUs;
Please make sure NEN server has started;
"""
import os
import sys
import json
import time
import shutil
import logging
import argparse
import subprocess
logger = logging.getLogger(__name__)
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%Y/%m/%d %H:%M:%S",
level=logging.INFO,
)
def read_lines(file, write_log=True):
if write_log:
logger.info(f"Reading {file}")
with open(file, "r", encoding="utf8") as f:
line_list = f.read().splitlines()
if write_log:
lines = len(line_list)
logger.info(f"Read {lines:,} lines")
return line_list
def read_json(file, write_log=True):
if write_log:
logger.info(f"Reading {file}")
with open(file, "r", encoding="utf8") as f:
data = json.load(f)
if write_log:
objects = len(data)
logger.info(f"Read {objects:,} objects")
return data
def write_json(file, data, indent=None, write_log=True):
if write_log:
objects = len(data)
logger.info(f"Writing {objects:,} objects")
with open(file, "w", encoding="utf8") as f:
json.dump(data, f, indent=indent)
if write_log:
logger.info(f"Written to {file}")
return data
def get_env(venv_dir):
env = os.environ.copy()
pi = env["PATH"].find(":")
assert pi > 0
bin_dir = os.path.join(venv_dir, "bin")
env["PATH"] = bin_dir + env["PATH"][pi:]
env["VIRTUAL_ENV"] = venv_dir
interpreter = os.path.join(bin_dir, "python")
return env, interpreter
class Task:
def __init__(self, name):
self.name = name
self.parent_list = []
self.process = None
self.finished = False
self.source = None
self.target = None
self.indent = None
return
def run(self):
logger.info(f"Task: {self.name}")
venv_dir = os.path.join("venv_dir", self.name)
venv_dir = os.path.abspath(venv_dir)
env, interpreter = get_env(venv_dir)
source_file = os.path.abspath(self.source)
target_dir = os.path.abspath(os.path.join(self.target, self.name))
command = self.get_command(interpreter, source_file, target_dir, self.indent)
logger.info(f"Command: {command}")
model_dir = os.path.join("model_dir", self.name)
os.makedirs(target_dir, exist_ok=False)
self.process = subprocess.Popen(command, env=env, cwd=model_dir)
return
def get_command(self, abspath_interpreter, abspath_source_file, abspath_target_dir, indent):
if self.name == "ner":
command = [
abspath_interpreter, "main.py",
"--source_file", abspath_source_file,
"--target_dir", abspath_target_dir,
"--indent", indent,
]
elif self.name == "pop":
command = [
abspath_interpreter, "main.py",
"--source_file", abspath_source_file,
"--target_dir", abspath_target_dir,
"--model_dir", "model",
"--number_measure_file", "number_measure.txt",
"--species_file", "species.txt",
"--indent", indent,
]
elif self.name == "or":
command = [
abspath_interpreter, "main.py",
"--source_file", abspath_source_file,
"--target_dir", abspath_target_dir,
"--model_dir", "model",
"--variant_tag", "mutation",
"--disease_tag", "disease",
"--complete_triplet_only",
"--batch_size", "512",
"--indent", indent,
]
elif self.name == "openie":
command = [
abspath_interpreter, "main.py",
"--source_file", abspath_source_file,
"--target_dir", abspath_target_dir,
"--jar_dir", "jar_dir/",
"--batch_size", "2000",
"--batch_processes", "4",
"--process_memory", "6g",
"--indent", indent,
]
elif self.name == "spacy":
command = [
abspath_interpreter, "main.py",
"--source_file", abspath_source_file,
"--target_file", os.path.join(abspath_target_dir, "target.json"),
"--batch_size", "50000",
"--indent", indent,
]
elif self.name == "nen":
command = [
abspath_interpreter, "main.py",
"--source_file", abspath_source_file,
"--target_file", os.path.join(abspath_target_dir, "target.json"),
"--batch_size", "10000",
"--processes", "20",
"--port", "8888",
"--indent", indent,
]
elif self.name == "cre":
command = [
abspath_interpreter, "main.py",
"--source_file", abspath_source_file,
"--target_dir", abspath_target_dir,
"--variant_tag", "mutation",
"--disease_tag", "disease",
"--indent", indent,
]
else:
assert False
return command
def check_finish(self):
if self.process is None or self.finished:
return
ret = self.process.poll()
if ret is None:
return
assert ret == 0
self.finished = True
self.remove_temporary_file()
logger.info(f"Cleanup: {self.name}")
return
def remove_temporary_file(self):
if self.name == "ner":
tmp_file_list = ["input.txt", "target.tsv"]
tmp_dir_list = []
elif self.name == "pop":
tmp_file_list = ["model_input.csv", "model_output.txt", "all_results.json", "test_results.json"]
tmp_dir_list = []
elif self.name == "or":
tmp_file_list = ["model_input.csv"]
tmp_dir_list = ["all_source", "all_target"]
elif self.name == "openie":
tmp_file_list = []
tmp_dir_list = ["all_source", "all_target"]
elif self.name == "spacy":
tmp_file_list = []
tmp_dir_list = []
elif self.name == "nen":
tmp_file_list = []
tmp_dir_list = []
elif self.name == "cre":
tmp_file_list = ["meta.csv", "input.txt", "output.txt", "output.txt.npy"]
tmp_dir_list = []
else:
assert False
for tmp_file in tmp_file_list:
os.remove(os.path.join(self.target, self.name, tmp_file))
for tmp_dir in tmp_dir_list:
shutil.rmtree(os.path.join(self.target, self.name, tmp_dir))
return
def initialize_all_task(source_file, target_dir, todo_name_list, indent):
name_to_task = {
name: Task(name)
for name in ["ner", "pop", "or", "openie", "spacy", "nen", "cre"]
}
todo_name_list = set(todo_name_list.split(","))
for name, task in name_to_task.items():
task.finished = name not in todo_name_list
task.target = target_dir
task.indent = str(indent)
if name == "ner":
task.parent_list = []
task.source = source_file
elif name == "pop":
task.parent_list = [name_to_task[name] for name in ["ner"]]
task.source = source_file
elif name == "or":
task.parent_list = [name_to_task[name] for name in ["pop"]]
if "ner" in todo_name_list:
task.source = os.path.join(target_dir, "ner", "target.json")
else:
task.source = source_file
elif name == "openie":
task.parent_list = [name_to_task[name] for name in ["ner"]]
if "ner" in todo_name_list:
task.source = os.path.join(target_dir, "ner", "target.json")
else:
task.source = source_file
elif name == "spacy":
task.parent_list = [name_to_task[name] for name in ["or"]]
if "ner" in todo_name_list:
task.source = os.path.join(target_dir, "ner", "target.json")
else:
task.source = source_file
elif name == "nen":
task.parent_list = [name_to_task[name] for name in ["ner"]]
if "ner" in todo_name_list:
task.source = os.path.join(target_dir, "ner", "target.json")
else:
task.source = source_file
elif name == "cre":
task.parent_list = [name_to_task[name] for name in ["spacy", "nen"]]
if "nen" in todo_name_list:
task.source = os.path.join(target_dir, "nen", "target.json")
else:
task.source = source_file
return name_to_task
def collect_result(source_file, target_dir, todo_name_list, indent):
source_data = read_json(source_file)
todo_name_list = todo_name_list.split(",")
for name in todo_name_list:
task_dir = os.path.join(target_dir, name)
task_file = os.path.join(task_dir, "target.json")
task_data = read_json(task_file)
assert len(source_data) == len(task_data)
for di, datum in enumerate(source_data):
if name == "ner" and "nen" not in todo_name_list:
datum["mention_list"] = task_data[di]["mention_list"]
elif name == "pop":
datum["population"] = task_data[di]["population"]
elif name == "or":
datum["odds_ratio"] = task_data[di]["var_dis_or_ci_pv"]
elif name == "openie":
datum["openie_ore"] = task_data[di]["triplet_list"]
elif name == "spacy":
datum["spacy_ore"] = task_data[di]["triplet_list"]
elif name == "nen":
datum["mention_list"] = task_data[di]["mention_list"]
elif name == "cre":
datum["rbert_cre"] = task_data[di]["triplet_list"]
os.remove(task_file)
os.rmdir(task_dir)
target_file = os.path.join(target_dir, "target.json")
indent = indent if indent >= 0 else None
write_json(target_file, source_data, indent)
return
def run_pubmedkb_core(arg):
name_to_task = initialize_all_task(arg.source_file, arg.target_dir, arg.task, arg.indent)
os.makedirs(arg.target_dir, exist_ok=False)
while True:
# Check all task status
all_finished = True
for _, task in name_to_task.items():
task.check_finish()
if not task.finished:
all_finished = False
if all_finished:
break
# Run tasks who: not running, not finished, parents are all finished
for _, task in name_to_task.items():
if task.process is not None or task.finished:
continue
for parent in task.parent_list:
if not parent.finished:
break
else:
task.run()
time.sleep(30)
collect_result(arg.source_file, arg.target_dir, arg.task, arg.indent)
return
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--source_file", type=str, default="source.json")
parser.add_argument("--target_dir", type=str, default="target_dir")
parser.add_argument("--task", type=str, default="ner,pop,or,openie,spacy,nen,cre")
parser.add_argument("--indent", type=int, default=2)
arg = parser.parse_args()
run_pubmedkb_core(arg)
return
if __name__ == "__main__":
main()
sys.exit()