2023-08-28 12:31:18 +02:00
|
|
|
/* Histogram class.
|
2018-08-22 11:29:39 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-06-26 20:15:26 +02:00
|
|
|
#include <vector>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#include <jansson.h>
|
|
|
|
|
2021-02-16 14:15:38 +01:00
|
|
|
#include <villas/log.hpp>
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
#define HEIGHT (LOG_WIDTH - 55)
|
|
|
|
#define SEQ 17
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
namespace villas {
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-04-03 10:00:02 +00:00
|
|
|
// Histogram structure used to collect statistics.
|
2019-06-03 17:31:12 +02:00
|
|
|
class Hist {
|
|
|
|
|
|
|
|
public:
|
2023-09-07 13:19:19 +02:00
|
|
|
using cnt_t = uintmax_t;
|
|
|
|
using idx_t = std::vector<cnt_t>::difference_type;
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Initialize struct Hist with supplied values and allocate memory for buckets.
|
|
|
|
Hist(int buckets = 0, cnt_t warmup = 0);
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Reset all counters and values back to zero.
|
|
|
|
void reset();
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Count a value within its corresponding bucket.
|
|
|
|
void put(double value);
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2024-04-09 11:17:49 +02:00
|
|
|
// Calculate the variance of all counted values.
|
2023-09-07 13:19:19 +02:00
|
|
|
double getVar() const;
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Calculate the mean average of all counted values.
|
|
|
|
double getMean() const;
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Calculate the standard derivation of all counted values.
|
|
|
|
double getStddev() const;
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2024-04-09 11:17:49 +02:00
|
|
|
// Print all statistical properties of distribution including a graphical plot of the histogram.
|
2024-04-09 11:18:22 +02:00
|
|
|
void print(Logger logger, bool details, std::string prefix = "") const;
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Print ASCII style plot of histogram.
|
|
|
|
void plot(Logger logger) const;
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Dump histogram data in Matlab format.
|
|
|
|
//
|
|
|
|
// @return The string containing the dump. The caller is responsible to free() the buffer.
|
|
|
|
char *dump() const;
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Prints Matlab struct containing all infos to file.
|
|
|
|
int dumpMatlab(FILE *f) const;
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2024-04-09 11:17:49 +02:00
|
|
|
// Write the histogram in JSON format to file \p f.
|
2023-09-07 13:19:19 +02:00
|
|
|
int dumpJson(FILE *f) const;
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Build a libjansson / JSON object of the histogram.
|
|
|
|
json_t *toJson() const;
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
double getHigh() const { return high; }
|
2019-06-04 16:55:21 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
double getLow() const { return low; }
|
2019-06-04 16:55:21 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
double getHighest() const { return highest; }
|
2019-06-04 16:55:21 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
double getLowest() const { return lowest; }
|
2019-06-04 16:55:21 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
double getLast() const { return last; }
|
2019-06-03 17:31:12 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cnt_t getTotal() const { return total; }
|
2019-06-04 16:55:21 +02:00
|
|
|
|
2019-06-03 17:31:12 +02:00
|
|
|
protected:
|
2023-09-07 13:19:19 +02:00
|
|
|
double resolution; // The distance between two adjacent buckets.
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
double high; // The value of the highest bucket.
|
|
|
|
double low; // The value of the lowest bucket.
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
double highest; // The highest value observed (may be higher than #high).
|
|
|
|
double lowest; // The lowest value observed (may be lower than #low).
|
|
|
|
double last; // The last value which has been put into the buckets.
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cnt_t total; // Total number of counted values.
|
|
|
|
cnt_t warmup; // Number of values which are used during warmup phase.
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
cnt_t higher; // The number of values which are higher than #high.
|
|
|
|
cnt_t lower; // The number of values which are lower than #low.
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
std::vector<cnt_t> data; // Bucket counters.
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
double _m[2], _s[2]; // Private variables for online variance calculation.
|
2018-08-22 11:29:39 +02:00
|
|
|
};
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
} // namespace villas
|