9 #include <nlohmann/json.hpp>
18 return std::make_unique<CounterStat>();
26 return {{
"Counter",
m_Counter}, {
"Type",
"Counter"}};
40 return std::make_unique<BasicStat>();
54 TaggedStat::TaggedStat(std::string tag,
int max_tag, std::string transform_name, std::function<
double(
double)> transform ) :
55 m_Tag(
TagContainer::create_empty_container(std::move(tag)) ),
57 m_Transform( std::move(transform) ),
58 m_TransformName( std::move(transform_name) )
69 throw std::logic_error(
"Missing tag!");
98 for(
long i = 0; i < other_size; ++i) {
114 MultiStat::MultiStat(std::unordered_map<std::string, std::unique_ptr<Statistics>> ss) : m_SubStats(std::move(ss)) {
131 entry.second->record(std::forward<T>(value));
138 new_map.emplace(entry.first, entry.second->clone());
140 return std::make_unique<MultiStat>(std::move(new_map));
145 entry.second->merge( *other.
m_SubStats.at(entry.first) );
151 result[
"Type"] =
"Multi";
154 data[entry.first] = entry.second->to_json();
156 result[
"Data"] = std::move(data);
162 entry.second->setup(source);
175 return std::make_unique<FullRecordStat>();
183 return {{
"Type",
"Full"}, {
"Values",
m_Data}};
187 m_Target( std::move(stat) ), m_ReductionName(std::move(reduction)) {
197 throw std::runtime_error(
"Unknown reduction operation");
219 auto type = source.at(
"type").get<std::string>();
220 if(type ==
"Basic") {
221 return std::make_unique<BasicStat>();
222 }
else if (type ==
"Counter") {
223 return std::make_unique<CounterStat>();
224 }
else if (type ==
"Tagged") {
225 std::function<double(
double)> transform;
226 std::string transform_name =
"lin";
227 if(source.contains(
"transform")) {
228 transform_name= source.at(
"transform");
229 if(transform_name ==
"log") {
230 transform = [](
double d){
return std::log(d); };
234 if(source.contains(
"max_tag")) {
235 max_tag = source.at(
"max_tag").get<
int>();
238 return std::make_unique<TaggedStat>(source.at(
"tag").get<std::string>(), max_tag,
239 std::move(transform_name), std::move(transform));
240 }
else if (type ==
"LinHist") {
242 source.at(
"min").get<
real_t>(), source.at(
"max").get<
real_t>());
243 }
else if (type ==
"LogHist") {
245 source.at(
"min").get<
real_t>(), source.at(
"max").get<
real_t>());
246 }
else if (type ==
"TagLinHist") {
248 source.at(
"max_tag").get<
int>(), source.at(
"bins").get<
int>(),
249 source.at(
"min").get<
real_t>(),
250 source.at(
"max").get<
real_t>());
251 }
else if (type ==
"TagLogHist") {
253 source.at(
"tag").get<std::string>(),
254 source.at(
"max_tag").get<
int>(), source.at(
"bins").get<
int>(),
255 source.at(
"min").get<
real_t>(),
256 source.at(
"max").get<
real_t>());
257 }
else if (type ==
"Multi") {
258 std::unordered_map<std::string, std::unique_ptr<Statistics>> sub_stats;
259 for(
auto& sub : source.at(
"stats").items()) {
262 return std::make_unique<MultiStat>(std::move(sub_stats));
263 }
else if (type ==
"Full") {
264 return std::make_unique<FullRecordStat>();
265 }
else if (type ==
"VectorReduction") {
266 return std::make_unique<VectorReductionStat>(
make_stat_from_json(source.at(
"stat")), source.at(
"reduction"));
269 throw std::runtime_error(
"Unknown statistics type");
void record_real(real_t value) override
void record_int(long value) override
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
std::unique_ptr< Statistics > clone() const override
void merge_imp(const BasicStat &other)
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
std::unique_ptr< Statistics > clone() const override
void merge_imp(const CounterStat &other)
void record_int(long integer) override
void record_int(long value) override
void merge_imp(const FullRecordStat &other)
void record_real(real_t value) override
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
std::unique_ptr< Statistics > clone() const override
std::vector< real_t > m_Data
std::unordered_map< std::string, std::unique_ptr< Statistics > > stats_map_t
std::unique_ptr< Statistics > clone() const override
void record_real(real_t value) override
void merge_imp(const MultiStat &other)
void record_int(long value) override
void record_vec(const DenseRealVector &vector) override
void setup(const StatisticsCollection &source) override
This function has to be called before the Statistics is used to collect data for the first time.
MultiStat(std::unordered_map< std::string, std::unique_ptr< Statistics >> ss)
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
This class manages a collection of named Statistics objects.
TagContainer get_tag_by_name(const std::string &name) const
Gets the tag with the given name.
A tag container combines a name with a shared pointer, which points to the tag value.
int get_value() const
Returns the current value of the tag. Requires the container to not be empty.
const std::string & get_name() const
returns the name of the associated tag
void record_int(long value) override
void record_real(real_t value) override
std::vector< double > m_Sums
std::vector< long > m_Counters
TaggedStat(std::string tag, int max_tag, std::string transform_name={}, std::function< double(double)> transform={})
void merge_imp(const TaggedStat &other)
std::vector< double > m_SumsSquared
void setup(const StatisticsCollection &source) override
This function has to be called before the Statistics is used to collect data for the first time.
std::string m_TransformName
std::function< double(double)> m_Transform
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
std::unique_ptr< Statistics > clone() const override
std::function< real_t(const DenseRealVector &)> m_Reduction
std::unique_ptr< Statistics > clone() const override
std::string m_ReductionName
VectorReductionStat(std::unique_ptr< Statistics > stat, std::string reduction)
void record_vec(const DenseRealVector &value) override
nlohmann::json to_json() const override
Converts the statistics current value into a json object.
std::unique_ptr< Statistics > m_Target
void merge_imp(const VectorReductionStat &other)
std::unique_ptr< Statistics > make_linear_histogram(int bins, real_t min, real_t max)
std::unique_ptr< stats::Statistics > make_stat_from_json(const nlohmann::json &source)
Generates a stats::Statistics object based on a json configuration.
std::unique_ptr< Statistics > make_logarithmic_histogram(int bins, real_t min, real_t max)
constexpr auto ssize(const C &c) -> std::common_type_t< std::ptrdiff_t, std::make_signed_t< decltype(c.size())>>
signed size free function. Taken from https://en.cppreference.com/w/cpp/iterator/size
types::DenseVector< real_t > DenseRealVector
Any dense, real values vector.
float real_t
The default type for floating point values.