Browse Source

Fixes centering of bars in hist_plot_stack and moves dashboard
generation into filval

Caleb Fangmeier 7 years ago
parent
commit
8446b2f3f6
2 changed files with 39 additions and 2 deletions
  1. 1 1
      filval/histogram_utils.py
  2. 38 1
      filval/plotter.py

+ 1 - 1
filval/histogram_utils.py

@@ -92,7 +92,7 @@ def hist_integral(hist, times_bin_width=True):
         return sum(values)
 
 
-def hist_normalize(hist, norm):
+def hist_normalize(hist, norm = 1):
     values, errors, edges = hist
     scale = norm/np.sum(values)
     return values*scale, errors*scale, edges

+ 38 - 1
filval/plotter.py

@@ -15,6 +15,7 @@ __all__ = ['Plot',
            'decl_plot',
            'grid_plot',
            'render_plots',
+           'generate_dashboard',
            'hist_plot',
            'hist_plot_stack',
            'hist2d_plot']
@@ -78,6 +79,42 @@ def decl_plot(fn):
     return f
 
 
+def generate_dashboard(plots, title, output='dashboard.htm', source_file=None):
+    from jinja2 import Environment, PackageLoader, select_autoescape
+    from os.path import join
+    from urllib.parse import quote
+
+    env = Environment(
+        loader=PackageLoader('plots', 'templates'),
+        autoescape=select_autoescape(['htm', 'html', 'xml']),
+    )
+    env.globals.update({'quote': quote,
+                        'enumerate': enumerate,
+                        'zip': zip,
+                        })
+
+    def render_to_file(template_name, **kwargs):
+        with open(join('output', output), 'w') as tempout:
+            template = env.get_template(template_name)
+            tempout.write(template.render(**kwargs))
+
+    def get_by_n(objs, n=2):
+        objs = list(objs)
+        while objs:
+            yield objs[:n]
+            objs = objs[n:]
+
+    if source_file is not None:
+        with open(source_file, 'r') as this_file:
+            source = this_file.read()
+    else:
+        source = "# Not supplied!!"
+
+    render_to_file('dashboard.htm', plots=get_by_n(plots, 3),
+                   title=title, source=source,
+                   outdir="figures/")
+
+
 def _add_stats(hist, title=''):
     fmt = r'''\begin{{eqnarray*}}
 \sum{{x_i}} &=& {sum:5.3f}                  \\
@@ -288,7 +325,7 @@ def hist_plot_stack(hists: list, labels: list = None):
         widths = []
         heights = []
         for left, right, content in zip(hist[2][:-1], hist[2][1:], hist[0]):
-            centers.append((right+left)/2)
+            centers.append((right + left) / 2)
             widths.append(right - left)
             heights.append(content)