gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool

Inifile parser base class. More...

#include <yaml_parser_base.hpp>

Inheritance diagram for Gambit::IniParser::Parser:

Public Member Functions

virtual void readFile (str filename)
 Read in the YAML file. More...
 
YAML::Node getParametersNode () const
 Getters for key/value section. More...
 
YAML::Node getPriorsNode () const
 
YAML::Node getPrinterNode () const
 
YAML::Node getScannerNode () const
 
YAML::Node getLoggerNode () const
 
YAML::Node getKeyValuePairNode () const
 
template<typename... args>
bool hasKey (args... keys) const
 
template<typename TYPE , typename... args>
TYPE getValue (args... keys) const
 
template<typename TYPE , typename... args>
TYPE getValueOrDef (TYPE def, const args &... keys) const
 
template<typename TYPE >
TYPE getModelParameterEntry (str model, str param, str key) const
 Getters for model/parameter section. More...
 
bool hasModelParameterEntry (str model, str param, str key) const
 Getters for model/parameter section. More...
 
const std::set< strgetModelNames () const
 Return list of model names (without "adhoc" model!) More...
 
const std::vector< strgetModelParameters (str model) const
 
const Options getOptions (str key) const
 Getter for options. More...
 

Protected Member Functions

YAML::Node filename_to_node (str)
 Read in the actual YAML file. More...
 
void basicParse (YAML::Node, str)
 Do the basic parsing of the YAML file. More...
 

Private Attributes

YAML::Node keyValuePairNode
 
YAML::Node parametersNode
 
YAML::Node priorsNode
 
YAML::Node printerNode
 
YAML::Node scannerNode
 
YAML::Node logNode
 

Detailed Description

Inifile parser base class.

Definition at line 48 of file yaml_parser_base.hpp.

Member Function Documentation

◆ basicParse()

void Gambit::IniParser::Parser::basicParse ( YAML::Node  root,
str  filename 
)
protected

Do the basic parsing of the YAML file.

Definition at line 154 of file yaml_parser_base.cpp.

References Gambit::Utils::ensure_path_exists(), hasKey(), Gambit::IniParser::inifile_error(), Gambit::Logging::LogMaster::initialise(), keyValuePairNode, LOCAL_INFO, Gambit::logger(), logNode, parametersNode, printerNode, priorsNode, Gambit::IniParser::recursiveImport(), scannerNode, and Gambit::Logging::LogMaster::set_log_debug_messages().

Referenced by getModelParameterEntry(), readFile(), and Gambit::IniParser::IniFile::readFile().

