BioDynaMo  v1.05.120-25dc9790
time_series.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 #ifndef CORE_ANALYSIS_TIME_SERIES_H_
15 #define CORE_ANALYSIS_TIME_SERIES_H_
16 
17 #include <functional>
18 #include <unordered_map>
19 #include <vector>
20 #include "core/analysis/reduce.h"
21 #include "core/real_t.h"
22 #include "core/util/root.h"
23 
24 namespace bdm {
25 
26 class Simulation;
27 
28 namespace experimental {
29 
40  public:
41  virtual ~DataTransformer() = default;
42 
43  virtual void TransformXValues(const std::vector<real_t>& old_x_values,
44  std::vector<real_t>& new_x_values) const = 0;
45  virtual void TransformYValues(const std::vector<real_t>& old_y_values,
46  std::vector<real_t>& new_y_values) const = 0;
47  virtual void TransformYErrorLow(
48  const std::vector<real_t>& old_y_error_low,
49  std::vector<real_t>& new_y_error_low) const = 0;
50  virtual void TransformYErrorHigh(
51  const std::vector<real_t>& old_y_error_high,
52  std::vector<real_t>& new_y_error_high) const = 0;
53 
55 };
56 
71  public:
75  void TransformXValues(const std::vector<real_t>& old_x_values,
76  std::vector<real_t>& new_x_values) const final;
77 
81  void TransformYValues(const std::vector<real_t>& old_y_values,
82  std::vector<real_t>& new_y_values) const final;
83 
88  void TransformYErrorLow(const std::vector<real_t>& old_y_error_low,
89  std::vector<real_t>& new_y_error_low) const final;
90 
95  void TransformYErrorHigh(const std::vector<real_t>& old_y_error_high,
96  std::vector<real_t>& new_y_error_high) const final;
97 
98  void SetXSlope(real_t slope);
99  void SetXIntercept(real_t intercept);
100  void SetYSlope(real_t slope);
101  void SetYIntercept(real_t intercept);
102  void SetYErrorLowSlope(real_t slope);
103  void SetYErrorLowIntercept(real_t intercept);
104  void SetYErrorHighSlope(real_t slope);
105  void SetYErrorHighIntercept(real_t intercept);
106 
107  private:
116 
118 };
119 
123 class TimeSeries {
124  public:
125  struct Data {
126  Data();
130  Data(const Data&);
131  ~Data();
132 
133  Data& operator=(const Data& other);
134 
136  real_t (*ycollector)(Simulation*) = nullptr;
137  real_t (*xcollector)(Simulation*) = nullptr;
138  std::vector<real_t> x_values;
139  std::vector<real_t> y_values;
140  std::vector<real_t> y_error_low;
141  std::vector<real_t> y_error_high;
143  };
144 
151  static void Load(const std::string& full_filepath, TimeSeries** restored);
152 
185  static void Merge(
186  TimeSeries* merged, const std::vector<TimeSeries>& time_series,
187  const std::function<void(const std::vector<real_t>&, real_t*, real_t*,
188  real_t*)>& merger);
189 
191  static real_t ComputeError(const TimeSeries& ts1, const TimeSeries& ts2);
192 
193  TimeSeries();
194  TimeSeries(const TimeSeries& other);
195  TimeSeries(TimeSeries&& other) noexcept;
196 
197  TimeSeries& operator=(TimeSeries&& other) noexcept;
198  TimeSeries& operator=(const TimeSeries& other);
199 
212  void AddCollector(const std::string& id, real_t (*ycollector)(Simulation*),
213  real_t (*xcollector)(Simulation*) = nullptr);
214 
238  void AddCollector(const std::string& id, Reducer<real_t>* y_reducer_collector,
239  real_t (*xcollector)(Simulation*) = nullptr);
240 
248  void Add(const std::string& id, const std::vector<real_t>& x_values,
249  const std::vector<real_t>& y_values);
250 
251  void Add(const std::string& id, const std::vector<real_t>& x_values,
252  const std::vector<real_t>& y_values,
253  const std::vector<real_t>& y_error);
254 
255  void Add(const std::string& id, const std::vector<real_t>& x_values,
256  const std::vector<real_t>& y_values,
257  const std::vector<real_t>& y_error_low,
258  const std::vector<real_t>& y_error_high);
259 
265  void Add(const TimeSeries& ts, const std::string& suffix);
266 
272  void AddTransformedData(const std::string& old_id,
273  const std::string& transformed_id,
274  const DataTransformer& transformer);
275 
277  void Update();
278 
280  bool Contains(const std::string& id) const;
281  uint64_t Size() const;
282  const std::vector<real_t>& GetXValues(const std::string& id) const;
283  const std::vector<real_t>& GetYValues(const std::string& id) const;
284  const std::vector<real_t>& GetYErrorLow(const std::string& id) const;
285  const std::vector<real_t>& GetYErrorHigh(const std::string& id) const;
286 
288  void ListEntries() const;
289 
291  void Save(const std::string& full_filepath) const;
292 
294  void SaveJson(const std::string& full_filepath) const;
295 
296  private:
297  std::unordered_map<std::string, Data> data_;
298 
300 };
301 
302 // The following custom streamer should be visible to rootcling for dictionary
303 // generation, but not to the interpreter!
304 #if (!defined(__CLING__) || defined(__ROOTCLING__)) && defined(USE_DICT)
305 
306 // The custom streamer is needed because ROOT can't stream function pointers
307 // by default.
308 inline void TimeSeries::Data::Streamer(TBuffer& R__b) {
309  if (R__b.IsReading()) {
310  R__b.ReadClassBuffer(TimeSeries::Data::Class(), this);
311  Long64_t l;
312  R__b.ReadLong64(l);
313  this->ycollector = reinterpret_cast<real_t (*)(Simulation*)>(l);
314  R__b.ReadLong64(l);
315  this->xcollector = reinterpret_cast<real_t (*)(Simulation*)>(l);
316  } else {
317  R__b.WriteClassBuffer(TimeSeries::Data::Class(), this);
318  Long64_t l = reinterpret_cast<Long64_t>(this->ycollector);
319  R__b.WriteLong64(l);
320  l = reinterpret_cast<Long64_t>(this->xcollector);
321  R__b.WriteLong64(l);
322  }
323 }
324 
325 #endif // !defined(__CLING__) || defined(__ROOTCLING__)
326 
327 } // namespace experimental
328 } // namespace bdm
329 
330 #endif // CORE_ANALYSIS_TIME_SERIES_H_
bdm::experimental::DataTransformer::BDM_CLASS_DEF
BDM_CLASS_DEF(DataTransformer, 1)
bdm::experimental::LinearTransformer::SetYIntercept
void SetYIntercept(real_t intercept)
Definition: time_series.cc:76
bdm::experimental::TimeSeries::ComputeError
static real_t ComputeError(const TimeSeries &ts1, const TimeSeries &ts2)
Computes the mean squared error between ts1 and ts2
Definition: time_series.cc:143
bdm::experimental::TimeSeries::operator=
TimeSeries & operator=(TimeSeries &&other) noexcept
Definition: time_series.cc:299
bdm::experimental::LinearTransformer::SetYErrorLowIntercept
void SetYErrorLowIntercept(real_t intercept)
Definition: time_series.cc:82
bdm::experimental::TimeSeries::GetYErrorHigh
const std::vector< real_t > & GetYErrorHigh(const std::string &id) const
Definition: time_series.cc:522
bdm::experimental::DataTransformer::TransformXValues
virtual void TransformXValues(const std::vector< real_t > &old_x_values, std::vector< real_t > &new_x_values) const =0
bdm::experimental::DataTransformer::~DataTransformer
virtual ~DataTransformer()=default
bdm::experimental::LinearTransformer::SetYErrorHighIntercept
void SetYErrorHighIntercept(real_t intercept)
Definition: time_series.cc:88
bdm
Definition: agent.cc:39
bdm::experimental::TimeSeries::TimeSeries
TimeSeries()
bdm::experimental::TimeSeries::Data::y_error_high
std::vector< real_t > y_error_high
Definition: time_series.h:141
reduce.h
bdm::experimental::LinearTransformer::SetXIntercept
void SetXIntercept(real_t intercept)
Definition: time_series.cc:72
bdm::experimental::TimeSeries::Data
Definition: time_series.h:125
bdm::real_t
double real_t
Definition: real_t.h:21
bdm::experimental::LinearTransformer::SetYSlope
void SetYSlope(real_t slope)
Definition: time_series.cc:75
bdm::experimental::TimeSeries::Data::xcollector
real_t(* xcollector)(Simulation *)
Definition: time_series.h:137
bdm::experimental::DataTransformer::TransformYErrorHigh
virtual void TransformYErrorHigh(const std::vector< real_t > &old_y_error_high, std::vector< real_t > &new_y_error_high) const =0
bdm::experimental::TimeSeries::Save
void Save(const std::string &full_filepath) const
Saves a root file to disk.
Definition: time_series.cc:535
bdm::experimental::LinearTransformer::x_intercept_
real_t x_intercept_
Definition: time_series.h:109
bdm::experimental::LinearTransformer::BDM_CLASS_DEF_OVERRIDE
BDM_CLASS_DEF_OVERRIDE(LinearTransformer, 1)
bdm::experimental::DataTransformer::TransformYValues
virtual void TransformYValues(const std::vector< real_t > &old_y_values, std::vector< real_t > &new_y_values) const =0
bdm::experimental::TimeSeries::GetYErrorLow
const std::vector< real_t > & GetYErrorLow(const std::string &id) const
Definition: time_series.cc:516
bdm::experimental::TimeSeries::Update
void Update()
Adds a new data point to all time series with a collector.
Definition: time_series.cc:368
bdm::experimental::TimeSeries::AddTransformedData
void AddTransformedData(const std::string &old_id, const std::string &transformed_id, const DataTransformer &transformer)
Definition: time_series.cc:335
bdm::experimental::LinearTransformer::y_error_high_intercept_
real_t y_error_high_intercept_
Definition: time_series.h:115
bdm::experimental::LinearTransformer::SetXSlope
void SetXSlope(real_t slope)
Definition: time_series.cc:71
bdm::experimental::LinearTransformer::y_slope_
real_t y_slope_
Definition: time_series.h:110
bdm::experimental::TimeSeries::Data::BDM_CLASS_DEF_NV
BDM_CLASS_DEF_NV(Data, 1)
bdm::experimental::LinearTransformer::TransformYErrorLow
void TransformYErrorLow(const std::vector< real_t > &old_y_error_low, std::vector< real_t > &new_y_error_low) const final
Transforms the y-error-low according to the linear transformation.
Definition: time_series.cc:48
bdm::experimental::LinearTransformer::TransformYValues
void TransformYValues(const std::vector< real_t > &old_y_values, std::vector< real_t > &new_y_values) const final
Transforms the y-values according to the linear transformation.
Definition: time_series.cc:38
bdm::experimental::TimeSeries::Data::y_values
std::vector< real_t > y_values
Definition: time_series.h:139
bdm::experimental::TimeSeries::data_
std::unordered_map< std::string, Data > data_
Definition: time_series.h:297
bdm::experimental::Reducer< real_t >
bdm::experimental::LinearTransformer::y_error_low_intercept_
real_t y_error_low_intercept_
Definition: time_series.h:113
bdm::experimental::TimeSeries::ListEntries
void ListEntries() const
Print all time series entry names to stdout.
Definition: time_series.cc:528
bdm::experimental::TimeSeries::Data::operator=
Data & operator=(const Data &other)
Definition: time_series.cc:119
bdm::experimental::LinearTransformer::TransformXValues
void TransformXValues(const std::vector< real_t > &old_x_values, std::vector< real_t > &new_x_values) const final
Transforms the x-values according to the linear transformation.
Definition: time_series.cc:28
root.h
bdm::experimental::LinearTransformer::TransformYErrorHigh
void TransformYErrorHigh(const std::vector< real_t > &old_y_error_high, std::vector< real_t > &new_y_error_high) const final
Transforms the y-error-high according to the linear transformation.
Definition: time_series.cc:59
bdm::experimental::TimeSeries::Data::x_values
std::vector< real_t > x_values
Definition: time_series.h:138
bdm::experimental::TimeSeries::Data::~Data
~Data()
Definition: time_series.cc:136
bdm::experimental::TimeSeries::GetYValues
const std::vector< real_t > & GetYValues(const std::string &id) const
Definition: time_series.cc:511
bdm::experimental::LinearTransformer::y_error_low_slope_
real_t y_error_low_slope_
Definition: time_series.h:112
bdm::experimental::TimeSeries::Data::Data
Data()
bdm::experimental::LinearTransformer::y_intercept_
real_t y_intercept_
Definition: time_series.h:111
bdm::experimental::LinearTransformer::y_error_high_slope_
real_t y_error_high_slope_
Definition: time_series.h:114
bdm::experimental::TimeSeries
Definition: time_series.h:123
bdm::experimental::LinearTransformer::SetYErrorHighSlope
void SetYErrorHighSlope(real_t slope)
Definition: time_series.cc:85
bdm::experimental::TimeSeries::GetXValues
const std::vector< real_t > & GetXValues(const std::string &id) const
Definition: time_series.cc:506
bdm::experimental::TimeSeries::Data::y_reducer_collector
Reducer< real_t > * y_reducer_collector
Definition: time_series.h:135
bdm::experimental::LinearTransformer::x_slope_
real_t x_slope_
Definition: time_series.h:108
bdm::experimental::TimeSeries::Merge
static void Merge(TimeSeries *merged, const std::vector< TimeSeries > &time_series, const std::function< void(const std::vector< real_t > &, real_t *, real_t *, real_t *)> &merger)
Definition: time_series.cc:198
bdm::experimental::TimeSeries::Add
void Add(const std::string &id, const std::vector< real_t > &x_values, const std::vector< real_t > &y_values)
Definition: time_series.cc:444
real_t.h
bdm::experimental::LinearTransformer::SetYErrorLowSlope
void SetYErrorLowSlope(real_t slope)
Definition: time_series.cc:79
bdm::experimental::TimeSeries::BDM_CLASS_DEF_NV
BDM_CLASS_DEF_NV(TimeSeries, 1)
bdm::experimental::TimeSeries::AddCollector
void AddCollector(const std::string &id, real_t(*ycollector)(Simulation *), real_t(*xcollector)(Simulation *)=nullptr)
Definition: time_series.cc:311
bdm::experimental::TimeSeries::Data::y_error_low
std::vector< real_t > y_error_low
Definition: time_series.h:140
bdm::experimental::DataTransformer::TransformYErrorLow
virtual void TransformYErrorLow(const std::vector< real_t > &old_y_error_low, std::vector< real_t > &new_y_error_low) const =0
bdm::experimental::DataTransformer
Definition: time_series.h:39
bdm::experimental::TimeSeries::Contains
bool Contains(const std::string &id) const
Returns whether a times series with given id exists in this object.
Definition: time_series.cc:498
bdm::experimental::LinearTransformer
Definition: time_series.h:70
bdm::experimental::TimeSeries::SaveJson
void SaveJson(const std::string &full_filepath) const
Saves a json representation to disk.
Definition: time_series.cc:540
bdm::Simulation
Definition: simulation.h:50
bdm::experimental::TimeSeries::Size
uint64_t Size() const
Definition: time_series.cc:503
bdm::experimental::TimeSeries::Load
static void Load(const std::string &full_filepath, TimeSeries **restored)
Definition: time_series.cc:193
bdm::experimental::TimeSeries::Data::ycollector
real_t(* ycollector)(Simulation *)
Definition: time_series.h:136