Nemo  2.3.56
Simulate forward-in-time genetic evolution in a spatially explicit, individual-based stochastic simulator
ParamsParser Class Referenceabstract

Provides interface to read input parameters from various sources and parses them. More...

#include <paramsparser.h>

+ Inheritance diagram for ParamsParser:
+ Collaboration diagram for ParamsParser:

Public Member Functions

 ParamsParser (const char *name)
 
virtual ~ParamsParser ()
 
void setName (const char *name)
 
map< string, string > get_inputParams ()
 
map< string, vector< string > > & getParsedParameters (const char *stream_name)
 
map< string, vector< string > > & getParsedParameters ()
 
map< string, string > & getParameters (const char *stream_name)
 
map< string, string > & getParameters ()
 
virtual bool read (const char *stream)=0
 Read/parse params & args from a file or a string or an R object. More...
 
void parse ()
 Builds the _parsedParams from the _inputParams. More...
 
string replaceMacro (const string &arg)
 Macros. More...
 
string parseMacroFunctionBlock (const string &in_arg)
 
string callMacro (const string &name, const string &argstr)
 
vector< string > getMacroArgs (const string &args, const int min_arg, const size_t max_arg, const string macro_name, const string syntax, bool lastArgIsSeparatorChar=true)
 
string getMacroSepParamChar (const string &sep_in, const string macro_name)
 
string getMacroParamValue (const string &str_in, const string &par_name, const string &macro_name)
 
string quote (const string &argstr)
 Macro "q" returns a quoted string. More...
 
string concat (const string &argstr)
 Macro "c" returns a character-delimited string of atomic arguments. More...
 
string rep (const string &argstr)
 
string seq (const string &argstr)
 
string tempseq (const string &argstr)
 
string matrix (const string &argstr)
 
string diag_matrix (const string &argstr)
 
string sym_matrix (const string &argstr)
 
string runif (const string &argstr)
 
string rnorm (const string &argstr)
 
string rpoiss (const string &argstr)
 
string rbernoul (const string &argstr)
 
string rgamma (const string &argstr)
 
string rlognorm (const string &argstr)
 
string rexp (const string &argstr)
 

Static Public Member Functions

static void getBlockArgument (istream &IN, char &c, string &arg)
 
static void getArguments (string &arg_str, vector< string > &arg_vect)
 

Protected Member Functions

void reset_inputParams ()
 
void add_inputParam (string &param, const string &arg)
 

Private Attributes

const char * _sname
 Attached file of stream name. More...
 
map< string, string > _inputParams
 The whole, unparsed set of input parameters. More...
 
map< string, vector< string > > _parsedParams
 The parsed set of simulation parameters after sequential parameters have been separated. More...
 
map< string, string(ParamsParser::*)(const string &)> _macroMap
 The macro table, mapping call name to caller. More...
 

Detailed Description

Provides interface to read input parameters from various sources and parses them.

Constructor & Destructor Documentation

◆ ParamsParser()

ParamsParser::ParamsParser ( const char *  name)
57{
58 _sname = name;
59
62 _macroMap["tempseq"] = &ParamsParser::tempseq;
68 _macroMap["rlognorm"] = &ParamsParser::rlognorm;
70 _macroMap["rbernoul"] = &ParamsParser::rbernoul;
74}
string rlognorm(const string &argstr)
Definition: paramsparser.cc:1339
string tempseq(const string &argstr)
Definition: paramsparser.cc:669
string rnorm(const string &argstr)
Definition: paramsparser.cc:1016
string matrix(const string &argstr)
Definition: paramsparser.cc:735
const char * _sname
Attached file of stream name.
Definition: paramsparser.h:106
string sym_matrix(const string &argstr)
Definition: paramsparser.cc:848
string concat(const string &argstr)
Macro "c" returns a character-delimited string of atomic arguments.
Definition: paramsparser.cc:482
map< string, string(ParamsParser::*)(const string &)> _macroMap
The macro table, mapping call name to caller.
Definition: paramsparser.h:115
string rbernoul(const string &argstr)
Definition: paramsparser.cc:1145
string seq(const string &argstr)
Definition: paramsparser.cc:607
string runif(const string &argstr)
Definition: paramsparser.cc:939
string quote(const string &argstr)
Macro "q" returns a quoted string.
Definition: paramsparser.cc:509
string rep(const string &argstr)
Definition: paramsparser.cc:534
string rpoiss(const string &argstr)
Definition: paramsparser.cc:1086
string rgamma(const string &argstr)
Definition: paramsparser.cc:1263
string diag_matrix(const string &argstr)
Definition: paramsparser.cc:781

References _macroMap, _sname, concat(), diag_matrix(), matrix(), quote(), rbernoul(), rep(), rgamma(), rlognorm(), rnorm(), rpoiss(), runif(), seq(), sym_matrix(), and tempseq().

◆ ~ParamsParser()

virtual ParamsParser::~ParamsParser ( )
inlinevirtual
55{}

Member Function Documentation

◆ add_inputParam()

void ParamsParser::add_inputParam ( string &  param,
const string &  arg 
)
inlineprotected
102{_inputParams[param] = arg;}
map< string, string > _inputParams
The whole, unparsed set of input parameters.
Definition: paramsparser.h:109

References _inputParams.

Referenced by StreamParser::read().

+ Here is the caller graph for this function:

◆ callMacro()

string ParamsParser::callMacro ( const string &  name,
const string &  argstr 
)
361{
362 string out;
363#ifdef _DEBUG_
364 message(" ^^^^ calling macro: %s(%s)\n",name.c_str(), argstr.c_str());
365#endif
366
367 auto elmnt = _macroMap.find(name);
368
369 if( elmnt == _macroMap.end())
370 {
371 fatal("macro \"%s(%s)\" could not be mapped to a known function.\n", name.c_str(), argstr.c_str());
372 }
373
374 out = (this->*(elmnt->second))(argstr);
375
376#ifdef _DEBUG_
377 message(" ^^^^ returning: %s\n",out.c_str());
378#endif
379
380 return out;
381}
void fatal(const char *str,...)
Definition: output.cc:96
void message(const char *message,...)
Definition: output.cc:40

References _macroMap, fatal(), and message().

Referenced by parseMacroFunctionBlock().

+ Here is the caller graph for this function:

◆ concat()

string ParamsParser::concat ( const string &  argstr)

Macro "c" returns a character-delimited string of atomic arguments.

Can be passed as arguments to other macros.

483{
484
485 string syntax="c(... , sep=\",\")";
486 string out;
487
488 size_t max_arg = INT64_MAX;
489
490 vector<string> macro_args = getMacroArgs(argstr, 2, max_arg, "c", syntax, true);
491
492 string sep = macro_args.back();
493
494 int last = macro_args.size() - 2; //omit separator at last position
495
496 // stick each element together, un-quoting them, if needed
497 for (int i = 0; i < last; ++i) {
498 out += tstring::removeEnclosingChar(macro_args[i], '"', '"', true) + sep;
499 }
500 // add last element without separator
501 out += tstring::removeEnclosingChar(macro_args[last], '"', '"', true);
502
503
504 return out;
505}
vector< string > getMacroArgs(const string &args, const int min_arg, const size_t max_arg, const string macro_name, const string syntax, bool lastArgIsSeparatorChar=true)
Definition: paramsparser.cc:385
static string removeEnclosingChar(const string &str, const char o, const char c, bool allowMissing=false)
Removes characters enclosing a string.
Definition: tstring.h:352

