-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_aitest.py
142 lines (118 loc) · 5.44 KB
/
run_aitest.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
#!/usr/bin/env python
"""
run_aitest.py/aitest - aitest driver command-line interface
"""
import os
import toml
import json
import argparse
import utils
from ailogger import ailogger
from path_config import PathConfig
from toml_keys import TomlKeys
from option_set import OptionSet
# test run class object
class TestRun:
title = ""
LOADED_DATASETS = {}
LOADED_MODELS = {}
LOADED_TESTCASES = {}
@staticmethod
def load_objects(datasets, models, testcases):
TestRun.LOADED_DATASETS = datasets
TestRun.LOADED_MODELS = models
TestRun.LOADED_TESTCASES = testcases
@staticmethod
def set_title(title):
TestRun.title = "".join(title.split()) #remove space in title
def __init__(self, testindex, dataset_key, model_key, testcase_key, out_format):
self.testindex = testindex
self.dataset_key = dataset_key
self.model_key = model_key
self.testcase_key = testcase_key
self.out_format = out_format
self.timestamp = None
def __str__(self):
return "dataset:{} | model:{} | testcase:{}".format(self.dataset_key, self.model_key, self.testcase_key)
def out_json_name(self):
self.timestamp = utils.get_timestamp()
return '%s/%s_%s_%s.json' % (self.title, self.timestamp, self.testcase_key, str(self.testindex))
def run_test(self, opt_set):
from _scripts.load_dataset import DataLoad
data = DataLoad.load_dataset(
self.dataset_key, TestRun.LOADED_DATASETS[self.dataset_key])
from _scripts.load_model import ModelLoad
model = ModelLoad.load_model(
self.model_key, TestRun.LOADED_MODELS[self.model_key])
from _scripts.load_testcase import TestcaseLoad
testfunc = TestcaseLoad.load_testcase(
self.testcase_key, TestRun.LOADED_TESTCASES[self.testcase_key])
# run test function
rets = testfunc(data=data, model_key=self.model_key, model=model, testfunc=testfunc,
testconfig=TestRun.LOADED_TESTCASES[self.testcase_key], result_path=os.path.join(PathConfig.get_results_path(),self.title), opt_set=opt_set)
try:
assert isinstance(rets, list)
except:
ailogger.exception('result must be in form of list')
raise
for index, ret in enumerate(rets):
if index >= len(self.out_format):
ailogger.warning("results length does not match with out format length")
continue
if self.out_format[index] == "json":
with open(os.path.join(PathConfig.get_results_path(),self.out_json_name()), 'w') as f:
ret.update({'timestamp':self.timestamp})
json.dump(ret, f, indent=2)
f.close()
else:
ailogger.warning("out format {} not supported yet".format(
self.out_format[index]))
# function to test run toml file
def run_toml(tomlfile, opt_set):
try:
assert os.path.exists(tomlfile)
except:
ailogger.exception(f"toml file path {tomlfile} doesn't exist")
raise
with open(tomlfile) as f:
loaded_toml = toml.load(f)
TomlKeys.validate_toml(loaded_toml)
ailogger.info("processing {} with title {}".format(
f.name, loaded_toml[TomlKeys.KEY_TITLE]))
# set testrun title
TestRun.set_title(loaded_toml[TomlKeys.KEY_TITLE])
# create a new folder under result path
if not os.path.exists(os.path.join(PathConfig.get_results_path(),TestRun.title)):
os.mkdir(os.path.join(PathConfig.get_results_path(),TestRun.title))
# load datasets,models,testcases
TestRun.load_objects(loaded_toml[TomlKeys.KEY_DATASETS],
loaded_toml[TomlKeys.KEY_MODELS], loaded_toml[TomlKeys.KEY_TESTCASES])
# run test one by one
for testindex, testrun_dict in enumerate(loaded_toml[TomlKeys.KEY_TESTRUN]):
testrun = TestRun(testindex, testrun_dict[TomlKeys.KEY_DATASET], testrun_dict[TomlKeys.KEY_MODEL],
testrun_dict[TomlKeys.KEY_TESTCASE], testrun_dict[TomlKeys.KEY_OUTFORMAT])
ailogger.info("running {}".format(testrun))
testrun.run_test(opt_set)
# test run completed
ailogger.info("testrun {} with title {} done !!!".format(
f.name, loaded_toml[TomlKeys.KEY_TITLE]))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--toml", required=True,
help="toml files of testcase separated by ',' e.g. abc.toml,def.toml,ghl.toml")
parser.add_argument("--train", required=False, default=False,
help="set the testcase to only train the model", action='store_true')
parser.add_argument("--test", required=False, default=False,
help="set the testcase to only to test the specified model path", action='store_true')
parser.add_argument("-m", "--model_path", required=False, default=None,
help="specify the model path for training/testing (compulsory for testing)")
args = parser.parse_args()
tomls_to_run = args.toml.split(",")
# validate PathConfig
PathConfig.validate()
# set up option set
opt_set = OptionSet(args.train, args.test, args.model_path)
ailogger.info(opt_set)
# run toml files one by one
for tomlfile in tomls_to_run:
run_toml(tomlfile, opt_set)