]> mj.ucw.cz Git - moe.git/blob - t/moe/eval.py
Fixes around DEBUG_LEVEL
[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.Loggers()
20         self.config = moe.config.ConfigTree()
21         self.main_pipe = moe.pipeline.MoePipeline("main")
22         self.test_pipe = moe.pipeline.MoePipeline("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", self.hook_init_dirs)
56         self.main_pipe.insert(15, "Eval.hook_load_task_config", self.hook_load_task_config)
57         self.main_pipe.insert(20, "Eval.hook_init_tasktype", self.hook_init_tasktype)
58         self.main_pipe.insert(90, "Eval.hook_write_metadata", self.hook_write_metadata)
59
60         # ininialize extensions (let them insert hooks) 
61         self.conf.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 = 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.debug('Running main pipeline')
78         self.main_pipe.run(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(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(pipe.dump(prefix='**** '))
92             self.log.ddebug('**************************')
93
94     def hook_init_dirs(self):
95         """(mainline at time 5) Create and check directories, fix directory variables.
96         .. note:: Currently only TDIR."""
97         self.config.fix('TDIR')
98         tdir = self['TDIR']
99         if os.path.isdir(tdir):
100             shutil.rmtree(tdir)
101         moe.util.mkdir_tree(tdir)
102     
103     def hook_load_task_config(self):
104         """(mainline at time 15) Load `TASK_CONFIG` and check `PDIR`, fixes `TASK`, `PDIR`, `TASK_CONFIG`."""
105         self.config.fix(['TASK', 'PDIR', 'TASK_CONFIG'])
106         self.log.debug('Loading task config %s', self['TASK_CONFIG'])
107         if not os.path.isdir(self['PDIR']):
108             raise moe.MoeError, "No such task %s in %s" % (self['TASK'], self['PDIR'])
109         self.config.parse_file(self['TASK_CONFIG'], level=50)
110         self.debug_dump_config()
111
112         self.stat["task"] = task  # Metadata
113     
114     def hook_init_tasktype(self):
115         """(mainline at time 20) Fix `TASK_TYPE`, initialize task type module."""
116
117         self.config.fix('TASK_TYPE')
118         task_type = self['TASK_TYPE']
119         self.log.debug('Loading module for TASK_TYPE: %r', task_type)
120         if not task_type:
121             raise MoeError, "Invalid TASK_TYPE: %r" % e
122         try:
123             self.tasktype_module = utils.load_module('moe.tasktypes.' + task_type)
124         except ImportError:
125             self.log.exception()
126             raise MoeError, 'Unknown TASK_TYPE: %r' % task_type
127         mod.tasktype_module.init(self)
128
129     def hook_write_metadata(self):
130         """(mainline at time 90) Write status metadata into file `STATUS_FILE`."""
131         self.log.debug('Writing status file %s', self['STATUS_FILE'])
132         self.status.write(self['STATUS_FILE'])
133         # TODO: dump to ddebug  
134
135
136