1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-23 00:00:01 +01:00
VILLASnode/common/lib/hist.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

280 lines
5.6 KiB
C++
Raw Normal View History

2019-06-03 17:31:12 +02:00
/** Histogram class.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2019-01-13 00:42:26 +01:00
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLAScommon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <time.h>
2019-04-23 12:57:51 +02:00
#include <villas/utils.hpp>
2019-04-07 15:12:32 +02:00
#include <villas/hist.hpp>
#include <villas/config.h>
2019-04-07 15:12:32 +02:00
#include <villas/table.hpp>
using namespace villas::utils;
2019-06-03 17:31:12 +02:00
namespace villas {
2019-06-03 17:31:12 +02:00
Hist::Hist(int buckets, Hist::cnt_t wu)
{
2019-06-03 17:31:12 +02:00
length = buckets;
warmup = wu;
2019-06-03 17:31:12 +02:00
data = (Hist::cnt_t *) (buckets ? alloc(length * sizeof(Hist::cnt_t)) : nullptr);
2019-06-03 17:31:12 +02:00
Hist::reset();
}
2019-06-03 17:31:12 +02:00
Hist::~Hist()
{
2019-06-03 17:31:12 +02:00
if (data)
free(data);
}
2019-06-03 17:31:12 +02:00
void Hist::put(double value)
{
2019-06-03 17:31:12 +02:00
last = value;
/* Update min/max */
2019-06-03 17:31:12 +02:00
if (value > highest)
highest = value;
if (value < lowest)
lowest = value;
2019-06-03 17:31:12 +02:00
if (total < warmup) {
}
2019-06-03 17:31:12 +02:00
else if (total == warmup) {
low = getMean() - 3 * getStddev();
high = getMean() + 3 * getStddev();
resolution = (high - low) / length;
}
else {
2019-06-03 17:31:12 +02:00
int idx = round((value - low) / resolution);
/* Check bounds and increment */
2019-06-03 17:31:12 +02:00
if (idx >= length)
higher++;
else if (idx < 0)
2019-06-03 17:31:12 +02:00
lower++;
else if (data != nullptr)
data[idx]++;
}
2019-06-03 17:31:12 +02:00
total++;
/* Online / running calculation of variance and mean
* by Donald Knuths 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;
}
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]);
/* Set up for next iteration */
2019-06-03 17:31:12 +02:00
_m[1] = _m[0];
_s[1] = _s[0];
}
}
2019-06-03 17:31:12 +02:00
void Hist::reset()
{
2019-06-03 17:31:12 +02:00
total = 0;
higher = 0;
lower = 0;
2019-06-03 17:31:12 +02:00
highest = -DBL_MAX;
lowest = DBL_MAX;
2019-06-03 17:31:12 +02:00
if (data)
memset(data, 0, length * sizeof(unsigned));
}
2019-06-03 17:31:12 +02:00
double Hist::getMean() const
{
2019-06-03 17:31:12 +02:00
return (total > 0) ? _m[0] : NAN;
}
2019-06-03 17:31:12 +02:00
double Hist::getVar() const
{
2019-06-03 17:31:12 +02:00
return (total > 1) ? _s[0] / (total - 1) : NAN;
}
2019-06-03 17:31:12 +02:00
double Hist::getStddev() const
{
2019-06-03 17:31:12 +02:00
return sqrt(getVar());
}
2019-06-03 17:34:52 +02:00
void Hist::print(bool details) const
{
2019-06-03 17:31:12 +02:00
if (total > 0) {
Hist::cnt_t missed = total - higher - lower;
info("Counted values: %ju (%ju between %f and %f)", total, missed, low, high);
info("Highest: %g", highest);
info("Lowest: %g", lowest);
info("Mu: %g", getMean());
info("1/Mu: %g", 1.0 / getMean());
info("Variance: %g", getVar());
info("Stddev: %g", getStddev());
2019-06-03 17:34:52 +02:00
if (details && total - higher - lower > 0) {
2019-06-03 17:31:12 +02:00
char *buf =dump();
2019-01-22 16:00:20 +01:00
info("Matlab: %s", buf);
free(buf);
2019-06-03 17:31:12 +02:00
plot();
}
}
else
2019-06-03 17:31:12 +02:00
info("Counted values: %ju", total);
}
2019-06-03 17:31:12 +02:00
void Hist::plot() const
{
2019-06-03 17:31:12 +02:00
Hist::cnt_t max = 1;
/* Get highest bar */
2019-06-03 17:31:12 +02:00
for (int i = 0; i < length; i++) {
if (data[i] > max)
max = data[i];
}
2019-04-07 15:12:32 +02:00
std::vector<TableColumn> cols = {
{ -9, TableColumn::align::RIGHT, "Value", "%+9.3g" },
{ -6, TableColumn::align::RIGHT, "Count", "%6ju" },
2019-04-10 09:25:38 +02:00
{ 0, TableColumn::align::LEFT, "Plot", "%s", "occurences" }
};
2019-04-07 15:12:32 +02:00
Table table = Table(cols);
/* Print plot */
2019-04-07 15:12:32 +02:00
table.header();
2019-06-03 17:31:12 +02:00
for (int i = 0; i < length; i++) {
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);
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);
free(buf);
}
}
2019-06-03 17:31:12 +02:00
char * Hist::dump() const
{
2019-04-07 15:12:32 +02:00
char *buf = (char *) alloc(128);
strcatf(&buf, "[ ");
2019-06-03 17:31:12 +02:00
for (int i = 0; i < length; i++)
strcatf(&buf, "%ju ", data[i]);
strcatf(&buf, "]");
return buf;
}
2019-06-03 17:31:12 +02:00
json_t * Hist::toJson() const
{
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
);
2019-06-03 17:31:12 +02:00
if (total > 0) {
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()
));
}
2019-06-03 17:31:12 +02:00
if (total - lower - higher > 0) {
json_buckets = json_array();
2019-06-03 17:31:12 +02:00
for (int i = 0; i < length; i++)
json_array_append(json_buckets, json_integer(data[i]));
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
{
2019-06-03 17:31:12 +02:00
json_t *j = Hist::toJson();
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
{
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();
fprintf(f, "'buckets', %s", buf);
free(buf);
}
else
2019-06-03 17:31:12 +02:00
fprintf(f, "'buckets', zeros(1, %d)", length);
fprintf(f, ")");
return 0;
}
2019-06-03 17:31:12 +02:00
} /* namespace villas */