ESP  0.1
The Example-based Sensor Predictions (ESP) system tries to bring machine learning to the maker community.
calibrator.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <GRT/GRT.h>
4 #include <set>
5 #include <string>
6 
16  public:
17  enum Result {
21  };
22 
23  CalibrateResult(Result result);
24  CalibrateResult(Result result, string message);
25 
26  Result getResult() const { return result_; }
27  string getResultString() const { return result_strings_[result_]; }
28  string getMessage() const { return result_message_; }
29 
30  private:
31  static const char *result_strings_[3];
32 
33  Result result_;
34  string result_message_;
35 
36  static const string kDefaultSuccessMessage;
37  static const string kDefaultWarningMessage;
38  static const string kDefaultFailureMessage;
39 };
40 
51  public:
52  typedef CalibrateResult (*CalibratorCallback)(const GRT::MatrixDouble&);
53 
64  CalibrateProcess(std::string name, std::string description,
66  : name_(name), description_(description), cb_(cb),
67  is_calibrated_(false) {}
68 
69  CalibrateResult calibrate(GRT::MatrixDouble data) {
70  CalibrateResult result = cb_(data);
71  if (result.getResult() != CalibrateResult::FAILURE) {
72  // Only failure is considered as not calibrated.
73  is_calibrated_ = true;
74  setData(data);
75  }
76  return result;
77  }
78  void clear() { data_.clear(); is_calibrated_ = false; }
79  bool isCalibrated() const { return is_calibrated_; }
80 
81  std::string getName() const { return name_; }
82  std::string getDescription() const { return description_; }
83 
84  void setData(GRT::MatrixDouble data) { data_ = data; }
85  GRT::MatrixDouble getData() { return data_; }
86  private:
87  std::string name_;
88  std::string description_;
90  bool is_calibrated_;
91  GRT::MatrixDouble data_;
92 };
93 
121 class Calibrator {
122  public:
130  using SimpleCalibrateFunc = std::function<double(double)>;
131 
144  using CalibrateFunc = std::function<vector<double>(vector<double>)>;
145 
151  Calibrator() : simple_calibrate_func_(nullptr), calibrate_func_(nullptr) {}
152 
158  : simple_calibrate_func_(f), calibrate_func_(nullptr) {}
164  : simple_calibrate_func_(nullptr), calibrate_func_(f) {}
165 
172  Calibrator& setCalibrateFunction(CalibrateFunc f);
173 
180  Calibrator& setCalibrateFunction(SimpleCalibrateFunc f);
188  Calibrator& addCalibrateProcess(CalibrateProcess cp);
189 
198  Calibrator& addCalibrateProcess(const string& name,
199  const string& description,
204  vector<CalibrateProcess>& getCalibrateProcesses() {
205  return calibrate_processes_;
206  }
207 
216  vector<double> calibrate(vector<double> input);
217 
218 
222  bool isCalibrated();
223 
224  private:
225  void registerCalibrateProcess(const CalibrateProcess& cp);
226 
227  bool isCalibrateProcessRegistered(const CalibrateProcess& cp);
228 
229  SimpleCalibrateFunc simple_calibrate_func_;
230  CalibrateFunc calibrate_func_;
231  vector<CalibrateProcess> calibrate_processes_;
232  std::set<std::string> registered_;
233 };
std::string getName() const
Definition: calibrator.h:81
std::function< double(double)> SimpleCalibrateFunc
Transforms each dimension of incoming samples of live sensor data.
Definition: calibrator.h:130
CalibrateResult(Result result)
Definition: calibrator.cpp:9
Specifies data samples and code used to calibrate incoming sensor data.
Definition: calibrator.h:121
std::string getDescription() const
Definition: calibrator.h:82
Calibrator(SimpleCalibrateFunc f)
Definition: calibrator.h:157
GRT::MatrixDouble getData()
Definition: calibrator.h:85
CalibrateResult indicates if the calibration is successful or not.
Definition: calibrator.h:15
std::function< vector< double >(vector< double >)> CalibrateFunc
Transforms incoming samples of live sensor data.
Definition: calibrator.h:144
Result getResult() const
Definition: calibrator.h:26
CalibrateResult(* CalibratorCallback)(const GRT::MatrixDouble &)
Definition: calibrator.h:52
CalibrateResult calibrate(const MatrixDouble &data)
Definition: user_accelerometer_gestures.cpp:27
void setData(GRT::MatrixDouble data)
Definition: calibrator.h:84
Definition: calibrator.h:20
Definition: calibrator.h:19
string getResultString() const
Definition: calibrator.h:27
Result
Definition: calibrator.h:17
Calibrator(CalibrateFunc f)
Definition: calibrator.h:163
CalibrateProcess(std::string name, std::string description, CalibratorCallback cb)
Definition: calibrator.h:64
bool isCalibrated() const
Definition: calibrator.h:79
string getMessage() const
Definition: calibrator.h:28
CalibrateResult calibrate(GRT::MatrixDouble data)
Definition: calibrator.h:69
A data sample to be collected by the user and callback for processing that sample.
Definition: calibrator.h:50
Definition: calibrator.h:18
vector< CalibrateProcess > & getCalibrateProcesses()
Definition: calibrator.h:204
Calibrator()
Definition: calibrator.h:151
CalibrateProcess::CalibratorCallback CalibratorCallback
Definition: calibrator.cpp:47
void clear()
Definition: calibrator.h:78