CEPS  24.01
Cardiac ElectroPhysiology Simulator
InputParameters.cpp
Go to the documentation of this file.
1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2  This file is part of CEPS.
3 
4  CEPS is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  CEPS is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with CEPS (see file LICENSE at root of project).
16  If not, see <https://www.gnu.org/licenses/>.
17 
18 
19  Copyright 2019-2024 Inria, Universite de Bordeaux
20 
21  Authors, in alphabetical order:
22 
23  Pierre-Elliott BECUE, Florian CARO, Yves COUDIERE(*), Andjela DAVIDOVIC,
24  Charlie DOUANLA-LONTSI, Marc FUENTES, Mehdi JUHOOR, Michael LEGUEBE(*),
25  Pauline MIGERDITICHAN, Valentin PANNETIER(*), Nejib ZEMZEMI.
26  * : currently active authors
27 
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
33 
34 //#include <algorithm>
35 
40 
41 
43  m_keyVals ({}),
44  m_warnDefault(true)
45 {}
46 
48  FileReader (s),
49  m_keyVals ({}),
50  m_warnDefault(warnDefault)
51 {
52  read();
53 }
54 
56 {
57 }
58 
59 void
61 {
62  m_keyVals.clear();
63 
64  CEPS_SAYS("Reading parameters from file " << m_fileName);
65  CEPS_ABORT_IF(not open(),
66  "InputParameters: unable to open file " << m_fileName
67  );
68 
70  CepsString l, key, value, KEY;
71  CepsBool readNewEntry(true);
72  while (getline(m_file, l))
73  lines.push_back(l);
74 
75  for (CepsUInt iLine=0U; iLine<lines.size(); iLine++)
76  {
77  CepsString line(lines.at(iLine));
78  if (readNewEntry)
79  {
80  if (line.find(':')!=line.npos and not line.starts_with('#'))
81  {
82  std::stringstream ss(line);
83  getline(ss,key, ':');
84  getline(ss,value);
85  // Remove spaces from keys and make it upper for storage
86  KEY = ceps::toKey(key);
87  value = ceps::trim (value);
88 
89  CEPS_WARNS_IF(KEY.empty(),
90  "In parameters file, key is empty at line " << iLine
91  );
92  CEPS_WARNS_IF(value.empty(),
93  "Empty value at line " << iLine << " of parameter file"
94  );
95 
96  // Several lines spanning entries
97  if (value.ends_with('\\'))
98  {
99  readNewEntry = false;
100  value.pop_back();
101  value = ceps::trim(value) + ' ';
102  }
103 
104  // Append if multiple lines have the same key
105  if (hasKey(keyType(KEY)))
106  m_keyVals.at(KEY) += CepsString(",") + value;
107  else
108  m_keyVals.insert(std::make_pair(keyType(KEY),value));
109  }
110  }
111  else
112  {
113  value = ceps::trim(line);
114  if (value.ends_with('\\'))
115  {
116  readNewEntry = false;
117  value.pop_back();
118  value = ceps::trim(value) + " ";
119  }
120  else
121  {
122  readNewEntry = true;
123  }
124  if (value.empty())
125  m_keyVals.at(keyType(KEY)).pop_back(); // remove added space
126  else
127  m_keyVals.at(keyType(KEY)) += value;
128  }
129  }
130 
131 }
132 
133 // -----------------------------------------------------------------=
134 // Get values from parameters
135 
136 CepsReal
138 {
139  keyType KEY = ceps::toKey(key);
140  errIfNoKey(key,"real value");
141  return ceps::toReal(m_keyVals.at(KEY));
142 }
143 
144 CepsReal
145 InputParameters::getReal(const keyType& key, const CepsReal& defVal) const
146 {
147  keyType KEY = ceps::toKey(key);
148  if (not m_keyVals.contains(KEY))
149  {
150  warnNoKey(key,std::to_string(defVal),"real value");
151  return defVal;
152  }
153  return ceps::toReal(m_keyVals.at(KEY));
154 }
155 
158 {
159  keyType KEY = ceps::toKey(key);
160  errIfNoKey(key,"vertex value");
161  return ceps::toMathVertex(ceps::split(m_keyVals.at(KEY)));
162 }
163 
165 InputParameters::getMathVertex(const keyType& key, const CepsMathVertex& defVal) const
166 {
167  keyType KEY = ceps::toKey(key);
168  if (not m_keyVals.contains(KEY))
169  {
170  std::stringstream def;
171  def << CepsString("(") << defVal(0) << "," << defVal(1) << "," << defVal(2) << ")";
172  warnNoKey(key,def.str(),"vertex value");
173  return defVal;
174  }
175  return ceps::toMathVertex(ceps::split(m_keyVals.at(KEY)));
176 }
177 
178 CepsInt
180 {
181  keyType KEY = ceps::toKey(key);
182  errIfNoKey(key,"integer value");
183  return ceps::toInt(m_keyVals.at(KEY));
184 }
185 
186 CepsInt
187 InputParameters::getInt(const keyType& key, const CepsInt& defVal) const
188 {
189  keyType KEY = ceps::toKey(key);
190  if (not m_keyVals.contains(KEY))
191  {
192  warnNoKey(key,std::to_string(defVal),"integer value");
193  return defVal;
194  }
195  return ceps::toInt(m_keyVals.at(KEY));
196 }
197 
200 {
201  keyType KEY = ceps::toKey(key);
202  errIfNoKey(key,"string value");
203  return m_keyVals.at(KEY);
204 }
205 
207 InputParameters::getString(const keyType& key, const CepsString& defVal) const
208 {
209  keyType KEY = ceps::toKey(key);
210  if (not m_keyVals.contains(KEY))
211  {
212  warnNoKey(key,defVal,"string value");
213  return defVal;
214  }
215  return m_keyVals.at(KEY);
216 }
217 
220 {
221  return this->getString(key);
222 }
223 
225 InputParameters::operator()(const keyType& key, const keyType& defVal) const
226 {
227  return this->getString(key, defVal);
228 }
229 
230 CepsBool
232 {
233  return m_keyVals.contains(ceps::toKey(key));
234 }
235 
236 CepsBool
238 {
239  keyType val = ceps::toKey(getString(key,"NO"));
240  CepsVector<CepsString> oks {"YES","TRUE","1","ON","OK"};
241  return ceps::contains(oks,val);
242 }
243 
244 CepsBool
246 {
247  keyType val = ceps::toKey(getString(key,"YES"));
248  CepsVector<CepsString> oks {"NO","FALSE","0","OFF"};
249  return ceps::contains(oks,val);
250 }
251 
252 void
253 InputParameters::errIfNoKey(const keyType& key, const CepsString& type) const
254 {
255  keyType KEY = ceps::toKey(key);
256  CEPS_ABORT_IF(not m_keyVals.contains(KEY),
257  "No " << type <<" with key \"" << key << "\" found in parameters file"
258  );
259 }
260 
261 void
262 InputParameters::warnNoKey(const keyType& key, const CepsString& defValue, const CepsString& type) const
263 {
264  CEPS_WARNS(
265  "No " << type << " with key \"" << key << "\" found in parameters file. Will use default: " << defValue
266  );
267 }
268 
269 void
271 (const keyType& key, const CepsString& value)
272 {
273  m_keyVals[ceps::toKey(key)] = ceps::trim(value);
274 }
275 
276 void
278 (const keyType& key, const CepsReal& value)
279 {
280  this->set(key,ceps::toString(value));
281 }
282 
283 void
285 (const keyType& key, const CepsInt& value)
286 {
287  this->set(key,ceps::toString(value));
288 }
289 
290 void
292 {
293  m_keyVals.erase(ceps::toKey(key));
294 }
295 
#define CEPS_SAYS(message)
Writes a message in the debug log and in the terminal (stdio).
#define CEPS_WARNS_IF(condition, message)
If condition is true, writes a warning in the debug log and in the terminal (stderr).
#define CEPS_ABORT_IF(condition, message)
Stops the execution with a message if condition is true. If testing is enabled, only throws a runtime...
#define CEPS_WARNS(message)
Writes a warning in the debug log and in the terminal (stderr).
std::basic_string< CepsChar > CepsString
C++ format string.
Definition: CepsTypes.hpp:128
std::vector< _Type, _Alloc > CepsVector
C++ vector.
Definition: CepsTypes.hpp:155
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
Definition: CepsTypes.hpp:109
float CepsReal
Need single precision floating point.
Definition: CepsTypes.hpp:100
Eigen::Matrix< CepsScalar, 3, 1 > CepsMathVertex
Vertex, eigen format.
Definition: CepsTypes.hpp:135
int32_t CepsInt
Need 32 bit integer.
Definition: CepsTypes.hpp:106
Base class that regroups common reader functionalities.
Definition: FileReader.hpp:43
virtual CepsBool open()
Opens the designated file in read mode.
Definition: FileReader.cpp:70
CepsString m_fileName
file to open
Definition: FileReader.hpp:145
std::ifstream m_file
stream
Definition: FileReader.hpp:146
void set(const keyType &key, const CepsString &value)
Adds or modifies an entry in the configuration.
InputParameters()
Empty constructor.
CepsBool isInactiveOption(const keyType &key) const
Tells if key exists in configuration and is of "0","NO","OFF" or "FALSE".
void read()
Reads all available parameters in specified file.
CepsString operator()(const keyType &key) const
Reads a CepsString from configuration.
CepsMap< keyType, CepsString > m_keyVals
The parameters stored as std::strings.
CepsMathVertex getMathVertex(const keyType &key) const
Reads 3 floating point values from configuration.
void errIfNoKey(const keyType &key, const CepsString &type) const
Prints an error message if file misses a key.
void erase(const keyType &key)
Removes an entry in the configuration.
CepsInt getInt(const keyType &key) const
Reads an integer value from configuration.
void warnNoKey(const keyType &key, const CepsString &defValue, const CepsString &type) const
Prints an warning message if file misses a key and use the default value.
CepsBool hasKey(const keyType &key) const
Tells if key exists in input file.
~InputParameters() override
Destructor.
CepsBool isActiveOption(const keyType &key) const
Tells if key exists in configuration and is of "1","YES","ON" or "TRUE".
CepsString getString(const keyType &key) const
Reads a CepsString from configuration.
CepsReal getReal(const keyType &key) const
Reads a floating point value from configuration.
CepsBool contains(const CepsVector< _Type, _Alloc > &vec, const _Type &item)
Tells if vectors contains a given item.
Definition: CepsVector.hpp:56
CepsString toString(_Tp value)
Convert a given value to a string (input has to be compatible with std::to_string)
Definition: CepsString.hpp:409
CepsString toKey(const CepsString &s)
Transform to key type a std::string, upper case and no spaces.
Definition: CepsString.cpp:157
CepsVector< CepsString > split(const CepsString &s, const CepsString &delimiters=CepsString(" \t"))
Splits a string using mulitple delimiters in a single string.
Definition: CepsString.cpp:38
CepsInt toInt(const CepsString &s)
Cast CepsString to CepsInt.
Definition: CepsString.cpp:270
CepsMathVertex toMathVertex(const CepsVector< CepsString > &vec)
Cast a CepsVector of CepsString to CepsMathVertex.
Definition: CepsString.cpp:227
CepsString trim(const CepsString &s)
Removes trailing and preceeding spaces.
Definition: CepsString.cpp:78
CepsReal toReal(const CepsString &s)
Cast CepsString to CepsReal.
Definition: CepsString.cpp:193