statistics_engine.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # BSD 3-Clause License
3 
4 # Copyright (c) 2019, Noam C. Golombek
5 # All rights reserved.
6 
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 
10 # 1. Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
12 
13 # 2. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 
17 # 3. Neither the name of the copyright holder nor the names of its
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
20 
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 
33 
34 import timeit
35 import numpy as np
36 
37 
38 class StatisticsEngine(object):
39  """A class to calculate run statistics"""
40 
41  def __init__(self):
42  self.frame_count = 0
46  self.frame_times_tf = []
51  self.pure_tf_spent = 0
52  self.overhead_spent = 0
53 
54  self.time_start = timeit.default_timer()
55 
56  def append_to_frame_times_tf(self, value):
57  """Self descriptive"""
58  self.frame_times_tf.append(value)
59 
61  """Self descriptive"""
62  self.frame_times_no_drawing.append(value)
63 
64  def append_to_frame_times_inner(self, value):
65  """Self descriptive"""
66  self.frame_times_inner.append(value)
67 
68  def append_to_frame_times_outer(self, value):
69  """Self descriptive"""
70  self.frame_times_outer.append(value)
71 
72  def add_to_inner_time_spent(self, value):
73  """Self descriptive"""
74  self.inner_time_spent = value
75 
76  def add_to_pure_tf_spent(self, value):
77  """Self descriptive"""
78  self.pure_tf_spent = value
79 
80  def add_to_overhead_spent(self, value):
81  """Self descriptive"""
82  self.overhead_spent = value
83 
84  def set_time_end_of_start(self, value):
85  """Self descriptive"""
86  self.time_end_of_start = value
87 
88  def set_frame_count(self, value):
89  """Self descriptive"""
90  self.frame_count = value
91 
92  def set_images_sizes(self, original_image_size, processing_image_size):
93  """Self descriptive"""
94  self.original_image_size = original_image_size
95  self.processing_image_size = processing_image_size
96 
97  def process_statistics(self):
98  """Process the statistics of data received up to this point
99  Input: None
100  Output: (str)
101  Statistics Summary"""
102 
103  # Simple stats
104  time_end = timeit.default_timer() # record end time of capture
105 
106  nCount = self.frame_count + 1
107  stat_string = ""
108  stat_string += "\n" + '\n' + 8 * (6 * '-' + '\t')
109 
110  stat_string += "\n" + \
111  'Preparation spent:\t\t%.4g sec' % (
112  self.time_end_of_start - self.time_start)
113  stat_string += "\n" + \
114  "Runs spent:\t\t\t%.4g sec" % (time_end - self.time_end_of_start)
115  stat_string += "\n" + \
116  "Netto time per iteration:\t%.4g sec" % (
117  self.inner_time_spent / nCount)
118  if self.pure_tf_spent > 0:
119  stat_string += "\n" + \
120  "Time in tf per iteration:\t%.4g sec" % (
121  self.pure_tf_spent / nCount)
122 
123  stat_string += "\n" + \
124  "Overhead per iteration:\t\t%.4g sec" % (
125  self.overhead_spent/nCount)
126 
127  stat_string += "\n"
128  str_to_logging = "Processing Image Size:\t\t" + \
129  np.round(self.processing_image_size).astype(int).__str__()
130  if 'original_image_size' in globals():
131  str_to_logging += "\tfrom\t" + \
132  np.round(self.original_image_size).astype(int).__str__()
133 
134  stat_string += "\n" + str_to_logging
135  stat_string += "\n" + "Number of frames:\t\t%d" % nCount
136 
137  stat_string += "\n" + 8 * (6 * '-' + '\t') + '\n\n'
138 
139  # Inner Stats:
140  def get_stat_array(a):
141  return (np.around(np.min(a[1:]), decimals=4),
142  np.around(np.mean(a[1:]), decimals=4),
143  np.around(np.max(a[1:]), decimals=4),
144  np.around(np.std(a[1:]), decimals=4))
145 
146  stat_string += "\n" + \
147  '\tStatistics:\t| \t Min \t|\t Mean \t|\t Max \t|\t Std \t|'
148  if len(self.frame_times_tf) > 0:
149  stat_string += "\n" + '\tTf Frames:\t|' + (4 * '\t %06.4f \t|') % \
150  (get_stat_array(self.frame_times_tf))
151 
152  stat_string += "\n" + '\tProc w/o draw:\t|' + (4 * '\t %06.4f \t|') % \
153  (get_stat_array(self.frame_times_no_drawing))
154 
155  stat_string += "\n" + '\tDrawing(only):\t|' + (4 * '\t %06.4f \t|') % \
156  (get_stat_array(np.subtract(self.frame_times_inner, self.frame_times_no_drawing)))
157 
158  stat_string += "\n" + '\tInner Frames: \t|' + (4 * '\t %06.4f \t|') % \
159  (get_stat_array(self.frame_times_inner))
160 
161  stat_string += "\n" + '\tOuter Frames: \t|' + (4 * '\t %06.4f \t|') % \
162  (get_stat_array(self.frame_times_outer))
163 
164  stat_string += "\n" + 8 * (6 * '-' + '\t') + '\n\n'
165 
166  # calculate processed frames per second
167  calc_fps = float(nCount /
168  (time_end - self.time_end_of_start))
169  stat_string += "\n" + 'Estimated process per second rate: %.4g fps' % calc_fps
170 
171  return stat_string
def set_images_sizes(self, original_image_size, processing_image_size)


cnn_bridge
Author(s): Noam C. Golombek , Alexander Beringolts
autogenerated on Mon Jun 10 2019 12:53:26