A library for working with phylogenetic and population genetic data.
v0.27.0
accumulator.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2019 Lucas Czech and HITS gGmbH
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
33 #include <algorithm>
34 #include <cmath>
35 
37 
38 namespace genesis {
39 namespace utils {
40 
41 // =================================================================================================
42 // Constructors and Rule of Five
43 // =================================================================================================
44 
46  const std::vector<double>& values,
47  double weight
48 ) {
49  for (const auto v : values) {
50  accumulate(v, weight);
51  }
52 }
53 
55  const std::vector<std::pair<double,double>>& weighted_values
56 ) {
57  for( auto const& pair : weighted_values ) {
58  accumulate(pair.first, pair.second);
59  }
60 }
61 
63 {
64  using std::swap;
65  swap(values_, other.values_);
66 }
67 
68 // =================================================================================================
69 // Accessors
70 // =================================================================================================
71 
73 {
74  return values_.cbegin();
75 }
76 
78 {
79  return values_.cend();
80 }
81 
82 // =================================================================================================
83 // Modifiers
84 // =================================================================================================
85 
87 {
88  values_.clear();
89  added_values_ = 0;
90 }
91 
93 {
94  accumulate(x, 1.0);
95 }
96 
97 void HistogramAccumulator::accumulate (double x, double weight)
98 {
99  values_[x] += weight;
100  ++added_values_;
101 }
102 
103 // =================================================================================================
104 // Properties
105 // =================================================================================================
106 
108 {
109  return values_.begin()->first;
110 }
111 
113 {
114  return values_.rbegin()->first;
115 }
116 
118 {
119  return values_.size();
120 }
121 
123 {
124  return values_.empty();
125 }
126 
134 {
135  return added_values_;
136 }
137 
138 // =================================================================================================
139 // Factory Methods
140 // =================================================================================================
141 
143  size_t num_bins,
144  bool integer_ranges
145 ) const
146 {
147  if (empty()) {
148  return Histogram(num_bins);
149  }
150 
151  auto lower = min();
152  auto upper = std::nextafter(max(), max() + 1);
153 
154  if (integer_ranges) {
155  lower = std::floor(lower);
156  upper = std::ceil(upper);
157  }
158 
159  auto h = Histogram(num_bins, lower, upper);
160  for (const auto& pair : values_) {
161  h.accumulate(pair.first, pair.second);
162  }
163  return h;
164 }
165 
167  size_t num_bins,
168  double min,
169  double max
170 ) const
171 {
172  auto h = Histogram(num_bins, min, max);
173  for (const auto& pair : values_) {
174  h.accumulate(pair.first, pair.second);
175  }
176  return h;
177 }
178 
179 } // namespace utils
180 } // namespace genesis
genesis::placement::swap
void swap(Sample &lhs, Sample &rhs)
Definition: sample.cpp:104
genesis::utils::HistogramAccumulator::clear
void clear()
Definition: accumulator.cpp:86
histogram.hpp
Header of Histogram class.
genesis::utils::HistogramAccumulator::added_values
size_t added_values() const
Return the number of values that have been added to the Accumulator.
Definition: accumulator.cpp:133
genesis::utils::HistogramAccumulator::HistogramAccumulator
HistogramAccumulator()
Definition: accumulator.hpp:77
genesis::utils::HistogramAccumulator::empty
bool empty() const
Definition: accumulator.cpp:122
genesis::utils::HistogramAccumulator::accumulate
void accumulate(double x, double weight)
Definition: accumulator.cpp:97
genesis::utils::HistogramAccumulator::increment
void increment(double x)
Definition: accumulator.cpp:92
accumulator.hpp
Header of Histogram Accumulator class.
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::HistogramAccumulator::const_iterator
std::map< double, double >::const_iterator const_iterator
Definition: accumulator.hpp:71
genesis::utils::HistogramAccumulator::size
size_t size() const
Definition: accumulator.cpp:117
genesis::utils::HistogramAccumulator::build_uniform_ranges_histogram
Histogram build_uniform_ranges_histogram(size_t num_bins, bool integer_ranges=false) const
Definition: accumulator.cpp:142
genesis::utils::HistogramAccumulator::min
double min() const
Definition: accumulator.cpp:107
genesis::utils::HistogramAccumulator::begin
const_iterator begin() const
Definition: accumulator.cpp:72
genesis::utils::HistogramAccumulator
Histogram helper class that collects data and builds a fitting Histogram.
Definition: accumulator.hpp:63
genesis::utils::HistogramAccumulator::end
const_iterator end() const
Definition: accumulator.cpp:77
genesis::utils::HistogramAccumulator::max
double max() const
Definition: accumulator.cpp:112
genesis::utils::HistogramAccumulator::swap
void swap(HistogramAccumulator &other)
Definition: accumulator.cpp:62
genesis::utils::Histogram
Histogram class for accumulating and summarizing data.
Definition: histogram.hpp:68