Newer
Older
* Copyright (c) 2012-2025, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#include "Histogram.h"
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <fstream>
#include "BaseLib/Logging.h"
namespace BaseLib
{
template <typename T>
int Histogram<T>::write(std::string const& file_name,
std::string const& data_set_name,
std::string const& param_name) const
{
if (file_name.empty())
{
ERR("No file name specified.");
return 1;
}
std::ofstream out(file_name);
if (!out)
{
ERR("Error writing histogram: Could not open file.");
return 1;
}
out << "# Histogram for parameter " << param_name << " of data set "
<< data_set_name << "\n";
std::size_t const n_bins = this->getNumberOfBins();
std::vector<std::size_t> const& bin_cnts(this->getBinCounts());
double const min(this->getMinimum());
double const bin_width(this->getBinWidth());
for (std::size_t k(0); k < n_bins; k++)
{
out << min + k * bin_width << " " << bin_cnts[k] << "\n";
}
out.close();
return 0;
}
template <typename T>
void Histogram<T>::prettyPrint(std::ostream& os,
const unsigned int line_width) const
{
const std::size_t count_max =
*std::max_element(histogram_.begin(), histogram_.end());
for (unsigned int bin = 0; bin < nr_bins_; ++bin)
{
os << "[" << min_ + bin * bin_width_ << ", "
<< min_ + (bin + 1) * bin_width_ << ")\t";
os << histogram_[bin] << "\t";
const int n_stars = static_cast<int>(
std::ceil(line_width * ((double)histogram_[bin] / count_max)));
for (int star = 0; star < n_stars; star++)
{
os << "*";
}
os << "\n";
}
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const Histogram<T>& h)
{
os << h.getNumberOfBins() << " " << h.getMinimum() << " " << h.getMaximum()
<< " ";
std::copy(h.getBinCounts().begin(), h.getBinCounts().end(),
std::ostream_iterator<std::size_t>(os, " "));
return os << std::endl;
}
template class Histogram<double>;
template std::ostream& operator<<(std::ostream& os, const Histogram<double>& h);
} // namespace BaseLib