References getMacroArgs(), and tstring::removeEnclosingChar().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ diag_matrix()

string ParamsParser::diag_matrix ( const string &  argstr)
782{
783 string syntax = "diag(x, n)";
784
785 int max_arg = 2, min_arg = 1;
786 string out;
787 string off_diag = "0";
788 size_t nrow;
789
790 vector<string> macro_args = getMacroArgs(argstr, min_arg, max_arg, "diag", syntax, false);
791
792 // the size is inferred from splitting the string provided
793 vector<string> elmnts = tstring::split(tstring::removeEnclosingChar(macro_args[0], '"', '"', true), ',');
794
795 if( macro_args.size() == 1) {
796
797 nrow = elmnts.size();
798
799 } else {
800 // already checked that it's not >2 arguments passed
801
802 nrow = size_t(tstring::str2uint(macro_args[1]));
803
804 // the value may contain several elements that might need to be repeated:
805 if(elmnts.size() > 1 && elmnts.size() != nrow) {
806
807 fatal("diag(): the number of elements to use for the diag matrix is different from parameter \"n\" in: diag(%s)\n\
808 use quoted rep() if you intend to repeat a pattern, eg: diag(q(rep(..., n1)),n2)\n", syntax.c_str());
809
810 } else if (elmnts.size() == 1){
811
812 // build the vector of elements used below
813
814 for(unsigned int i = 0; i < nrow-1; ++i)
815 elmnts.push_back(elmnts[0]);
816 }
817 }
818
819 out += "{";
820
821 for (unsigned int r = 0, c, e = 0; r < nrow; ++r) {
822
823 out += "\n{";
824
825 for (c = 0; c < nrow - 1 && e < elmnts.size(); ++c) {
826
827 if(c == r)
828 out += elmnts[e++] + ",";
829 else
830 out += off_diag + ",";
831 }
832
833 if(c == r)
834 out += elmnts[e++] + "}";
835 else
836 out += off_diag + "}";
837
838 }
839
840 // close the matrix block
841 out += "}";
842
843 return out;
844}
static vector< string > split(const string &str, const char delim, bool splitOnce=false)
splits a string into substrings (tokens) delimited by a single character.
Definition: tstring.h:128
static unsigned int str2uint(const string &str)
Converts a string into an unsigned integer.
Definition: tstring.h:51

References fatal(), getMacroArgs(), tstring::removeEnclosingChar(), tstring::split(), and tstring::str2uint().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ get_inputParams()

map< string, string > ParamsParser::get_inputParams ( )
inline
59{return _inputParams;}

References _inputParams.

◆ getArguments()

void ParamsParser::getArguments ( string &  arg_str,
vector< string > &  arg_vect 
)
static
172{
173 istringstream IN, BLOCK;
174 string arg, block_elmnt;
175 vector<string> block_args;
176 int dummy_line = 0;
177 char c;
178
179 IN.str(arg_str);
180
181 while(IN.get(c) && IN.good() && !IN.eof()){
182
183 if(isspace(c)) {
185 continue;
186 }
187
188 if(c == '{' || c == '(' || c == '[' || c == '\"')
189 {
190 getBlockArgument(IN, c, arg); //because it may span more than one line
191 }
192 else //not a block argument
193 {
194 IN.putback(c);
195 IN>>arg; //this stops if a whitespace is found, thus not including macro arguments if a macro
196 }
197
198
199 arg_vect.push_back( arg );
200 }
201}
static void getBlockArgument(istream &IN, char &c, string &arg)
Definition: paramsparser.cc:205
static bool removeSpaceAndComment(istream &IN, int &l_count, bool keepLast=false)
Removes whitespace char on a line until a non-ws or EOL is reached.
Definition: paramsparser.cc:1510

References getBlockArgument(), and StreamParser::removeSpaceAndComment().

Referenced by parse().

+ Here is the caller graph for this function:

◆ getBlockArgument()

void ParamsParser::getBlockArgument ( istream &  IN,
char &  c,
string &  arg 
)
static
206{
207 char e = 0;
208 int dummy_line = 0;
209
210 switch (start_c) {
211 case '{': e = '}';
212 break;
213 case '(': e = ')';
214 break;
215 case '[': e = ']';
216 break;
217 default:
218 break;
219 }
220
221 arg = StreamParser::readUntilCharacter(IN, dummy_line, start_c, (const char)e);
222
223// cout<<"added arg to vector: "<<out<<" next char='"<<IN.peek()<<"'\n";
224}
static string readUntilCharacter(istream &IN, int &l_count, const char start_c, const char end_c)
Definition: paramsparser.cc:1660

References StreamParser::readUntilCharacter().

Referenced by getArguments().

+ Here is the caller graph for this function:

◆ getMacroArgs()

vector< string > ParamsParser::getMacroArgs ( const string &  args,
const int  min_arg,
const size_t  max_arg,
const string  macro_name,
const string  syntax,
bool  lastArgIsSeparatorChar = true 
)
387{
388
389// cout<<" getMacroArgs:: argstr="<<argstr<<endl;
390
391 vector<string> macro_args = tstring::splitExcludeEnclosedDelimiters(argstr, ',');
392
393 if(macro_args.size() > max_arg ){
394 fatal("macro \"%s\" can have max %i arguments: %s\n", macro_name.c_str(), max_arg, syntax.c_str());
395 }
396 else if (macro_args.size() < min_arg) {
397 fatal("macro \"%s\" must have at least %i arguments: %s\n", macro_name.c_str(), min_arg, syntax.c_str());
398 }
399
400 size_t last_arg = macro_args.size() -1;
401
402 string sep = ","; // default sep
403
404 // treat the last argument specially if it is a separator char
405 // or add the default separator char to the arg vector if not specified in input
406 if(lastArgIsSeparatorChar) {
407
408 // make sure it was specified in input
409 if(macro_args.size() > min_arg) {
410 // check if separator is given in arg (we'll assume it is the last arg...)
411 if(argstr.rfind("sep") != string::npos) {
412
413 // get the separator char from the last character string
414 sep = getMacroSepParamChar(macro_args[ last_arg ], macro_name);
415
416 macro_args[ last_arg ] = sep; // we replace the string "sep=..." with the char
417
418 } else
419 macro_args.push_back(sep); //default
420 } else
421 macro_args.push_back(sep); //default
422 }
423
424// auto print = [this](const string& s){cout<<" getMacroArgs:: "<<s<<endl;};
425//
426// for_each(macro_args.cbegin(), macro_args.cend(), print);
427
428 return macro_args;
429}
string getMacroSepParamChar(const string &sep_in, const string macro_name)
Definition: paramsparser.cc:433
static vector< string > splitExcludeEnclosedDelimiters(const string &str, const char delim=',', const string &encloser="([{\"")
Splits a string into substrings (tokens) delimited by a single character.
Definition: tstring.h:155

References fatal(), getMacroSepParamChar(), and tstring::splitExcludeEnclosedDelimiters().

Referenced by concat(), diag_matrix(), matrix(), quote(), rbernoul(), rep(), rexp(), rgamma(), rlognorm(), rnorm(), rpoiss(), runif(), seq(), sym_matrix(), and tempseq().

+ Here is the caller graph for this function:

◆ getMacroParamValue()

