OGS
BaseLib::Histogram< T > Class Template Reference

Detailed Description

template<typename T>
class BaseLib::Histogram< T >

Basic Histogram implementation.

Creates histogram from input data of type T.

Definition at line 30 of file Histogram.h.

#include <Histogram.h>

Public Types

using Data
 

Public Member Functions

template<typename InputIterator >
 Histogram (InputIterator first, InputIterator last, const int nr_bins=16, const bool computeHistogram=true)
 Underlying input data vector type.
 
 Histogram (std::vector< T > data, const unsigned int nr_bins=16, const bool computeHistogram=true)
 
void update ()
 
void setMinimum (const T &minimum)
 
void setMaximum (const T &maximum)
 
const DatagetSortedData () const
 
const std::vector< std::size_t > & getBinCounts () const
 
const unsigned int & getNumberOfBins () const
 
const T & getMinimum () const
 
const T & getMaximum () const
 
const T & getBinWidth () const
 
void prettyPrint (std::ostream &os, const unsigned int line_width=16) const
 
int write (std::string const &file_name, std::string const &data_set_name, std::string const &param_name) const
 

Protected Member Functions

void init (const bool computeHistogram=true)
 

Protected Attributes

Data data_
 
const unsigned int nr_bins_
 
std::vector< std::size_t > histogram_
 
min_
 
max_
 Minimum and maximum input data values.
 
bin_width_
 

Private Attributes

bool dirty_
 When set update() will recompute histogram.
 

Member Typedef Documentation

◆ Data

template<typename T >
using BaseLib::Histogram< T >::Data
Initial value:
typename std::vector<double>

Definition at line 33 of file Histogram.h.

Constructor & Destructor Documentation

◆ Histogram() [1/2]

template<typename T >
template<typename InputIterator >
BaseLib::Histogram< T >::Histogram ( InputIterator first,
InputIterator last,
const int nr_bins = 16,
const bool computeHistogram = true )
inline

Underlying input data vector type.

Creates histogram of the given element in the range [first, last).

Input data is copied into std::vector.

Parameters
firstRange of elements to create histogram from.
lastRange of elements to create histogram from.
nr_binsNumber of bins in histogram.
computeHistogramCompute histogram if set. If not set user must call update() before accessing data.

Definition at line 48 of file Histogram.h.

50 : data_(first, last), nr_bins_(nr_bins), dirty_(true)
51 {
52 init(computeHistogram);
53 }
bool dirty_
When set update() will recompute histogram.
Definition Histogram.h:147
void init(const bool computeHistogram=true)
Definition Histogram.h:125
const unsigned int nr_bins_
Definition Histogram.h:141

References BaseLib::Histogram< T >::init().

◆ Histogram() [2/2]

template<typename T >
BaseLib::Histogram< T >::Histogram ( std::vector< T > data,
const unsigned int nr_bins = 16,
const bool computeHistogram = true )
inlineexplicit

Creates histogram from std::vector.

Parameters
dataInput vector.
nr_binsNumber of bins in histogram.
computeHistogramCompute histogram if set. If not set user must call update() before accessing data.

Definition at line 61 of file Histogram.h.

63 : data_(std::move(data)), nr_bins_(nr_bins), dirty_(true)
64 {
65 init(computeHistogram);
66 }

References BaseLib::Histogram< T >::init().

Member Function Documentation

◆ getBinCounts()

template<typename T >
const std::vector< std::size_t > & BaseLib::Histogram< T >::getBinCounts ( ) const
inline

Definition at line 110 of file Histogram.h.

110{ return histogram_; }
std::vector< std::size_t > histogram_
Definition Histogram.h:142

References BaseLib::Histogram< T >::histogram_.

Referenced by BaseLib::operator<<().

◆ getBinWidth()

template<typename T >
const T & BaseLib::Histogram< T >::getBinWidth ( ) const
inline

Definition at line 114 of file Histogram.h.

114{ return bin_width_; }

References BaseLib::Histogram< T >::bin_width_.

◆ getMaximum()

template<typename T >
const T & BaseLib::Histogram< T >::getMaximum ( ) const
inline

Definition at line 113 of file Histogram.h.

113{ return max_; }
T max_
Minimum and maximum input data values.
Definition Histogram.h:143

References BaseLib::Histogram< T >::max_.

Referenced by BaseLib::operator<<().

◆ getMinimum()

template<typename T >
const T & BaseLib::Histogram< T >::getMinimum ( ) const
inline

Definition at line 112 of file Histogram.h.

112{ return min_; }

References BaseLib::Histogram< T >::min_.

Referenced by BaseLib::operator<<().

◆ getNumberOfBins()

template<typename T >
const unsigned int & BaseLib::Histogram< T >::getNumberOfBins ( ) const
inline

Definition at line 111 of file Histogram.h.

111{ return nr_bins_; }

References BaseLib::Histogram< T >::nr_bins_.

Referenced by BaseLib::operator<<().

◆ getSortedData()

template<typename T >
const Data & BaseLib::Histogram< T >::getSortedData ( ) const
inline

Definition at line 109 of file Histogram.h.

109{ return data_; }

References BaseLib::Histogram< T >::data_.

◆ init()

template<typename T >
void BaseLib::Histogram< T >::init ( const bool computeHistogram = true)
inlineprotected

Initialize class members after constructor call.

Definition at line 125 of file Histogram.h.

126 {
127 std::sort(data_.begin(), data_.end());
128 histogram_.resize(nr_bins_);
129 min_ = data_.front();
130 max_ = data_.back();
132
133 dirty_ = true;
134 if (computeHistogram)
135 {
136 update();
137 }
138 }

