View.py
Go to the documentation of this file.
1 import Controller
2 import common_tools as tools
3 import view_tools
4 from BatchLog import BatchLog
5 import os
6 import Canvas
7 import styles
8 import naming_schema
9 import cPickle
10 
12 
13  def __init__(self, controller, verbose=True):
14  self.verbose = verbose
15  self.controller = controller
16  self.plot_base = "%s/%s/v%s/figures"%(os.environ["NOVAANAVALID_GRID"],
17  self.controller.name,
18  self.controller.version)
19  tools.mkdir(self.plot_base)
20  if self.verbose: print "view : ControllerView plot base: %s"%self.plot_base
22 
23  self.plot_groups = {}
24 
25  def component_view(self, canvas):
26  """
27  This will compose a view of each component of the controller
28  individually.
29 
30  These plots are order by category (reco, true, mixed), with
31  one page provided for each dataset and one page provided
32  overlaying all datasets.
33 
34  Components that are not complete are skipped
35  """
36  if self.verbose: print "view : Controller %s has %i component types"%(self.controller.name,len(self.component_type_keys))
37 
38  for component_type in self.component_type_keys:
39  if self.verbose:
40  print "view : Component type: %s, has %i instances"%(component_type, len(self.component_types[component_type]))
41 
42  self.find_complete_components(component_type)
43  if self.verbose: print "view : found %i complete components"%len(self.complete_components)
44 
45  if len(self.complete_components):
46  self.plot_groups[component_type] = []
47  self.draw_component_plots(canvas)
48 
49  if self.verbose: print "view : plot groups found: %s"%repr(self.plot_groups[component_type])
50 
51  def draw_component_plots(self, canvas):
52  plots = {}
53  open_files = []
54 
55  plot_names = []
56  self.plot_information = {}
57  # loop over the current complete components extracting plots and draw them stand-alone
58  for component in self.complete_components:
59  plots[component.dataset] = {}
60  plot_dict, open_root_file = tools.openPlots(component.root_file, skip_2d=True)
61  # if self.verbose: print "view : component %s, dataset: %s, opened %i histograms"%(component.name, component.dataset, len(plot_dict.keys()))
62  open_files.append(open_root_file)
63 
64  component.plot_dir = "%s/%s/v%s/%s"%(self.plot_base, component.name, component.version, component.dataset)
65  tools.mkdir(component.plot_dir)
66 
67  for plot_name in plot_dict.keys():
68  plot_names.append(plot_name)
69  plots[component.dataset][plot_name] = plot_dict[plot_name]
70 
71  name = "%s/%s.png"%(component.plot_dir,plot_name)
72  draw_config = styles.parseDrawingConfig(plot_name, [component])
73  canvas.draw([plot_dict[plot_name]],
74  draw_config.labels,
75  draw_config.axis_labels,
76  colours=draw_config.colours,
77  lines=draw_config.lines,
78  draw_options=draw_config.draw_options,
79  save_as=name)
80 
81  self.plot_information[name] = tools.getPlotStats([plot_dict[plot_name]])
82  self.plot_information[name].datasets = [component.dataset]
83  self.plot_information[name].names = [naming_schema.datasetToLabel(component.dataset)]
84  self.plot_information[name].component_name = component.name
85  self.plot_information[name].component_version = component.version
86  self.plot_information[name].controller_name = self.controller.name
87  self.plot_information[name].controller_version = self.controller.version
88  self.plot_information[name].group = naming_schema.datasetToLabel(component.dataset)
89  if self.plot_information[name].group not in self.plot_groups[component.name]: self.plot_groups[component.name].append(self.plot_information[name].group)
90 
91  plot_names = list(set(plot_names))
92  # next draw them overlaid
93  self.group_components()
94  for plot_name in plot_names:
95  for component_group in self.grouped_components:
96  components = []
97  for component in component_group:
98  if (plot_name not in plots[component.dataset].keys()): continue
99  components.append(component)
100  if len(components) < 2: continue
101 
102  to_show = [plots[c.dataset][plot_name] for c in components]
103  joined_name = "_".join([c.dataset for c in components])
104 
105  overlay_dir = "%s/%s/v%s/%s"%(self.plot_base, component.name, component.version, joined_name)
106  tools.mkdir(overlay_dir)
107 
108  name = "%s/%s.png"%(overlay_dir,plot_name)
109  draw_config = styles.parseDrawingConfig(plot_name, [c for c in components])
110  canvas.draw(to_show,
111  draw_config.labels,
112  draw_config.axis_labels,
113  colours=draw_config.colours,
114  lines=draw_config.lines,
115  draw_options=draw_config.draw_options,
116  save_as=name)
117 
118  self.plot_information[name] = tools.getPlotStats(to_show)
119  self.plot_information[name].datasets = [c.dataset for c in components]
120  self.plot_information[name].names = [naming_schema.datasetToLabel(c.dataset) for c in components]
121  self.plot_information[name].component_name = component.name
122  self.plot_information[name].component_version = component.version
123  self.plot_information[name].controller_name = self.controller.name
124  self.plot_information[name].controller_version = self.controller.version
125  if "_nd_" in component.dataset:
126  self.plot_information[name].group = "ND overlays"
127  else:
128  self.plot_information[name].group = "FD overlays"
129 
130  if self.plot_information[name].group not in self.plot_groups[component.name]: self.plot_groups[component.name].append(self.plot_information[name].group)
131 
132  for open_file in open_files:
133  open_file.Close()
134 
135  def find_complete_components(self, component_type):
137  for i_c,component in enumerate(self.component_types[component_type]):
138  if self.verbose: print "view : [%i]: dataset: %s"%(i_c, component.dataset)
139 
140  if (not self.check_log_status(component)): continue
141  if (not self.check_root_files(component)): continue
142 
143  self.complete_components.append(component)
144 
145  def check_root_files(self, component):
146  # for now only accept components that produce exactly one root file
147  root_files = tools.findRootOutput(component)
148  if len(root_files) != 1:
149  print "view: component %s, has %i root files"%(component.name, len(root_files))
150  tools.findRootOutput(component, verbose=True)
151  return False
152  # to avoid repeated calls lets assign the root file to the component
153  component.root_file = root_files[0]
154  return True
155 
156  def check_log_status(self, component):
157  job_sub_id = tools.getJobID(component.lines)
158  logs = BatchLog(tools.fetchLogs(job_sub_id))
159 
160  if (logs.log_status() != "completed") or (logs.err_status() != "no error"):
161  # print "view : component hasn't completed successfully"
162  print "view : - log status: ",logs.log_status()
163  print "view : - err status: ",logs.err_status()
164  return False
165 
166  return True
167 
169  """
170  return a mapping of component types to components
171  """
172  self.component_types = {}
173  for component in self.controller.components:
174  if component.name in ["hello_world"]: continue
175  if component.name not in self.component_types.keys():
176  self.component_types[component.name] = []
177  self.component_types[component.name].append(component)
178  self.component_type_keys = self.component_types.keys()
179  self.component_type_keys.sort()
180 
181  def group_components(self):
182  """
183  this looks at the current complete components and groups them
184  """
186  # ND and FD
187  nd_components = [c for c in self.complete_components if "_nd_" in c.dataset]
188  if len(nd_components): self.grouped_components.append(nd_components)
189 
190  fd_components = [c for c in self.complete_components if "_fd_" in c.dataset]
191  if len(fd_components): self.grouped_components.append(fd_components)
192 
193  def write(self, output_directory):
194  self.output_directory = output_directory
195  self.file_name = self.output_directory+"/controller_view.pkl"
196  print "view : Saving as: %s"%self.file_name
197  cPickle.dump(self,open(self.file_name,"w"))
keys
Reco plots.
Definition: caf_analysis.py:46
def draw_component_plots(self, canvas)
Definition: View.py:51
def __init__(self, controller, verbose=True)
Definition: View.py:13
def check_root_files(self, component)
Definition: View.py:145
def find_component_types(self)
Definition: View.py:168
def write(self, output_directory)
Definition: View.py:193
def check_log_status(self, component)
Definition: View.py:156
def component_view(self, canvas)
Definition: View.py:25
def find_complete_components(self, component_type)
Definition: View.py:135
def parseDrawingConfig(plot_name, components, verbose=False)
parse drawing configuration options from the name of a histogram and a list of components ...
Definition: styles.py:10
def datasetToLabel(name)
convert the dataset name to a label
procfile open("FD_BRL_v0.txt")
def group_components(self)
Definition: View.py:181