|
| 1 | +// This file is part of the AliceVision project. |
| 2 | +// Copyright (c) 2024 AliceVision contributors. |
| 3 | +// Copyright (c) 2012 openMVG contributors. |
| 4 | +// This Source Code Form is subject to the terms of the Mozilla Public License, |
| 5 | +// v. 2.0. If a copy of the MPL was not distributed with this file, |
| 6 | +// You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | + |
| 11 | +#include <algorithm> |
| 12 | +#include <cmath> |
| 13 | +#include <numeric> |
| 14 | +#include <string> |
| 15 | +#include <iostream> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +namespace aliceVision { |
| 19 | + |
| 20 | + |
| 21 | +/** Get back the min, mean, median and the max |
| 22 | + * values of an iterable sequence. |
| 23 | + */ |
| 24 | +template<typename Type> |
| 25 | +struct BoxStats |
| 26 | +{ |
| 27 | + Type min{}, max{}, mean{}, median{}, firstQuartile{}, thirdQuartile{}; |
| 28 | + |
| 29 | + BoxStats() = default; |
| 30 | + |
| 31 | + template<typename DataInputIterator> |
| 32 | + BoxStats(DataInputIterator begin, DataInputIterator end) |
| 33 | + { |
| 34 | + compute(begin, end); |
| 35 | + } |
| 36 | + |
| 37 | + template<typename DataInputIterator> |
| 38 | + void compute(DataInputIterator begin, DataInputIterator end) |
| 39 | + { |
| 40 | + if (std::distance(begin, end) < 1) |
| 41 | + { |
| 42 | + min = 0; |
| 43 | + max = 0; |
| 44 | + mean = 0; |
| 45 | + median = 0; |
| 46 | + firstQuartile = 0; |
| 47 | + thirdQuartile = 0; |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + std::vector<Type> vec_val(begin, end); |
| 52 | + std::sort(vec_val.begin(), vec_val.end()); |
| 53 | + min = vec_val[0]; |
| 54 | + max = vec_val[vec_val.size() - 1]; |
| 55 | + mean = accumulate(vec_val.begin(), vec_val.end(), Type(0)) / static_cast<Type>(vec_val.size()); |
| 56 | + median = vec_val[vec_val.size() / 2]; |
| 57 | + firstQuartile = vec_val[vec_val.size() / 4]; |
| 58 | + thirdQuartile = vec_val[(vec_val.size() * 3) / 4]; |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +template<typename Type> |
| 63 | +inline std::ostream& operator<<(std::ostream& os, const BoxStats<Type> obj) |
| 64 | +{ |
| 65 | + os << "\t min: " << obj.min |
| 66 | + << "\n" |
| 67 | + "\t mean: " |
| 68 | + << obj.mean |
| 69 | + << "\n" |
| 70 | + "\t median: " |
| 71 | + << obj.median |
| 72 | + << "\n" |
| 73 | + "\t max: " |
| 74 | + << obj.max |
| 75 | + << "\n" |
| 76 | + "\t first quartile: " |
| 77 | + << obj.firstQuartile |
| 78 | + << "\n" |
| 79 | + "\t third quartile: " |
| 80 | + << obj.thirdQuartile; |
| 81 | + |
| 82 | + return os; |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +} // namespace aliceVision |
0 commit comments