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

#include <Histogram.h>

Public Types

using Data = typename std::vector< double >
 

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

Private Attributes

bool dirty_
 When set update() will recompute histogram. More...
 

Member Typedef Documentation

◆ Data

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

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

52  : data_(first, last), nr_bins_(nr_bins), dirty_(true)
53  {
54  init(computeHistogram);
55  }
bool dirty_
When set update() will recompute histogram.
Definition: Histogram.h:194
void init(const bool computeHistogram=true)
Definition: Histogram.h:172
const unsigned int nr_bins_
Definition: Histogram.h:188

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

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

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

112 { return histogram_; }
std::vector< std::size_t > histogram_
Definition: Histogram.h:189

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

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

◆ getBinWidth()

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

Definition at line 116 of file Histogram.h.

116 { return bin_width_; }

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

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

◆ getMaximum()

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

Definition at line 115 of file Histogram.h.

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

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

Referenced by BaseLib::operator<<().

◆ getMinimum()

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

Definition at line 114 of file Histogram.h.

114 { return min_; }

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

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

◆ getNumberOfBins()

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

Definition at line 113 of file Histogram.h.

113 { return nr_bins_; }

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

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

◆ getSortedData()

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

Definition at line 111 of file Histogram.h.

111 { 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 172 of file Histogram.h.

173  {
174  std::sort(data_.begin(), data_.end());
175  histogram_.resize(nr_bins_);
176  min_ = data_.front();
177  max_ = data_.back();
178  bin_width_ = (max_ - min_) / nr_bins_;
179 
180  dirty_ = true;
181  if (computeHistogram)
182  {
183  update();
184  }
185  }

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().

◆ prettyPrint()

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

Definition at line 118 of file Histogram.h.

119  {
120  const std::size_t count_max =
121  *std::max_element(histogram_.begin(), histogram_.end());
122  for (unsigned int bin = 0; bin < nr_bins_; ++bin)
123  {
124  os << "[" << min_ + bin * bin_width_ << ", "
125  << min_ + (bin + 1) * bin_width_ << ")\t";
126  os << histogram_[bin] << "\t";
127 
128  const int n_stars =
129  std::ceil(line_width * ((double)histogram_[bin] / count_max));
130  for (int star = 0; star < n_stars; star++)
131  {
132  os << "*";
133  }
134  os << "\n";
135  }
136  }

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

◆ setMaximum()

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

Definition at line 105 of file Histogram.h.

106  {
107  max_ = maximum;
108  dirty_ = true;
109  }

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

101  {
102  min_ = minimum;
103  dirty_ = true;
104  }

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

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

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
inline

Definition at line 138 of file Histogram.h.

140  {
141  if (file_name.empty())
142  {
143  ERR("No file name specified.");
144  return 1;
145  }
146 
147  std::ofstream out(file_name);
148  if (!out)
149  {
150  ERR("Error writing histogram: Could not open file.");
151  return 1;
152  }
153 
154  out << "# Histogram for parameter " << param_name << " of data set "
155  << data_set_name << "\n";
156  std::size_t const n_bins = this->getNumberOfBins();
157  std::vector<std::size_t> const& bin_cnts(this->getBinCounts());
158  double const min(this->getMinimum());
159  double const bin_width(this->getBinWidth());
160 
161  for (std::size_t k(0); k < n_bins; k++)
162  {
163  out << min + k * bin_width << " " << bin_cnts[k] << "\n";
164  }
165  out.close();
166  return 0;
167  }
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
const T & getBinWidth() const
Definition: Histogram.h:116
const unsigned int & getNumberOfBins() const
Definition: Histogram.h:113
const T & getMinimum() const
Definition: Histogram.h:114
const std::vector< std::size_t > & getBinCounts() const
Definition: Histogram.h:112

References ERR(), BaseLib::Histogram< T >::getBinCounts(), BaseLib::Histogram< T >::getBinWidth(), BaseLib::Histogram< T >::getMinimum(), and BaseLib::Histogram< T >::getNumberOfBins().

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

Member Data Documentation

◆ bin_width_

◆ 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 file: