-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmodel_hole.py
101 lines (85 loc) · 4.41 KB
/
testmodel_hole.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
from launch_hole import OntoHolE
from base import Experiment
from logger import Logger
from extvars import ParamsPostProcessing
import random
import sys
import os
import trident
import argparse
import pickle
class TestModel(Experiment):
def __init__(self):
super(TestModel, self).__init__()
self.parser2 = argparse.ArgumentParser(prog='Knowledge Graph experiment', conflict_handler='resolve')
self.parser2.add_argument('--input', type=str, help='', required=True)
self.parser2.add_argument('--testValid', type=str, help='', default='valid')
self.parser2.add_argument('--fileTestValid', type=str, help='', required=True)
self.parser2.add_argument('--vectors', type=str, help='', required=True)
self.parser2.add_argument('--exvars', type=bool, help='', default=False)
self.parser2.add_argument('--testmode', type=bool, help='', default=False)
self.parser2.add_argument('--sampletest', type=float, help='', default=1)
self.parser2.add_argument('--resultsdir', type=str, help='Path to the directory that contains the results of the experiments', default='.')
self.parser2.add_argument('--resultsfilename', type=str, help='Name of the file that contains the results of the experiments', default='results')
self.parser2.add_argument('--post_threshold', type=int, help='', default=0)
self.parser2.add_argument('--post_nvars', type=int, help='', default=10)
self.parser2.add_argument('--post_minsize', type=int, help='', default=3)
self.parser2.add_argument('--post_minmatch', type=float, help='', default=0.5)
self.parser2.add_argument('--post_defaultincr', type=int, help='', default=10)
self.parser2.add_argument('--post2_enable', type=bool, help='', default=False)
self.parser2.add_argument('--post2_increment', type=int, help='', default=10)
self.parser2.add_argument('--post2_topk', type=int, help='', default=10)
self.evaluator = OntoHolE
def run(self):
self.args = self.parser2.parse_args()
self.logger = Logger(self.args.resultsdir, self.args.resultsfilename, 'INFO')
self.logger.startExperiment(sys.argv)
self.log = self.logger.getLog()
dbpath = os.path.dirname(self.args.input) + "/vlog"
self.db = trident.Db(dbpath)
#set up params postprocessing
paramspostp = ParamsPostProcessing(self.args.post_threshold,
self.args.post_nvars,
self.args.post_minsize,
self.args.post_minmatch,
self.args.post_defaultincr,
self.args.post2_enable,
self.args.post2_increment,
self.args.post2_topk)
# Load the vectors
with open(self.args.vectors, 'rb') as finv:
vectors = pickle.load(finv)
# Load the input
with open(self.args.input, 'rb') as fin:
data = pickle.load(fin)
self.logger.setInput(data)
self.dictr = data['relations']
self.dicte = data['entities']
nameSetToTest = self.args.testValid
ev = vectors['ev']
N = len(data['entities'])
true_triples = data['train_subs'] + data['test_subs'] + data['valid_subs']
with open(self.args.fileTestValid, 'rb') as fileTV:
setToTest = pickle.load(fileTV)
testMode = self.args.testmode
sampleTests = self.args.sampletest
while True:
tester = self.evaluator(self.log, setToTest,
true_triples,
N, ev.getNExtVars(),
ev, data['relations'],
data['entities'],
paramspostp,
sampleTests,
self.neval)
# Do the test
pos_v, fpos_v = tester.positions(vectors['model'], self.args.exvars, ev, self.db)
fmrr_valid = self.ranking_scores(pos_v, fpos_v, 0, nameSetToTest)
if testMode:
print(fmrr_valid)
# Change the parameters
else:
break
self.logger.stopExperiment()
if __name__ == '__main__':
TestModel().run()