Histogram.cpp
Go to the documentation of this file.
1 // kate: replace-tabs off; indent-width 4; indent-mode normal
2 // vim: ts=4:sw=4:noexpandtab
3 /*
4 
5 Copyright (c) 2010--2012,
6 François Pomerleau and Stephane Magnenat, ASL, ETHZ, Switzerland
7 You can contact the authors at <f dot pomerleau at gmail dot com> and
8 <stephane at magnenat dot net>
9 
10 All rights reserved.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright
15  notice, this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright
17  notice, this list of conditions and the following disclaimer in the
18  documentation and/or other materials provided with the distribution.
19  * Neither the name of the <organization> nor the
20  names of its contributors may be used to endorse or promote products
21  derived from this software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL ETH-ASL BE LIABLE FOR ANY
27 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "Histogram.h"
37 
38 #include <iostream>
39 #include <fstream>
40 #include <iomanip>
41 #include <limits>
42 #include <algorithm>
43 #include <boost/format.hpp>
44 
45 namespace PointMatcherSupport
46 {
47  using namespace std;
48 
49  template<typename T>
50  Histogram<T>::Histogram(const size_t binCount, const std::string& name, const std::string&
51  filePrefix, const bool dumpStdErrOnExit):
52  binCount(binCount),
53  name(name),
54  filePrefix(filePrefix),
55  dumpStdErrOnExit(dumpStdErrOnExit)
56  {
57  }
58 
59  template<typename T>
61  {
62  T meanV, varV, medianV, lowQt, highQt, minV, maxV;
63  uint64_t maxBinC;
64  if (!dumpStdErrOnExit && filePrefix.empty())
65  return;
66 
67  const vector<uint64_t> bins(computeStats(meanV, varV, medianV, lowQt, highQt, minV, maxV, maxBinC));
68 
69  if (!filePrefix.empty())
70  {
71  std::cerr << "writing to " << (filePrefix + name + "Stats.csv") << std::endl;
72  std::ofstream ofs_stats((filePrefix + name + "Stats.csv").c_str());
73  dumpStatsHeader(ofs_stats);
74  ofs_stats << endl;
75  dumpStats(ofs_stats);
76 
77  std::cerr << "writing to " << (filePrefix + name + ".csv") << std::endl;
78  std::ofstream ofs((filePrefix + name + ".csv").c_str());
79  for (size_t i = 0; i < this->size(); ++i)
80  ofs << ((*this)[i]) << "\n";
81  }
82 
83  if (dumpStdErrOnExit)
84  {
85  std::cerr.precision(4);
86  std::cerr.fill(' ');
87  std::cerr.flags(std::ios::left);
88  std::cerr << "Histogram " << name << ":\n";
89  std::cerr << " count: " << this->size() << ", mean: " << meanV << ", var: " << varV << ", median: " << medianV << ", min: " << minV << ", max: " << maxV << ", lowQt: " << lowQt << ", highQt: " << highQt << ", maxBinC: " << maxBinC << "\n";
90  if(this->size() > 1)
91  {
92  for (size_t i = 0; i < binCount; ++i)
93  {
94  const T v(minV + i * (maxV - minV) / T(binCount));
95  std::cerr << " " << std::setw(10) << v << " (" << std::setw(6) << bins[i] << ") : ";
96  //std::cerr << (bins[i] * 60) / maxBinC << " " ;
97  if (maxBinC > 0) {
98  for (size_t j = 0; j < (bins[i] * 60) / maxBinC; ++j)
99  std::cerr << "*";
100  }
101  std::cerr << "\n";
102  }
103  std::cerr << std::endl;
104  }
105  }
106  }
107 
108  template<typename T>
109  vector<uint64_t> Histogram<T>::computeStats(T& meanV, T& varV, T& medianV, T& lowQt, T& highQt, T& minV, T& maxV, uint64_t& maxBinC)
110  {
111  typedef typename std::vector<T>::iterator Iterator;
112  vector<uint64_t> bins(binCount, 0);
113 
114  //assert(this->size() > 0);
115  if(this->size() > 0)
116  {
117  // basic stats
118  meanV = 0;
119  minV = std::numeric_limits<T>::max();
120  maxV = std::numeric_limits<T>::min();
121  for (size_t i = 0; i < this->size(); ++i)
122  {
123  const T v((*this)[i]);
124  meanV += v;
125  minV = std::min<T>(minV, v);
126  maxV = std::max<T>(maxV, v);
127  }
128  meanV /= T(this->size());
129  // var and hist
130  std::fill(bins.begin(), bins.end(), uint64_t(0));
131  maxBinC = 0;
132  varV = 0;
133  if (minV == maxV)
134  {
135  medianV = lowQt = highQt = minV;
136  return bins;
137  }
138  for (size_t i = 0; i < this->size(); ++i)
139  {
140  const T v((*this)[i]);
141  varV += (v - meanV)*(v - meanV);
142  const size_t index((v - minV) * (binCount) / ((maxV - minV) * (1+std::numeric_limits<T>::epsilon()*10)));
143  //std::cerr << "adding value " << v << " to index " << index << std::endl;
144  ++bins[index];
145  maxBinC = std::max<uint64_t>(maxBinC, bins[index]);
146  }
147  varV /= T(this->size());
148  // median
149  const Iterator lowQtIt(this->begin() + (this->size() / 4));
150  const Iterator medianIt(this->begin() + (this->size() / 2));
151  const Iterator highQtIt(this->begin() + (3*this->size() / 4));
152  std::nth_element(this->begin(), medianIt, this->end());
153  medianV = *medianIt;
154  std::nth_element(this->begin(), lowQtIt, this->end());
155  lowQt = *lowQtIt;
156  std::nth_element(this->begin(), highQtIt, this->end());
157  highQt = *highQtIt;
158  }
159  else
160  {
161  meanV = std::numeric_limits<T>::quiet_NaN();
162  varV = std::numeric_limits<T>::quiet_NaN();
163  medianV = std::numeric_limits<T>::quiet_NaN();
164  lowQt = std::numeric_limits<T>::quiet_NaN();
165  highQt = std::numeric_limits<T>::quiet_NaN();
166  minV = std::numeric_limits<T>::quiet_NaN();
167  maxV = std::numeric_limits<T>::quiet_NaN();
168  maxBinC = 0;
169  }
170  return bins;
171  }
172 
173  template<typename T>
174  void Histogram<T>::dumpStats(std::ostream& os)
175  {
176  T meanV, varV, medianV, lowQt, highQt, minV, maxV;
177  uint64_t maxBinC;
178  const vector<uint64_t> bins(computeStats(meanV, varV, medianV, lowQt, highQt, minV, maxV, maxBinC));
179  os << this->size() << ", " << meanV << ", " << varV << ", " << medianV << ", " << lowQt << ", " << highQt << ", " << minV << ", " << maxV << ", " << binCount << ", ";
180 
181  for (size_t i = 0; i < binCount; ++i)
182  os << bins[i] << ", ";
183  os << maxBinC;
184  }
185 
186  template<typename T>
187  void Histogram<T>::dumpStatsHeader(std::ostream& os) const
188  {
189  os << name + "_count, ";
190  os << name + "_mean, ";
191  os << name + "_var, ";
192  os << name + "_median, ";
193  os << name + "_low_quartile, ";
194  os << name + "_high_quartile, ";
195  os << name + "_min_value, ";
196  os << name + "_max_value, ";
197  os << name + "_bin_count, ";
198  for (size_t i = 0; i < binCount; ++i)
199  os << (boost::format("%1%_bin_%2%,") % name % i).str();
200  os << name + "_max_elements_per_bin ";
201  }
202 
203  template struct Histogram<unsigned>;
204  template struct Histogram<float>;
205  template struct Histogram<double>;
206 } // namespace PointMatcherSupport
PointMatcherSupport::Histogram::computeStats
std::vector< uint64_t > computeStats(T &meanV, T &varV, T &medianV, T &lowQt, T &highQt, T &minV, T &maxV, uint64_t &maxBinC)
This function compute statistics and writes them into the variables passed as reference.
Definition: Histogram.cpp:109
PointMatcherSupport::Histogram::~Histogram
virtual ~Histogram()
Definition: Histogram.cpp:60
testing::internal::string
::std::string string
Definition: gtest.h:1979
PointMatcherSupport::Histogram::dumpStatsHeader
void dumpStatsHeader(std::ostream &os) const
Definition: Histogram.cpp:187
Histogram.h
std
PointMatcherSupport::Histogram::Histogram
Histogram(const size_t binCount, const std::string &name, const std::string &filePrefix, const bool dumpStdErrOnExit)
Definition: Histogram.cpp:50
PointMatcherSupport
Functions and classes that are not dependant on scalar type are defined in this namespace.
Definition: Bibliography.cpp:45
PointMatcherSupport::Histogram
Definition: Histogram.h:46
PointMatcherSupport::Histogram::dumpStats
void dumpStats(std::ostream &os)
Definition: Histogram.cpp:174


mp2p_icp
Author(s):
autogenerated on Wed Oct 23 2024 02:45:40