Commit 0920e2ba authored by Marius Isken's avatar Marius Isken
Browse files

classing Baraddur // Config // flexible rundir

parent 9aa78392
......@@ -2,10 +2,12 @@ import tornado.ioloop
import grond
import os.path as op
import logging
import numpy as num # noqa
import numpy as num
from collections import OrderedDict
from pyrocko.guts import Object, Bool, String # noqa
from tornado.web import RequestHandler, StaticFileHandler
from tornado import gen
......@@ -19,18 +21,6 @@ from bokeh import layouts
from bokeh.plotting import figure
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
rootLogger = logging.getLogger('')
rootLogger.addHandler(console)
rootLogger.setLevel(logging.DEBUG)
rundir = '/home/marius/Development/testing/grond/rundir'
problem = grond.core.load_problem_info(rundir)
def makeColorGradient(misfits, fr=1., fg=.5, fb=1.,
pr=0, pg=2.5, pb=4):
misfits /= misfits.max()
......@@ -40,9 +30,20 @@ def makeColorGradient(misfits, fr=1., fg=.5, fb=1.,
return ['#%02x%02x%02x' % (r[i], g[i], b[i]) for i in xrange(misfits.size)]
class Status(RequestHandler):
class BaraddurRequestHandler(RequestHandler):
def initialize(self, config):
self.config = config
class MisfitsPlot(BokehHandler):
class BaraddurBokehHandler(BokehHandler):
def __init__(self, config, *args, **kwargs):
BokehHandler.__init__(self, *args, **kwargs)
self.config = config
class Status(BaraddurRequestHandler):
class MisfitsPlot(BaraddurBokehHandler):
def modify_document(self, doc):
self.nmodels = 0
......@@ -63,7 +64,8 @@ class Status(RequestHandler):
@gen.coroutine
def update_misfits(self):
mx, misfits = grond.core.load_problem_data(
rundir, problem, skip_models=self.nmodels)
self.config.rundir, self.config.problem,
skip_models=self.nmodels)
new_nmodels = mx.shape[0]
fits = num.mean(misfits, axis=1)
......@@ -80,17 +82,19 @@ class Status(RequestHandler):
self.render('status.html',
pages=pages,
misfit_plot=autoload_server(None, url='/misfit_plot'),
problem=problem)
problem=self.config.problem)
class Parameters(RequestHandler):
class Parameters(BaraddurRequestHandler):
class ParameterPlots(BokehHandler):
class ParameterPlots(BaraddurBokehHandler):
ncols = 4
def modify_document(self, doc):
self.nmodels = 0
problem = self.config.problem
self.source = ColumnDataSource()
for p in ['n'] + [p.name for p in problem.parameters]:
self.source.add([], p)
......@@ -116,8 +120,14 @@ class Parameters(RequestHandler):
@gen.coroutine
def update_parameters(self):
mx, misfits = grond.core.load_problem_data(
rundir, problem, skip_models=self.nmodels)
problem = self.config.problem
try:
mx, misfits = grond.core.load_problem_data(
self.config.rundir, problem, skip_models=self.nmodels)
except IOError:
return
new_nmodels = mx.shape[0]
new_data = {}
......@@ -139,19 +149,19 @@ class Parameters(RequestHandler):
parameter_plot=autoload_server(
None,
url='/parameter_plot'),
problem=problem)
problem=self.config.problem)
class Summary(RequestHandler):
class Summary(BaraddurRequestHandler):
@gen.coroutine
def get(self):
self.render('summary.html',
pages=pages,
problem=problem)
problem=self.config.problem)
class MainHandler(RequestHandler):
class MainHandler(BaraddurRequestHandler):
@gen.coroutine
def get(self):
......@@ -166,39 +176,80 @@ pages = OrderedDict([
('Sequences', MainHandler),
])
bokeh_apps = {}
for cat, tornado_handler in pages.iteritems():
handler_docs = getattr(tornado_handler, 'bokeh_handlers', None)
if handler_docs is None:
continue
for url, bokeh_handler in handler_docs.iteritems():
bokeh_apps['/%s' % url] =\
Application(bokeh_handler())
if __name__ == '__main__':
handlers = [(r'/', pages.values()[0])] +\
[(r'/%s' % title, handler)
class BaraddurConfig(Object):
rundir = String.T()
template_path = String.T(
default='tpl',
optional=True)
debug = Bool.T(
default=True,
optional=True)
@property
def problem(self):
return grond.core.load_problem_info(self.rundir)
class Baraddur(BokehServer):
def __init__(self, rundir=None, *args, **kwargs):
self.config = BaraddurConfig(rundir=rundir)
BokehServer.__init__(
self,
self.get_bokeh_apps(),
io_loop=tornado.ioloop.IOLoop.current(),
extra_patterns=self.get_tornado_handlers(),
hosts='0.0.0.0')
tornado_app = self._tornado
tornado_app.settings['template_path'] = self.config.template_path
if self.config.debug:
tornado_app.settings.setdefault('autoreload', True)
tornado_app.settings.setdefault('compiled_template_cache', False)
tornado_app.settings.setdefault('static_hash_cache', False)
tornado_app.settings.setdefault('serve_traceback', True)
# Automatically reload modified modules
from tornado import autoreload
autoreload.start()
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
rootLogger = logging.getLogger('')
rootLogger.addHandler(console)
rootLogger.setLevel(logging.DEBUG)
def get_bokeh_apps(self):
bokeh_apps = {}
for tornado_handler in pages.itervalues():
handler_docs = getattr(tornado_handler, 'bokeh_handlers',
None)
if handler_docs is None:
continue
for url, bokeh_handler in handler_docs.iteritems():
bokeh_apps['/%s' % url] = Application(bokeh_handler(
self.config))
return bokeh_apps
def get_tornado_handlers(self):
return [(r'/', pages.values()[0])] +\
[(r'/%s' % title, handler,
{'config': self.config})
for title, handler in pages.iteritems()] +\
[(r'/css/(.*)', StaticFileHandler,
{'path': op.join(op.dirname(__file__), 'css')})]
server = BokehServer(
bokeh_apps,
io_loop=tornado.ioloop.IOLoop.current(),
extra_patterns=handlers,
hosts='0.0.0.0')
tornado_app = server._tornado
tornado_app.settings['template_path'] = 'tpl'
tornado_app.settings.setdefault('autoreload', True)
tornado_app.settings.setdefault('compiled_template_cache', False)
tornado_app.settings.setdefault('static_hash_cache', False)
tornado_app.settings.setdefault('serve_traceback', True)
# Automatically reload modified modules
from tornado import autoreload
autoreload.start()
server.start()
tornado.ioloop.IOLoop.current().start()
def start(self):
BokehServer.start(self)
tornado.ioloop.IOLoop.current().start()
if __name__ == '__main__':
baraddur = Baraddur(
rundir='/home/marius/Development/testing/grond/rundir')
baraddur.start()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment