bpp-phyl3  3.0.0
LikelihoodCalculation.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_LIKELIHOODCALCULATION_H
6 #define BPP_PHYL_LIKELIHOOD_DATAFLOW_LIKELIHOODCALCULATION_H
7 
9 #include <Eigen/Core>
10 
12 #include "Definitions.h"
13 
14 namespace bpp
15 {
22 {
23 private:
25 
26 protected:
27  /******************************************/
31 
32 public:
35  context_(context)
36  {}
37 
39  ParameterList& paramList) :
41  context_(context)
42  {
43  shareParameters_(paramList);
44  }
45 
46 
47  /*
48  * @brief Copy the likelihood calculation IN THE SAME CONTEXT.
49  *
50  */
51 
54  context_(lik.context_),
55  likelihood_()
56  {}
57 
59  {
60  throw bpp::Exception("LikelihoodCalculation clone should not happen.");
61  }
62 
64  {
65  if (!likelihood_)
67 
68  return likelihood_;
69  }
70 
71  virtual bool isInitialized() const
72  {
73  return likelihood_ != 0;
74  }
75 
76  const Context& getContext() const
77  {
78  return context_;
79  }
80 
81  /*
82  * @brief Return likelihood_ without any computation, used to build likelihood_.
83  *
84  */
86  {
87  likelihood_ = ll;
88  }
89 
90  virtual void cleanAllLikelihoods()
91  {
92  if (likelihood_)
94  likelihood_.reset();
95  }
96 
97 /*
98  * @brief fix Factor such that valRef value becomes normal.
99  *
100  */
101 
102 protected:
103  /* @brief Build the likelihood DF */
104 
105  virtual void makeLikelihoods()
106  {
107  throw Exception("LikelihoodCalculation:: makeLikelihoods should not be called. Probably likelihood_ is null.");
108  }
109 
111  {
112  return context_;
113  }
114 
116  {
117  return likelihood_;
118  }
119 };
120 
123 
125  public LikelihoodCalculation
126 {
127 public:
129  LikelihoodCalculation(context) {}
130 
133  {}
134 
135 
137  {
138  throw bpp::Exception("AlignedLikelihoodCalculation clone should not happen.");
139  }
140 
141 protected:
142  /******************************************/
145  /* For Data used for output (ie non shrunk if ever)
146  *
147  */
148 
150 
151  /* For Data used for computation (ie shrunked data if ever)
152  *
153  */
154 
156 
157 public:
158  /*
159  * @brief Return the ref to the Sitelikelihoods_ vector on data
160  * (shrunked or not).
161  *
162  * @brief shrunk: bool true if vector on shrunked data (default: false)
163  *
164  */
166  {
168  makeLikelihoods();
169 
170  if (shrunk && patternedSiteLikelihoods_)
172  else
173  return siteLikelihoods_;
174  }
175 
176  /*
177  * @brief Return the ref to the Sitelikelihoods_ vector on data
178  * (shrunked or not).
179  *
180  * @brief ll: Site Likelihoods
181  * @brief shrunk: given Likelihoods are on shrunked data (default false)
182  *
183  */
185  bool shrunk = false)
186  {
187  if (shrunk)
189  else
190  siteLikelihoods_ = ll;
191  }
192 
193  /*
194  * @brief Return Likelihood on a site
195  * @param pos : site position
196  * @param shrunk : on shrunked data (default false)
197  */
198  DataLik getLikelihoodForASite(size_t pos, bool shrunk = false)
199  {
201  makeLikelihoods();
202 
203  if (shrunk && patternedSiteLikelihoods_)
204  return patternedSiteLikelihoods_->targetValue()(Eigen::Index(pos));
205  else
206  return siteLikelihoods_->targetValue()(Eigen::Index(pos));
207  }
208 
209  DataLik getLogLikelihoodForASite(size_t pos, bool shrunk = false)
210  {
211  using namespace std;
212  return log(getLikelihoodForASite(pos, shrunk));
213  }
214 
222  {
223  auto vLik = getSiteLikelihoods(false)->targetValue();
224  VDataLik v;
225  copyEigenToBpp(vLik, v);
226  return v;
227  }
228 
229  /*
230  * @brief Clean all the existing likelihoods (ie remove them from
231  * Context), and all unique dependencies.
232  *
233  *
234  */
236  {
238 
239  if (siteLikelihoods_)
240  {
242  siteLikelihoods_.reset();
243  }
245  {
248  }
249  }
250 
251 protected:
252  virtual void makeLikelihoods()
253  {
254  throw Exception("AlignedLikelihoodCalculation:: makeLikelihoods should not be called. Probably both siteLikelihoods_ and patternedSiteLikelihoods_ are null.");
255  }
256 };
257 } // namespace bpp
258 #endif // BPP_PHYL_LIKELIHOOD_DATAFLOW_LIKELIHOODCALCULATION_H
void shareParameters_(const ParameterList &parameters)
SiteLikelihoodsRef getSiteLikelihoods(bool shrunk=false)
DataLik getLogLikelihoodForASite(size_t pos, bool shrunk=false)
VDataLik getLikelihoodPerSite()
Get the likelihood for each site.
void setSiteLikelihoods(SiteLikelihoodsRef ll, bool shrunk=false)
AlignedLikelihoodCalculation(const AlignedLikelihoodCalculation &lik)
AlignedLikelihoodCalculation * clone() const
DataLik getLikelihoodForASite(size_t pos, bool shrunk=false)
Context for dataflow node construction.
Definition: DataFlow.h:527
bool erase(const NodeRef &r)
Remove an element from the map, only if it has no dependent nodes, and likewise down the graph.
Definition: DataFlow.cpp:363
LikelihoodCalculation(Context &context)
virtual bool isInitialized() const
const Context & getContext() const
ValueRef< DataLik > getLikelihoodNode()
ValueRef< DataLik > getLikelihoodNode_()
LikelihoodCalculation(const LikelihoodCalculation &lik)
LikelihoodCalculation(Context &context, ParameterList &paramList)
LikelihoodCalculation * clone() const
void setLikelihoodNode(ValueRef< DataLik > ll)
Defines the basic types of data flow nodes.
double log(const ExtendedFloat &ef)
std::vector< DataLik > VDataLik
Definition: Definitions.h:23
std::shared_ptr< Value< T > > ValueRef
Shared pointer alias for Value<T>.
Definition: DataFlow.h:84
template void copyEigenToBpp(const ExtendedFloatMatrixXd &eigenMatrix, std::vector< std::vector< double >> &bppMatrix)
ValueRef< RowLik > SiteLikelihoodsRef