155  {
156  recursiveImport(root,filename);
157  parametersNode = root["Parameters"];
158  priorsNode = root["Priors"];
159  printerNode = root["Printer"];
160  scannerNode = root["Scanner"];
161  logNode = root["Logger"];
162  keyValuePairNode = root["KeyValues"];
163 
164  // Set default output path
165  std::string defpath;
166  if(hasKey("default_output_path"))
167  {
168  defpath = getValue<std::string>("default_output_path");
169  }
170  else
171  {
172  // Assign a default default (;)) path based on the yaml file name
173  // Ridiculously we have to parse manually in C++ since no
174  // standard library tools for doing this exist...
175  // Assumes that file extension has only one dot, or that
176  // there is no file extension. Should work anyway if more
177  // dots, will just get a directory name with a dot in it.
178  size_t fname_start = filename.find_last_of("/\\");
179  size_t fname_end = filename.find_last_of(".");
180  str fname = filename.substr(fname_start+1,fname_end);
181  defpath = "runs/" + fname + "/";
182  }
183  scannerNode["default_output_path"] = Utils::ensure_path_exists(defpath+"/scanner_plugins/");
184  logNode ["default_output_path"] = Utils::ensure_path_exists(defpath+"/logs/");
185  printerNode["options"]["default_output_path"] = Utils::ensure_path_exists(defpath+"/samples/");
186 
187  // Postprocessor is currently incompatible with 'print_timing_data', so need to pass this option on for checking
188  scannerNode["print_timing_data"] = getValueOrDef<bool>(false,"print_timing_data");
189 
190  // Pass on minimum recognised lnlike and offset to Scanner
191  scannerNode["model_invalid_for_lnlike_below"] = getValueOrDef<double>(0.9*std::numeric_limits<double>::lowest(), "likelihood", "model_invalid_for_lnlike_below");
192  if (hasKey("likelihood", "lnlike_offset"))
193  scannerNode["lnlike_offset"] = getValue<double>("likelihood", "lnlike_offset");
194 
195  // Set fatality of exceptions
196  if (hasKey("exceptions"))
197  {
198  // Iterate over the map of all recognised exception objects
199  std::map<const char*,exception*>::const_iterator iter;
200  for (iter = exception::all_exceptions().begin(); iter != exception::all_exceptions().end(); ++iter)
201  {
202  // Check if the exception has an entry in the YAML file
203  if (hasKey("exceptions",iter->first))
204  {
205  // Retrieve the entry and set the exception's 'fatal' flag accordingly.
206  str value = getValue<str>("exceptions",iter->first);
207  if (value == "fatal")
208  {
209  iter->second->set_fatal(true);
210  }
211  else if (value == "non-fatal")
212  {
213  iter->second->set_fatal(false);
214  }
215  else
216  {
217  str error_msg = "Unrecognised entry \"" + value + "\" for exceptions key \"" + iter->first + "\" in input file.";
218  inifile_error().raise(LOCAL_INFO,error_msg);
219  }
220  }
221  }
222  }
223 
224  // Parse the logging setup node, and initialise the LogMaster object
225  std::string prefix;
226  if(logNode["prefix"])
227  {
228  prefix = logNode["prefix"].as<std::string>();
229  }
230  else
231  {
232  prefix = logNode["default_output_path"].as<std::string>()+"/";
233  }
234 
235  // map storing info used to set up logger objects
236  std::map<std::set<std::string>,std::string> loggerinfo;
237  if(logNode["redirection"])
238  {
239  YAML::Node redir = logNode["redirection"];
240  for(YAML::const_iterator it=redir.begin(); it!=redir.end(); ++it)
241  {
242  std::set<std::string> tags;
243  std::string filename;
244  // Iterate through tags and add them to the set
245  YAML::Node yamltags = it->first;
246  for(YAML::const_iterator it2=yamltags.begin();it2!=yamltags.end();++it2)
247  {
248  tags.insert( it2->as<std::string>() );
249  }
250  filename = (it->second).as<std::string>();
251 
252  // Add entry to the loggerinfo map
253  if((filename=="stdout") or (filename=="stderr"))
254  {
255  // Special cases to trigger redirection to standard output streams
256  loggerinfo[tags] = filename;
257  }
258  else
259  {
260  // The logger won't be able to create the log files if the prefix
261  // directory doesn't exist, so let us now make sure that it does
262  loggerinfo[tags] = Utils::ensure_path_exists(prefix + filename);
263  }
264  }
265  }
266  else
267  {
268  // Use default log file only
269  std::set<std::string> tags;
270  std::string filename;
271  tags.insert("Default");
272  filename = "default.log";
273  loggerinfo[tags] = Utils::ensure_path_exists(prefix + filename);
274  }
275  // Initialise global LogMaster object
276  bool master_debug = (keyValuePairNode["debug"]) ? keyValuePairNode["debug"].as<bool>() : false;
277  bool logger_debug = (logNode["debug"]) ? logNode["debug"].as<bool>() : false;
278  logger().set_log_debug_messages(master_debug or logger_debug);
279  logger().initialise(loggerinfo);
280 
281  // Parse the Parameters node and expand out some shorthand syntax
282  // e.g.
283  // model1
284  // param1: 5.678
285  // expands to
286  // model1
287  // param1:
288  // fixed_value: 5.678
289  // Parameter must have no entries besides the value for this syntax to be valid
290  }
void recursiveImport(const YAML::Node &node, const std::string &filename)
#define LOCAL_INFO
Definition: local_info.hpp:34
bool hasKey(args... keys) const
void set_log_debug_messages(bool flag)
Choose whether "Debug" tagged log messages will be ignored (i.e. not logged)
Definition: logmaster.hpp:138
EXPORT_SYMBOLS Logging::LogMaster & logger()
Function to retrieve a reference to the Gambit global log object.
Definition: logger.cpp:95
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
EXPORT_SYMBOLS const str & ensure_path_exists(const str &)
Ensure that a path exists (and then return the path, for chaining purposes)
void initialise(std::vector< std::pair< std::set< std::string >, std::string >> &)
Function to construct loggers according to blueprint.
Definition: logmaster.cpp:227
error & inifile_error()
IniFile errors.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ filename_to_node()

YAML::Node Gambit::IniParser::Parser::filename_to_node ( str  filename)
protected

Read in the actual YAML file.

Definition at line 135 of file yaml_parser_base.cpp.

References Gambit::IniParser::inifile_error(), and LOCAL_INFO.

Referenced by getModelParameterEntry(), readFile(), and Gambit::IniParser::IniFile::readFile().

