2019-06-03 17:31:12 +02:00
|
|
|
|
/** Histogram class.
|
2018-08-22 11:29:39 +02:00
|
|
|
|
*
|
2022-12-14 17:39:07 +01:00
|
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:05:42 -04:00
|
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-05-19 17:40:10 +02:00
|
|
|
|
* @license Apache License 2.0
|
2018-08-22 11:29:39 +02:00
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
2019-06-23 16:26:44 +02:00
|
|
|
|
#include <cmath>
|
2019-06-26 20:15:26 +02:00
|
|
|
|
#include <algorithm>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2019-04-23 12:57:51 +02:00
|
|
|
|
#include <villas/utils.hpp>
|
2019-04-07 15:12:32 +02:00
|
|
|
|
#include <villas/hist.hpp>
|
2021-08-11 12:40:19 -04:00
|
|
|
|
#include <villas/config.hpp>
|
2019-04-07 15:12:32 +02:00
|
|
|
|
#include <villas/table.hpp>
|
2019-10-27 20:23:47 +01:00
|
|
|
|
#include <villas/exceptions.hpp>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2021-02-16 14:15:38 +01:00
|
|
|
|
using namespace villas;
|
2019-05-30 12:43:37 +02:00
|
|
|
|
using namespace villas::utils;
|
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
namespace villas {
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2019-06-26 20:15:26 +02:00
|
|
|
|
Hist::Hist(int buckets, Hist::cnt_t wu) :
|
2020-09-11 16:01:16 +02:00
|
|
|
|
resolution(0),
|
2022-03-25 17:33:34 +01:00
|
|
|
|
high(0),
|
|
|
|
|
low(0),
|
2020-09-11 16:01:16 +02:00
|
|
|
|
highest(std::numeric_limits<double>::min()),
|
|
|
|
|
lowest(std::numeric_limits<double>::max()),
|
2022-03-25 17:33:34 +01:00
|
|
|
|
last(0),
|
2020-09-11 16:01:16 +02:00
|
|
|
|
total(0),
|
|
|
|
|
warmup(wu),
|
|
|
|
|
higher(0),
|
|
|
|
|
lower(0),
|
|
|
|
|
data(buckets, 0),
|
|
|
|
|
_m{0, 0},
|
|
|
|
|
_s{0, 0}
|
|
|
|
|
{ }
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
void Hist::put(double value)
|
2018-08-22 11:29:39 +02:00
|
|
|
|
{
|
2019-06-03 17:31:12 +02:00
|
|
|
|
last = value;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
|
// Update min/max
|
2019-06-03 17:31:12 +02:00
|
|
|
|
if (value > highest)
|
|
|
|
|
highest = value;
|
|
|
|
|
if (value < lowest)
|
|
|
|
|
lowest = value;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2019-06-26 20:15:26 +02:00
|
|
|
|
if (data.size()) {
|
|
|
|
|
if (total < warmup) {
|
2022-12-02 17:16:44 +01:00
|
|
|
|
// We are still in warmup phase... Waiting for more samples...
|
2019-06-26 20:15:26 +02:00
|
|
|
|
}
|
2019-09-16 15:57:10 +02:00
|
|
|
|
else if (data.size() && total == warmup && warmup != 0) {
|
2019-06-26 20:15:26 +02:00
|
|
|
|
low = getMean() - 3 * getStddev();
|
|
|
|
|
high = getMean() + 3 * getStddev();
|
|
|
|
|
resolution = (high - low) / data.size();
|
|
|
|
|
}
|
2020-09-11 16:01:16 +02:00
|
|
|
|
else if (data.size() && (total == warmup) && (warmup == 0)) {
|
2022-12-02 17:16:44 +01:00
|
|
|
|
// There is no warmup phase
|
2020-09-11 16:01:16 +02:00
|
|
|
|
// TODO resolution = ?
|
|
|
|
|
}
|
2019-06-26 20:15:26 +02:00
|
|
|
|
else {
|
|
|
|
|
idx_t idx = std::round((value - low) / resolution);
|
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
|
// Check bounds and increment
|
2019-06-26 20:15:26 +02:00
|
|
|
|
if (idx >= (idx_t) data.size())
|
|
|
|
|
higher++;
|
|
|
|
|
else if (idx < 0)
|
|
|
|
|
lower++;
|
|
|
|
|
else
|
|
|
|
|
data[idx]++;
|
|
|
|
|
}
|
2018-08-22 11:29:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
total++;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
|
// Online / running calculation of variance and mean
|
|
|
|
|
// by Donald Knuth’s Art of Computer Programming, Vol 2, page 232, 3rd edition
|
2019-06-03 17:31:12 +02:00
|
|
|
|
if (total == 1) {
|
|
|
|
|
_m[1] = _m[0] = value;
|
|
|
|
|
_s[1] = 0.0;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-06-03 17:31:12 +02:00
|
|
|
|
_m[0] = _m[1] + (value - _m[1]) / total;
|
|
|
|
|
_s[0] = _s[1] + (value - _m[1]) * (value - _m[0]);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
|
// Set up for next iteration
|
2019-06-03 17:31:12 +02:00
|
|
|
|
_m[1] = _m[0];
|
|
|
|
|
_s[1] = _s[0];
|
2018-08-22 11:29:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
void Hist::reset()
|
2018-08-22 11:29:39 +02:00
|
|
|
|
{
|
2019-06-03 17:31:12 +02:00
|
|
|
|
total = 0;
|
|
|
|
|
higher = 0;
|
|
|
|
|
lower = 0;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2019-06-26 20:15:26 +02:00
|
|
|
|
highest = std::numeric_limits<double>::min();
|
|
|
|
|
lowest = std::numeric_limits<double>::max();
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2019-06-26 20:15:26 +02:00
|
|
|
|
for (auto &elm : data)
|
|
|
|
|
elm = 0;
|
2018-08-22 11:29:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
double Hist::getMean() const
|
2018-08-22 11:29:39 +02:00
|
|
|
|
{
|
2019-06-26 20:15:26 +02:00
|
|
|
|
return total > 0 ? _m[0] : std::numeric_limits<double>::quiet_NaN();
|
2018-08-22 11:29:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
double Hist::getVar() const
|
2018-08-22 11:29:39 +02:00
|
|
|
|
{
|
2019-06-26 20:15:26 +02:00
|
|
|
|
return total > 1 ? _s[0] / (total - 1) : std::numeric_limits<double>::quiet_NaN();
|
2018-08-22 11:29:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
double Hist::getStddev() const
|
2018-08-22 11:29:39 +02:00
|
|
|
|
{
|
2019-06-03 17:31:12 +02:00
|
|
|
|
return sqrt(getVar());
|
2018-08-22 11:29:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 14:15:38 +01:00
|
|
|
|
void Hist::print(Logger logger, bool details) const
|
2018-08-22 11:29:39 +02:00
|
|
|
|
{
|
2019-06-03 17:31:12 +02:00
|
|
|
|
if (total > 0) {
|
|
|
|
|
Hist::cnt_t missed = total - higher - lower;
|
|
|
|
|
|
2021-02-16 14:15:38 +01:00
|
|
|
|
logger->info("Counted values: {} ({} between {} and {})", total, missed, low, high);
|
|
|
|
|
logger->info("Highest: {:g}", highest);
|
|
|
|
|
logger->info("Lowest: {:g}", lowest);
|
|
|
|
|
logger->info("Mu: {:g}", getMean());
|
|
|
|
|
logger->info("1/Mu: {:g}", 1.0 / getMean());
|
|
|
|
|
logger->info("Variance: {:g}", getVar());
|
|
|
|
|
logger->info("Stddev: {:g}", getStddev());
|
2019-06-03 17:31:12 +02:00
|
|
|
|
|
2019-06-03 17:34:52 +02:00
|
|
|
|
if (details && total - higher - lower > 0) {
|
2019-07-01 08:30:05 +02:00
|
|
|
|
char *buf = dump();
|
2021-02-16 14:15:38 +01:00
|
|
|
|
logger->info("Matlab: {}", buf);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
free(buf);
|
|
|
|
|
|
2021-02-16 14:15:38 +01:00
|
|
|
|
plot(logger);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2021-02-16 14:15:38 +01:00
|
|
|
|
logger->info("Counted values: {}", total);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 14:15:38 +01:00
|
|
|
|
void Hist::plot(Logger logger) const
|
2018-08-22 11:29:39 +02:00
|
|
|
|
{
|
2022-12-02 17:16:44 +01:00
|
|
|
|
// Get highest bar
|
2019-06-26 20:15:26 +02:00
|
|
|
|
Hist::cnt_t max = *std::max_element(data.begin(), data.end());
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2019-04-07 15:12:32 +02:00
|
|
|
|
std::vector<TableColumn> cols = {
|
2019-06-23 16:12:53 +02:00
|
|
|
|
{ -9, TableColumn::Alignment::RIGHT, "Value", "%+9.3g" },
|
|
|
|
|
{ -6, TableColumn::Alignment::RIGHT, "Count", "%6ju" },
|
|
|
|
|
{ 0, TableColumn::Alignment::LEFT, "Plot", "%s", "occurences" }
|
2018-08-22 11:29:39 +02:00
|
|
|
|
};
|
|
|
|
|
|
2021-02-16 14:15:38 +01:00
|
|
|
|
Table table = Table(logger, cols);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
|
// Print plot
|
2019-04-07 15:12:32 +02:00
|
|
|
|
table.header();
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
2019-06-26 20:15:26 +02:00
|
|
|
|
for (size_t i = 0; i < data.size(); i++) {
|
2019-06-03 17:31:12 +02:00
|
|
|
|
double value = low + (i) * resolution;
|
|
|
|
|
Hist::cnt_t cnt = data[i];
|
2019-04-07 15:12:32 +02:00
|
|
|
|
int bar = cols[2].getWidth() * ((double) cnt / max);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
|
|
char *buf = strf("%s", "");
|
|
|
|
|
for (int i = 0; i < bar; i++)
|
|
|
|
|
buf = strcatf(&buf, "\u2588");
|
|
|
|
|
|
2019-04-10 09:25:38 +02:00
|
|
|
|
table.row(3, value, cnt, buf);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
char * Hist::dump() const
|
2018-08-22 11:29:39 +02:00
|
|
|
|
{
|
2019-07-01 08:30:05 +02:00
|
|
|
|
char *buf = new char[128];
|
2019-10-27 20:23:47 +01:00
|
|
|
|
if (!buf)
|
2020-07-04 16:20:21 +02:00
|
|
|
|
throw MemoryAllocationError();
|
2019-10-27 20:23:47 +01:00
|
|
|
|
|
|
|
|
|
memset(buf, 0, 128);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
|
|
strcatf(&buf, "[ ");
|
|
|
|
|
|
2019-06-26 20:15:26 +02:00
|
|
|
|
for (auto elm : data)
|
|
|
|
|
strcatf(&buf, "%ju ", elm);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
|
|
strcatf(&buf, "]");
|
|
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
json_t * Hist::toJson() const
|
2018-08-22 11:29:39 +02:00
|
|
|
|
{
|
|
|
|
|
json_t *json_buckets, *json_hist;
|
|
|
|
|
|
|
|
|
|
json_hist = json_pack("{ s: f, s: f, s: i }",
|
2019-06-03 17:31:12 +02:00
|
|
|
|
"low", low,
|
|
|
|
|
"high", high,
|
|
|
|
|
"total", total
|
2018-08-22 11:29:39 +02:00
|
|
|
|
);
|
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
if (total > 0) {
|
2018-08-22 11:29:39 +02:00
|
|
|
|
json_object_update(json_hist, json_pack("{ s: i, s: i, s: f, s: f, s: f, s: f, s: f }",
|
2019-06-03 17:31:12 +02:00
|
|
|
|
"higher", higher,
|
|
|
|
|
"lower", lower,
|
|
|
|
|
"highest", highest,
|
|
|
|
|
"lowest", lowest,
|
|
|
|
|
"mean", getMean(),
|
|
|
|
|
"variance", getVar(),
|
|
|
|
|
"stddev", getStddev()
|
2018-08-22 11:29:39 +02:00
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
if (total - lower - higher > 0) {
|
2018-08-22 11:29:39 +02:00
|
|
|
|
json_buckets = json_array();
|
|
|
|
|
|
2019-06-26 20:15:26 +02:00
|
|
|
|
for (auto elm : data)
|
|
|
|
|
json_array_append(json_buckets, json_integer(elm));
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
|
|
json_object_set(json_hist, "buckets", json_buckets);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return json_hist;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
int Hist::dumpJson(FILE *f) const
|
2018-08-22 11:29:39 +02:00
|
|
|
|
{
|
2019-06-03 17:31:12 +02:00
|
|
|
|
json_t *j = Hist::toJson();
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
|
|
int ret = json_dumpf(j, f, 0);
|
|
|
|
|
|
|
|
|
|
json_decref(j);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
|
int Hist::dumpMatlab(FILE *f) const
|
2018-08-22 11:29:39 +02:00
|
|
|
|
{
|
|
|
|
|
fprintf(f, "struct(");
|
2019-06-03 17:31:12 +02:00
|
|
|
|
fprintf(f, "'low', %f, ", low);
|
|
|
|
|
fprintf(f, "'high', %f, ", high);
|
|
|
|
|
fprintf(f, "'total', %ju, ", total);
|
|
|
|
|
fprintf(f, "'higher', %ju, ", higher);
|
|
|
|
|
fprintf(f, "'lower', %ju, ", lower);
|
|
|
|
|
fprintf(f, "'highest', %f, ", highest);
|
|
|
|
|
fprintf(f, "'lowest', %f, ", lowest);
|
|
|
|
|
fprintf(f, "'mean', %f, ", getMean());
|
|
|
|
|
fprintf(f, "'variance', %f, ", getVar());
|
|
|
|
|
fprintf(f, "'stddev', %f, ", getStddev());
|
|
|
|
|
|
|
|
|
|
if (total - lower - higher > 0) {
|
|
|
|
|
char *buf = dump();
|
2018-08-22 11:29:39 +02:00
|
|
|
|
fprintf(f, "'buckets', %s", buf);
|
|
|
|
|
free(buf);
|
|
|
|
|
}
|
|
|
|
|
else
|
2019-06-26 20:15:26 +02:00
|
|
|
|
fprintf(f, "'buckets', zeros(1, %zu)", data.size());
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
|
|
fprintf(f, ")");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-06-03 17:31:12 +02:00
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
|
} // namespace villas
|