]> mj.ucw.cz Git - moe.git/blob - t/moe/eval.py
1e54b4ee270e387cfd7bb53090f282a43fbaaaf7
[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     """
17
18     def __init__(self):
19         self.log = moe.log.Logers()
20         self.config = moe.config.ConfigTree()
21         self.main_pipe = moe.pipeline.MoePipeline("main")
22         self.test_pipe = moe.pipeline.MoePipeline("test")
23         self.stat = moe.status.MoeStatus()
24
25     def __getitem__(self, key):
26         return self.config[key]
27
28     def init(self, overrides=[]):
29         "Initializes most part of Eval before running the pipeline. See the timeline for details."
30         self.log.info("Initializing ...")
31         
32         # set basic builtins
33         self.config.parse('HOME=\'%s\'' % os.getcwd(), source="<builtins>", level=0)
34         self.config.parse('CONFIG="{HOME}/config"', source="<builtins>", level=0)
35         self.config.parse('LOG="{HOME}/log"', source="<builtins>", level=0)
36         self.config.parse('DEBUG_LEVEL="0"', source="<builtins>", level=0)
37         self.config.parse('VERBOSE=""', source="<builtins>", level=0)
38         self.config.parse('TDIR="{HOME}/test"', source="<temp-builtins>", level=0) # -> config
39         self.config.parse('USER_LOG="{TDIR}/log"', source="<temp-builtins>", level=0) # -> config
40         # apply overrides
41         for ov in overrides:
42           self.config.parse(ov, source="<overrides>", level=100)
43         
44         # load config file
45         self.config.fix('CONFIG')
46         self.config.parse_file(self['CONFIG'], level=30)
47         # fix variables
48         self.config.fix(['LOG', 'USER_LOG', 'VERBOSE', 'HOME', 'DEBUG_LEVEL', 'TDIR'])
49         # start logging
50         self.log.open_eval_log(self['LOG'], self['DEBUG_LEVEL'], redirect_fds = True)
51         self.log.open_user_log(self['USER_LOG'])
52         self.debug_dump_config()
53
54         # init and check TDIR
55         self.debug('Cleaning TDIR: %s'%self['TDIR'])
56         self.init_TDIR()
57         
58         # insert hooks into main pipeline
59         # TODO
60         # TODO moe.box.init(self)
61
62         # insert custom hooks
63         self.conf.fix('HOOKS')
64         self.main_pipe.configure(self['HOOKS'])
65
66     def run(self):
67         "Run the main pipeline."
68         self.debug_dump_pipe(self.main_pipe)
69         self.debug('Running main pipeline')
70         self.main_pipe.run(self)
71
72
73     def init_TDIR(self):
74         test = self['TDIR']
75         if os.path.isdir(test):
76             shutil.rmtree(test)
77         try: #TODO: Remove
78             moe.util.mkdir_tree(test)   
79         except OSError, err:
80             raise moe.MoeError, "Cannot create %s: %s" % (test, err.strerror)
81
82     def debug_dump_config(self):
83       "Dumps config at level DDEBUG (only compiles the dump if main level is low enough)."
84       if self.log.level <= 5:
85         self.log.ddebug('****** Config dump: ******')
86         self.log.ddebug(self.config.dump('**** '))
87         self.log.ddebug('**************************')
88
89     def debug_dump_pipe(self, pipe):
90       "Dumps pipeline `pipe` at level DDEBUG (only compiles the dump if main level low enough)."
91       if self.log.level <= 5:
92         self.log.ddebug('****** Pipeline %r dump: ******'%pipe,name)
93         self.log.ddebug(pipe.dump(prefix='**** '))
94         self.log.ddebug('**************************')
95
96 #TODO ...
97     def init_task(self):
98         task = self['TASK']
99         task_dir = self['PDIR']
100         if not os.path.isdir(task_dir):
101             raise moe.MoeError, "No such task %s" % task
102
103         task_cfg = moe.config.MoeConfig(name = os.path.join(task_dir, "config"), type='task')
104         self.cfgs.push(task_cfg)
105         self.log_config(3, "after loading the task")
106
107         self.stat["task"] = task
108
109         type = self['TASK_TYPE']
110         if type == "batch" or type == "interactive":
111             moe.batch.prepare_pipe(self)
112         elif type == "opendata":
113             raise moe.MoeError, "Opendata tasks not implemented yet"
114         else:
115             raise moe.MoeError, "Unknown task type " + type
116