A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
norm_logarithmic.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TOOLS_COLOR_NORM_LOGARITHMIC_H_
2 #define GENESIS_UTILS_TOOLS_COLOR_NORM_LOGARITHMIC_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 
37 
38 #include <cmath>
39 #include <limits>
40 #include <vector>
41 
42 namespace genesis {
43 namespace utils {
44 
45 // =================================================================================================
46 // Color Normalization Logarithmic
47 // =================================================================================================
48 
54 {
55 public:
56 
57  // -------------------------------------------------------------------------
58  // Constructors and Rule of Five
59  // -------------------------------------------------------------------------
60 
65  : ColorNormalizationLinear( 1.0, 100.0 )
66  {}
67 
71  ColorNormalizationLogarithmic( double min, double max )
72  : ColorNormalizationLinear( min, max )
73  {}
74 
79  ColorNormalizationLogarithmic( std::vector<double> const& values )
80  {
81  autoscale( values.begin(), values.end() );
82  }
83 
88  template <class ForwardIterator>
89  ColorNormalizationLogarithmic( ForwardIterator first, ForwardIterator last )
90  {
91  autoscale( first, last );
92  }
93 
94  virtual ~ColorNormalizationLogarithmic() = default;
95 
98 
101 
102  // -------------------------------------------------------------------------
103  // Settings
104  // -------------------------------------------------------------------------
105 
106  double base() const
107  {
108  return base_;
109  }
110 
112  {
113  base_ = value;
114  return *this;
115  }
116 
117  bool exponential_labels() const
118  {
119  return exp_labels_;
120  }
121 
123  {
124  exp_labels_ = value;
125  return *this;
126  }
127 
128  // -------------------------------------------------------------------------
129  // Virtual Functions
130  // -------------------------------------------------------------------------
131 
135  virtual bool is_valid_() const override
136  {
137  return 0.0 < min_value() && min_value() < max_value();
138  }
139 
140 protected:
141 
145  virtual void is_valid_or_throw_() const override
146  {
147  if( min_value() >= max_value() ) {
148  throw std::runtime_error( "Invalid Color Normalization with min >= max" );
149  }
150  if( min_value() <= 0.0 ) {
151  throw std::runtime_error( "Invalid Color Normalization with min <= 0.0" );
152  }
153  }
154 
155  virtual double normalize_( double value ) const override
156  {
157  // Already checked by base class.
158  assert( min_value() <= value && value <= max_value() );
159  assert( is_valid_() );
160 
161  // As we have 0 < min <= value, this must hold. Otherwise, log won't work.
162  assert( 0.0 < value );
163 
164  // Bring value into the range [ 0.0, 1.0 ].
165  auto const lg_min = std::log( min_value() ) / std::log( base_ );
166  auto const lg_max = std::log( max_value() ) / std::log( base_ );
167  auto const lg_val = std::log( value ) / std::log( base_ );
168  return ( lg_val - lg_min ) / ( lg_max - lg_min );
169  }
170 
171  // -------------------------------------------------------------------------
172  // Data Members
173  // -------------------------------------------------------------------------
174 
175 private:
176 
177  double base_ = 10.0;
178  bool exp_labels_ = false;
179 
180 };
181 
182 } // namespace utils
183 } // namespace genesis
184 
185 #endif // include guard
ColorNormalizationLogarithmic & exponential_labels(bool value)
ColorNormalizationLogarithmic(double min, double max)
Constructor that sets min() and max() to the provided values.
Default Color normalization, using a sequential linear scaling in the range [ min, max ].
Definition: norm_linear.hpp:59
virtual void is_valid_or_throw_() const override
Throw if the ranges are incorrect.
ColorNormalizationLogarithmic(ForwardIterator first, ForwardIterator last)
Constructor that sets min() and max() to the min and max of the provided range, and mid() to their mi...
double max_value() const
Minimum value, that is, where to end the color scale.
ColorNormalizationLinear & autoscale(std::vector< double > const &values)
virtual bool is_valid_() const override
Return whether the ranges are correct.
Header of Color class.
ColorNormalizationLogarithmic & operator=(ColorNormalizationLogarithmic const &)=default
double min_value() const
Minimum value, that is, where to begin the color scale.
ColorNormalizationLogarithmic(std::vector< double > const &values)
Constructor that sets min() and max() to the min and max of the provided values, and mid() to their m...
ColorNormalizationLogarithmic & base(double value)
virtual double normalize_(double value) const override
Normalization function.
ColorNormalizationLogarithmic()
Constructor that sets min == 1.0 and max == 100.0.
Color normalization for a logarithmic scale.