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