string ParamsParser::getMacroParamValue ( const string &  str_in,
const string &  par_name,
const string &  macro_name 
)
471{
472 vector<string> param_args = tstring::splitExcludeEnclosedDelimiters(str_in, '=');
473
474 if(param_args[0] != par_name)
475 fatal("expected \"%s\" as input param to macro \"%s\", received \"%s\"\n", par_name.c_str(), macro_name.c_str(), str_in.c_str());
476
477 return param_args[1];
478}

References fatal(), and tstring::splitExcludeEnclosedDelimiters().

Referenced by rep(), sym_matrix(), and tempseq().

+ Here is the caller graph for this function:

◆ getMacroSepParamChar()

string ParamsParser::getMacroSepParamChar ( const string &  sep_in,
const string  macro_name 
)
434{
435
436 if( tstring::isanumber(sep_in))
437 fatal("last argument \"sep\" of macro \"%s\" must be a character string in \"%s\"\n", macro_name.c_str(), sep_in.c_str());
438
439 vector<string> sep_args = tstring::splitExcludeEnclosedDelimiters(sep_in, '=');
440
441 string sep;
442
443 // check format of sep argument specification: sep="c", or "c"
444 if(sep_args.size() == 0)
445 fatal("wrong format of \"sep\" argument to macro \"%s\" in \"%s\"\n", macro_name.c_str(), sep_in.c_str());
446
447 if(sep_args[0] == "sep") {
448
449 if(sep_args.size() > 2){
450 fatal("argument \"sep\" of macro \"%s\" must receive only one value in \"%s\" \n", macro_name.c_str(), sep_in.c_str());
451 }
452
453 sep = sep_args[1];
454
455 } else {
456 sep = sep_args[0];
457 }
458
459 if(sep[0] != '"') {
460 fatal("argument to \"sep\" of macro \"%s\" must be enclosed with two \" in \"%s\" \n", macro_name.c_str(), sep_in.c_str());
461 }
462
463 sep = tstring::removeEnclosingChar(sep, '"', '"');
464
465 return sep;
466}
static bool isanumber(const string &str)
Check whether the string is a number.
Definition: tstring.h:381

References fatal(), tstring::isanumber(), tstring::removeEnclosingChar(), and tstring::splitExcludeEnclosedDelimiters().

Referenced by getMacroArgs().

+ Here is the caller graph for this function:

◆ getParameters() [1/2]

map< string, string > & ParamsParser::getParameters ( )
inline
63{return _inputParams;}

References _inputParams.

Referenced by getParsedParameters().

+ Here is the caller graph for this function:

◆ getParameters() [2/2]

map< string, string > & ParamsParser::getParameters ( const char *  stream_name)
79{
80#ifdef _DEBUG_
81 cout<<"ParamsParser::getParameters:"<<endl;
82#endif
83 if(stream_name == NULL) {
84
85 if(_sname == NULL)
86
87 fatal("ParamsParser::getParameters::no stream attached to the parser!\n");
88
89 else if( !(read(_sname)) ) // read the input text stream and extract parameters and their arguments
90
91 fatal("ParamsParser::getParameters::failed from file \"%s\"\n",_sname);
92
93 } else if( !(read(stream_name)) ) // read the input text stream and extract parameters and their arguments
94
95 fatal("ParamsParser::getParameters::failed from file \"%s\"\n",stream_name);
96
97 if(stream_name != NULL) _sname = stream_name;
98
99 // pairs of parameter name & values have been recorder in _inputParams map
100 return _inputParams;
101}
virtual bool read(const char *stream)=0
Read/parse params & args from a file or a string or an R object.

References _inputParams, _sname, fatal(), and read().

Referenced by BinaryDataLoader::extractPop().

+ Here is the caller graph for this function:

◆ getParsedParameters() [1/2]

map< string, vector< string > > & ParamsParser::getParsedParameters ( )
inline
61{return _parsedParams;}
map< string, vector< string > > _parsedParams
The parsed set of simulation parameters after sequential parameters have been separated.
Definition: paramsparser.h:112

References _parsedParams.

◆ getParsedParameters() [2/2]

map< string, vector< string > > & ParamsParser::getParsedParameters ( const char *  stream_name)
106{
107#ifdef _DEBUG_
108 cout<<"ParamsParser::getParsedParameters:"<<endl;
109#endif
110 // read the input parameters from the input text stream
111 getParameters(stream_name);
112
113 // parse the input text stream
114 parse();
115
116 return _parsedParams;
117}
map< string, string > & getParameters()
Definition: paramsparser.h:63
void parse()
Builds the _parsedParams from the _inputParams.
Definition: paramsparser.cc:121

References _parsedParams, getParameters(), and parse().

Referenced by SimRunner::run().

+ Here is the caller graph for this function:

◆ matrix()

string ParamsParser::matrix ( const string &  argstr)
736{
737 string syntax = "matrix(x, nrow, ncol)";
738
739 int max_arg = 3, min_arg = 3;
740
741 vector<string> macro_args = getMacroArgs(argstr, min_arg, max_arg, "matrix", syntax, false);
742
743 if(macro_args[0][0] != '"' && macro_args[0].back() != '"')
744 fatal("matrix(): first argument must be quoted with \" in: matrix(%s)\n",argstr.c_str());
745
746 vector<string> elmnts = tstring::split(tstring::removeEnclosingChar(macro_args[0], '"', '"', false), ',');
747
748 unsigned int nrow = tstring::str2uint(macro_args[1]);
749 unsigned int ncol = tstring::str2uint(macro_args[2]);
750
751 if(elmnts.size() < nrow*ncol)
752 fatal("matrix(): not enough elements provided, got %i, must be rows*columns = %i\n", elmnts.size(), nrow*ncol);
753
754 if(elmnts.size() > nrow*ncol)
755 fatal("matrix(): too many elements provided, got %i, must be rows*columns = %i\n", elmnts.size(), nrow*ncol);
756
757 // start writing the matrix into the output string
758 string out("{");
759
760 for (unsigned int r = 0, e = 0; r < nrow; ++r) {
761
762 out += "\n{";
763
764 for (unsigned int c = 0; c < ncol - 1 && e < elmnts.size(); ++c, ++e) {
765
766 out += elmnts[e] + ",";
767 }
768
769 out += elmnts[e++] + "}";
770
771 }
772
773 // close the matrix block
774 out += "}";
775
776 return out;
777}

References fatal(), getMacroArgs(), tstring::removeEnclosingChar(), tstring::split(), and tstring::str2uint().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ parse()

void ParamsParser::parse ( )

Builds the _parsedParams from the _inputParams.

This defines rules of sequential, matricial params, etc.

122{
123#ifdef _DEBUG_
124 cout<<"ParamsParser::parse:"<<endl;
125#endif
126
127 // empty container map of parameters parsed into key - value(s) pairs
128 _parsedParams.clear();
129
130 // local holder of the arguments read in the input stream, contains whole lines from input
131 vector< string > argvect;
132 string arg;
133
134 // go through all parameters previously read in the input text stream and parse param values into multiple values
135 map<string, string>::iterator param = _inputParams.begin();
136
137 while(param != _inputParams.end()) {
138
139 // first, check and replace macros:
140 param->second = replaceMacro(param->second);
141
142 argvect.clear();
143
144 if((param->first == "stats") || (param->first == "stat") )
145
146 argvect.push_back(param->second); //this one cannot be a sequential parameter
147
148 else { //other params:
149
150 getArguments(param->second, argvect);
151
152 }
153
154 _parsedParams[param->first] = argvect;
155
156#ifdef _DEBUG_
157 message(" %s (", param->first.c_str());
158 unsigned int nb=argvect.size() ;
159 for(unsigned int i=0 ;i<nb; ++i){
160 cout<<argvect[i]<<" ";
161 }
162 message("| %i args)\n", nb);
163#endif
164
165 param++;
166 }
167}
string replaceMacro(const string &arg)
Macros.
Definition: paramsparser.cc:228
static void getArguments(string &arg_str, vector< string > &arg_vect)
Definition: paramsparser.cc:171

