sot-talos-balance  2.0.5
Collection of dynamic-graph entities aimed at implementing balance control on talos.
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 
19 
20 #include <iomanip> // std::setprecision
21 
22 using std::map;
23 using std::ostringstream;
24 using std::string;
25 
27  static Statistics s;
28  return s;
29 }
30 
31 Statistics::Statistics() : active(true) {
32  records_of = new map<string, QuantityData>();
33 }
34 
36 
38  return (records_of->find(name) != records_of->end());
39 }
40 
41 void Statistics::store(string name, const double& value) {
42  if (!active) return;
43 
44  // Just works if not already present
45  records_of->insert(make_pair(name, QuantityData()));
46 
47  QuantityData& quant_info = records_of->find(name)->second;
48 
49  quant_info.stops++;
50 
51  // Update last value
52  quant_info.last = value;
53 
54  // Update min/max
55  if (value >= quant_info.max) quant_info.max = value;
56  if (value <= quant_info.min || quant_info.min == 0) quant_info.min = value;
57 
58  // Update total
59  quant_info.total += value;
60 }
61 
63  if (!active) return;
64 
65  map<string, QuantityData>::iterator it;
66 
67  for (it = records_of->begin(); it != records_of->end(); ++it) {
68  reset(it->first);
69  }
70 }
71 
72 void Statistics::report_all(int precision, std::ostream& output) {
73  if (!active) return;
74 
75  output
76  << "\n*** STATISTICS (min - avg - max - last - nSamples - total) ***\n";
77  map<string, QuantityData>::iterator it;
78  for (it = records_of->begin(); it != records_of->end(); ++it) {
79  if (it->second.stops > 0) report(it->first, precision, output);
80  }
81 }
82 
83 void Statistics::reset(string name) {
84  if (!active) return;
85 
86  // Try to recover Quantity data
87  if (!quantity_exists(name))
88  throw StatisticsException("Quantity not initialized.");
89 
90  QuantityData& quant_info = records_of->find(name)->second;
91 
92  quant_info.total = 0;
93  quant_info.min = 0;
94  quant_info.max = 0;
95  quant_info.last = 0;
96  quant_info.stops = 0;
97 }
98 
100  std::cout << "Statistics active." << std::endl;
101  active = true;
102 }
103 
105  std::cout << "Statistics inactive." << std::endl;
106  active = false;
107 }
108 
109 void Statistics::report(string name, int precision, std::ostream& output) {
110  if (!active) return;
111 
112  // Try to recover Quantity data
113  if (!quantity_exists(name))
114  throw StatisticsException("Quantity not initialized.");
115 
116  QuantityData& quant_info = records_of->find(name)->second;
117 
118  string pad = "";
119  for (std::string::size_type i = name.length(); i < STATISTICS_MAX_NAME_LENGTH;
120  i++)
121  pad.append(" ");
122 
123  output << name << pad;
124  output << std::fixed << std::setprecision(precision) << (quant_info.min)
125  << "\t";
126  output << std::fixed << std::setprecision(precision)
127  << (quant_info.total / (long double)quant_info.stops) << "\t";
128  output << std::fixed << std::setprecision(precision) << (quant_info.max)
129  << "\t";
130  output << std::fixed << std::setprecision(precision) << (quant_info.last)
131  << "\t";
132  output << std::fixed << std::setprecision(precision) << quant_info.stops
133  << "\t";
134  output << std::fixed << std::setprecision(precision) << quant_info.total
135  << std::endl;
136 }
137 
138 long double Statistics::get_total(string name) {
139  // Try to recover Quantity data
140  if (!quantity_exists(name))
141  throw StatisticsException("Quantity not initialized.");
142 
143  QuantityData& quant_info = records_of->find(name)->second;
144 
145  return quant_info.total;
146 }
147 
148 long double Statistics::get_average(string name) {
149  // Try to recover Quantity data
150  if (!quantity_exists(name))
151  throw StatisticsException("Quantity not initialized.");
152 
153  QuantityData& quant_info = records_of->find(name)->second;
154 
155  return (quant_info.total / (long double)quant_info.stops);
156 }
157 
158 long double Statistics::get_min(string name) {
159  // Try to recover Quantity data
160  if (!quantity_exists(name))
161  throw StatisticsException("Quantity not initialized.");
162 
163  QuantityData& quant_info = records_of->find(name)->second;
164 
165  return quant_info.min;
166 }
167 
168 long double Statistics::get_max(string name) {
169  // Try to recover Quantity data
170  if (!quantity_exists(name))
171  throw StatisticsException("Quantity not initialized.");
172 
173  QuantityData& quant_info = records_of->find(name)->second;
174 
175  return quant_info.max;
176 }
177 
178 long double Statistics::get_last(string name) {
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.last;
186 }
Statistics::QuantityData::total
long double total
Definition: statistics.hh:121
Statistics::active
bool active
Definition: statistics.hh:137
Statistics::get_max
long double get_max(std::string name)
Definition: statistics.cpp:168
Statistics::records_of
std::map< std::string, QuantityData > * records_of
Definition: statistics.hh:141
Statistics::QuantityData::last
long double last
Definition: statistics.hh:130
Statistics::get_last
long double get_last(std::string name)
Definition: statistics.cpp:178
StatisticsException
Definition: statistics.hh:28
Statistics::get_min
long double get_min(std::string name)
Definition: statistics.cpp:158
Statistics::QuantityData::max
long double max
Definition: statistics.hh:127
Statistics::get_average
long double get_average(std::string name)
Definition: statistics.cpp:148
Statistics::QuantityData::stops
int stops
Definition: statistics.hh:133
Statistics::store
void store(std::string name, const double &value)
Definition: statistics.cpp:41
Statistics::report
void report(std::string name, int precision=2, std::ostream &output=std::cout)
Definition: statistics.cpp:109
STATISTICS_MAX_NAME_LENGTH
#define STATISTICS_MAX_NAME_LENGTH
Definition: statistics.hh:25
getStatistics
Statistics & getStatistics()
Definition: statistics.cpp:26
statistics.hh
Statistics
A class to compute statistics about quantities of interest.
Definition: statistics.hh:66
Statistics::report_all
void report_all(int precision=2, std::ostream &output=std::cout)
Definition: statistics.cpp:72
Statistics::get_total
long double get_total(std::string name)
Definition: statistics.cpp:138
Statistics::turn_on
void turn_on()
Definition: statistics.cpp:99
sot_talos_balance.test.appli_admittance_end_effector.value
value
Definition: appli_admittance_end_effector.py:70
Statistics::quantity_exists
bool quantity_exists(std::string name)
Definition: statistics.cpp:37
Statistics::~Statistics
~Statistics()
Definition: statistics.cpp:35
Statistics::QuantityData::min
long double min
Definition: statistics.hh:124
Statistics::reset
void reset(std::string name)
Definition: statistics.cpp:83
Statistics::Statistics
Statistics()
Definition: statistics.cpp:31
Statistics::reset_all
void reset_all()
Definition: statistics.cpp:62
sot_talos_balance.test.appli_dcm_zmp_control.name
name
Definition: appli_dcm_zmp_control.py:298
Statistics::QuantityData
Definition: statistics.hh:117
Statistics::turn_off
void turn_off()
Definition: statistics.cpp:104