A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
accumulator.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2018 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 (const auto 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
Histogram build_uniform_ranges_histogram(size_t num_bins, bool integer_ranges=false) const
Header of Histogram class.
void swap(SequenceSet &lhs, SequenceSet &rhs)
void swap(HistogramAccumulator &other)
Definition: accumulator.cpp:62
Histogram class for accumulating and summarizing data.
Definition: histogram.hpp:68
Histogram helper class that collects data and builds a fitting Histogram.
Definition: accumulator.hpp:62
size_t added_values() const
Return the number of values that have been added to the Accumulator.
void accumulate(double x, double weight)
Definition: accumulator.cpp:97
const_iterator begin() const
Definition: accumulator.cpp:72
Header of Histogram Accumulator class.
std::map< double, double >::const_iterator const_iterator
Definition: accumulator.hpp:70