server.py 5.95 KB
Newer Older
marius's avatar
marius committed
1
2
3
import tornado.ioloop
import grond
import os.path as op
Marius Isken's avatar
Marius Isken committed
4
5
import logging
import numpy as num  # noqa
marius's avatar
marius committed
6
7
8

from collections import OrderedDict

Marius Isken's avatar
Marius Isken committed
9
from tornado.web import RequestHandler, StaticFileHandler
marius's avatar
marius committed
10
11
12
from tornado import gen

from bokeh.embed import autoload_server
Marius Isken's avatar
Marius Isken committed
13
14
15
from bokeh.application import Application
from bokeh.server.server import Server as BokehServer
from bokeh.application.handlers import Handler as BokehHandler
marius's avatar
marius committed
16

Marius Isken's avatar
Marius Isken committed
17
18
from bokeh.models import ColumnDataSource
from bokeh import layouts
marius's avatar
marius committed
19
20
21
from bokeh.plotting import figure


Marius Isken's avatar
Marius Isken committed
22
23
24
25
26
27
28
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)
marius's avatar
marius committed
29

Marius Isken's avatar
Marius Isken committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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()
    r = num.sin(fr * misfits + pr) * 127 + 128
    g = num.sin(fg * misfits + pg) * 127 + 128
    b = num.sin(fb * misfits + pb) * 127 + 128
    return ['#%02x%02x%02x' % (r[i], g[i], b[i]) for i in xrange(misfits.size)]


class Status(RequestHandler):

    class MisfitsPlot(BokehHandler):

        def modify_document(self, doc):
            self.source = ColumnDataSource(
                data={'n': [],
                      'gm': [],
                      'bm': []})
            self.update_misfits()

            plot = figure(webgl=True,
                          x_axis_label='Iteration #',
                          y_axis_label='Misfit')
            plot.scatter('n', 'gm',
                         source=self.source, alpha=.4)

            doc.add_root(plot)
            doc.add_periodic_callback(self.update_misfits, 1e3)

        @gen.coroutine
        def update_misfits(self):
            mx, misfits = grond.core.load_problem_data(rundir, problem)
            fits = num.mean(misfits, axis=1)
            fits1 = fits[:, 0]
            fits2 = fits[:, 1]
            self.source.data = dict(gm=fits1,
                                    bm=fits2,
                                    n=num.arange(fits1.size) + 1)

    bokeh_handlers = {'misfit_plot': MisfitsPlot}

    @gen.coroutine
    def get(self):
        self.render('status.html',
                    pages=pages,
                    misfit_plot=autoload_server(None, url='/misfit_plot'),
                    problem=problem)


class Parameters(RequestHandler):

    class ParameterPlots(BokehHandler):

        ncols = 4

        def modify_document(self, doc):
            self.source = ColumnDataSource(
                data=dict.fromkeys(
                    ['n', 'color'] + [p.name for p in problem.parameters], []))
            self.update_parameters()

            plots = []
            for par in problem.parameters:
                fig = figure(webgl=True,
                             x_axis_label='Iteration #',
                             y_axis_label='%s [%s]' % (par.label, par.unit))
                fig.scatter('n', par.name,
                            source=self.source, alpha=.4,
                            color='color')
                plots.append(fig)
            plots += ([None] * (self.ncols - (len(plots) % self.ncols)))

            grid = layouts.gridplot(
                plots,
                responsive=True,
                ncols=self.ncols)

            doc.add_root(grid)
            doc.add_periodic_callback(self.update_parameters, 15*1e3)

        @gen.coroutine
        def update_parameters(self):
            mx, misfits = grond.core.load_problem_data(rundir, problem)
            data = {}
            for ip, par in enumerate(problem.parameters):
                data[par.name] = mx[:, ip]
            data['n'] = num.arange(mx.shape[0]) + 1

            mf = num.mean(misfits, axis=1)[:, 0]
            data['color'] = makeColorGradient(mf)

            self.source.data = data

    bokeh_handlers = {'parameter_plot': ParameterPlots}
marius's avatar
marius committed
128
129
130
131

    @gen.coroutine
    def get(self):

Marius Isken's avatar
Marius Isken committed
132
133
134
135
136
137
138
139
140
141
142
143
        self.render('parameter_plots.html',
                    pages=pages,
                    parameter_plot=autoload_server(
                        None,
                        url='/parameter_plot'),
                    problem=problem)


class Summary(RequestHandler):

    @gen.coroutine
    def get(self):
marius's avatar
marius committed
144
145
        self.render('summary.html',
                    pages=pages,
Marius Isken's avatar
Marius Isken committed
146
                    problem=problem)
marius's avatar
marius committed
147
148
149


class MainHandler(RequestHandler):
Marius Isken's avatar
Marius Isken committed
150

marius's avatar
marius committed
151
152
153
154
155
156
157
    @gen.coroutine
    def get(self):
        self.render('index.html',
                    pages=pages)


pages = OrderedDict([
Marius Isken's avatar
Marius Isken committed
158
159
    ('Status', Status),
    ('Parameters', Parameters),
marius's avatar
marius committed
160
161
162
163
    ('Summary', Summary),
    ('Sequences', MainHandler),
])

Marius Isken's avatar
Marius Isken committed
164
165
166
167
168
169
170
171
172
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())

marius's avatar
marius committed
173
174
if __name__ == '__main__':

Marius Isken's avatar
Marius Isken committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
    handlers = [(r'/', pages.values()[0])] +\
               [(r'/%s' % title, handler)
                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()
marius's avatar
marius committed
199
    tornado.ioloop.IOLoop.current().start()