pydfnWorks
python wrapper for dfnWorks
plot_fram_information.py
Go to the documentation of this file.
1 """
2  :filename: gen_output.py
3  :synopsis: Main driver for dfnGen output report
4  :version: 1.0
5  :maintainer: Jeffrey Hyman
6  :moduleauthor: Jeffrey Hyman <jhyman@lanl.gov>
7 """
8 
9 import matplotlib.pylab as plt
10 import numpy as np
11 
12 
14  """ Gathers information from the file 'rejections.dat' about FRAM and creates a bar-chart named fram_information.png
15 
16  Parameters
17  -------------
18  params : dictionary
19  Output report dictionary containing general parameters. See output_report for more details
20 
21  Returns
22  -----------
23  None
24 
25  Notes
26  --------
27  Output image is named fram_information.png in the network sub-directory
28 
29  """
30  # Gather Data
31  if params["verbose"]:
32  print("--> Plotting FRAM information")
33 
34  rejections = {}
35  with open('rejections.dat', "r") as fp:
36  for line in fp.readlines():
37  parsed_line = line.split(": ")
38  variable = parsed_line[0]
39  value = parsed_line[1].rstrip()
40  rejections[variable] = int(value)
41  total_number_of_rejections = float(sum(rejections.values()))
42  fig, axs = plt.subplots(figsize=(16, 8))
43  plt.title("FRAM information")
44 
45  labels = []
46  cnts = []
47  for key in rejections.keys():
48  labels.append(key)
49  cnts.append(rejections[key])
50 
51  cnts = np.asarray(cnts)
52  sort_index = np.argsort(cnts)[::-1]
53  cnts = cnts[sort_index]
54  new_labels = []
55  for i in sort_index:
56  new_labels.append(labels[i])
57  labels = new_labels
58 
59  # Make a bar chart!
60  y_pos = np.arange(len(labels))
61  h = 0.8
62  horizBar = axs.barh(y_pos,
63  cnts,
64  color="k",
65  alpha=0.6,
66  height=h,
67  align='center')
68 
69  axs.set_yticks(y_pos)
70  axs.set_yticklabels(labels, fontsize=14)
71  axs.invert_yaxis() # labels read top-to-bottom
72  axs.set_xlabel('Number of re-samples', fontsize=14)
73  axs.set_xticklabels(axs.get_xticks().astype(int), fontsize=16)
74  axs.grid(alpha=0.1)
75  axs.axis([0, 1.1 * max(cnts), axs.get_ylim()[0], axs.get_ylim()[1]])
76  axs.set_title("Re-sampling Histogram", fontsize=18)
77 
78  # Add notes to the end of bars
79  for bar in horizBar:
80  width = bar.get_width()
81  label = f" {int(width):d}\n {100*width/total_number_of_rejections:0.2f}\%"
82  axs.text(bar.get_width() + 2,
83  bar.get_y() + 0.5 * h,
84  label,
85  va='center',
86  fontsize=16)
87  plt.savefig(f"{params['output_dir']}/network/fram_information.png")