References BaseLib::Histogram< T >::bin_width_, BaseLib::Histogram< T >::data_, BaseLib::Histogram< T >::dirty_, BaseLib::Histogram< T >::histogram_, BaseLib::Histogram< T >::max_, BaseLib::Histogram< T >::min_, BaseLib::Histogram< T >::nr_bins_, and BaseLib::Histogram< T >::update().

Referenced by BaseLib::Histogram< T >::Histogram(), and BaseLib::Histogram< T >::Histogram().

◆ prettyPrint()

template<typename T >
void BaseLib::Histogram< T >::prettyPrint ( std::ostream & os,
const unsigned int line_width = 16 ) const

Definition at line 55 of file Histogram.cpp.

57{
58 const std::size_t count_max =
59 *std::max_element(histogram_.begin(), histogram_.end());
60 for (unsigned int bin = 0; bin < nr_bins_; ++bin)
61 {
62 os << "[" << min_ + bin * bin_width_ << ", "
63 << min_ + (bin + 1) * bin_width_ << ")\t";
64 os << histogram_[bin] << "\t";
65
66 const int n_stars = static_cast<int>(
67 std::ceil(line_width * ((double)histogram_[bin] / count_max)));
68 for (int star = 0; star < n_stars; star++)
69 {
70 os << "*";
71 }
72 os << "\n";
73 }
74}

◆ setMaximum()

template<typename T >
void BaseLib::Histogram< T >::setMaximum ( const T & maximum)
inline

Definition at line 103 of file Histogram.h.

104 {
105 max_ = maximum;
106 dirty_ = true;
107 }

References BaseLib::Histogram< T >::dirty_, and BaseLib::Histogram< T >::max_.

◆ setMinimum()

template<typename T >
void BaseLib::Histogram< T >::setMinimum ( const T & minimum)
inline

Definition at line 98 of file Histogram.h.

99 {
100 min_ = minimum;
101 dirty_ = true;
102 }

References BaseLib::Histogram< T >::dirty_, and BaseLib::Histogram< T >::min_.

◆ update()

template<typename T >
void BaseLib::Histogram< T >::update ( )
inline

Updates histogram using sorted data_ vector.

Start histogram creation with first element. Then find first element in the next histogram bin. Number of elements in the bin is the difference between these two iterators.

[0.1, 0.2, ..., 0.7 , ..., 0.7+binWidth = 0.9,  1.0  , ..., last]
                it             itEnd - 1      itEnd

Definition at line 78 of file Histogram.h.

79 {
80 if (!dirty_)
81 {
82 return;
83 }
84
86
87 auto it = data_.begin();
88 for (unsigned int bin = 0; bin < nr_bins_; bin++)
89 {
90 auto itEnd = std::upper_bound(it, data_.end(),
91 min_ + (bin + 1) * bin_width_);
92 histogram_[bin] = std::distance(it, itEnd);
93 it = itEnd;
94 }
95 dirty_ = false;
96 }

References BaseLib::Histogram< T >::bin_width_, BaseLib::Histogram< T >::data_, BaseLib::Histogram< T >::dirty_, BaseLib::Histogram< T >::histogram_, BaseLib::Histogram< T >::max_, BaseLib::Histogram< T >::min_, and BaseLib::Histogram< T >::nr_bins_.

Referenced by BaseLib::Histogram< T >::init().

◆ write()

template<typename T >
int BaseLib::Histogram< T >::write ( std::string const & file_name,
std::string const & data_set_name,
std::string const & param_name ) const

Definition at line 22 of file Histogram.cpp.

25{
26 if (file_name.empty())
27 {
28 ERR("No file name specified.");
29 return 1;
30 }
31
32 std::ofstream out(file_name);
33 if (!out)
34 {
35 ERR("Error writing histogram: Could not open file.");
36 return 1;
37 }
38
39 out << "# Histogram for parameter " << param_name << " of data set "
40 << data_set_name << "\n";
41 std::size_t const n_bins = this->getNumberOfBins();
42 std::vector<std::size_t> const& bin_cnts(this->getBinCounts());
43 double const min(this->getMinimum());
44 double const bin_width(this->getBinWidth());
45
46 for (std::size_t k(0); k < n_bins; k++)
47 {
48 out << min + k * bin_width << " " << bin_cnts[k] << "\n";
49 }
50 out.close();
51 return 0;
52}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
const T & getBinWidth() const
Definition Histogram.h:114
const T & getMinimum() const
Definition Histogram.h:112
const unsigned int & getNumberOfBins() const
Definition Histogram.h:111
const std::vector< std::size_t > & getBinCounts() const
Definition Histogram.h:110

References ERR().

Referenced by MeshToolsLib::ElementQualityInterface::writeHistogram().

Member Data Documentation

◆ bin_width_

template<typename T >
T BaseLib::Histogram< T >::bin_width_
protected

◆ data_

template<typename T >
Data BaseLib::Histogram< T >::data_
protected

◆ dirty_

template<typename T >
bool BaseLib::Histogram< T >::dirty_
private

◆ histogram_

template<typename T >
std::vector<std::size_t> BaseLib::Histogram< T >::histogram_
protected

◆ max_

template<typename T >
T BaseLib::Histogram< T >::max_
protected

◆ min_

◆ nr_bins_

template<typename T >
const unsigned int BaseLib::Histogram< T >::nr_bins_
protected

The documentation for this class was generated from the following files: