sot-talos-balance  1.6.0
statistics.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2017 CNRS
3 //
4 // This file is part of tsid
5 // tsid is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 // tsid is distributed in the hope that it will be
10 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
11 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Lesser Public License for more details. You should have
13 // received a copy of the GNU Lesser General Public License along with
14 // tsid If not, see
15 // <http://www.gnu.org/licenses/>.
16 //
17 
18 #include <iomanip> // std::setprecision
20 
21 using std::map;
22 using std::string;
23 using std::ostringstream;
24 
25 
27 {
28  static Statistics s;
29  return s;
30 }
31 
33  : active(true)
34 {
35  records_of = new map<string, QuantityData>();
36 }
37 
39 {
40  delete records_of;
41 }
42 
43 bool Statistics::quantity_exists(string name)
44 {
45  return (records_of->find(name) != records_of->end());
46 }
47 
48 void Statistics::store(string name, const double & value)
49 {
50  if (!active) return;
51 
52  // Just works if not already present
53  records_of->insert(make_pair(name, QuantityData()));
54 
55  QuantityData& quant_info = records_of->find(name)->second;
56 
57  quant_info.stops++;
58 
59  // Update last value
60  quant_info.last = value;
61 
62  // Update min/max
63  if ( value >= quant_info.max )
64  quant_info.max = value;
65  if ( value <= quant_info.min || quant_info.min == 0 )
66  quant_info.min = value;
67 
68  // Update total
69  quant_info.total += value;
70 }
71 
73 {
74  if (!active) return;
75 
76  map<string, QuantityData>::iterator it;
77 
78  for (it = records_of->begin(); it != records_of->end(); ++it) {
79  reset(it->first);
80  }
81 }
82 
83 void Statistics::report_all(int precision, std::ostream& output)
84 {
85  if (!active) return;
86 
87  output<< "\n*** STATISTICS (min - avg - max - last - nSamples - total) ***\n";
88  map<string, QuantityData>::iterator it;
89  for (it = records_of->begin(); it != records_of->end(); ++it) {
90  if(it->second.stops>0)
91  report(it->first, precision, output);
92  }
93 }
94 
95 void Statistics::reset(string name)
96 {
97  if (!active) return;
98 
99  // Try to recover Quantity data
100  if ( !quantity_exists(name) )
101  throw StatisticsException("Quantity not initialized.");
102 
103  QuantityData& quant_info = records_of->find(name)->second;
104 
105  quant_info.total = 0;
106  quant_info.min = 0;
107  quant_info.max = 0;
108  quant_info.last = 0;
109  quant_info.stops = 0;
110 }
111 
113 {
114  std::cout << "Statistics active." << std::endl;
115  active = true;
116 }
117 
119 {
120  std::cout << "Statistics inactive." << std::endl;
121  active = false;
122 }
123 
124 void Statistics::report(string name, int precision, std::ostream& output)
125 {
126  if (!active) return;
127 
128  // Try to recover Quantity data
129  if ( !quantity_exists(name) )
130  throw StatisticsException("Quantity not initialized.");
131 
132  QuantityData& quant_info = records_of->find(name)->second;
133 
134  string pad = "";
135  for (std::string::size_type i = name.length(); i<STATISTICS_MAX_NAME_LENGTH; i++)
136  pad.append(" ");
137 
138  output << name << pad;
139  output << std::fixed << std::setprecision(precision)
140  << (quant_info.min) << "\t";
141  output << std::fixed << std::setprecision(precision)
142  << (quant_info.total / (long double) quant_info.stops) << "\t";
143  output << std::fixed << std::setprecision(precision)
144  << (quant_info.max) << "\t";
145  output << std::fixed << std::setprecision(precision)
146  << (quant_info.last) << "\t";
147  output << std::fixed << std::setprecision(precision)
148  << quant_info.stops << "\t";
149  output << std::fixed << std::setprecision(precision)
150  << quant_info.total << std::endl;
151 }
152 
153 long double Statistics::get_total(string name)
154 {
155  // Try to recover Quantity data
156  if ( !quantity_exists(name) )
157  throw StatisticsException("Quantity not initialized.");
158 
159  QuantityData& quant_info = records_of->find(name)->second;
160 
161  return quant_info.total;
162 
163 }
164 
165 long double Statistics::get_average(string name)
166 {
167  // Try to recover Quantity data
168  if ( !quantity_exists(name) )
169  throw StatisticsException("Quantity not initialized.");
170 
171  QuantityData& quant_info = records_of->find(name)->second;
172 
173  return (quant_info.total / (long double)quant_info.stops);
174 
175 }
176 
177 long double Statistics::get_min(string name)
178 {
179  // Try to recover Quantity data
180  if ( !quantity_exists(name) )
181  throw StatisticsException("Quantity not initialized.");
182 
183  QuantityData& quant_info = records_of->find(name)->second;
184 
185  return quant_info.min;
186 
187 }
188 
189 long double Statistics::get_max(string name)
190 {
191  // Try to recover Quantity data
192  if ( !quantity_exists(name) )
193  throw StatisticsException("Quantity not initialized.");
194 
195  QuantityData& quant_info = records_of->find(name)->second;
196 
197  return quant_info.max;
198 
199 }
200 
201 long double Statistics::get_last(string name)
202 {
203  // Try to recover Quantity data
204  if ( !quantity_exists(name) )
205  throw StatisticsException("Quantity not initialized.");
206 
207  QuantityData& quant_info = records_of->find(name)->second;
208 
209  return quant_info.last;
210 }
void report(std::string name, int precision=2, std::ostream &output=std::cout)
Definition: statistics.cpp:124
bool quantity_exists(std::string name)
Definition: statistics.cpp:43
void turn_on()
Definition: statistics.cpp:112
long double get_total(std::string name)
Definition: statistics.cpp:153
A class to compute statistics about quantities of interest.
Definition: statistics.hh:69
void turn_off()
Definition: statistics.cpp:118
#define STATISTICS_MAX_NAME_LENGTH
Definition: statistics.hh:26
Statistics & getStatistics()
Definition: statistics.cpp:26
void reset(std::string name)
Definition: statistics.cpp:95
void reset_all()
Definition: statistics.cpp:72
std::map< std::string, QuantityData > * records_of
Definition: statistics.hh:153
long double get_min(std::string name)
Definition: statistics.cpp:177
long double get_average(std::string name)
Definition: statistics.cpp:165
long double get_max(std::string name)
Definition: statistics.cpp:189
long double get_last(std::string name)
Definition: statistics.cpp:201
void store(std::string name, const double &value)
Definition: statistics.cpp:48
void report_all(int precision=2, std::ostream &output=std::cout)
Definition: statistics.cpp:83