136  {
137  YAML::Node root;
138  // Read inifile file
139  try
140  {
141  root = YAML::LoadFile(filename);
142  }
143  catch (YAML::Exception &e)
144  {
145  std::ostringstream msg;
146  msg << "Error reading Inifile \""<<filename<<"\"! ";
147  msg << "Please check that file exist!" << endl;
148  msg << "(yaml-cpp error: "<<e.what()<<" )";
149  inifile_error().raise(LOCAL_INFO,msg.str());
150  }
151  return root;
152  }
#define LOCAL_INFO
Definition: local_info.hpp:34
error & inifile_error()
IniFile errors.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getKeyValuePairNode()

YAML::Node Gambit::IniParser::Parser::getKeyValuePairNode ( ) const

Definition at line 299 of file yaml_parser_base.cpp.

References keyValuePairNode.

Referenced by Gambit::IniParser::IniFile::readFile().

299 {return keyValuePairNode;}
Here is the caller graph for this function:

◆ getLoggerNode()

YAML::Node Gambit::IniParser::Parser::getLoggerNode ( ) const

Definition at line 298 of file yaml_parser_base.cpp.

References logNode.

Referenced by Gambit::IniParser::IniFile::readFile().

298 {return logNode;}
Here is the caller graph for this function:

◆ getModelNames()

const std::set< str > Gambit::IniParser::Parser::getModelNames ( ) const

Return list of model names (without "adhoc" model!)

Definition at line 310 of file yaml_parser_base.cpp.

References parametersNode.

Referenced by getModelParameterEntry(), Gambit::Likelihood_Container_Factory::Likelihood_Container_Factory(), and main().

311  {
312  std::set<str> result;
313  for (YAML::const_iterator it = parametersNode.begin(); it!=parametersNode.end(); ++it)
314  {
315  if (it->first.as<std::string>() != "adhoc")
316  result.insert( it->first.as<std::string>() );
317  }
318  return result;
319  }
Here is the caller graph for this function:

◆ getModelParameterEntry()

template<typename TYPE >
TYPE Gambit::IniParser::Parser::getModelParameterEntry ( str  model,
str  param,
str  key 
) const
inline

Getters for model/parameter section.

Definition at line 91 of file yaml_parser_base.hpp.

References basicParse(), filename_to_node(), getModelNames(), getModelParameters(), getOptions(), hasModelParameterEntry(), Gambit::IniParser::inifile_error(), LOCAL_INFO, and parametersNode.

92  {
93  if (not parametersNode[model][param][key]) inifile_error().raise(LOCAL_INFO,model + "." + param + "." + key + "not found in inifile.");
94  return parametersNode[model][param][key].as<TYPE>();
95  }
#define LOCAL_INFO
Definition: local_info.hpp:34
error & inifile_error()
IniFile errors.
Here is the call graph for this function:

◆ getModelParameters()

const std::vector< std::string > Gambit::IniParser::Parser::getModelParameters ( str  model) const

Definition at line 321 of file yaml_parser_base.cpp.

References parametersNode.

Referenced by getModelParameterEntry(), and Gambit::Likelihood_Container_Factory::Likelihood_Container_Factory().

322  {
323  std::vector<std::string> result;
324  if (parametersNode[model])
325  {
326  for (YAML::const_iterator it = parametersNode[model].begin(); it!=parametersNode[model].end(); ++it)
327  {
328  result.push_back( it->first.as<std::string>() );
329  }
330  }
331  return result;
332  }
Here is the caller graph for this function:

◆ getOptions()

const Options Gambit::IniParser::Parser::getOptions ( str  key) const

Getter for options.

Definition at line 335 of file yaml_parser_base.cpp.

References hasKey(), and keyValuePairNode.

Referenced by getModelParameterEntry().

336  {
337  if (hasKey(key, "options"))
338  {
339  return Options(keyValuePairNode[key]["options"]);
340  }
341  else
342  {
343  return Options(keyValuePairNode[key]);
344  }
345  }
bool hasKey(args... keys) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getParametersNode()

YAML::Node Gambit::IniParser::Parser::getParametersNode ( ) const

Getters for key/value section.

Definition at line 294 of file yaml_parser_base.cpp.

References parametersNode.

Referenced by main().

294 {return parametersNode;}
Here is the caller graph for this function:

◆ getPrinterNode()

YAML::Node Gambit::IniParser::Parser::getPrinterNode ( ) const

Definition at line 296 of file yaml_parser_base.cpp.

References printerNode.

Referenced by main(), and Gambit::IniParser::IniFile::readFile().

296 {return printerNode;}
Here is the caller graph for this function:

◆ getPriorsNode()

YAML::Node Gambit::IniParser::Parser::getPriorsNode ( ) const

