histogram.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "cartographer/common/histogram.h"
00018 
00019 #include <algorithm>
00020 #include <numeric>
00021 #include <string>
00022 
00023 #include "absl/strings/str_cat.h"
00024 #include "absl/strings/str_format.h"
00025 #include "cartographer/common/port.h"
00026 #include "glog/logging.h"
00027 
00028 namespace cartographer {
00029 namespace common {
00030 
00031 void Histogram::Add(const float value) { values_.push_back(value); }
00032 
00033 std::string Histogram::ToString(const int buckets) const {
00034   CHECK_GE(buckets, 1);
00035   if (values_.empty()) {
00036     return "Count: 0";
00037   }
00038   const float min = *std::min_element(values_.begin(), values_.end());
00039   const float max = *std::max_element(values_.begin(), values_.end());
00040   const float mean =
00041       std::accumulate(values_.begin(), values_.end(), 0.f) / values_.size();
00042   std::string result = absl::StrCat("Count: ", values_.size(), "  Min: ", min,
00043                                     "  Max: ", max, "  Mean: ", mean);
00044   if (min == max) {
00045     return result;
00046   }
00047   CHECK_LT(min, max);
00048   float lower_bound = min;
00049   int total_count = 0;
00050   for (int i = 0; i != buckets; ++i) {
00051     const float upper_bound =
00052         (i + 1 == buckets)
00053             ? max
00054             : (max * (i + 1) / buckets + min * (buckets - i - 1) / buckets);
00055     int count = 0;
00056     for (const float value : values_) {
00057       if (lower_bound <= value &&
00058           (i + 1 == buckets ? value <= upper_bound : value < upper_bound)) {
00059         ++count;
00060       }
00061     }
00062     total_count += count;
00063     absl::StrAppendFormat(&result, "\n[%f, %f%c", lower_bound, upper_bound,
00064                           i + 1 == buckets ? ']' : ')');
00065     constexpr int kMaxBarChars = 20;
00066     const int bar =
00067         (count * kMaxBarChars + values_.size() / 2) / values_.size();
00068     result += "\t";
00069     for (int i = 0; i != kMaxBarChars; ++i) {
00070       result += (i < (kMaxBarChars - bar)) ? " " : "#";
00071     }
00072     absl::StrAppend(&result, "\tCount: ", count, " (",
00073                     count * 1e2f / values_.size(), "%)",
00074                     "\tTotal: ", total_count, " (",
00075                     total_count * 1e2f / values_.size(), "%)");
00076     lower_bound = upper_bound;
00077   }
00078   return result;
00079 }
00080 
00081 }  // namespace common
00082 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35