ParameterStorage.h
Go to the documentation of this file.
1 #ifndef _utl_ParameterStorage_h_
2 #define _utl_ParameterStorage_h_
3 
4 #include <map>
5 #include <string>
6 #include <vector>
7 #include <utl/AugerException.h>
8 #include <utility>
9 #include <sstream>
10 
11 
12 namespace utl {
13 
29  template<typename IndexEnum, typename ParameterType = double, typename ParameterCovarianceType = double>
31 
32  public:
33  typedef std::map<IndexEnum, std::pair<ParameterType, bool>> ParameterMap;
34  typedef std::map<std::pair<IndexEnum, IndexEnum>, std::pair<ParameterCovarianceType, bool>> ParameterCovarianceMap;
35  typedef std::vector<IndexEnum> EnumVector;
36 
38  ParameterStorage(const std::string& storagename = "Unnamed") : fParameterStorageName(storagename) { }
39 
41  bool HasParameter(const IndexEnum param) const
42  { return fParameterMap.find(param) != fParameterMap.end(); }
43 
45  const ParameterType&
46  GetParameter(const IndexEnum param, const std::string& origin = "unknown")
47  const
48  {
49  const auto it = fParameterMap.find(param);
50  if (it != fParameterMap.end())
51  return it->second.first;
52  else {
53  std::ostringstream err;
54  err << "Tried to get unset parameter (" << param << ") from ParameterStorage "
55  << fParameterStorageName << " with call from \"" << origin << "\" origin.";
56  throw NonExistentComponentException(err.str().c_str());
57  }
58  }
59 
61  bool
62  GetParameterLockStatus(const IndexEnum param, const std::string& origin = "unknown")
63  const
64  {
65  const auto it = fParameterMap.find(param);
66  if (it != fParameterMap.end())
67  return it->second.second;
68  else {
69  std::ostringstream err;
70  err << "Tried to get locking status of unset parameter (" << param << ") from ParameterStorage "
71  << fParameterStorageName << " with call from \"" << origin << "\" origin.";
72  throw NonExistentComponentException(err.str().c_str());
73  }
74  }
75 
77  void
78  SetParameter(const IndexEnum param, const ParameterType value, const bool lock, const std::string& origin = "unknown")
79  {
80  const auto it = fParameterMap.find(param);
81  if (it != fParameterMap.end()) {
82  if (it->second.second) { // parameter is already set, is it locked?
83  std::ostringstream err;
84  err << "Tried to re-set locked parameter (" << param << ") in ParameterStorage "
85  << fParameterStorageName << " with call from \"" << origin << "\" origin.";
86  throw InvalidConfigurationException(err.str().c_str());
87  }
88  }
89  fParameterMap[param] = std::make_pair(value, lock);
90  }
91 
93  void
94  DeleteParameter(const IndexEnum param, const std::string& origin = "unknown")
95  {
96  for (const auto& pv : fParameterMap)
97  DeleteParameterCovariance(param, pv.first);
98  const auto it = fParameterMap.find(param);
99  if (it != fParameterMap.end()) {
100  if (it->second.second) {
101  std::ostringstream err;
102  err << "Tried to delete locked parameter (" << param << ") in ParameterStorage "
103  << fParameterStorageName << " with call from \"" << origin << "\" origin.";
104  throw InvalidConfigurationException(err.str().c_str());
105  }
106  fParameterMap.erase(it);
107  }
108  }
109 
111  bool
112  HasParameterCovariance(const IndexEnum param1, const IndexEnum param2)
113  const
114  {
115  const auto covarianceKey = GenCovarianceKey(param1, param2);
116  return fParameterCovarianceMap.find(covarianceKey) != fParameterCovarianceMap.end();
117  }
118 
120  const ParameterCovarianceType&
121  GetParameterCovariance(const IndexEnum param1, const IndexEnum param2, const std::string& origin = "unknown")
122  const
123  {
124  const auto covarianceKey = GenCovarianceKey(param1, param2);
125  const auto it = fParameterCovarianceMap.find(covarianceKey);
126  if (it != fParameterCovarianceMap.end())
127  return it->second.first;
128  else {
129  std::ostringstream err;
130  err << "Tried to get unset parameter covariance (" << covarianceKey.first << ", "
131  << covarianceKey.second << ") from ParameterStorage " << fParameterStorageName
132  << " with call from \"" << origin << "\" origin.";
133  throw NonExistentComponentException(err.str().c_str());
134  }
135  }
136 
138  bool
139  GetParameterCovarianceLockStatus(const IndexEnum param1, const IndexEnum param2, const std::string& origin = "unknown")
140  const
141  {
142  const auto covarianceKey = GenCovarianceKey(param1, param2);
143  const auto it = fParameterCovarianceMap.find(covarianceKey);
144  if (it != fParameterCovarianceMap.end())
145  return it->second.second;
146  else {
147  std::ostringstream err;
148  err << "Tried to get locking status of unset parameter covariance (" << covarianceKey.first << ", "
149  << covarianceKey.second << ") from ParameterStorage " << fParameterStorageName
150  << " with call from \"" << origin << "\" origin.";
151  throw NonExistentComponentException(err.str().c_str());
152  }
153  }
154 
156  void
157  SetParameterCovariance(const IndexEnum param1, const IndexEnum param2, const ParameterCovarianceType value, const bool lock, const std::string& origin = "unknown")
158  {
159  const auto covarianceKey = GenCovarianceKey(param1, param2);
160 
161  // check if both underlying parameters are actually set, throw exception if not
162  if (!HasParameter(param1) || !HasParameter(param2)) {
163  std::ostringstream err;
164  err << "Tried to set parameter covariance (" << covarianceKey.first << ", "
165  << covarianceKey.second << ") in ParameterStorage " << fParameterStorageName
166  << " without beforehand setting parameter(s) ";
167  if (!HasParameter(param1))
168  err << '(' << param1 << ") ";
169  if (!HasParameter(param2))
170  err << '(' << param2 << ") ";
171  err << "with call from \"" << origin << "\" origin.";
172  throw InvalidConfigurationException(err.str().c_str());
173  }
174 
175  const auto it = fParameterCovarianceMap.find(covarianceKey);
176  if (it != fParameterCovarianceMap.end()) {
177  if (it->second.second) { // parameter covariance is already set, is it locked?
178  std::ostringstream err;
179  err << "Tried to re-set locked parameter covariance (" << covarianceKey.first << ", "
180  << covarianceKey.second << ") in ParameterStorage " << fParameterStorageName
181  << " with call from \"" << origin << "\" origin.";
182  throw InvalidConfigurationException(err.str().c_str());
183  }
184  }
185  fParameterCovarianceMap[covarianceKey] = std::make_pair(value, lock);
186  }
187 
189  void
190  DeleteParameterCovariance(const IndexEnum param1, const IndexEnum param2, const std::string& origin = "unknown")
191  {
192  const auto covarianceKey = GenCovarianceKey(param1, param2);
193  if (HasParameterCovariance(param1, param2)) {
194  if (fParameterCovarianceMap[covarianceKey].second) {
195  std::ostringstream err;
196  err << "Tried to delete locked parameter covariance (" << covarianceKey.first << ", "
197  << covarianceKey.second << ") in ParameterStorage " << fParameterStorageName
198  << " with call from \"" << origin << "\" origin.";
199  throw InvalidConfigurationException(err.str().c_str());
200  }
201  fParameterCovarianceMap.erase(covarianceKey);
202  }
203  }
204 
206  std::vector<IndexEnum>
207  GetEnumVector()
208  const
209  {
210  std::vector<IndexEnum> enumVector;
211  enumVector.reserve(fParameterMap.size());
212  for (const auto& pv : fParameterMap)
213  enumVector.push_back(pv.first);
214  return enumVector;
215  }
216 
218  std::vector<std::pair<IndexEnum, IndexEnum>>
219  GetCovarianceEnumVector()
220  const
221  {
222  std::vector<std::pair<IndexEnum, IndexEnum>> enumVector;
223  enumVector.reserve(fParameterCovarianceMap.size());
224  for (const auto& pc : fParameterCovarianceMap)
225  enumVector.push_back(pc.first);
226  return enumVector;
227  }
228 
229  private:
231  std::pair<IndexEnum, IndexEnum> GenCovarianceKey(const IndexEnum param1, const IndexEnum param2) const
232  { return (long(param1) < long(param2)) ? std::make_pair(param1, param2) : std::make_pair(param2, param1); }
233 
236 
239 
241 
242  };
243 
244 }
245 
246 
247 #endif
ParameterMap fParameterMap
the map containing the stored parameters of type ParameterType
std::pair< IndexEnum, IndexEnum > GenCovarianceKey(const IndexEnum param1, const IndexEnum param2) const
helper function to create the key for the covariance map from the parameters using sorting ...
bool HasParameter(const IndexEnum param) const
determine whether a parameter has been set
ParameterStorage(const std::string &storagename="Unnamed")
Constructor that sets a name for the storage, helpful to get useful exception messages.
Base class for exceptions trying to access non-existing components.
ParameterCovarianceMap fParameterCovarianceMap
the map containing the stored parameter covariances of type ParameterCovarianceType ...
std::map< IndexEnum, std::pair< ParameterType, bool > > ParameterMap
const double second
Definition: GalacticUnits.h:32
std::vector< IndexEnum > EnumVector
std::string fParameterStorageName
std::map< std::pair< IndexEnum, IndexEnum >, std::pair< ParameterCovarianceType, bool > > ParameterCovarianceMap
Template class allowing to store parameters and their covariances using enums to address them with a ...

, generated on Tue Sep 26 2023.