]> mj.ucw.cz Git - moe.git/blob - t/moe/eval.py
Changed clause->formula in ConfigCondition, added formula pretty-printing
[moe.git] / t / moe / eval.py
1 #!/usr/bin/env python
2
3 import moe
4 import moe.config
5 import moe.box
6 import moe.log
7 import moe.status
8 import moe.pipeline
9 import moe.batch
10 import moe.util
11 import os.path
12 import shutil
13
14 class Eval:
15
16     def __init__(self):
17         self.log = moe.log.default
18         self.cfgs = moe.config.MoeConfigStack()
19         self.builtins = moe.config.MoeConfig(type="builtins")
20         self.cfgs.push(self.builtins)
21         self.main_pipe = moe.pipeline.MoePipeline("main")
22         self.test_pipe = moe.pipeline.MoePipeline("test")
23         self.stat = moe.status.MoeStatus()
24         pass
25
26     def __getitem__(self, key):
27         return self.cfgs[key]
28
29     def init(self, overrides=None):
30         self.log.progress("Initializing... ")
31         self.init_global(overrides)
32         self.init_test()
33         self.init_logs()
34         self.init_task()
35         moe.box.init(self)
36         self.log.progress("OK\n")
37
38     def init_global(self, overrides):
39         main_cfg = moe.config.MoeConfig(name = os.path.join(self['HOME'], "config"), type="main")
40         self.cfgs.push(main_cfg)
41         if overrides:
42             self.cfgs.push(overrides)
43
44     def init_test(self):
45         test = self['TDIR']
46         if os.path.isdir(test):
47             shutil.rmtree(test)
48         try:
49             moe.util.mkdir_tree(test)   
50         except OSError, err:
51             raise moe.MoeError, "Cannot create %s: %s" % (test, err.strerror)
52
53     def init_logs(self):
54         self.log = moe.log.MoeLog()
55         if self["V"]:
56             self.log.verbosity = int(self["V"])
57         self.log.open(os.path.join(self["TDIR"], "log"))
58         self.default_log = self.log
59         moe.log.default = self.log
60         self.log_config(3, "before loading the task")
61
62     def init_task(self):
63         task = self['TASK']
64         task_dir = self['PDIR']
65         if not os.path.isdir(task_dir):
66             raise moe.MoeError, "No such task %s" % task
67
68         task_cfg = moe.config.MoeConfig(name = os.path.join(task_dir, "config"), type='task')
69         self.cfgs.push(task_cfg)
70         self.log_config(3, "after loading the task")
71
72         self.stat["task"] = task
73
74         type = self['TASK_TYPE']
75         if type == "batch" or type == "interactive":
76             moe.batch.prepare_pipe(self)
77         elif type == "opendata":
78             raise moe.MoeError, "Opendata tasks not implemented yet"
79         else:
80             raise moe.MoeError, "Unknown task type " + type
81
82     def run(self):
83         self.log_config(2, "for the task pipeline")
84         self.main_pipe.configure(self["HOOKS"])
85         if self.log.verbosity >= 2:
86             self.main_pipe.dump(self.log.log_file, prefix="\t")
87         self.main_pipe.run(self)
88
89     def log_config(self, verb, msg):
90         if self.log.verbosity >= verb:
91             self.log.say("@@@ Configuration stack %s @@@\n" % msg)
92             self.cfgs.dump_defs(self.log.log_file, prefix="\t")
93         if self.log.verbosity >= 99:
94             self.log.say("@@@@@@ Resolved configuration %s @@@@@@\n" % msg)
95             self.cfgs.dump(self.log.log_file, prefix="\t")