My Project
statistics.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef mia_core_statistics_hh
22 #define mia_core_statistics_hh
23 
24 #include <cmath>
25 #include <mia/core/filter.hh>
26 
27 
29 
36 struct FMeanVariance: public TFilter< std::pair<double, double>> {
37 
43 
51  template <typename T>
52  result_type operator()( const T& data) const;
53 };
54 
60 struct FMedianMAD: public TFilter< std::pair<double, double>> {
61 
67 
74  template <typename T>
75  result_type operator()( const T& data) const;
76 private:
77  double median(std::vector<double>& buf)const;
78 };
79 
80 
81 
82 template <typename T>
84 {
85  double sum = 0.0;
86  double sum2 = 0.0;
87  double n = data.size();
88 
89  for (auto i = data.begin(); i != data.end(); ++i) {
90  sum += *i;
91  sum2 += *i * *i;
92  }
93 
94  FMeanVariance::result_type result = {0.0, 0.0};
95 
96  if (n > 0) {
97  result.first = sum / n;
98 
99  if (n > 1)
100  result.second = sqrt((sum2 - result.first * sum) / (n - 1));
101  }
102 
103  return result;
104 }
105 
106 template <typename T>
108 {
109  std::vector<double> buffer(data.size());
110  copy(data.begin(), data.end(), buffer.begin());
112  result.first = median(buffer);
113  transform(buffer.begin(), buffer.end(), buffer.begin(),
114  [&result](double x) {
115  return fabs(x - result.first);
116  });
117  result.second = median(buffer);
118  return result;
119 }
120 
121 double FMedianMAD::median(std::vector<double>& buf)const
122 {
123  if (buf.empty())
124  return 0.0;
125 
126  if (buf.size() & 1) {
127  auto i = buf.begin() + (buf.size() - 1) / 2;
128  std::nth_element(buf.begin(), i, buf.end());
129  return *i;
130  } else {
131  auto i1 = buf.begin() + buf.size() / 2 - 1;
132  auto i2 = buf.begin() + buf.size() / 2;
133  std::nth_element(buf.begin(), i1, buf.end());
134  std::nth_element(buf.begin(), i2, buf.end());
135  return (*i1 + *i2) / 2.0;
136  }
137 }
138 
140 #endif
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
Functor to be called by mia::filter to evaluate mean and variance of a series of data.
Definition: statistics.hh:36
TFilter< std::pair< double, double > >::result_type result_type
Definition: statistics.hh:42
result_type operator()(const T &data) const
Definition: statistics.hh:83
Functor to be called by mia::filter to evaluate median and median average distance (MAD) of a series ...
Definition: statistics.hh:60
TFilter< std::pair< double, double > >::result_type result_type
Definition: statistics.hh:66
result_type operator()(const T &data) const
Definition: statistics.hh:107
base class for all filer type functors.
Definition: core/filter.hh:70