A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
normalization.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TOOLS_COLOR_NORMALIZATION_H_
2 #define GENESIS_UTILS_TOOLS_COLOR_NORMALIZATION_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <cassert>
35 #include <cmath>
36 #include <limits>
37 #include <map>
38 #include <stdexcept>
39 #include <string>
40 #include <vector>
41 
42 namespace genesis {
43 namespace utils {
44 
45 // =================================================================================================
46 // Color Normalization
47 // =================================================================================================
48 
53 {
54 public:
55 
56  // -------------------------------------------------------------------------
57  // Constructors and Rule of Five
58  // -------------------------------------------------------------------------
59 
60  ColorNormalization() = default;
61  virtual ~ColorNormalization() = default;
62 
63  ColorNormalization(ColorNormalization const&) = default;
65 
68 
69  // -------------------------------------------------------------------------
70  // Accessors
71  // -------------------------------------------------------------------------
72 
83  double mask_value() const
84  {
85  return mask_value_;
86  }
87 
91  bool is_valid() const
92  {
93  return is_valid_();
94  }
95 
96  // -------------------------------------------------------------------------
97  // Modificators
98  // -------------------------------------------------------------------------
99 
104  {
105  mask_value_ = value;
106  return *this;
107  }
108 
109  // -------------------------------------------------------------------------
110  // Normalize
111  // -------------------------------------------------------------------------
112 
119  double operator()( double value ) const
120  {
121  if( ! std::isfinite( value ) || value == mask_value_ ) {
122  return std::numeric_limits<double>::quiet_NaN();
123  }
124  return normalize_( value );
125  }
126 
132  std::vector<double> operator() ( std::vector<double> const& values ) const
133  {
134  using it_type = std::vector<double>::const_iterator;
135  return operator()<it_type>( values.begin(), values.end() );
136  }
137 
143  template <class ForwardIterator>
144  std::vector<double> operator()( ForwardIterator first, ForwardIterator last ) const
145  {
146  std::vector<double> result;
147  while( first != last ) {
148  result.push_back( operator()( *first ) );
149  ++first;
150  }
151  return result;
152  }
153 
154  // -------------------------------------------------------------------------
155  // (Pure) Virtual Functions
156  // -------------------------------------------------------------------------
157 
158 protected:
159 
168  virtual double normalize_( double value ) const = 0;
169 
173  virtual bool is_valid_() const = 0;
174 
175  // -------------------------------------------------------------------------
176  // Data Members
177  // -------------------------------------------------------------------------
178 
179 private:
180 
181  double mask_value_ = std::numeric_limits<double>::quiet_NaN();
182 
183 };
184 
185 } // namespace utils
186 } // namespace genesis
187 
188 #endif // include guard
std::vector< double > operator()(ForwardIterator first, ForwardIterator last) const
Return the normalized values for a range of values.
ColorNormalization & operator=(ColorNormalization const &)=default
virtual double normalize_(double value) const =0
Normalization function.
virtual bool is_valid_() const =0
Return whether ranges and other values are correct.
virtual ~ColorNormalization()=default
double operator()(double value) const
Normalize a value into range [ 0.0, 1.0 ].
ColorNormalization & mask_value(double value)
Mask value that identifies invalid values.
Base class for color normalization.
bool is_valid() const
Return whether ranges and other values are correct.
double mask_value() const
Mask value that identifies invalid values.