Definition at line 295 of file yaml_parser_base.cpp.

References priorsNode.

Referenced by main().

295 {return priorsNode;}
Here is the caller graph for this function:

◆ getScannerNode()

YAML::Node Gambit::IniParser::Parser::getScannerNode ( ) const

Definition at line 297 of file yaml_parser_base.cpp.

References scannerNode.

Referenced by main(), and Gambit::IniParser::IniFile::readFile().

297 {return scannerNode;}
Here is the caller graph for this function:

◆ getValue()

template<typename TYPE , typename... args>
TYPE Gambit::IniParser::Parser::getValue ( args...  keys) const
inline

Definition at line 71 of file yaml_parser_base.hpp.

References Gambit::getVariadicNode(), Gambit::IniParser::inifile_error(), keyValuePairNode, LOCAL_INFO, and Gambit::stringifyVariadic().

72  {
73  const YAML::Node node = getVariadicNode(keyValuePairNode, keys...);
74  if (not node) inifile_error().raise(LOCAL_INFO,"No inifile entry for [" + stringifyVariadic(keys...) + "]");
75  return NodeUtility::getNode<TYPE>(node);
76  }
#define LOCAL_INFO
Definition: local_info.hpp:34
const std::string stringifyVariadic()
const YAML::Node getVariadicNode(const YAML::Node &node)
error & inifile_error()
IniFile errors.
Here is the call graph for this function:

◆ getValueOrDef()

template<typename TYPE , typename... args>
TYPE Gambit::IniParser::Parser::getValueOrDef ( TYPE  def,
const args &...  keys 
) const
inline

Definition at line 78 of file yaml_parser_base.hpp.

References Gambit::LogTags::def, Gambit::getVariadicNode(), and keyValuePairNode.

Referenced by Gambit::DRes::DependencyResolver::calcObsLike(), Gambit::DRes::DependencyResolver::generateTree(), main(), Gambit::DRes::DependencyResolver::resolveDependency(), Gambit::DRes::DependencyResolver::resolveDependencyFromRules(), and Gambit::DRes::DependencyResolver::solveRequirement().

79  {
80  const YAML::Node node = getVariadicNode(keyValuePairNode, keys...);
81  if (not node)
82  {
83  return def;
84  }
85  return NodeUtility::getNode<TYPE>(node);
86  }
const YAML::Node getVariadicNode(const YAML::Node &node)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hasKey()

template<typename... args>
bool Gambit::IniParser::Parser::hasKey ( args...  keys) const
inline

Definition at line 66 of file yaml_parser_base.hpp.

References Gambit::getVariadicNode(), and keyValuePairNode.

Referenced by basicParse(), and getOptions().

67  {
68  return getVariadicNode(keyValuePairNode, keys...);
69  }
const YAML::Node getVariadicNode(const YAML::Node &node)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hasModelParameterEntry()

bool Gambit::IniParser::Parser::hasModelParameterEntry ( str  model,
str  param,
str  key 
) const

Getters for model/parameter section.

Definition at line 304 of file yaml_parser_base.cpp.

References parametersNode.

Referenced by getModelParameterEntry().

305  {
306  return parametersNode[model][param][key];
307  }
Here is the caller graph for this function:

◆ readFile()

void Gambit::IniParser::Parser::readFile ( str  filename)
virtual

Read in the YAML file.

Reimplemented in Gambit::IniParser::IniFile.

Definition at line 129 of file yaml_parser_base.cpp.

References basicParse(), and filename_to_node().

Referenced by main().

130  {
131  YAML::Node root = filename_to_node(filename);
132  basicParse(root,filename);
133  }
YAML::Node filename_to_node(str)
Read in the actual YAML file.
void basicParse(YAML::Node, str)
Do the basic parsing of the YAML file.
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ keyValuePairNode

YAML::Node Gambit::IniParser::Parser::keyValuePairNode
private

◆ logNode

YAML::Node Gambit::IniParser::Parser::logNode
private

◆ parametersNode

YAML::Node Gambit::IniParser::Parser::parametersNode
private

◆ printerNode

YAML::Node Gambit::IniParser::Parser::printerNode
private

◆ priorsNode

YAML::Node Gambit::IniParser::Parser::priorsNode
private

Definition at line 117 of file yaml_parser_base.hpp.

Referenced by basicParse(), and getPriorsNode().

◆ scannerNode

YAML::Node Gambit::IniParser::Parser::scannerNode
private

Definition at line 119 of file yaml_parser_base.hpp.

Referenced by basicParse(), and getScannerNode().


The documentation for this class was generated from the following files: