bpp-core3  3.0.0
NumCalcApplicationTools.cpp
Go to the documentation of this file.
1 //
2 // File: NumCalcApplicationTools.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2009-03-29 15:13:00
6 //
7 
8 /*
9  Copyright or © or Copr. CNRS, (January 13, 2009)
10 
11  This software is a computer program whose purpose is to provide classes
12  for numerical calculus.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 
42 #include "../Numeric/NumConstants.h"
43 #include "../Text/KeyvalTools.h"
44 #include "ApplicationTools.h"
46 
47 using namespace bpp;
48 using namespace std;
49 
50 vector<int> NumCalcApplicationTools::seqFromString(const std::string& s, const std::string& delim, const std::string& seqdelim)
51 {
52  vector<int> seq;
53  unique_ptr<StringTokenizer> st(new StringTokenizer(s, delim, true));
54  while (st->hasMoreToken())
55  {
56  unique_ptr<StringTokenizer> st2(new StringTokenizer(st->nextToken(), seqdelim, true));
57 
58  if (st2->numberOfRemainingTokens() > 1)
59  {
60  vector<int> tmp = VectorTools::seq(TextTools::toInt(st2->getToken(0)), TextTools::toInt(st2->getToken(1)), 1);
61  VectorTools::append(seq, tmp);
62  }
63  else
64  {
65  seq.push_back(TextTools::toInt(st2->getToken(0)));
66  }
67  }
68  return seq;
69 }
70 
71 
72 vector<double> NumCalcApplicationTools::getVector(const std::string& desc)
73 {
74  vector<double> values;
75  string key, val;
76 
77  if (desc.substr(0, 3) == "seq") // Bounds specified as sequence
78  {
79  map<string, string> keyvals;
80  KeyvalTools::multipleKeyvals(desc.substr(4, desc.size() - 5), keyvals);
81  if (keyvals.find("from") == keyvals.end())
82  throw Exception("Unvalid sequence specification, missing 'from' key: " + desc.substr(3, desc.size() - 5));
83  if (keyvals.find("to") == keyvals.end())
84  throw Exception("Unvalid sequence specification, missing 'to' key: " + desc.substr(3, desc.size() - 5));
85  if (keyvals.find("step") == keyvals.end() && keyvals.find("size") == keyvals.end())
86  throw Exception("Unvalid sequence specification, missing 'step' or 'size' key: " + desc.substr(3, desc.size() - 5));
87  double start = TextTools::toDouble(keyvals["from"]);
88  double end = TextTools::toDouble(keyvals["to"]);
89  if (keyvals.find("step") != keyvals.end())
90  {
91  double step = TextTools::toDouble(keyvals["step"]);
92  for (double x = start; x <= end + NumConstants::TINY(); x += step)
93  {
94  values.push_back(x);
95  }
96  }
97  else
98  {
99  int size = TextTools::toInt(keyvals["size"]);
100  double step = (end - start) / (double)size;
101  for (int i = 0; i < size - 1; i++)
102  {
103  values.push_back(start + i * step);
104  }
105  values.push_back(end); // for rounding purpose.
106  }
107  }
108  else // Direct enumaration of values
109  {
110  StringTokenizer st(desc, ",");
111  while (st.hasMoreToken())
112  values.push_back(TextTools::toDouble(st.nextToken()));
113  }
114  return values;
115 }
116 
117 
118 double NumCalcApplicationTools::getDefaultValue(const ParameterList& pl, const std::string& name, double x)
119 {
120  for (unsigned int i = 0; i < pl.size(); i++)
121  {
122  const Parameter& p = pl[i];
123  if (p.getName() == name)
124  return p.getValue();
125  }
126  return x;
127 }
128 
129 
131  map<string, string>& params,
132  const string& suffix,
133  bool suffixIsOptional,
134  bool warn)
135 {
136  unsigned int nbParams = ApplicationTools::getParameter<unsigned int>("grid.number_of_parameters", params, 1, suffix, suffixIsOptional, warn);
137  ParameterGrid* grid = new ParameterGrid();
138  for (unsigned int i = 0; i < nbParams; i++)
139  {
140  string name = ApplicationTools::getStringParameter("grid.parameter" + TextTools::toString(i + 1) + ".name", params, "", suffix, suffixIsOptional, warn);
141  vector<double> values = getVector(ApplicationTools::getStringParameter("grid.parameter" + TextTools::toString(i + 1) + ".values", params, "", suffix, suffixIsOptional, warn));
142  grid->addDimension(name, values);
143  }
144  return grid;
145 }
static std::string getStringParameter(const std::string &parameterName, const std::map< std::string, std::string > &params, const std::string &defaultValue, const std::string &suffix="", bool suffixIsOptional=true, int warn=0)
Get a string parameter.
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
static void multipleKeyvals(const std::string &desc, std::map< std::string, std::string > &keyvals, const std::string &split=",", bool nested=true)
Split a string into several keys and corresponding values (General purpose function).
Definition: KeyvalTools.cpp:60
static std::vector< int > seqFromString(const std::string &s, const std::string &delim=",", const std::string &seqdelim="-")
Build a vector of integers as described by a string.
static double getDefaultValue(const ParameterList &pl, const std::string &name, double x)
Returns the value of the Parameter of the given name if it exists; otherwise returns the default valu...
static ParameterGrid * getParameterGrid(std::map< std::string, std::string > &params, const std::string &suffix="", bool suffixIsOptional=true, bool warn=true)
Design a parameter grid from input options.
static std::vector< double > getVector(const std::string &desc)
Build a vector of double from a structured text description.
static double TINY()
Definition: NumConstants.h:82
This class is a data structure to specify a set of parameter values (most likely for evaluation by a ...
Definition: FunctionTools.h:56
void addDimension(const std::string &name, const Vdouble &values)
Add a new dimension (parameter name + corresponding values).
The parameter list object.
Definition: ParameterList.h:65
size_t size() const
Definition: ParameterList.h:92
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:218
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:211
A tokenizer for strings.
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
bool hasMoreToken() const
Tell if some tokens are still available.
static void append(std::vector< T > &vec1, const std::vector< T > &vec2)
Append the content of a std::vector to another one.
Definition: VectorTools.h:1938
static std::vector< T > seq(T from, T to, T by)
Build a sequence std::vector.
Definition: VectorTools.h:414
int toInt(const std::string &s, char scientificNotation)
Convert from string to int.
Definition: TextTools.cpp:246
double toDouble(const std::string &s, char dec, char scientificNotation)
Convert from string to double.
Definition: TextTools.cpp:255
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153