29 namespace xerus {
namespace misc {
35 for (
const auto &h : _other.
buckets) {
43 double logRate = log(_value)/log(
base);
44 REQUIRE(std::isfinite(_value),
"tried to add illegal value " << _value <<
" into a histogram");
45 buckets[int(logRate)] += _count;
50 std::ifstream in(_fileName);
53 std::getline(in, line);
54 REQUIRE(line ==
"# raw data:",
"unknown histogram file format " << _fileName);
57 REQUIRE(c ==
'#',
"missing information in histogram file " << _fileName);
59 std::getline(in, line);
60 std::getline(in, line);
61 std::stringstream bucketData(line);
63 REQUIRE(c ==
'#',
"missing information in histogram file " << _fileName);
65 while (bucketData >> bucketIndex) {
67 if (!(bucketData >> count)) {
68 LOG(fatal,
"missing bucket count in histogram file " << _fileName);
70 result.
buckets[bucketIndex] = count;
72 size_t accountedTime=0;
73 for (
auto &h : result.
buckets) {
74 accountedTime += h.second;
76 REQUIRE(accountedTime == result.
totalCount,
"histogram data inconsistent in file " << _fileName);
81 std::ofstream out(_fileName);
82 out <<
"# raw data:\n";
86 out <<
' ' << h.first <<
' ' << h.second;
88 out <<
"\n# plotable data:\n";
89 if (!buckets.empty()) {
90 int firstOutput = buckets.begin()->first - 1;
91 int lastOutput = buckets.rbegin()->first + 1;
92 for (
int i=firstOutput; i<=lastOutput; ++i) {
94 if (buckets.count(i) > 0) {
95 out << double(buckets.find(i)->second)/
double(totalCount) <<
'\n';
Default include file for the misc (ie. non-tensor) functionality.
void dump_to_file(const std::string &_fileName) const
bool approx_equal(T _a, T _b, T _eps=4 *std::numeric_limits< T >::epsilon()) noexcept
: Checks whether the relative difference between _a and _b (i.e. |a-b|/(|a|/2+|b|/2)) is smaller than...
The main namespace of xerus.
Header file for comfort functions and macros that should not be exported in the library.
LogHistogram & operator+=(const LogHistogram &_other)
static LogHistogram read_from_file(const std::string &_fileName)
Header file for the histogram classes.
A logarithmic histogram, i.e. the size of all buckets is given by a constant factor [x - x*base) ...
void add(double _value, size_t _count=1)
std::map< int, size_t > buckets
LogHistogram(const double _base)
constexpr T pow(const T &_base, const uint32 _exp) noexcept
: Calculates _base^_exp by binary exponentiation