Loading [MathJax]/extensions/tex2jax.js
A library for working with phylogenetic and population genetic data.
v0.32.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
heat_map.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2023 Lucas Czech
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 <lczech@carnegiescience.edu>
20  Department of Plant Biology, Carnegie Institution For Science
21  260 Panama Street, Stanford, CA 94305, USA
22 */
23 
32 
33 #include <cassert>
34 #include <cmath>
35 #include <stdexcept>
36 
44 
45 namespace genesis {
46 namespace utils {
47 
48 // =================================================================================================
49 // Helper Functions and Function Templates
50 // =================================================================================================
51 
53  MinMaxPair<double> const& min_max,
54  HeatmapParameters const& parameters
55 ) {
56  auto result = min_max;
57  if( std::isfinite( parameters.min_value )) {
58  result.min = parameters.min_value;
59  }
60  if( std::isfinite( parameters.max_value )) {
61  result.max = parameters.max_value;
62  }
63  return result;
64 }
65 
66 template<class ColorNorm>
68  Matrix<double> const& values,
69  HeatmapParameters const& parameters
70 ) {
71  // Prepare the matrix with the same dimensions as the input.
72  auto result = Matrix<utils::Color>( values.rows(), values.cols() );
73 
74  // Get the min and max value to use, and make a color normalization.
75  auto min_max = finite_minimum_maximum( values.begin(), values.end() );
76  min_max = heat_map_matrix_update_min_max_( min_max, parameters );
77  auto norm = ColorNorm( min_max.min, min_max.max );
78 
79  // Fill the pixels.
80  for( size_t row = 0; row < values.rows(); ++row ) {
81  for( size_t col = 0; col < values.cols(); ++col ) {
82  result( row, col ) = parameters.color_map( norm, values( row, col ));
83  }
84  }
85  return result;
86 }
87 
88 template<class ColorNorm>
90  Matrix<double> const& values,
91  HeatmapParameters const& parameters
92 ) {
93  // Prepare the matrix with the same dimensions as the input.
94  auto result = Matrix<utils::Color>( values.rows(), values.cols() );
95 
96  // Fill the pixels, normalizing each row.
97  for( size_t row = 0; row < values.rows(); ++row ) {
98  // Get the min and max value to use, and make a color normalization.
99  auto min_max = finite_minimum_maximum( values.row(row).begin(), values.row(row).end() );
100  min_max = heat_map_matrix_update_min_max_( min_max, parameters );
101  auto norm = ColorNorm( min_max.min, min_max.max );
102 
103  for( size_t col = 0; col < values.cols(); ++col ) {
104  result( row, col ) = parameters.color_map( norm, values( row, col ));
105  }
106  }
107  return result;
108 }
109 
110 template<class ColorNorm>
112  Matrix<double> const& values,
113  HeatmapParameters const& parameters
114 ) {
115  // Prepare the matrix with the same dimensions as the input.
116  auto result = Matrix<utils::Color>( values.rows(), values.cols() );
117 
118  // Fill the pixels, normalizing each col.
119  // We here traverse the matrix col-first, which is a bit slower, but needed to avoid
120  // recomputing the norm for every pixel. Alternatively, we could compute them once in a vector
121  // first, but well, this works, too.
122  for( size_t col = 0; col < values.cols(); ++col ) {
123  // Get the min and max value to use, and make a color normalization.
124  auto min_max = finite_minimum_maximum( values.col(col).begin(), values.col(col).end() );
125  min_max = heat_map_matrix_update_min_max_( min_max, parameters );
126  auto norm = ColorNorm( min_max.min, min_max.max );
127 
128  for( size_t row = 0; row < values.rows(); ++row ) {
129  result( row, col ) = parameters.color_map( norm, values( row, col ));
130  }
131  }
132  return result;
133 }
134 
135 template<class ColorNorm>
137  Matrix<double> const& values,
138  HeatmapParameters const& parameters
139 ) {
140  switch( parameters.normalization_range ) {
142  return heat_map_matrix_range_all_<ColorNorm>( values, parameters );
143  }
145  return heat_map_matrix_range_row_<ColorNorm>( values, parameters );
146  }
148  return heat_map_matrix_range_col_<ColorNorm>( values, parameters );
149  }
150  }
151  throw std::invalid_argument( "heat_map_matrix_range_(): Invalid NormalizationRange" );
152 }
153 
154 // =================================================================================================
155 // Heat Map Matrix Functions
156 // =================================================================================================
157 
158 std::unique_ptr<ColorNormalization> make_heatmap_color_norm(
159  Matrix<double> const& values,
160  HeatmapParameters const& parameters
161 ) {
162  // Error checks
164  throw std::invalid_argument(
165  "Can only make ColorNormalization for heat map with whole matrix value normalization."
166  );
167  }
168 
169  // Get the min and max value to use. Repetivive call, but well... refactor later...
170  auto min_max = finite_minimum_maximum( values.begin(), values.end() );
171  min_max = heat_map_matrix_update_min_max_( min_max, parameters );
172 
173  // Make a color norm for the given type... this is repetitive code from make_heatmap_matrix(),
174  // and we need to refactor that in the future. But works for now.
175  switch( parameters.color_norm ) {
177  return utils::make_unique<ColorNormalizationLinear>( min_max.min, min_max.max );
178  }
180  return utils::make_unique<ColorNormalizationLogarithmic>( min_max.min, min_max.max );
181  }
183  return utils::make_unique<ColorNormalizationDiverging>( min_max.min, min_max.max );
184  }
185  }
186  throw std::invalid_argument( "make_heatmap_color_norm(): Invalid HeatmapParameters::ColorNorm" );
187 }
188 
190  Matrix<double> const& values,
191  HeatmapParameters const& parameters
192 ) {
193  switch( parameters.color_norm ) {
195  return heat_map_matrix_range_<ColorNormalizationLinear>( values, parameters );
196  }
198  return heat_map_matrix_range_<ColorNormalizationLogarithmic>( values, parameters );
199  }
201  return heat_map_matrix_range_<ColorNormalizationDiverging>( values, parameters );
202  }
203  }
204  throw std::invalid_argument( "make_heatmap_matrix(): Invalid HeatmapParameters::ColorNorm" );
205 }
206 
207 } // namespace utils
208 } // namespace genesis
genesis::utils::HeatmapParameters::NormalizationRange::kAll
@ kAll
genesis::utils::HeatmapParameters::color_norm
ColorNorm color_norm
Select which ColorNormalization to apply to the data.
Definition: heat_map.hpp:116
genesis::utils::Matrix::cols
size_t cols() const
Definition: containers/matrix.hpp:181
heat_map.hpp
genesis::utils::HeatmapParameters::color_map
ColorMap color_map
Set the ColorMap with all its properties to use for the heatmap.
Definition: heat_map.hpp:111
genesis::utils::heat_map_matrix_update_min_max_
MinMaxPair< double > heat_map_matrix_update_min_max_(MinMaxPair< double > const &min_max, HeatmapParameters const &parameters)
Definition: heat_map.cpp:52
genesis::utils::Matrix::end
iterator end()
Definition: containers/matrix.hpp:304
genesis::utils::heat_map_matrix_range_
Matrix< utils::Color > heat_map_matrix_range_(Matrix< double > const &values, HeatmapParameters const &parameters)
Definition: heat_map.cpp:136
std.hpp
Provides some valuable additions to STD.
genesis::utils::Matrix::col
MatrixCol< self_type, value_type > col(size_t col)
Definition: containers/matrix.hpp:271
genesis::utils::HeatmapParameters::ColorNorm::kLogarithmic
@ kLogarithmic
genesis::utils::HeatmapParameters::normalization_range
NormalizationRange normalization_range
Range about which to calculate the min and max value for the normalization.
Definition: heat_map.hpp:129
genesis::utils::Matrix
Definition: placement/function/emd.hpp:53
genesis::utils::HeatmapParameters
Definition: heat_map.hpp:50
matrix.hpp
genesis::utils::HeatmapParameters::ColorNorm::kDiverging
@ kDiverging
genesis::utils::make_heatmap_color_norm
std::unique_ptr< ColorNormalization > make_heatmap_color_norm(Matrix< double > const &values, HeatmapParameters const &parameters)
Definition: heat_map.cpp:158
genesis::utils::heat_map_matrix_range_row_
Matrix< utils::Color > heat_map_matrix_range_row_(Matrix< double > const &values, HeatmapParameters const &parameters)
Definition: heat_map.cpp:89
genesis::utils::heat_map_matrix_range_col_
Matrix< utils::Color > heat_map_matrix_range_col_(Matrix< double > const &values, HeatmapParameters const &parameters)
Definition: heat_map.cpp:111
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
norm_diverging.hpp
statistics.hpp
genesis::utils::HeatmapParameters::min_value
double min_value
Minimum value to use for the color normalization.
Definition: heat_map.hpp:139
norm_linear.hpp
genesis::utils::heat_map_matrix_range_all_
Matrix< utils::Color > heat_map_matrix_range_all_(Matrix< double > const &values, HeatmapParameters const &parameters)
Definition: heat_map.cpp:67
genesis::utils::Matrix::begin
iterator begin()
Definition: containers/matrix.hpp:299
genesis::utils::Matrix::row
MatrixRow< self_type, value_type > row(size_t row)
Definition: containers/matrix.hpp:247
genesis::utils::MinMaxPair
Store a pair of min and max values.
Definition: statistics.hpp:105
genesis::utils::Matrix::rows
size_t rows() const
Definition: containers/matrix.hpp:176
genesis::utils::HeatmapParameters::ColorNorm::kLinear
@ kLinear
normalization.hpp
genesis::utils::finite_minimum_maximum
MinMaxPair< double > finite_minimum_maximum(ForwardIterator first, ForwardIterator last)
Return the minimum and the maximum of a range of double values.
Definition: statistics.hpp:239
genesis::utils::make_heatmap_matrix
Matrix< Color > make_heatmap_matrix(Matrix< double > const &values, HeatmapParameters const &parameters)
Definition: heat_map.cpp:189
genesis::utils::MinMaxPair::min
T min
Definition: statistics.hpp:107
genesis::utils::HeatmapParameters::max_value
double max_value
Maximum value to use for the color normalization.
Definition: heat_map.hpp:146
genesis::utils::HeatmapParameters::NormalizationRange::kCol
@ kCol
genesis::utils::HeatmapParameters::NormalizationRange::kRow
@ kRow
norm_logarithmic.hpp