References _inputParams, _parsedParams, getArguments(), message(), and replaceMacro().

Referenced by getParsedParameters().

+ Here is the caller graph for this function:

◆ parseMacroFunctionBlock()

string ParamsParser::parseMacroFunctionBlock ( const string &  in_arg)
255{
256 string front, tail, rest, out_arg;
257 string name;
258
259// cout<<"parseMacroFunctionBlock <"<< in_arg <<">\n";
260
261 size_t opar_pos, cpar_pos, first_macro_ch;
262
263 // extract the name of the macro, first find the opening parenthesis
264 opar_pos = in_arg.find_first_of('(', 1);
265
266 //cut the string at the opening par to look for the macro name in the front part
267 front = in_arg.substr(0, opar_pos);
268
269 //cut the string after the opening par to look for macro arguments and more macros in the rest
270 rest = in_arg.substr(opar_pos+1, in_arg.size() - opar_pos - 1);
271
272 //a macro name can only start after the last delimiter in front of it
273 first_macro_ch = front.find_last_of("(){}[], =");
274 // '=' is included because arguments to macros can be named and assigned a call to a nested macro
275 // eg: tempseq(at=seq(),seq=seq())
276
277 //the open par may not be part of a macro, or there might not be any delim in front, check this:
278 if(first_macro_ch == string::npos)
279 first_macro_ch = 0; // no delim before name, start at first pos in string
280 else
281 first_macro_ch++; //skip the last delimiter
282
283 // check if we have a macro name in front and extract it
284 if(opar_pos > first_macro_ch)
285 {
286 name = in_arg.substr(first_macro_ch, opar_pos - first_macro_ch);
287 // keep what was in front of the macro name;
288 front = in_arg.substr(0, first_macro_ch);
289 }
290 else // first_macro_ch == opar_pos; no name, no macro, skip
291 { // this corresponds to cases where multiple blocks are provided in input:
292 // param_name (... macro1(...),...) (... macro2(...), ...)
293 // the second block causes problems if we lose the brackets on the way
294
295 name = "";
296
297 // keep what was in front of the false macro block, including the opening '(';
298 front = in_arg.substr(0, first_macro_ch+1);
299 }
300
301 out_arg += front;
302
303 // before reading the arguments of the macro function call, check for nested macros, recursively
304 if(rest.find_first_of('(', 1) != string::npos)
305 {
306 //recursion
307 //replace the macro name by the result of the call to the nested macro in the macro_args vect
308 rest = ( parseMacroFunctionBlock(rest) );
309
310 }
311 //no more nested block, close recursion and return arguments
312
313// cout<< " processing: "<<rest<<endl;
314
315 // find the limit of the macro args
316 cpar_pos = rest.find_first_of(')');
317
318 if ( cpar_pos != string::npos)
319 {
320 // record what is left after the macro args, excluding the closing macro ')'
321 if( cpar_pos < rest.size()-1 ){
322
323 tail = rest.substr(cpar_pos+1, rest.size() - cpar_pos - 1);
324 }
325 else
326 tail = "";
327 }
328 else
329 {
330 fatal("Error while parsing argument string \"%s\" for macros, closing bracket not found\n", (rest+tail).c_str());
331 }
332
333 rest = rest.substr(0, cpar_pos); // this is the string containing the args to the macro
334
335 if(name == "")
336 {
337
338// cout<< " not calling macro: name =\"\"; arg string is <"<<rest<<">"<<"\n";
339
340 out_arg += rest +")" + tail; //need to put back the closing ')' because not a macro
341
342 }
343 else
344 {
345
346// cout<< " calling macro: "<<name<<"("<<rest<<")"<<"\n";
347
348 // call the macro and paste the result into the output string
349 out_arg += callMacro(name, rest) + tail;
350
351 }
352
353// cout<< " returning "<<out_arg<<endl;
354 // return the processed string
355 return out_arg;
356}
string parseMacroFunctionBlock(const string &in_arg)
Definition: paramsparser.cc:254
string callMacro(const string &name, const string &argstr)
Definition: paramsparser.cc:360

References callMacro(), fatal(), and parseMacroFunctionBlock().

Referenced by parseMacroFunctionBlock(), and replaceMacro().

+ Here is the caller graph for this function:

◆ quote()

string ParamsParser::quote ( const string &  argstr)

Macro "q" returns a quoted string.

Useful to quote result of call to enclosed macro.

510{
511
512 string syntax="q(... , sep=\",\")";
513 string out;
514
515 size_t max_arg = INT64_MAX;
516
517 vector<string> macro_args = getMacroArgs(argstr, 1, max_arg, "q", syntax, true);
518
519 string sep = macro_args.back();
520
521 int last = macro_args.size() - 2; //omit separator at last position
522
523 for (int i = 0; i < last; ++i) {
524 out += macro_args[i] + sep;
525 }
526 // add last element without separator
527 out += macro_args[last];
528
529 return "\"" + out + "\"";
530}

References getMacroArgs().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ rbernoul()

string ParamsParser::rbernoul ( const string &  argstr)
1146{
1147 string syntax = "rbernoul(n, p=0.5, sep=\",\")";
1148
1149 int max_arg = 3, min_arg = 1;
1150 double p = 0.5;
1151 unsigned int num_deviates;
1152
1153 vector<string> macro_args = getMacroArgs(argstr, min_arg, max_arg, "rbernoul", syntax, true);
1154
1155 // get and check the number of deviates we need to draw
1156 if(!tstring::isanumber(macro_args[0]))
1157 fatal("rbernoul(): first argument must be a positive integer in: rbernoul(%s)\n", argstr.c_str());
1158
1159 num_deviates = tstring::str2uint(macro_args[0]);
1160
1161 if(!num_deviates > 0)
1162 fatal("rbernoul(): first argument must be a positive integer in: rbernoul(%s)\n", argstr.c_str());
1163
1164
1165 // get the mean value, if present
1166 // we omit last arg macro_args[2] which is always the separator character
1167 if(macro_args.size() == 3){
1168
1169 vector<string> param_args = tstring::splitExcludeEnclosedDelimiters(macro_args[1], '=');
1170
1171 if(param_args[0] == "p") {
1172
1173 p = tstring::str2dble(param_args[1]);
1174
1175 } else if(param_args.size() == 1) {
1176
1177 // the string "mean=" can be omitted by user
1178
1179 p = tstring::str2dble(param_args[0]);
1180
1181 } else {
1182
1183 fatal("rbernoul() syntax is: %s\n",syntax.c_str());
1184 }
1185 }
1186
1187 string sep = macro_args.back();
1188
1189 // compute the random deviates
1190 string out;
1191
1192 auto draw = [p](){return tstring::dble2str(RAND::Bernoulli(p));};
1193
1194 for(unsigned int i = 0; i < num_deviates -1; ++i)
1195 out += draw() + sep;
1196
1197 out += draw();
1198
1199 return out;
1200}
static double Bernoulli(double p)
Definition: Uniform.h:430
static double str2dble(const string &str)
Converts a string into a double.
Definition: tstring.h:84
static string dble2str(const double d)
Writes a floating-point value into a string.
Definition: tstring.h:115

