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.

51 {
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 data_, dirty_, init(), and nr_bins_.

◆ 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.

64 {
66 }

References data_, dirty_, init(), and nr_bins_.

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 histogram_.

Referenced by BaseLib::operator<<(), and write().

◆ 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 bin_width_.

Referenced by write().

◆ 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 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 min_.

Referenced by BaseLib::operator<<(), and write().

◆ 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 nr_bins_.

Referenced by BaseLib::operator<<(), and write().

◆ getSortedData()

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

Definition at line 109 of file Histogram.h.

109{ return data_; }

References 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;
135 {
136 update();
137 }
138 }

References bin_width_, data_, dirty_, histogram_, max_, min_, nr_bins_, and update().

Referenced by Histogram(), and 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{
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>(
68 for (int star = 0; star < n_stars; star++)
69 {
70 os << "*";
71 }
72 os << "\n";
73 }
74}

References bin_width_, histogram_, min_, and nr_bins_.

◆ 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 dirty_, and 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 dirty_, and 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_);
93 it = itEnd;
94 }
95 dirty_ = false;
96 }

References bin_width_, data_, dirty_, histogram_, max_, min_, and nr_bins_.

Referenced by 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
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();
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:48
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(), getBinCounts(), getBinWidth(), getMinimum(), and getNumberOfBins().

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

Member Data Documentation

◆ bin_width_

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

Definition at line 144 of file Histogram.h.

Referenced by getBinWidth(), init(), prettyPrint(), and update().

◆ data_

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

Definition at line 140 of file Histogram.h.

Referenced by Histogram(), Histogram(), getSortedData(), init(), and update().

◆ dirty_

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

When set update() will recompute histogram.

Definition at line 147 of file Histogram.h.

Referenced by Histogram(), Histogram(), init(), setMaximum(), setMinimum(), and update().

◆ histogram_

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

Definition at line 142 of file Histogram.h.

Referenced by getBinCounts(), init(), prettyPrint(), and update().

◆ max_

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

Minimum and maximum input data values.

Definition at line 143 of file Histogram.h.

Referenced by getMaximum(), init(), setMaximum(), and update().

◆ min_

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

Definition at line 143 of file Histogram.h.

Referenced by getMinimum(), init(), prettyPrint(), setMinimum(), and update().

◆ nr_bins_

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

Definition at line 141 of file Histogram.h.

Referenced by Histogram(), Histogram(), getNumberOfBins(), init(), prettyPrint(), and update().


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