bpp-phyl3  3.0.0
Model.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_LIKELIHOOD_DATAFLOW_MODEL_H
6 #define BPP_PHYL_LIKELIHOOD_DATAFLOW_MODEL_H
7 
8 #include <Bpp/Exceptions.h>
12 #include <functional>
13 #include <unordered_map>
14 
15 #include "Definitions.h"
16 
17 namespace bpp
18 {
19 inline MatrixDimension transitionMatrixDimension (std::size_t nbState)
20 {
21  return {Eigen::Index (nbState), Eigen::Index (nbState)};
22 }
23 
25 {
26  return RowVectorDimension (Eigen::Index (nbState));
27 }
28 
29 
33 // class TransitionModelInterface;
34 
46  public Value<std::shared_ptr<BranchModelInterface>>,
48 {
49  // private:
50  // Context& context_;
51 
52 public:
55 
56  ConfiguredModel(Context& context, NodeRefVec&& deps, std::shared_ptr<BranchModelInterface>&& model);
57  virtual ~ConfiguredModel();
58 
59  ConfiguredModel* clone() const override
60  {
61  throw bpp::Exception("ConfiguredModel clone should not happen.");
62  }
63 
64  std::string description() const final;
65  std::string debugInfo() const final;
66 
67  std::string color() const final
68  {
69  return "red";
70  }
71 
72  bool compareAdditionalArguments(const Node_DF& other) const override;
73 
74  std::size_t hashAdditionalArguments () const override;
75 
78 
79  NodeRef recreate(Context& c, NodeRefVec&& deps) final;
80 
81  const ConfiguredParameter& getConfiguredParameter(const std::string& name) const
82  {
83  return static_cast<const ConfiguredParameter&>(parameter(name));
84  }
85 
86 private:
87  void compute() override
88  {
89  model_->matchParametersValues(getParameters());
90  }
91 
92  std::shared_ptr<BranchModelInterface> model_;
93 };
94 
103 class EquilibriumFrequenciesFromModel : public Value<Eigen::RowVectorXd>
104 {
105 public:
108  using T = Eigen::RowVectorXd;
109 
111 
112  std::string debugInfo () const final;
113 
114  bool compareAdditionalArguments (const Node_DF& other) const;
115 
116  NodeRef derive (Context& c, const Node_DF& node) final;
117  NodeRef recreate (Context& c, NodeRefVec&& deps) final;
118 
119  std::string color () const final
120  {
121  return "#ffff66";
122  }
123 
124 private:
125  void compute () final;
126 
128 };
129 
141 class TransitionMatrixFromModel : public Value<Eigen::MatrixXd>
142 {
143 public:
146  using T = Eigen::MatrixXd;
147 
148 private:
150 
151 public:
154 
155  std::string debugInfo () const final;
156 
157  bool compareAdditionalArguments (const Node_DF& other) const;
158 
159  NodeRef derive (Context& c, const Node_DF& node) final;
160  NodeRef recreate (Context& c, NodeRefVec&& deps) final;
161 
162  std::string color () const final
163  {
164  return "#aaff00";
165  }
166 
167  std::string description () const final
168  {
169  return "TransitionMatrix";
170  }
171 
172  std::string shape() const
173  {
174  return "octagon";
175  }
176 
177 private:
178  void compute () final;
179 };
180 
192 {
193 public:
197 
198 private:
200 
201 public:
204 
205  std::string debugInfo() const final;
206 
207  bool compareAdditionalArguments(const Node_DF& other) const;
208 
209  NodeRef derive(Context& c, const Node_DF& node) final;
210  NodeRef recreate(Context& c, NodeRefVec&& deps) final;
211 
212  std::string color () const final
213  {
214  return "#aaff00";
215  }
216 
217  std::string description () const final
218  {
219  return "TransitionFunction";
220  }
221 
222  std::string shape() const
223  {
224  return "octagon";
225  }
226 
227 private:
228  void compute() final;
229 
230 public:
231  static std::shared_ptr<Self> create(Context& c, NodeRefVec&& deps, const Dimension<T>& dim);
232 };
233 
242 class ProbabilitiesFromMixedModel : public Value<Eigen::RowVectorXd>
243 {
244 public:
247  using T = Eigen::RowVectorXd;
248 
250 
251  std::string debugInfo () const final;
252 
253  std::string color() const final
254  {
255  return "blue";
256  }
257 
258  bool compareAdditionalArguments (const Node_DF& other) const final;
259 
260  NodeRef derive (Context& c, const Node_DF& node) final;
261  NodeRef recreate (Context& c, NodeRefVec&& deps) final;
262 
263 private:
264  void compute () final;
265 
266  Dimension<T> nbClass_;
267 
268 public:
269  static std::shared_ptr<Self> create (Context& c, NodeRefVec&& deps);
270 };
271 
280 class ProbabilityFromMixedModel : public Value<double>
281 {
282 private:
283  size_t nCat_;
284 
285 public:
288  using T = double;
289 
290  ProbabilityFromMixedModel (NodeRefVec&& deps, size_t nCat);
291 
292  std::string debugInfo () const final;
293 
294  bool compareAdditionalArguments (const Node_DF& other) const final;
295 
296  NodeRef derive (Context& c, const Node_DF& node) final;
297  NodeRef recreate (Context& c, NodeRefVec&& deps) final;
298 
299  std::string color() const final
300  {
301  return "blue";
302  }
303 
304 private:
305  void compute () final;
306 
307 public:
308  static std::shared_ptr<Self> create (Context& c, NodeRefVec&& deps, size_t nCat);
309 };
310 } // namespace bpp
311 #endif // BPP_PHYL_LIKELIHOOD_DATAFLOW_MODEL_H
const Parameter & parameter(const std::string &name) const override
const ParameterList & getParameters() const override
Interface for all Branch models.
Likelihood transition model.
Definition: Model.h:48
ConfiguredModel * clone() const override
Definition: Model.h:59
virtual ~ConfiguredModel()
bool compareAdditionalArguments(const Node_DF &other) const override
Compare node-specific configuration to another.
Definition: Model.cpp:43
std::shared_ptr< BranchModelInterface > model_
Definition: Model.h:92
NumericalDerivativeConfiguration config
Configuration for numerical derivation of computation nodes using this Model.
Definition: Model.h:77
std::string debugInfo() const final
Node debug info (default = ""): user defined detailed info for DF graph debug.
Definition: Model.cpp:36
const ConfiguredParameter & getConfiguredParameter(const std::string &name) const
Definition: Model.h:81
std::size_t hashAdditionalArguments() const override
Return the hash of node-specific configuration.
Definition: Model.cpp:58
std::string description() const final
Node pretty name (default = type name).
Definition: Model.cpp:34
std::string color() const final
Definition: Model.h:67
ConfiguredModel(Context &context, NodeRefVec &&deps, std::shared_ptr< BranchModelInterface > &&model)
Definition: Model.cpp:16
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
Definition: Model.cpp:64
void compute() override
Computation implementation.
Definition: Model.h:87
Data flow node representing a parameter.
Definition: Parameter.h:27
Context for dataflow node construction.
Definition: DataFlow.h:527
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
Definition: Model.cpp:105
EquilibriumFrequenciesFromModel Self
Definition: Model.h:106
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
Definition: Model.cpp:90
EquilibriumFrequenciesFromModel(NodeRefVec &&deps, const Dimension< T > &dim)
Definition: Model.cpp:73
std::string color() const final
Definition: Model.h:119
bool compareAdditionalArguments(const Node_DF &other) const
Compare node-specific configuration to another.
Definition: Model.cpp:85
std::string debugInfo() const final
Node debug info (default = ""): user defined detailed info for DF graph debug.
Definition: Model.cpp:78
void compute() final
Computation implementation.
Definition: Model.cpp:110
Base dataflow Node class.
Definition: DataFlow.h:152
Eigen::RowVectorXd T
Definition: Model.h:247
Dimension< T > targetDimension_
Definition: Model.h:199
std::string shape() const
Definition: Model.h:222
bpp::TransitionFunction T
Definition: Model.h:196
std::string description() const final
Node pretty name (default = type name).
Definition: Model.h:217
std::string shape() const
Definition: Model.h:172
std::string description() const final
Node pretty name (default = type name).
Definition: Model.h:167
Dimension< T > targetDimension_
Definition: Model.h:149
Abstract Node storing a value of type T.
Definition: DataFlow.h:352
Defines the basic types of data flow nodes.
std::vector< NodeRef > NodeRefVec
Alias for a dependency vector (of NodeRef).
Definition: DataFlow.h:81
RowVectorDimension equilibriumFrequenciesDimension(std::size_t nbState)
Definition: Model.h:24
std::shared_ptr< Node_DF > NodeRef
Definition: DataFlow.h:78
std::function< VectorLik(const VectorLik &)> TransitionFunction
MatrixDimension transitionMatrixDimension(std::size_t nbState)
Definition: Model.h:19
Store a dimension for type T.
Basic matrix dimension type.
Configuration for a numerical derivation: what delta to use, and type of derivation.