References RAND::Bernoulli(), tstring::dble2str(), fatal(), getMacroArgs(), tstring::isanumber(), tstring::splitExcludeEnclosedDelimiters(), tstring::str2dble(), and tstring::str2uint().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ read()

virtual bool ParamsParser::read ( const char *  stream)
pure virtual

Read/parse params & args from a file or a string or an R object.

Params and their args are put in the _inputParams.

Implemented in FileParser, BinaryFileParser, and StreamParser.

Referenced by getParameters().

+ Here is the caller graph for this function:

◆ rep()

string ParamsParser::rep ( const string &  argstr)
535{
536
537 string syntax="rep(x, n, each=1, sep=\",\")";
538
539 int max_arg = 4;
540
541 vector<string> macro_args = getMacroArgs(argstr, 2, max_arg, "rep", syntax, true);
542
543// auto print = [this](const string& s){cout<<" "<<s<<endl;};
544//
545// for_each(macro_args.cbegin(), macro_args.cend(), print);
546
547 auto num_args = macro_args.size();
548
549 // ---- get the string to repeat ----
550 string what = macro_args[0];
551
552 //removing enclosing "" if added:
553 what = tstring::removeEnclosingChar(what, '"', '"', true);
554
555 // ---- the second argument is the number of repetition ----
556 if ( !tstring::isanumber(macro_args[1]) ) {
557 fatal("rep(): second argument must be a number in: rep(x, n, each=1, sep=\",\")\n");
558 }
559
560 int num = tstring::str2int(macro_args[1]);
561
562 // ---- looking for the "each" argument ----
563 auto has_each = [] (const string& s) {return (s.find("each") != string::npos);};
564
565 auto argPos = find_if(macro_args.begin(), macro_args.end(), has_each);
566
567 string arg;
568
569 if( argPos != macro_args.end() )
570 arg = getMacroParamValue(*argPos, "each", syntax);
571 else
572 arg = "1"; // default value if argument is absent, mean "each" must be named
573
574 int each = tstring::str2int(arg);
575
576 // the separator char has been set at the back by default
577 string sep;
578
579 sep = macro_args[num_args-1];
580
581
582 // processing the input in case where "each" has been specified
583 vector<string> elmnts;
584
585 if(argPos != macro_args.end())
586 elmnts = tstring::split(tstring::removeEnclosingChar(macro_args[0], '"', '"', true), ',');
587 else
588 elmnts.push_back(what);
589
590 string out;
591
592 for(int i = 0; i < num; ++i) {
593 for(auto e = 0; e < elmnts.size(); ++e) {
594 for(int j = 0; j < each; ++j) {
595 out += elmnts[e] + sep;
596 }
597 }
598 }
599 // remove last separator
600 out = tstring::removeLastCharOf(out, ',');
601
602 return out;
603}
string getMacroParamValue(const string &str_in, const string &par_name, const string &macro_name)
Definition: paramsparser.cc:470
static int str2int(const string &str)
Converts a string into an integer.
Definition: tstring.h:73
static string removeLastCharOf(const string &str, const char c)
Removes the last of a character found in a string.
Definition: tstring.h:333

References fatal(), getMacroArgs(), getMacroParamValue(), tstring::isanumber(), tstring::removeEnclosingChar(), tstring::removeLastCharOf(), tstring::split(), and tstring::str2int().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ replaceMacro()

string ParamsParser::replaceMacro ( const string &  arg)

Macros.

229{
230 // we now need to check if a macro is part of the argument string
231 // this is detected if the arg string holds a "(" as in "macro_name( "
232 // or "(" is within the whole string as in "macro_name(arg1"
233 // or "(" is within a block argument, we skip the first char to account for that last case
234
235 string out_arg;
236
237 if( arg.find_first_of('(', 1) != string::npos )
238 {
239 // there might be a macro, get it
240
241 out_arg = parseMacroFunctionBlock(arg);
242
243 }
244 else // no macro
245 {
246 out_arg = arg;
247 }
248
249 return out_arg;
250}

References parseMacroFunctionBlock().

Referenced by parse().

+ Here is the caller graph for this function:

◆ reset_inputParams()

void ParamsParser::reset_inputParams ( )
inlineprotected
101{_inputParams.clear();}

References _inputParams.

Referenced by StreamParser::read().

+ Here is the caller graph for this function:

◆ rexp()

string ParamsParser::rexp ( const string &  argstr)
1205{
1206 string syntax = "rexp(n, mean=1, sep=\",\")";
1207
1208 int max_arg = 3, min_arg = 2;
1209 double mean=1.0;
1210 unsigned int num_deviates;
1211
1212 vector<string> macro_args = getMacroArgs(argstr, min_arg, max_arg, "rexp", syntax, true);
1213
1214 // get and check the number of deviates we need to draw
1215 if(!tstring::isanumber(macro_args[0]))
1216 fatal("rexp(): first argument must be a positive integer in: rexp(%s)\n", argstr.c_str());
1217
1218 num_deviates = tstring::str2uint(macro_args[0]);
1219
1220 if(!num_deviates > 0)
1221 fatal("rexp(): first argument must be a positive integer in: rexp(%s)\n", argstr.c_str());
1222
1223
1224 // get the mean value, if present
1225 // we omit last arg macro_args[2] which is always the separator character
1226 if(macro_args.size() == 3){
1227
1228 vector<string> param_args = tstring::splitExcludeEnclosedDelimiters(macro_args[1], '=');
1229
1230 if(param_args[0] == "mean") {
1231
1232 mean = tstring::str2dble(param_args[1]);
1233
1234 } else if(param_args.size() == 1) {
1235
1236 // the string "mean=" can be omitted by user
1237
1238 mean = tstring::str2dble(param_args[0]);
1239
1240 } else {
1241
1242 fatal("rexp() syntax is: %s\n",syntax.c_str());
1243 }
1244 }
1245
1246 string sep = macro_args.back();
1247
1248 // compute the random deviates
1249 string out;
1250
1251 auto draw = [mean](){return tstring::dble2str(RAND::Exponential(mean));};
1252
1253 for(unsigned int i = 0; i < num_deviates -1; ++i)
1254 out += draw() + sep;
1255
1256 out += draw();
1257
1258 return out;
1259}
static double Exponential(double mu)
Definition: Uniform.h:448

References tstring::dble2str(), RAND::Exponential(), fatal(), getMacroArgs(), tstring::isanumber(), tstring::splitExcludeEnclosedDelimiters(), tstring::str2dble(), and tstring::str2uint().

◆ rgamma()

