Belle II Software  release-08-01-10
entities.py
1 
8 import numpy as np
9 
10 from hep_ipython_tools.entities import Statistics, StatisticsColumn
11 
12 
14 
15  """
16  Dictionary list holding the module statistics (like the C++ class ModuleStatistics)
17  """
18 
19  def __init__(self, stats, categories):
20  """Create a new module statistics entity."""
21 
22  self.namename = stats.name
23 
24  self.time_sumtime_sum = self.get_dictget_dict(stats.time_sum, categories)
25 
26  self.time_meantime_mean = self.get_dictget_dict(stats.time_mean, categories)
27 
28  self.time_stddevtime_stddev = self.get_dictget_dict(stats.time_stddev, categories)
29 
30  self.memory_summemory_sum = self.get_dictget_dict(stats.memory_sum, categories)
31 
32  self.memory_meanmemory_mean = self.get_dictget_dict(stats.memory_mean, categories)
33 
34  self.memory_stddevmemory_stddev = self.get_dictget_dict(stats.memory_stddev, categories)
35 
36  self.callscalls = self.get_dictget_dict(stats.calls, categories)
37 
38  @staticmethod
39  def get_dict(function, categories):
40  """
41  Call the function on each information in the categories and return a dict
42  name -> function(value)
43  """
44  return {name: function(category) for name, category in categories}
45 
46  def __getitem__(self, item):
47  """Convenience function for the display."""
48  if item == "name":
49  return self.namename
50  elif item == "calls":
51  return self.callscalls["EVENT"]
52  elif item == "mem":
53  return self.memory_meanmemory_mean["EVENT"] * 1E-3
54  elif item == "time":
55  return np.round(self.time_sumtime_sum["EVENT"] * 1E-9, 2)
56  elif item == "eventtime":
57  return np.round(self.time_meantime_mean["EVENT"] * 1E-6, 2), "±", np.round(self.time_stddevtime_stddev["EVENT"] * 1E-6, 2)
58 
59 
61 
62  """
63  As the ipython_handler_basf2 statistics is not pickable, we can not store it into the queue.
64  So we write a wrapper which unpacks the needed properties.
65  """
66 
67  def __str__(self):
68  """ Make the str behave like before """
69  return self.strstr
70 
71  def __repr__(self):
72  """ Make the repr behave like before """
73  return self.strstr
74 
75  def __init__(self, statistics):
76  """ Initialize with the C++ statistics """
77  modules = []
78 
79  columns = [StatisticsColumn("name", "Name"),
80  StatisticsColumn("calls", "Calls"), StatisticsColumn("mem", "VMemory (MB)"),
81  StatisticsColumn("time", "Time (s)"), StatisticsColumn("eventtime", "Time (ms)/Call", True)]
82 
83  categories = [("INIT", statistics.INIT),
84  ("BEGIN_RUN", statistics.BEGIN_RUN),
85  ("EVENT", statistics.EVENT),
86  ("END_RUN", statistics.END_RUN),
87  ("TERM", statistics.TERM),
88  ("TOTAL", statistics.TOTAL)]
89 
90  for stats in statistics.modules:
91  modules.append(ModuleStatistics(stats, categories))
92 
93  modules.append(ModuleStatistics(statistics.get_global(), categories))
94 
95 
96  self.strstr = statistics()
97 
98  super().__init__(columns, modules)