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 21 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 24 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 39 of file Histogram.h.

42 {
44 }
bool dirty_
When set update() will recompute histogram.
Definition Histogram.h:138
void init(const bool computeHistogram=true)
Definition Histogram.h:116
const unsigned int nr_bins_
Definition Histogram.h:132

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 52 of file Histogram.h.

55 {
57 }

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 101 of file Histogram.h.

101{ return histogram_; }
std::vector< std::size_t > histogram_
Definition Histogram.h:133

References histogram_.

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

◆ getBinWidth()

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

Definition at line 105 of file Histogram.h.

105{ return bin_width_; }

References bin_width_.

Referenced by write().

◆ getMaximum()

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

Definition at line 104 of file Histogram.h.

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

References max_.

Referenced by BaseLib::operator<<().

◆ getMinimum()

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

Definition at line 103 of file Histogram.h.

103{ 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 102 of file Histogram.h.

102{ 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 100 of file Histogram.h.

100{ 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 116 of file Histogram.h.

117 {
118 std::sort(data_.begin(), data_.end());
119 histogram_.resize(nr_bins_);
120 min_ = data_.front();
121 max_ = data_.back();
123
124 dirty_ = true;
126 {
127 update();
128 }
129 }

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 48 of file Histogram.cpp.

50{
52 *std::max_element(histogram_.begin(), histogram_.end());
53 for (unsigned int bin = 0; bin < nr_bins_; ++bin)
54 {
55 os << "[" << min_ + bin * bin_width_ << ", "
56 << min_ + (bin + 1) * bin_width_ << ")\t";
57 os << histogram_[bin] << "\t";
58
59 const int n_stars = static_cast<int>(
61 for (int star = 0; star < n_stars; star++)
62 {
63 os << "*";
64 }
65 os << "\n";
66 }
67}

References bin_width_, histogram_, min_, and nr_bins_.

◆ setMaximum()

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

Definition at line 94 of file Histogram.h.

95 {
96 max_ = maximum;
97 dirty_ = true;
98 }

References dirty_, and max_.

◆ setMinimum()

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

Definition at line 89 of file Histogram.h.

90 {
91 min_ = minimum;
92 dirty_ = true;
93 }

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 69 of file Histogram.h.

70 {
71 if (!dirty_)
72 {
73 return;
74 }
75
77
78 auto it = data_.begin();
79 for (unsigned int bin = 0; bin < nr_bins_; bin++)
80 {
81 auto itEnd = std::upper_bound(it, data_.end(),
82 min_ + (bin + 1) * bin_width_);
84 it = itEnd;
85 }
86 dirty_ = false;
87 }

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 15 of file Histogram.cpp.

18{
19 if (file_name.empty())
20 {
21 ERR("No file name specified.");
22 return 1;
23 }
24
26 if (!out)
27 {
28 ERR("Error writing histogram: Could not open file.");
29 return 1;
30 }
31
32 out << "# Histogram for parameter " << param_name << " of data set "
33 << data_set_name << "\n";
34 std::size_t const n_bins = this->getNumberOfBins();
36 double const min(this->getMinimum());
37 double const bin_width(this->getBinWidth());
38
39 for (std::size_t k(0); k < n_bins; k++)
40 {
41 out << min + k * bin_width << " " << bin_cnts[k] << "\n";
42 }
43 out.close();
44 return 0;
45}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
const T & getBinWidth() const
Definition Histogram.h:105
const T & getMinimum() const
Definition Histogram.h:103
const unsigned int & getNumberOfBins() const
Definition Histogram.h:102
const std::vector< std::size_t > & getBinCounts() const
Definition Histogram.h:101

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 135 of file Histogram.h.

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

◆ data_

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

Definition at line 131 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 138 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 133 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 134 of file Histogram.h.

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

◆ min_

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

Definition at line 134 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 132 of file Histogram.h.

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


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