string ParamsParser::rgamma ( const string &  argstr)
1264{
1265 string syntax = "rgamma(n, a, b, sep=\",\")";
1266
1267 // a and b params are mandatory, with no default
1268
1269 int max_arg = 4, min_arg = 3;
1270 double a = 0.0, b = 1.0;
1271 unsigned int num_deviates;
1272
1273 vector<string> macro_args = getMacroArgs(argstr, min_arg, max_arg, "rgamma", syntax, true);
1274
1275 // get and check the number of deviates we need to draw
1276 if(!tstring::isanumber(macro_args[0]))
1277 fatal("rgamma(): first argument must be a positive integer in: rgamma(%s)\n", argstr.c_str());
1278
1279 num_deviates = tstring::str2uint(macro_args[0]);
1280
1281 if(!num_deviates > 0)
1282 fatal("rgamma(): first argument must be a positive integer in: rgamma(%s)\n", argstr.c_str());
1283
1284
1285 // omit last arg which is always the separator character
1286
1287 for (unsigned int i = 1; i < macro_args.size()-1; ++i) {
1288
1289 vector<string> param_args = tstring::splitExcludeEnclosedDelimiters(macro_args[i], '=');
1290
1291 if(param_args[0] == "a") {
1292
1293 a = tstring::str2dble(param_args[1]);
1294
1295 } else if(param_args[0] == "b") {
1296
1297 b = tstring::str2dble(param_args[1]);
1298
1299 } else if(param_args.size() == 1) {
1300
1301 // the strings "a=" or "b=" can be omitted by the user
1302 // we assume meaning by position
1303
1304 if(i == 1)
1305 a = tstring::str2dble(param_args[0]);
1306 else if (i == 2)
1307 b = tstring::str2dble(param_args[0]);
1308
1309 } else {
1310
1311 fatal("rgamma() syntax is: %s.\n",syntax.c_str());
1312 }
1313 }
1314
1315 if(a < 0)
1316 fatal("rgamma(): shape parameter \"a\" must be positive in %s.\n",syntax.c_str());
1317
1318 if(b < 0)
1319 fatal("rgamma(): rate parameter \"b\" must be positive in %s.\n",syntax.c_str());
1320
1321
1322 string sep = macro_args.back();
1323
1324 // compute the random deviates
1325 string out;
1326
1327 auto draw = [a, b](){return tstring::dble2str(RAND::Gamma(a,b));};
1328
1329 for(unsigned int i = 0; i < num_deviates -1; ++i)
1330 out += draw() + sep;
1331
1332 out += draw();
1333
1334 return out;
1335}
static double Gamma(double a, double b)
Definition: Uniform.h:386

References tstring::dble2str(), fatal(), RAND::Gamma(), getMacroArgs(), tstring::isanumber(), tstring::splitExcludeEnclosedDelimiters(), tstring::str2dble(), and tstring::str2uint().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ rlognorm()

string ParamsParser::rlognorm ( const string &  argstr)
1340{
1341 string syntax = "rlognorm(n, mean, sd, sep=\",\")";
1342
1343 // a and b params are mandatory, with no default
1344
1345 int max_arg = 4, min_arg = 3;
1346 double a = 0.0, b = 1.0;
1347 unsigned int num_deviates;
1348
1349 vector<string> macro_args = getMacroArgs(argstr, min_arg, max_arg, "rlognorm", syntax, true);
1350
1351 // get and check the number of deviates we need to draw
1352 if(!tstring::isanumber(macro_args[0]))
1353 fatal("rlognorm(): first argument must be a positive integer in: rlognorm(%s)\n", argstr.c_str());
1354
1355 num_deviates = tstring::str2uint(macro_args[0]);
1356
1357 if(!num_deviates > 0)
1358 fatal("rlognorm(): first argument must be a positive integer in: rlognorm(%s)\n", argstr.c_str());
1359
1360
1361 // omit last arg which is always the separator character
1362
1363 for (unsigned int i = 1; i < macro_args.size()-1; ++i) {
1364
1365 vector<string> param_args = tstring::splitExcludeEnclosedDelimiters(macro_args[i], '=');
1366
1367 if(param_args[0] == "mean") {
1368
1369 a = tstring::str2dble(param_args[1]);
1370
1371 } else if(param_args[0] == "sd") {
1372
1373 b = tstring::str2dble(param_args[1]);
1374
1375 } else if(param_args.size() == 1) {
1376
1377 // the strings "mean=" or "sd=" can be omitted by the user
1378 // we assume meaning by position
1379
1380 if(i == 1)
1381 a = tstring::str2dble(param_args[0]);
1382 else if (i == 2)
1383 b = tstring::str2dble(param_args[0]);
1384
1385 } else {
1386
1387 fatal("rlognorm() syntax is: %s\n",syntax.c_str());
1388 }
1389 }
1390
1391 string sep = macro_args.back();
1392
1393 // compute the random deviates
1394 string out;
1395
1396 auto draw = [a, b](){return tstring::dble2str(RAND::LogNormal(a,b));};
1397
1398 for(unsigned int i = 0; i < num_deviates -1; ++i)
1399 out += draw() + sep;
1400
1401 out += draw();
1402
1403 return out;
1404}
static double LogNormal(double zeta, double sigma)
Definition: Uniform.h:358

References tstring::dble2str(), fatal(), getMacroArgs(), tstring::isanumber(), RAND::LogNormal(), tstring::splitExcludeEnclosedDelimiters(), tstring::str2dble(), and tstring::str2uint().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ rnorm()

string ParamsParser::rnorm ( const string &  argstr)
1017{
1018 string syntax = "rnorm(n, mean=0, sd=1, sep=\",\")";
1019
1020 int max_arg = 4, min_arg = 1;
1021 double mean = 0.0, sd = 1.0;
1022 unsigned int num_deviates;
1023
1024 vector<string> macro_args = getMacroArgs(argstr, min_arg, max_arg, "rnorm", syntax, true);
1025
1026 // get and check the number of deviates we need to draw
1027 if(!tstring::isanumber(macro_args[0]))
1028 fatal("rnorm(): first argument must be a positive integer in: rnorm(%s)\n", argstr.c_str());
1029
1030 num_deviates = tstring::str2uint(macro_args[0]);
1031
1032 if(!num_deviates > 0)
1033 fatal("rnorm(): first argument must be a positive integer in: rnorm(%s)\n", argstr.c_str());
1034
1035 // get the mean and sd values, if present
1036 if(macro_args.size()-1 > 1){
1037
1038 // omit last arg which is always the separator character
1039
1040 for (unsigned int i = 1; i < macro_args.size()-1; ++i) {
1041
1042 vector<string> param_args = tstring::splitExcludeEnclosedDelimiters(macro_args[i], '=');
1043
1044 if(param_args[0] == "mean") {
1045
1046 mean = tstring::str2dble(param_args[1]);
1047
1048 } else if(param_args[0] == "sd") {
1049
1050 sd = tstring::str2dble(param_args[1]);
1051
1052 } else if(param_args.size() == 1) {
1053
1054 // the "mean=" and "sd=" can be omitted
1055
1056 if(i == 1)
1057 mean = tstring::str2dble(param_args[0]);
1058 else if (i == 2)
1059 sd = tstring::str2dble(param_args[0]);
1060
1061 } else {
1062
1063 fatal("rnorm() syntax is: %s\n",syntax.c_str());
1064 }
1065
1066 }
1067 }
1068
1069 string sep = macro_args.back();
1070
1071 // compute the random deviates
1072 string out;
1073
1074 auto draw = [mean, sd](){return tstring::dble2str(mean + RAND::Gaussian(sd));};
1075
1076 for(unsigned int i = 0; i < num_deviates -1; ++i)
1077 out += draw() + sep;
1078
1079 out += draw();
1080
1081 return out;
1082}
static double Gaussian(double sigma)
Definition: Uniform.h:262

