mediapipe/mediapipe2/framework/profiler/reporter/statistic.cc
2021-06-10 23:01:19 +00:00

47 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "mediapipe/framework/profiler/reporter/statistic.h"
#include <cmath>
#include "mediapipe/framework/calculator.pb.h"
#include "mediapipe/framework/calculator_profile.pb.h"
namespace mediapipe {
namespace reporter {
// Pushes a single value into the statistics, updating mean and stddev.
void Statistic::Push(double x) {
++counter_;
if (counter_ == 1) {
mean_ = x;
ssd_ = 0.0;
total_impl_ = x;
} else {
// Implementing Welfords algorithm for computing variance.
auto old_mean = mean_;
mean_ = mean_ + (x - mean_) / counter_;
ssd_ = ssd_ + (x - mean_) * (x - old_mean);
total_impl_ += x;
}
}
// Returns the number of data points used to calculator the mean and
// stddev.
int Statistic::data_count() const { return counter_; }
// Returns the mean of the data pushed into this statistic.
double Statistic::mean() const { return (counter_ > 0) ? mean_ : 0.0; }
// Returns the variance of the data pushed into this statistic.
double Statistic::variance() const {
return ((counter_ > 1) ? ssd_ / (counter_ - 1) : 0.0);
}
// Returns the standard deviation of the data pushed into this statistic.
double Statistic::stddev() const { return std::sqrt(variance()); }
double Statistic::total() const { return total_impl_; }
} // namespace reporter
} // namespace mediapipe