bpp-core3  3.0.0
NumCalcApplicationTools.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "../Numeric/NumConstants.h"
6 #include "../Text/KeyvalTools.h"
7 #include "ApplicationTools.h"
9 
10 using namespace bpp;
11 using namespace std;
12 
13 vector<int> NumCalcApplicationTools::seqFromString(const std::string& s, const std::string& delim, const std::string& seqdelim)
14 {
15  vector<int> seq;
16  unique_ptr<StringTokenizer> st(new StringTokenizer(s, delim, true));
17  while (st->hasMoreToken())
18  {
19  unique_ptr<StringTokenizer> st2(new StringTokenizer(st->nextToken(), seqdelim, true));
20 
21  if (st2->numberOfRemainingTokens() > 1)
22  {
23  vector<int> tmp = VectorTools::seq(TextTools::toInt(st2->getToken(0)), TextTools::toInt(st2->getToken(1)), 1);
24  VectorTools::append(seq, tmp);
25  }
26  else
27  {
28  seq.push_back(TextTools::toInt(st2->getToken(0)));
29  }
30  }
31  return seq;
32 }
33 
34 
35 vector<double> NumCalcApplicationTools::getVector(const std::string& desc)
36 {
37  vector<double> values;
38  string key, val;
39 
40  if (desc.substr(0, 3) == "seq") // Bounds specified as sequence
41  {
42  map<string, string> keyvals;
43  KeyvalTools::multipleKeyvals(desc.substr(4, desc.size() - 5), keyvals);
44  if (keyvals.find("from") == keyvals.end())
45  throw Exception("Unvalid sequence specification, missing 'from' key: " + desc.substr(3, desc.size() - 5));
46  if (keyvals.find("to") == keyvals.end())
47  throw Exception("Unvalid sequence specification, missing 'to' key: " + desc.substr(3, desc.size() - 5));
48  if (keyvals.find("step") == keyvals.end() && keyvals.find("size") == keyvals.end())
49  throw Exception("Unvalid sequence specification, missing 'step' or 'size' key: " + desc.substr(3, desc.size() - 5));
50 
51  const short ONE=0;
52  const short LOG=1;
53  const short EXP=2;
54  const short DIX=3;
55 
56  short scale=ONE;
57 
58  if (keyvals.find("scale") != keyvals.end())
59  {
60  string sc=keyvals["scale"];
61  if (sc == "log")
62  scale=LOG;
63  else {
64  if (sc == "exp")
65  scale=EXP;
66  else {
67  if (sc == "10^")
68  scale = DIX;
69  else
70  throw Exception("Unknown scale " + sc + " for vector. Ask developpers.");
71  }
72  }
73  }
74 
75  double start = TextTools::toDouble(keyvals["from"]);
76  double end = TextTools::toDouble(keyvals["to"]);
77  if (keyvals.find("step") != keyvals.end())
78  {
79  double step = TextTools::toDouble(keyvals["step"]);
80  for (double x = start; x <= end + NumConstants::TINY(); x += step)
81  {
82  double y;
83  switch (scale)
84  {
85  case ONE:
86  y=x;
87  break;
88  case LOG:
89  y=log(x);
90  break;
91  case EXP:
92  y=exp(x);
93  break;
94  case DIX:
95  y=pow(10,x);
96  break;
97  default:
98  y=x;
99  }
100  values.push_back(y);
101  }
102  }
103  else
104  {
105  int size = TextTools::toInt(keyvals["size"]);
106  double step = (end - start) / (double)size;
107  for (int i = 0; i < size - 1; i++)
108  {
109  double x = start + i * step;
110  double y;
111  switch (scale)
112  {
113  case ONE:
114  y=x;
115  break;
116  case LOG:
117  y=log(x);
118  break;
119  case EXP:
120  y=exp(x);
121  break;
122  case DIX:
123  y=pow(10,x);
124  break;
125  default:
126  y=x;
127  }
128  values.push_back(y);
129  }
130  double y;
131  switch (scale)
132  {
133  case ONE:
134  y=end;
135  break;
136  case LOG:
137  y=log(end);
138  break;
139  case EXP:
140  y=exp(end);
141  break;
142  case DIX:
143  y=pow(10,end);
144  break;
145  default:
146  y=end;
147  }
148  values.push_back(y); // for rounding purpose.
149  }
150  }
151  else // Direct enumaration of values
152  {
153  StringTokenizer st(desc, ",");
154  while (st.hasMoreToken())
155  values.push_back(TextTools::toDouble(st.nextToken()));
156  }
157  return values;
158 }
159 
160 
161 double NumCalcApplicationTools::getDefaultValue(const ParameterList& pl, const std::string& name, double x)
162 {
163  for (unsigned int i = 0; i < pl.size(); i++)
164  {
165  const Parameter& p = pl[i];
166  if (p.getName() == name)
167  return p.getValue();
168  }
169  return x;
170 }
171 
172 
174  map<string, string>& params,
175  const string& suffix,
176  bool suffixIsOptional,
177  bool warn)
178 {
180  ApplicationTools::displayMessage("ParameterGrid");
181  unsigned int nbParams = ApplicationTools::getParameter<unsigned int>("grid.number_of_parameters", params, 1, suffix, suffixIsOptional, warn);
182  auto grid = std::make_shared<ParameterGrid>();
183  for (unsigned int i = 0; i < nbParams; i++)
184  {
185  string name = ApplicationTools::getStringParameter("grid.parameter" + TextTools::toString(i + 1) + ".name", params, "", suffix, suffixIsOptional, warn);
186  vector<double> values = getVector(ApplicationTools::getStringParameter("grid.parameter" + TextTools::toString(i + 1) + ".values", params, "", suffix, suffixIsOptional, warn));
187  grid->addDimension(name, values);
188  IntervalConstraint bb(VectorTools::min(values),VectorTools::max(values),true,true);
190  }
191  return grid;
192 }
A tokenizer for strings.
static std::vector< double > getVector(const std::string &desc)
Build a vector of double from a structured text description.
double toDouble(const std::string &s, char dec, char scientificNotation)
Convert from string to double.
Definition: TextTools.cpp:217
static T max(const std::vector< T > &v)
Template function to get the maximum value of a std::vector.
Definition: VectorTools.h:1111
An interval, either bounded or not, which can also have infinite bounds.
Definition: Constraints.h:101
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 displayResult(const std::string &text, const T &result)
Print a result message.
size_t size() const
Definition: ParameterList.h:56
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
static std::shared_ptr< 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 void append(std::vector< T > &vec1, const std::vector< T > &vec2)
Append the content of a std::vector to another one.
Definition: VectorTools.h:1899
The parameter list object.
Definition: ParameterList.h:27
const std::string & getToken(size_t pos) const
Get a particular token.
size_t numberOfRemainingTokens() const
Tell how many tokens are available.
static double TINY()
Definition: NumConstants.h:46
static T min(const std::vector< T > &v)
Template function to get the minimum value of a std::vector.
Definition: VectorTools.h:1090
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...
std::string getDescription() const override
Give a short description on the type of constraint.
Definition: Constraints.h:234
static void displayMessage(const std::string &text)
Print a message.
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:174
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
int toInt(const std::string &s, char scientificNotation)
Convert from string to int.
Definition: TextTools.cpp:208
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.
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:181
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:23
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:115
static std::vector< T > seq(T from, T to, T by)
Build a sequence std::vector.
Definition: VectorTools.h:377
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.