References tstring::dble2str(), fatal(), RAND::Gaussian(), getMacroArgs(), tstring::isanumber(), tstring::splitExcludeEnclosedDelimiters(), tstring::str2dble(), and tstring::str2uint().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ rpoiss()

string ParamsParser::rpoiss ( const string &  argstr)
1087{
1088 string syntax = "rpois(n, mean=1, sep=\",\")";
1089
1090 int max_arg = 3, min_arg = 1;
1091 double mean = 1.0;
1092 unsigned int num_deviates;
1093
1094 vector<string> macro_args = getMacroArgs(argstr, min_arg, max_arg, "rpois", syntax, true);
1095
1096 // get and check the number of deviates we need to draw
1097 if(!tstring::isanumber(macro_args[0]))
1098 fatal("rpois(): first argument must be a positive integer in: rpois(%s)\n", argstr.c_str());
1099
1100 num_deviates = tstring::str2uint(macro_args[0]);
1101
1102 if(!num_deviates > 0)
1103 fatal("rpois(): first argument must be a positive integer in: rpois(%s)\n", argstr.c_str());
1104
1105
1106 // get the mean value, if present
1107 // we omit last arg macro_args[2] which is always the separator character
1108 if(macro_args.size() == 3){
1109
1110 vector<string> param_args = tstring::splitExcludeEnclosedDelimiters(macro_args[1], '=');
1111
1112 if(param_args[0] == "mean") {
1113
1114 mean = tstring::str2dble(param_args[1]);
1115
1116 } else if(param_args.size() == 1) {
1117
1118 // the string "mean=" can be omitted by user
1119
1120 mean = tstring::str2dble(param_args[0]);
1121
1122 } else {
1123
1124 fatal("rpois() syntax is: %s\n",syntax.c_str());
1125 }
1126 }
1127
1128 string sep = macro_args.back();
1129
1130 // compute the random deviates
1131 string out;
1132
1133 auto draw = [mean](){return tstring::dble2str(RAND::Poisson(mean));};
1134
1135 for(unsigned int i = 0; i < num_deviates -1; ++i)
1136 out += draw() + sep;
1137
1138 out += draw();
1139
1140 return out;
1141}
static double Poisson(double mean)
From the Numerical Recieps.
Definition: Uniform.h:220

References tstring::dble2str(), fatal(), getMacroArgs(), tstring::isanumber(), RAND::Poisson(), tstring::splitExcludeEnclosedDelimiters(), tstring::str2dble(), and tstring::str2uint().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ runif()

