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  for (auto i = data.begin(); i != data.end(); ++i) {
89  sum += *i;
90  sum2 += *i * *i;
91  }
92 
93  FMeanVariance::result_type result = {0.0, 0.0};
94 
95  if (n > 0) {
96  result.first = sum / n;
97  if (n > 1)
98  result.second = sqrt((sum2 - result.first * sum) / (n - 1));
99  }
100  return result;
101 }
102 
103 template <typename T>
105 {
106  std::vector<double> buffer(data.size());
107  copy(data.begin(), data.end(), buffer.begin());
108 
109  FMedianMAD::result_type result;
110  result.first = median(buffer);
111 
112  transform(buffer.begin(), buffer.end(), buffer.begin(),
113  [&result](double x) {return fabs(x - result.first);});
114  result.second = median(buffer);
115  return result;
116 }
117 
118 double FMedianMAD::median(std::vector<double>& buf)const
119 {
120  if (buf.empty())
121  return 0.0;
122 
123  if (buf.size() & 1) {
124  auto i = buf.begin() + (buf.size() - 1) / 2;
125  std::nth_element(buf.begin(), i, buf.end());
126  return *i;
127  }else{
128  auto i1 = buf.begin() + buf.size() / 2 - 1;
129  auto i2 = buf.begin() + buf.size() / 2;
130  std::nth_element(buf.begin(), i1, buf.end());
131  std::nth_element(buf.begin(), i2, buf.end());
132  return (*i1 + *i2) / 2.0;
133  }
134 }
135 
137 #endif
Functor to be called by mia::filter to evaluate mean and variance of a series of data.
Definition: statistics.hh:36
base class for all filer type functors.
Definition: core/filter.hh:70
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
TFilter< std::pair< double, double > >::result_type result_type
Definition: statistics.hh:66
result_type operator()(const T &data) const
Definition: statistics.hh:104
double fabs(const T3DVector< T > &t)
A way to get the norm of a T3DVector using faba syntax.
Definition: 3d/vector.hh:333
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
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36