]> mj.ucw.cz Git - eval.git/blob - t/moe/eval.py
8c996e29a2111bb0974fa09dd2505958f7ad9cfa
[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.Loggers()
20         self.config = moe.config.ConfigTree()
21         self.main_pipe = moe.pipeline.Pipeline(self, "main")
22         self.test_pipe = moe.pipeline.Pipeline(self, "test")
23         self.status = 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('EXTENSIONS = ""', source="<builtins>", level=0)
39         
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'], int(self['DEBUG_LEVEL']), redirect_fds = True)
51         self.log.open_user_log(self['USER_LOG'])
52         self.debug_dump_config()
53
54         # insert hooks into main pipeline
55         self.main_pipe.insert(5, "Eval.hook_init_dirs", hook_init_dirs)
56         self.main_pipe.insert(15, "Eval.hook_load_task_config", hook_load_task_config)
57         self.main_pipe.insert(20, "Eval.hook_init_tasktype", hook_init_tasktype)
58         self.main_pipe.insert(90, "Eval.hook_write_metadata", hook_write_metadata)
59
60         # ininialize extensions (let them insert hooks) 
61         self.config.fix('EXTENSIONS')
62         exts = self['EXTENSIONS'].split()
63         for e in exts:
64             if not e:
65                 raise MoeError, "Invalid extension name: %r" % e
66             self.log.debug("Loading extension %s", e)
67             try:
68                 mod = moe.util.load_module('moe.exts.' + e)
69             except ImportError:
70                 self.log.exception()
71                 raise MoeError, 'Unknown extension: %r' % e
72             mod.init(self)
73         
74     def run(self):
75         "Run the main pipeline."
76         self.debug_dump_pipe(self.main_pipe)
77         self.log.debug('Running main pipeline')
78         self.main_pipe.run(e=self)
79
80     def debug_dump_config(self):
81         "Dumps config at level DDEBUG (only compiles the dump if main level is low enough)."
82         if self.log.level <= 5:
83             self.log.ddebug('****** Config dump: ******')
84             self.log.ddebug('\n'.join(self.config.dump('*  ')))
85             self.log.ddebug('**************************')
86
87     def debug_dump_pipe(self, pipe):
88         "Dumps pipeline `pipe` at level DDEBUG (only compiles the dump if main level low enough)."
89         if self.log.level <= 5:
90             self.log.ddebug('****** Pipeline %r dump: ******'%pipe.name)
91             self.log.ddebug('\n'.join(pipe.dump(prefix='*  ')))
92             self.log.ddebug('**************************')
93     
94     def debug_dump_status(self):
95         "Dumps status metadata at level DDEBUG (only compiles the dump if main level low enough)."
96         if self.log.level <= 5:
97             self.log.ddebug('****** Status dump: ******')
98             self.log.ddebug('\n'.join(self.status.dump(prefix='*  ')))
99             self.log.ddebug('**************************')
100
101 def hook_init_dirs(e):
102     """(mainline at time 5) Create and check directories, fix directory variables.
103     .. note:: Currently only TDIR."""
104     e.config.fix('TDIR')
105     tdir = e['TDIR']
106     if os.path.isdir(tdir):
107         shutil.rmtree(tdir)
108     moe.util.mkdir_tree(tdir)
109
110 def hook_load_task_config(e):
111     """(mainline at time 15) Load `TASK_CONFIG` and check `PDIR`, fixes `TASK`, `PDIR`, `TASK_CONFIG`."""
112     e.config.fix(['TASK', 'PDIR', 'TASK_CONFIG'])
113     e.log.debug('Loading task config %s', self['TASK_CONFIG'])
114     if not os.path.isdir(e['PDIR']):
115         raise moe.MoeError, "No such task %s in %s" % (e['TASK'], self['PDIR'])
116     e.config.parse_file(self['TASK_CONFIG'], level=50)
117     e.debug_dump_config()
118
119     e.status["task"] = task  # Metadata
120
121 def hook_init_tasktype(e):
122     """(mainline at time 20) Fix `TASK_TYPE`, initialize task type module."""
123
124     e.config.fix('TASK_TYPE')
125     task_type = e['TASK_TYPE']
126     e.log.debug('Loading module for TASK_TYPE: %r', task_type)
127     if not task_type:
128         raise MoeError, "Invalid TASK_TYPE: %r" % e
129     try:
130         e.tasktype_module = utils.load_module('moe.tasktypes.' + task_type)
131     except ImportError:
132         e.log.exception()
133         raise MoeError, 'Unknown TASK_TYPE: %r' % task_type
134     mod.tasktype_module.init(e)
135
136 def hook_write_metadata(e):
137     """(mainline at time 90) Write status metadata into file `STATUS_FILE`."""
138     e.log.debug('Writing status file %s', self['STATUS_FILE'])
139     e.status.write(self['STATUS_FILE'])
140     # TODO: dump to ddebug      
141
142
143