string ParamsParser::runif ( const string &  argstr)
940{
941 string syntax = "runif(n, min=0, max=1, sep=\",\")";
942
943 int max_arg = 4, min_arg = 1;
944 double min = 0.0, max = 1.0;
945 unsigned int num_deviates;
946
947 vector<string> macro_args = getMacroArgs(argstr, min_arg, max_arg, "runif", syntax, true);
948
949 // get and check the number of deviates we need to draw
950 if(!tstring::isanumber(macro_args[0]))
951 fatal("runif(): first argument must be a positive integer in: runif(%s)\n", argstr.c_str());
952
953 num_deviates = tstring::str2uint(macro_args[0]);
954
955 if(!num_deviates > 0)
956 fatal("runif(): first argument must be a positive integer in: runif(%s)\n", argstr.c_str());
957
958
959// map<string, double> dev_param; dev_param["min"] = 0.0; dev_param["max"] = 1.0;
960//
961// auto get_param_value = [&](const string& str_in){
962// vector<string> param_args = tstring::splitExcludeEnclosedDelimiters(str_in, '=');
963//
964// };
965
966 // get the min and max values, if present
967 if(macro_args.size()-1 > 1){
968
969 // omit last arg which is always the separator character
970
971 for (unsigned int i = 1; i < macro_args.size()-1; ++i) {
972
973 vector<string> param_args = tstring::splitExcludeEnclosedDelimiters(macro_args[i], '=');
974
975 if(param_args[0] == "min") {
976
977 min = tstring::str2dble(param_args[1]);
978
979 } else if(param_args[0] == "max") {
980
981 max = tstring::str2dble(param_args[1]);
982
983 } else if(param_args.size() == 1) {
984
985 // the "min=" and "max=" can be omitted
986
987 if(i == 1)
988 min = tstring::str2dble(param_args[0]);
989 else if (i == 2)
990 max = tstring::str2dble(param_args[0]);
991
992 }else {
993
994 fatal("runif() syntax is: %s\n",syntax.c_str());
995 }
996 }
997 }
998
999 string sep = macro_args.back();
1000
1001 // compute the random deviates
1002 string out;
1003
1004 auto draw = [min, max](){return tstring::dble2str((max-min)*RAND::Uniform() + min);};
1005
1006 for(unsigned int i = 0; i < num_deviates -1; ++i)
1007 out += draw() + sep;
1008
1009 out += draw();
1010
1011 return out;
1012}
static double Uniform()
Generates a random number from [0.0, 1.0[ uniformly distributed.
Definition: Uniform.h:125

References tstring::dble2str(), fatal(), getMacroArgs(), tstring::isanumber(), tstring::splitExcludeEnclosedDelimiters(), tstring::str2dble(), tstring::str2uint(), and RAND::Uniform().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ seq()

string ParamsParser::seq ( const string &  argstr)
608{
609 string syntax="seq(from, to, by, sep=\",\")";
610
611 int max_arg = 4;
612
613 vector<string> macro_args = getMacroArgs(argstr, 3, max_arg, "seq", syntax, true);
614
615 string sep = macro_args[ 3 ];
616
617 for (int i = 0; i < 3; ++i) {
618
619 if(!tstring::isanumber(macro_args[i]))
620 fatal("seq(): argument %i must be a number in: seq(%s)\n", i+1, argstr.c_str());
621
622 }
623
624 // now create the sequence of numbers:
625 string out;
626
627 int index = 0;
628 double current = tstring::str2dble(macro_args[0]);
629 double to = tstring::str2dble(macro_args[1]);
630 double i = tstring::str2dble(macro_args[2]);
631
632 double sign = 1.0; // indicate whether the sequence is increasing (+1.0) or decreasing (-1.0)
633
634 // check if the sequence is decreasing
635 if(current > to) {
636
637 sign = -1.0;
638
639 if(i > 0)
640 fatal("seq(): third argument \"by\" must be a negative number when from > to in: seq(%s).\n", argstr.c_str());
641
642// i *= sign; // must be negative when decrement
643 }
644
645 // check the sign of the increment if increasing sequence
646 if(current < to && i < 0) {
647
648 fatal("seq(): third argument \"by\" must be a positive number when from < to in: seq(%s).\n", argstr.c_str());
649
650// i *= -1.0; //make it positive
651 }
652 // loop until reaching "to"
653 while(sign*(current-to) < 1.0e-15) { // to avoid precision errors, it's a workaround, setting epsilon to a fixed value
654
655 out += tstring::dble2str(current);
656
657 current += i;
658
659 // check next value to place the separator thus avoiding adding a separator after the last value
660 if(sign*(current-to) < 1.0e-15)
661 out += sep;
662 };
663
664 return out;
665}

References tstring::dble2str(), fatal(), getMacroArgs(), tstring::isanumber(), and tstring::str2dble().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ setName()

void ParamsParser::setName ( const char *  name)
inline
57{_sname = name;}

References _sname.

Referenced by BinaryDataLoader::extractPop().

+ Here is the caller graph for this function:

◆ sym_matrix()

string ParamsParser::sym_matrix ( const string &  argstr)
849{
850 string syntax = "smatrix(x, nrow, diag=0)";
851
852 // first arg is row-oriented
853 int max_arg = 3, min_arg = 2;
854 string out;
855 size_t nrow;
856
857 vector<string> macro_args = getMacroArgs(argstr, min_arg, max_arg, "smatrix", syntax, false);
858
859 // the size is inferred from splitting the string provided
860 vector<string> upper = tstring::split(tstring::removeEnclosingChar(macro_args[0], '"', '"', true), ',');
861
862 if(!tstring::isanumber(macro_args[1]))
863 fatal("smatrix(): secong argument \"nrow\" is not a number in: smatrix(%s)\n", argstr.c_str());
864 else
865 nrow = tstring::str2int(macro_args[1]);
866
867 size_t num_el = nrow*(nrow-1)/2;
868
869 //fill "upper" if only one value was passed, that value will be copied
870 if( upper.size() == 1) {
871 string val = upper[0];
872 upper.assign(num_el, val);
873 }
874
875 // check size
876 if( upper.size() != nrow*(nrow-1)/2 )
877 fatal("smatrix(): number of elements (%i) passed doesn't match with the number of rows (%i) specified in: smatrix(%s)\n", upper.size(), nrow, argstr.c_str());
878
879 // look for diagonal elements
880 auto is_diag = [] (const string& s) {return (s.find("diag") != string::npos);};
881
882 vector<string>::const_iterator diagPos = find_if(macro_args.begin(), macro_args.end(), is_diag);
883
884 vector<string> diag;
885 string diag_arg;
886
887 if( diagPos != macro_args.end() ) // user specified the diagonal elements
888 {
889 diag_arg = *diagPos;
890 //remove the front string "diag=" and get the argument value
891 diag_arg = getMacroParamValue(diag_arg, "diag", syntax);
892
893 diag = tstring::split(tstring::removeEnclosingChar(diag_arg, '"', '"', true), ',');
894
895 if(diag.size() == 1) {
896 string val = diag[0];
897 diag.assign(nrow, val);
898 }
899 else if (diag.size() != nrow) {
900 fatal("smatrix(): number of elements to \"diag\" doesn't match with the matrix size in: smatrix(%s)\n", argstr.c_str());
901 }
902
903 } else
904 diag.assign(nrow, "0");
905
906 // create the matrix
907
908 TMatrix mat(nrow, nrow);
909
910 for (unsigned int r = 0, c, e = 0, d = 0; r < nrow ; ++r) {
911
912 for (c = r; c < nrow && e < upper.size(); ++c) {
913
914 if(c == r)
915 mat.set(r, c, tstring::str2dble(diag[d++]));
916 else
917 mat.set(r, c, tstring::str2dble(upper[e++]));
918 }
919
920 // catch the last diagonal element of the matrix, after having copied all off-diagonal elements
921 if(c == r)
922 mat.set(r, c, tstring::str2dble(diag[d++]));
923 }
924
925 // fill the lower triangle:
926 for (unsigned int r = nrow-1; r > 0 ; --r) {
927
928 for(unsigned int c = 0; c < r; ++c)
929
930 mat.set(r, c, mat.get(c,r));
931
932 }
933
934 return mat.to_string();
935}
A class to handle matrix in params, coerces matrix into a vector of same total size.
Definition: tmatrix.h:49

References fatal(), TMatrix::get(), getMacroArgs(), getMacroParamValue(), tstring::isanumber(), tstring::removeEnclosingChar(), TMatrix::set(), tstring::split(), tstring::str2dble(), tstring::str2int(), and TMatrix::to_string().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

◆ tempseq()

string ParamsParser::tempseq ( const string &  argstr)
670{
671 string syntax = "tempseq(at, seq) ";
672
673 int max_arg = 2, min_arg = 2;
674
675 vector<string> macro_args = getMacroArgs(argstr, min_arg, max_arg, "tempseq", syntax, false);
676
677 //get arg values by name:
678 string arg;
679 vector<string>::const_iterator argPos;
680
681
682 // look for a named argument
683
684 // check if arg "at" is named ( at="..." )
685 auto has_at = [] (const string& s) {return (s.find("at") != string::npos);};
686
687 argPos = find_if(macro_args.begin(), macro_args.end(), has_at);
688
689 if( argPos != macro_args.end() )
690 arg = getMacroParamValue(*argPos, "at", syntax);
691 else
692 arg = macro_args[0]; // default assignment by position
693
694 vector<string> at_time = tstring::split(tstring::removeEnclosingChar(arg, '"', '"', false), ',');
695
696 if(at_time[0] != "0")
697 fatal("tempseq(): argument \"at\" must start with value \'0\' (for parameter's initial value at generation 0) in: tempseq(%s).\n",argstr.c_str());
698
699 // check if arg "seq" is named:
700 auto has_seq = [] (const string& s) {return (s.find("seq") != string::npos);};
701
702 argPos = find_if(macro_args.begin(), macro_args.end(), has_seq);
703
704 if( argPos != macro_args.end() )
705 arg = getMacroParamValue(*argPos, "seq", syntax);
706 else
707 arg = macro_args[1]; // default assignment by position
708
709 vector<string> values = tstring::split(tstring::removeEnclosingChar(arg, '"', '"', false), ',');
710
711 //------- process input and build output string ---------
712 size_t num_gen = at_time.size();
713 size_t num_val = values.size();
714
715 if(num_val < num_gen)
716 fatal("tempseq(): argument \"seq\" contains less values than \"at\" in: tempseq(%s).\n",argstr.c_str());
717
718 if(num_val > num_gen)
719 fatal("tempseq(): argument \"seq\" contains more values than \"at\" in: tempseq(%s)\n",argstr.c_str());
720
721 string out = "(";
722
723 for(auto i = 0; i < num_gen-1; ++i) {
724
725 out += "@g" + at_time[i] + " " + values[ i % num_val] + ", ";
726 }
727
728 out += "@g" + at_time[num_gen-1] + " " + values[ (num_gen-1) % num_val] + ")";
729
730 return out;
731}

References fatal(), getMacroArgs(), getMacroParamValue(), tstring::removeEnclosingChar(), and tstring::split().

Referenced by ParamsParser().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _inputParams

map< string, string > ParamsParser::_inputParams
private

The whole, unparsed set of input parameters.

Referenced by add_inputParam(), get_inputParams(), getParameters(), parse(), and reset_inputParams().

◆ _macroMap

map< string, string (ParamsParser::* )(const string& )> ParamsParser::_macroMap
private

The macro table, mapping call name to caller.

Referenced by callMacro(), and ParamsParser().

◆ _parsedParams

map< string, vector< string > > ParamsParser::_parsedParams
private

The parsed set of simulation parameters after sequential parameters have been separated.

Referenced by getParsedParameters(), and parse().

◆ _sname

const char* ParamsParser::_sname
private

Attached file of stream name.

Referenced by getParameters(), ParamsParser(), and setName().


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

Generated for Nemo v2.3.56 by  doxygen 1.9.0 -- Nemo is hosted on  Download Nemo

Locations of visitors to this page
Catalogued on GSR