DiSMEC++
statistics.h
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #ifndef DISMEC_STATS_STATISTICS_H
7 #define DISMEC_STATS_STATISTICS_H
8 
9 #include "stats_base.h"
10 #include <unordered_map>
11 #include <functional>
12 
13 namespace dismec::stats {
14 
15  class CounterStat final : public StatImplBase<CounterStat> {
16  public:
17  ~CounterStat() override = default;
18  void record_int(long integer) override;
19 
20  [[nodiscard]] std::unique_ptr<Statistics> clone() const override;
21  void merge_imp(const CounterStat& other);
22  [[nodiscard]] nlohmann::json to_json() const override;
23  private:
24  long m_Counter = 0;
25  };
26 
27  class BasicStat final : public StatImplBase<BasicStat> {
28  public:
29  ~BasicStat() override = default;
30  void record_int(long value) override;
31  void record_real(real_t value) override;
32 
33  [[nodiscard]] std::unique_ptr<Statistics> clone() const override;
34  void merge_imp(const BasicStat& other);
35  [[nodiscard]] nlohmann::json to_json() const override;
36  private:
37  long m_Counter = 0;
38  double m_Sum = 0;
39  double m_SumSquared = 0;
40  };
41 
42  class TaggedStat final : public StatImplBase<TaggedStat> {
43  public:
44  TaggedStat(std::string tag, int max_tag, std::string transform_name = {}, std::function<double(double)> transform = {});
45  ~TaggedStat() override = default;
46  void record_int(long value) override;
47  void record_real(real_t value) override;
48 
49  [[nodiscard]] std::unique_ptr<Statistics> clone() const override;
50  void merge_imp(const TaggedStat& other);
51  [[nodiscard]] nlohmann::json to_json() const override;
52 
53  void setup(const StatisticsCollection& source) override;
54  private:
55  std::vector<long> m_Counters;
56  std::vector<double> m_Sums;
57  std::vector<double> m_SumsSquared;
58 
60  int m_MaxTag = -1;
61 
62  std::function<double(double)> m_Transform;
63  std::string m_TransformName;
64  };
65 
66  class MultiStat final : public StatImplBase<MultiStat> {
67  public:
68  MultiStat(std::unordered_map<std::string, std::unique_ptr<Statistics>> ss);
69  ~MultiStat() override = default;
70  void record_int(long value) override;
71  void record_real(real_t value) override;
72  void record_vec(const DenseRealVector& vector) override;
73 
74  [[nodiscard]] std::unique_ptr<Statistics> clone() const override;
75  void merge_imp(const MultiStat& other);
76  [[nodiscard]] nlohmann::json to_json() const override;
77 
78  void setup(const StatisticsCollection& source) override;
79  private:
80  using stats_map_t = std::unordered_map<std::string, std::unique_ptr<Statistics>>;
82 
83  template<class T>
84  void do_record(T&&);
85  };
86 
87  class FullRecordStat final: public StatImplBase<FullRecordStat> {
88  public:
89  FullRecordStat() = default;
90  ~FullRecordStat() override = default;
91 
92  void record_int(long value) override;
93  void record_real(real_t value) override;
94  [[nodiscard]] std::unique_ptr<Statistics> clone() const override;
95  void merge_imp(const FullRecordStat& other);
96  [[nodiscard]] nlohmann::json to_json() const override;
97 
98  private:
99  std::vector<real_t> m_Data;
100  };
101 
102  class VectorReductionStat final: public StatImplBase<VectorReductionStat> {
103  public:
104  VectorReductionStat(std::unique_ptr<Statistics> stat, std::string reduction);
105  ~VectorReductionStat() override = default;
106 
107  // this is needed so that the default implementation for vector recording in StatImplBase does not cause an error.
108  using Statistics::record;
109  void record_vec(const DenseRealVector& value) override;
110  [[nodiscard]] std::unique_ptr<Statistics> clone() const override;
111  void merge_imp(const VectorReductionStat& other);
112  [[nodiscard]] nlohmann::json to_json() const override;
113 
114  private:
115  std::unique_ptr<Statistics> m_Target;
116  std::function<real_t(const DenseRealVector&)> m_Reduction;
117  std::string m_ReductionName;
118  };
119 }
120 
121 #endif //DISMEC_STATS_STATISTICS_H
void record_real(real_t value) override
Definition: statistics.cpp:33
void record_int(long value) override
Definition: statistics.cpp:29
~BasicStat() override=default
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
Definition: statistics.cpp:49
std::unique_ptr< Statistics > clone() const override
Definition: statistics.cpp:39
void merge_imp(const BasicStat &other)
Definition: statistics.cpp:43
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
Definition: statistics.cpp:25
std::unique_ptr< Statistics > clone() const override
Definition: statistics.cpp:17
void merge_imp(const CounterStat &other)
Definition: statistics.cpp:21
~CounterStat() override=default
void record_int(long integer) override
Definition: statistics.cpp:13
void record_int(long value) override
Definition: statistics.cpp:170
void merge_imp(const FullRecordStat &other)
Definition: statistics.cpp:177
void record_real(real_t value) override
Definition: statistics.cpp:166
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
Definition: statistics.cpp:182
~FullRecordStat() override=default
std::unique_ptr< Statistics > clone() const override
Definition: statistics.cpp:174
std::vector< real_t > m_Data
Definition: statistics.h:99
std::unordered_map< std::string, std::unique_ptr< Statistics > > stats_map_t
Definition: statistics.h:80
std::unique_ptr< Statistics > clone() const override
Definition: statistics.cpp:135
void record_real(real_t value) override
Definition: statistics.cpp:121
stats_map_t m_SubStats
Definition: statistics.h:81
void merge_imp(const MultiStat &other)
Definition: statistics.cpp:143
void record_int(long value) override
Definition: statistics.cpp:118
void record_vec(const DenseRealVector &vector) override
Definition: statistics.cpp:124
void setup(const StatisticsCollection &source) override
This function has to be called before the Statistics is used to collect data for the first time.
Definition: statistics.cpp:160
MultiStat(std::unordered_map< std::string, std::unique_ptr< Statistics >> ss)
Definition: statistics.cpp:114
~MultiStat() override=default
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
Definition: statistics.cpp:149
Helper class for implementing Statistics classes.
Definition: stats_base.h:127
This class manages a collection of named Statistics objects.
Definition: collection.h:47
void record(int integer)
Definition: stats_base.h:70
A tag container combines a name with a shared pointer, which points to the tag value.
Definition: stats_base.h:30
void record_int(long value) override
Definition: statistics.cpp:62
void record_real(real_t value) override
Definition: statistics.cpp:66
std::vector< double > m_Sums
Definition: statistics.h:56
std::vector< long > m_Counters
Definition: statistics.h:55
TaggedStat(std::string tag, int max_tag, std::string transform_name={}, std::function< double(double)> transform={})
Definition: statistics.cpp:54
void merge_imp(const TaggedStat &other)
Definition: statistics.cpp:90
std::vector< double > m_SumsSquared
Definition: statistics.h:57
void setup(const StatisticsCollection &source) override
This function has to be called before the Statistics is used to collect data for the first time.
Definition: statistics.cpp:110
std::string m_TransformName
Definition: statistics.h:63
~TaggedStat() override=default
std::function< double(double)> m_Transform
Definition: statistics.h:62
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
Definition: statistics.cpp:105
std::unique_ptr< Statistics > clone() const override
Definition: statistics.cpp:86
std::function< real_t(const DenseRealVector &)> m_Reduction
Definition: statistics.h:116
std::unique_ptr< Statistics > clone() const override
Definition: statistics.cpp:205
VectorReductionStat(std::unique_ptr< Statistics > stat, std::string reduction)
Definition: statistics.cpp:186
~VectorReductionStat() override=default
void record_vec(const DenseRealVector &value) override
Definition: statistics.cpp:201
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
Definition: statistics.cpp:213
std::unique_ptr< Statistics > m_Target
Definition: statistics.h:115
void merge_imp(const VectorReductionStat &other)
Definition: statistics.cpp:209
nlohmann::json json
Definition: model-io.cpp:22
types::DenseVector< real_t > DenseRealVector
Any dense, real values vector.
Definition: matrix_types.h:40
float real_t
The default type for floating point values.
Definition: config.h:17