]> mj.ucw.cz Git - eval.git/blob - t/moe/eval.py
Updated documentation and tidyed up eval.py
[eval.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         with open(self['CONFIG'], 'r') as f:
47           self.config.parse(f, source=self['CONFIG'], level=30)
48         # fix variables
49         self.config.fix(['LOG', 'USER_LOG', 'VERBOSE', 'HOME', 'DEBUG_LEVEL', 'TDIR'])
50         # start logging
51         self.log.open_eval_log(self['LOG'], self['DEBUG_LEVEL'], redirect_fds = True)
52         self.log.open_user_log(self['USER_LOG'])
53         self.debug_dump_config()
54
55         # init and check TDIR
56         self.debug('Cleaning TDIR: %s'%self['TDIR'])
57         self.init_TDIR()
58         
59         # insert hooks into main pipeline
60         # TODO
61         # TODO moe.box.init(self)
62
63         # insert custom hooks
64         self.conf.fix('HOOKS')
65         self.main_pipe.configure(self['HOOKS'])
66
67     def run(self):
68         "Run the main pipeline."
69         self.debug_dump_pipe(self.main_pipe)
70         self.debug('Running main pipeline')
71         self.main_pipe.run(self)
72
73
74     def init_TDIR(self):
75         test = self['TDIR']
76         if os.path.isdir(test):
77             shutil.rmtree(test)
78         try: #TODO: Remove
79             moe.util.mkdir_tree(test)   
80         except OSError, err:
81             raise moe.MoeError, "Cannot create %s: %s" % (test, err.strerror)
82
83     def debug_dump_config(self):
84       "Dumps at level DDEBUG and only compiles the dump if main level low enough."
85       if self.log.level <= 5:
86         self.log.ddebug('****** Config dump: ******'))
87         self.log.ddebug(self.config.dump('**** '))
88         self.log.ddebug('**************************'))
89
90     def debug_dump_pipe(self, pipe):
91       "Dumps at level DDEBUG and only compiles the dump if main level low enough."
92       if self.log.level <= 5:
93         self.log.ddebug('****** Pipeline %r dump: ******'%pipe,name))
94         self.log.ddebug(pipe.dump(prefix='**** '))
95         self.log.ddebug('**************************'))
96
97 #TODO ...
98     def init_task(self):
99         task = self['TASK']
100         task_dir = self['PDIR']
101         if not os.path.isdir(task_dir):
102             raise moe.MoeError, "No such task %s" % task
103
104         task_cfg = moe.config.MoeConfig(name = os.path.join(task_dir, "config"), type='task')
105         self.cfgs.push(task_cfg)
106         self.log_config(3, "after loading the task")
107
108         self.stat["task"] = task
109
110         type = self['TASK_TYPE']
111         if type == "batch" or type == "interactive":
112             moe.batch.prepare_pipe(self)
113         elif type == "opendata":
114             raise moe.MoeError, "Opendata tasks not implemented yet"
115         else:
116             raise moe.MoeError, "Unknown task type " + type
117