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