10 import tornado.httpserver
13 __folder__ = os.path.abspath(os.path.dirname(__file__))
17 template_folder=os.path.join(__folder__,
"templates"),
18 static_folder=os.path.join(__folder__,
"static")
23 def jsonify_nvd3(chart):
30 script_start = chart.htmlcontent.find(
'<script>') + 8
31 script_end = chart.htmlcontent.find(
'</script>')
33 result=chart.container,
34 script=chart.htmlcontent[script_start:script_end].strip()
38 def visualize_summary(filename):
40 data = np.loadtxt(filename)
41 except Exception
as e:
42 return 'Cannot load file {}: {}'.format(filename, str(e))
43 chart_name = os.path.splitext(os.path.basename(filename))[0]
44 chart = nvd3.lineChart(
45 name=chart_name +
'_summary_chart',
46 height=args.chart_height,
50 step = max(data.shape[0] / -args.sample, 1)
53 xdata = np.arange(0, data.shape[0], step)
55 chart.add_serie(x=xdata, y=data[xdata, 0], name=
'min')
56 chart.add_serie(x=xdata, y=data[xdata, 1], name=
'max')
57 chart.add_serie(x=xdata, y=data[xdata, 2], name=
'mean')
58 chart.add_serie(x=xdata, y=data[xdata, 2] + data[xdata, 3], name=
'm+std')
59 chart.add_serie(x=xdata, y=data[xdata, 2] - data[xdata, 3], name=
'm-std')
60 return jsonify_nvd3(chart)
63 def visualize_print_log(filename):
65 data = np.loadtxt(filename)
67 data = data[:, np.newaxis]
68 except Exception
as e:
69 return 'Cannot load file {}: {}'.format(filename, str(e))
70 chart_name = os.path.splitext(os.path.basename(filename))[0]
71 chart = nvd3.lineChart(
72 name=chart_name +
'_log_chart',
73 height=args.chart_height,
77 step = max(data.shape[0] / -args.sample, 1)
80 xdata = np.arange(0, data.shape[0], step)
82 if data.shape[1] == 1:
84 trunc_size = data.shape[0] / step
85 running_mat = data[:trunc_size * step].reshape((trunc_size, step))
88 y=running_mat.min(axis=1),
93 y=running_mat.max(axis=1),
96 chart.add_serie(x=xdata, y=data[xdata, 0], name=chart_name)
98 for i
in range(0, min(data.shape[1], args.max_curves)):
103 name=
'{}[{}]'.format(chart_name, i)
106 return jsonify_nvd3(chart)
109 def visualize_file(filename):
110 fullname = os.path.join(args.root, filename)
111 if filename.endswith(
'summary'):
112 return visualize_summary(fullname)
113 elif filename.endswith(
'log'):
114 return visualize_print_log(fullname)
116 return flask.jsonify(
117 result=
'Unsupport file: {}'.format(filename),
124 files = glob.glob(os.path.join(args.root,
"*.*"))
126 names = [os.path.basename(f)
for f
in files]
127 return flask.render_template(
135 @app.route(
'/visualization/<string:name>')
136 def visualization(name):
137 ret = visualize_file(name)
142 parser = argparse.ArgumentParser(
"The mint visualizer.")
148 help=
"The flask port to use." 155 help=
"The root folder to read files for visualization." 161 help=
"The max number of curves to show in a dump tensor." 167 help=
"The chart height for nvd3." 174 help=
"Sample every given number of data points. A negative " 175 "number means the total points we will sample on the " 176 "whole curve. Default 100 points." 179 args = parser.parse_args(argv)
180 server = tornado.httpserver.HTTPServer(tornado.wsgi.WSGIContainer(app))
181 server.listen(args.port)
182 print(
"Tornado server starting on port {}.".format(args.port))
183 tornado.ioloop.IOLoop.instance().start()
186 if __name__ ==
'__main__':