bpp-core3  3.0.0
FunctionTools.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 "../../App/ApplicationTools.h"
6 #include "FunctionTools.h"
7 
8 using namespace bpp;
9 
10 // From the STL;
11 #include <algorithm>
12 using namespace std;
13 
14 void ParameterGrid::addDimension(const std::string& name, const Vdouble& values)
15 {
16  if (find(names_.begin(), names_.end(), name) != names_.end())
17  throw Exception("ParameterGrid::addDimension(). A dimension with name '" + name + "' already exists in the grid.");
18  if (values.size() == 0)
19  throw Exception("ParameterGrid::addDimension(). Empty vector given! The dimension should at least contain one point.");
20  names_.push_back(name);
21  grid_.push_back(values);
22 }
23 
24 const Vdouble& ParameterGrid::getPointsForDimension(const std::string& name) const
25 {
26  for (unsigned int i = 0; i < names_.size(); i++)
27  {
28  if (names_[i] == name)
29  return grid_[i];
30  }
31  throw Exception("ParameterGrid::getPointsForDimension(). No dimension with name '" + name + "' was found in the grid.");
32 }
33 
34 const Vdouble& ParameterGrid::getPointsForDimension(unsigned int i) const
35 {
36  if (i >= names_.size())
37  throw IndexOutOfBoundsException("ParameterGrid::getPointsForDimension().", i, 0, names_.size() - 1);
38  return grid_[i];
39 }
40 
42 {
43  if (grid_.size() == 0)
44  return 0;
45  size_t n = 1;
46  for (size_t i = 0; i < grid_.size(); i++)
47  {
48  n *= grid_[i].size();
49  }
50  return n;
51 }
52 
53 shared_ptr<DataTable> FunctionTools::computeGrid(
54  FunctionInterface& function,
55  const ParameterGrid& grid)
56 {
57  // Init stuff...
58  size_t n = grid.getNumberOfDimensions();
59  if (n == 0)
60  return make_shared<DataTable>(0);
61  ; // Empty data table returned.
62 
63  VVdouble points = grid.getPoints();
64 
65  // Get the parameter list. this may throw an exception if the grid does not
66  // match the function parameters...
67  auto parNames = grid.getDimensionNames();
68  ParameterList pl = function.getParameters().createSubList(parNames);
69  auto colNames = parNames;
70 
71  colNames.push_back("value");
72  auto data = make_shared<DataTable>(colNames);
73 
74  for (unsigned int i = 0; i < n; i++)
75  {
77  }
78 
79  // Iterate over all dimensions:
80  unsigned int currentDimension = 0;
81  vector<unsigned int> currentPointInDimension(n);
82  vector<string> row(n + 1);
83  size_t nbPoints = grid.getTotalNumberOfPoints();
84  ApplicationTools::displayMessage("Computing likelihood profile...");
85  for (unsigned int i = 0; true; ++i)
86  {
87  ApplicationTools::displayGauge(i, nbPoints - 1, '=');
88  // We start by adding the current point to the table:
89  for (unsigned int j = 0; j < n; ++j)
90  {
91  row[j] = TextTools::toString(pl[j].getValue());
92  }
93  row[n] = TextTools::toString(function.f(pl),12);
94  data->addRow(row);
95 
96  // Now increment iterator:
97  bool dimensionChanged = false;
98  while (currentDimension < n && currentPointInDimension[currentDimension] == points[currentDimension].size() - 1)
99  {
100  currentDimension++;
101  dimensionChanged = true;
102  }
103  // Stopping condition:
104  if (currentDimension == n)
105  break;
106 
107  currentPointInDimension[currentDimension]++;
108  if (dimensionChanged)
109  {
110  for (unsigned int j = 0; j < currentDimension; j++)
111  {
112  currentPointInDimension[j] = 0;
113  }
114  currentDimension = 0;
115  }
116 
117  // Set the new parameter value:
118  for (unsigned int j = 0; j < points.size(); j++)
119  {
120  pl.setParameterValue(grid.getDimensionName(j), points[j][currentPointInDimension[j]]);
121  }
122  }
124  // and we are done:
125  return data;
126 }
const std::string & getDimensionName(unsigned int i) const
Definition: FunctionTools.h:42
virtual ParameterList createSubList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
STL namespace.
This is the function abstract class.
Definition: Functions.h:52
virtual void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
The parameter list object.
Definition: ParameterList.h:27
static std::shared_ptr< DataTable > computeGrid(FunctionInterface &function, const ParameterGrid &grid)
Evaluates a function on all points in a given grid.
This class is a data structure to specify a set of parameter values (most likely for evaluation by a ...
Definition: FunctionTools.h:20
size_t getTotalNumberOfPoints() const
const VVdouble & getPoints() const
Definition: FunctionTools.h:55
std::vector< double > Vdouble
Definition: VectorTools.h:34
static void displayMessage(const std::string &text)
Print a message.
size_t getNumberOfDimensions() const
Definition: FunctionTools.h:48
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
static void displayGauge(size_t iter, size_t total, char symbol='>', const std::string &mes="")
Display a gauge.
Index out of bounds exception class.
Definition: Exceptions.h:131
const Vdouble & getPointsForDimension(unsigned int i) const
void addDimension(const std::string &name, const Vdouble &values)
Add a new dimension (parameter name + corresponding values).
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:115
std::vector< Vdouble > VVdouble
Definition: VectorTools.h:35
const std::vector< std::string > & getDimensionNames() const
Definition: FunctionTools.h:40