MaCh3  2.2.3
Reference Guide
parameters.cpp
Go to the documentation of this file.
1 // pybind includes
2 #include <pybind11/pybind11.h>
3 #include <pybind11/stl.h>
4 #include <pybind11/numpy.h>
5 // MaCh3 includes
8 
9 namespace py = pybind11;
10 
13 public:
14  /* Inherit the constructors */
16 
17  /* Trampoline (need one for each virtual function) */
18  double GetLikelihood() override {
19  PYBIND11_OVERRIDE_NAME(
20  double, /* Return type */
21  ParameterHandlerBase, /* Parent class */
22  "get_likelihood", /* Name in python*/
23  GetLikelihood /* Name of function in C++ (must match Python name) */
24  );
25  }
26 
27  void ProposeStep() override {
28  PYBIND11_OVERRIDE_NAME(
29  void, /* Return type */
30  ParameterHandlerBase, /* Parent class */
31  "propose_step", /* Name in python*/
32  ProposeStep /* Name of function in C++ (must match Python name) */
33  );
34  }
35 };
36 
37 
38 void initParameters(py::module &m){
39 
40  auto m_parameters = m.def_submodule("parameters");
41  m_parameters.doc() =
42  "This is a Python binding of MaCh3s C++ parameters library.";
43 
44 
45  // Bind the systematic type enum that lets us set different types of systematics
46  py::enum_<SystType>(m_parameters, "SystematicType")
47  .value("Normalisation", SystType::kNorm)
48  .value("Spline", SystType::kSpline)
49  .value("Functional", SystType::kFunc)
50  .value("N_Systematic_Types", SystType::kSystTypes);
51 
52 
53  py::class_<ParameterHandlerBase, PyParameterHandlerBase /* <--- trampoline*/>(m_parameters, "ParameterHandlerBase")
54  .def(
55  py::init<const std::vector<std::string>&, const char *, double, int, int>(),
56  "Construct a parameters object from a set of yaml files that define the systematic parameters \n\
57  :param yaml_files: The name of the yaml file to initialise from. \n\
58  :param name: the name of this ParameterHandler object. \n\
59  :param threshold: threshold PCA threshold from 0 to 1. Default is -1 and means no PCA. \n\
60  :param first_PCA_par: FirstPCAdpar First PCA parameter that will be decomposed. \n\
61  :param last_PCA_par: LastPCAdpar First PCA parameter that will be decomposed.",
62  py::arg("yaml_files"),
63  py::arg("name"),
64  py::arg("threshold") = -1.0,
65  py::arg("firs_PCA_par") = -999,
66  py::arg("last_PCA_par") = -999
67  )
68 
69  .def(
70  "calculate_likelihood",
72  "Calculate penalty term based on inverted covariance matrix."
73  )
74 
75  .def(
76  "throw_par_prop",
78  "Throw the proposed parameter by magnitude *mag* X sigma. \n\
79  :param mag: This value multiplied by the prior value of each parameter will be the width of the distribution that the parameter values are drawn from. ",
80  py::arg("mag") = 1.0
81  )
82 
83  .def(
84  "get_internal_par_name",
85  [](ParameterHandlerBase &self, int index)
86  {
87  // do this to disambiguate between the std::string and const char* version of this fn
88  std::string ret;
89  ret = self.GetParName(index);
90  return ret;
91  },
92  "Get the internally used name of this parameter. \n\
93  :param index: The global index of the parameter",
94  py::arg("index")
95  )
96 
97  .def(
98  "get_fancy_par_name",
99  [](ParameterHandlerBase &self, int index)
100  {
101  // do this to disambiguate between the std::string and const char* version of this fn
102  std::string ret;
103  ret = self.GetParFancyName(index);
104  return ret;
105  },
106  "Get the name of this parameter. \n\
107  :param index: The global index of the parameter",
108  py::arg("index")
109  )
110 
111  .def(
112  "get_n_pars",
114  "Get the number of parameters that this ParameterHandler object knows about."
115  )
116 
117  .def(
118  "propose_step",
120  "Propose a step based on the covariances. Also feel free to overwrite if you want something more funky."
121  )
122 
123  .def(
124  "get_proposal_array",
125  [](ParameterHandlerBase &self)
126  {
127  return py::memoryview::from_buffer<double>(
128  self.GetParPropVec().data(), // the data pointer
129  {self.GetNParameters()}, // shape
130  {sizeof(double)} // shape
131  );
132  },
133  "Bind a python array to the parameter proposal values for this ParameterHandler object. \n\
134  This allows you to set e.g. a numpy array to 'track' the parameter proposal values. You could either use this to directly set the proposals, or to just read the values proposed by e.g. throw_par_prop() \n\
135  :warning: This should be set *AFTER* all of the parameters have been read in from the config file as it resizes the array to fit the number of parameters. \n\
136  :param array: This is the array that will be set. Size and contents don't matter as it will be changed to fit the parameters. "
137  )
138 
139 
140  ; // End of ParameterHandlerBase binding
141 
142 
143  py::class_<ParameterHandlerGeneric, ParameterHandlerBase /* <--- trampoline*/>(m_parameters, "ParameterHandlerGeneric")
144  .def(
145  py::init<const std::vector<std::string>&, const char *, double, int, int>(),
146  "Construct a systematic ParameterHandler object from a set of yaml files that define the systematic parameters \n\
147  :param yaml_files: The name of the yaml file to initialise from. \n\
148  :param name: the name of this ParameterHandler object. \n\
149  :param threshold: threshold PCA threshold from 0 to 1. Default is -1 and means no PCA. \n\
150  :param first_PCA_par: FirstPCAdpar First PCA parameter that will be decomposed. \n\
151  :param last_PCA_par: LastPCAdpar First PCA parameter that will be decomposed.",
152  py::arg("yaml_files"),
153  py::arg("name") = "xsec_cov",
154  py::arg("threshold") = -1.0,
155  py::arg("firs_PCA_par") = -999,
156  py::arg("last_PCA_par") = -999
157  )
158 
159  .def(
160  "get_par_type",
162  "Get what type of systematic this parameters is (see :py:class:`pyMaCh3.m_parameters.SystematicType` for possible types). \n\
163  :param index: The global index of the parameter",
164  py::arg("index")
165  )
166 
167  .def(
168  "get_par_spline_type",
170  "Get what type of spline this parameter is set to use (assuming that it is a spline type parameter). \n\
171  :param index: The index of the spline parameter",
172  py::arg("index")
173  )
174 
175  .def(
176  "get_par_spline_name",
178  "Get the name of the spline associated with a spline parameter. This is generally what it is called in input spline files and can in principle be different to the parameters name. \n\
179  :param index: The index of the spline parameter",
180  py::arg("index")
181  )
182 
183  ; // End of ParameterHandlerGeneric binding
184 }
@ kNorm
For normalisation parameters.
@ kSpline
For splined parameters (1D)
@ kSystTypes
This only enumerates.
@ kFunc
For functional parameters.
Base class responsible for handling of systematic error parameters. Capable of using PCA or using ada...
virtual void ProposeStep()
Generate a new proposed state.
void ThrowParProp(const double mag=1.)
Throw the proposed parameter by mag sigma. Should really just have the user specify this throw by hav...
double CalcLikelihood() const _noexcept_
Calc penalty term based on inverted covariance matrix.
ParameterHandlerBase(const std::vector< std::string > &YAMLFile, std::string name, double threshold=-1, int FirstPCAdpar=-999, int LastPCAdpar=-999)
ETA - constructor for a YAML file.
Class responsible for handling of systematic error parameters with different types defined in the con...
SplineInterpolation GetParSplineInterpolation(const int i) const
Get interpolation type for a given parameter.
EW: As ParameterHandlerBase is an abstract base class we have to do some gymnastics to get it to get ...
Definition: parameters.cpp:12
double GetLikelihood() override
Return CalcLikelihood if some params were thrown out of boundary return LARGE_LOGL
Definition: parameters.cpp:18
void ProposeStep() override
Generate a new proposed state.
Definition: parameters.cpp:27
SystType GetParamType(const int i) const
Returns enum describing our param type.
int GetNParameters() const
Get number of params which will be different depending if using Eigen decomposition or not.
std::string GetParSplineName(const int i) const
Get the name of the spline associated with the spline at index i.
void initParameters(py::module &m)
Definition: parameters.cpp:38