MaCh3  2.8.0
Reference Guide
Classes | Functions
parameters.h File Reference
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include "Parameters/ParameterHandlerBase.h"
#include "Parameters/ParameterHandlerGeneric.h"
Include dependency graph for parameters.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  PyParameterHandlerBase
 EW: As ParameterHandlerBase is an abstract base class we have to do some gymnastics to get it to get it into python. More...
 

Functions

void initParametersModule (py::module &m_parameters)
 

Detailed Description

Author
Ewan Miller

Definition in file parameters.h.

Function Documentation

◆ initParametersModule()

void initParametersModule ( py::module &  m_parameters)

Definition at line 43 of file parameters.h.

43  {
44  // Bind the systematic type enum that lets us set different types of systematics
45  py::enum_<SystType>(m_parameters, "SystematicType")
46  .value("Normalisation", SystType::kNorm)
47  .value("Spline", SystType::kSpline)
48  .value("Functional", SystType::kFunc)
49  .value("N_Systematic_Types", SystType::kSystTypes);
50 
51  py::class_<ParameterHandlerBase, PyParameterHandlerBase /* <--- trampoline*/>(m_parameters, "ParameterHandlerBase")
52  .def(
53  py::init<std::string &, std::string, M3::float_t, int, int>(),
54  "Construct a parameters object from a set of yaml files that define the systematic parameters \n\
55  :param name: the name of this ParameterHandler object. \n\
56  :param file: The name of the yaml file to initialise from. \n\
57  :param threshold: threshold PCA threshold from 0 to 1. Default is -1 and means no PCA. \n\
58  :param first_PCA_par: FirstPCAdpar First PCA parameter that will be decomposed. \n\
59  :param last_PCA_par: LastPCAdpar First PCA parameter that will be decomposed.",
60  py::arg("name"),
61  py::arg("file"),
62  py::arg("threshold") = -1.0,
63  py::arg("firs_PCA_par") = -999,
64  py::arg("last_PCA_par") = -999)
65 
66  .def(
67  "calculate_likelihood",
69  "Calculate penalty term based on inverted covariance matrix.")
70 
71  .def(
72  "get_internal_par_name",
73  [](ParameterHandlerBase &self, int index)
74  {
75  // do this to disambiguate between the std::string and const char* version of this fn
76  std::string ret;
77  ret = self.GetParName(index);
78  return ret;
79  },
80  "Get the internally used name of this parameter. \n\
81  :param index: The global index of the parameter",
82  py::arg("index"))
83 
84  .def(
85  "get_fancy_par_name",
86  [](ParameterHandlerBase &self, int index)
87  {
88  // do this to disambiguate between the std::string and const char* version of this fn
89  std::string ret;
90  ret = self.GetParFancyName(index);
91  return ret;
92  },
93  "Get the name of this parameter. \n\
94  :param index: The global index of the parameter",
95  py::arg("index"))
96 
97  .def(
98  "get_n_pars",
100  "Get the number of parameters that this ParameterHandler object knows about.")
101 
102  .def(
103  "propose_step",
105  "Propose a step based on the covariances. Also feel free to overwrite if you want something more funky.")
106 
107  .def(
108  "get_proposal_array",
109  [](ParameterHandlerBase &self)
110  {
111  // Get the number of parameters
112  size_t n_pars = self.GetNParameters();
113 
114  // Get pointer to the data
115  const M3::float_t* data_ptr = self.GetParPropVec().data();
116 
117  // Create a numpy array that copies the data
118  // This ensures the numpy array owns its data and won't have lifetime issues
119  py::array_t<M3::float_t> result(n_pars);
120  auto buf = result.request();
121  M3::float_t* result_ptr = static_cast<M3::float_t*>(buf.ptr);
122 
123  // Copy the data
124  std::memcpy(result_ptr, data_ptr, n_pars * sizeof(M3::float_t));
125 
126  return result;
127  },
128  "Get the parameter proposal values as a numpy array. \n\
129  This returns a copy of the current proposal values. \n\
130  :return: A numpy array containing the proposal values for all parameters.")
131 
132  .def("set_parameters", [](ParameterHandlerBase &self, py::object pars_obj = py::none())
133  {
134  if (pars_obj.is_none()) {
135  self.SetParameters();
136  } else {
137  // This handles both numpy arrays and Python lists
138  std::vector<double> pars_vec = pars_obj.cast<std::vector<double>>();
139  self.SetParameters(pars_vec);
140  } }, py::arg("pars") = py::none(),
141  R"pbdoc(
142  Set parameter values using array.
143 
144  Parameters
145  ----------
146  pars : numpy.ndarray or list of float, optional
147  Array holding new values for every parameter.
148  Must have same size as the number of parameters in the covariance class.
149  If not provided, parameters are set to their pre-fit values.
150 
151  Examples
152  --------
153  >>> import numpy as np
154  >>> handler.set_parameters(np.array([1.0, 2.0, 3.0]))
155  >>> handler.set_parameters([1.0, 2.0, 3.0])
156  >>> handler.set_parameters()
157  )pbdoc")
158 
159 
160  .def("get_par_init", &ParameterHandlerBase::GetParPreFit, py::arg("index"),
161  "Get initial value of parameter at index i\n\
162  :param index: index of the parameter")
163 
164  .def("get_lower_bound", &ParameterHandlerBase::GetLowerBound, py::arg("index"),
165  "Get the lower bound of parameter at index i. \n\
166  :param index: index of the parameter")
167 
168  .def("get_upper_bound", &ParameterHandlerBase::GetUpperBound, py::arg("index"),
169  "Get the upper bound of parameter at index i. \n\
170  :param index: index of the parameter")
171 
172  .def("get_flat_prior", &ParameterHandlerBase::GetFlatPrior, py::arg("index"),
173  "Is the parameter at index i flat?. \n\
174  :param index: index of the parameter")
175 
176  .def("get_par_error", &ParameterHandlerBase::GetError, py::arg("index"),
177  "The prior error on parameter at index i \n\
178  :param index: index of the parameter")
179 
180  .def("get_par_fixed", static_cast<bool (ParameterHandlerBase::*)(const int) const>(&ParameterHandlerBase::IsParameterFixed), py::arg("index"),
181  "Is the parameter at index i fixed \n\
182  :param index: index of the parameter")
183 
184  .def("get_prior_cov", [](ParameterHandlerBase &self)
185  {
186  auto mat = self.GetCovMatrix();
187  if (!mat){
188  throw std::runtime_error("TMatrixDSym pointer is null");
189  }
190  int n = mat->GetNrows();
191  const double *data = mat->GetMatrixArray();
192  py::array_t<float> result({n, n});
193  // Shove matrix into the array
194  std::transform(data, data + n * n,
195  result.mutable_data(),
196  [](double v)
197  { return static_cast<float>(v); });
198 
199  return result; },
200  "Get the prior covariance")
201 
202  ; // End of ParameterHandlerBase binding
203 
204  py::class_<ParameterHandlerGeneric, ParameterHandlerBase /* <--- trampoline*/>(m_parameters, "ParameterHandlerGeneric")
205  .def(
206  py::init<const std::vector<std::string>&, const char *, M3::float_t, int, int>(),
207  "Construct a systematic ParameterHandler object from a set of yaml files that define the systematic parameters \n\
208  :param yaml_files: The name of the yaml file to initialise from. \n\
209  :param name: the name of this ParameterHandler object. \n\
210  :param threshold: threshold PCA threshold from 0 to 1. Default is -1 and means no PCA. \n\
211  :param first_PCA_par: FirstPCAdpar First PCA parameter that will be decomposed. \n\
212  :param last_PCA_par: LastPCAdpar First PCA parameter that will be decomposed.",
213  py::arg("yaml_files"),
214  py::arg("name") = "xsec_cov",
215  py::arg("threshold") = -1.0,
216  py::arg("firs_PCA_par") = -999,
217  py::arg("last_PCA_par") = -999
218  )
219 
220  .def(
221  "get_par_type",
223  "Get what type of systematic this parameters is (see :py:class:`pyMaCh3.m_parameters.SystematicType` for possible types). \n\
224  :param index: The global index of the parameter",
225  py::arg("index")
226  )
227 
228  .def(
@ kNorm
For normalisation parameters.
@ kSpline
For splined parameters (1D)
@ kSystTypes
This only enumerates.
@ kFunc
For functional parameters.
Base class for handling systematic uncertainty parameters.
virtual void ProposeStep()
Generate a new proposed state.
double GetUpperBound(const int i) const
Get upper parameter bound in which it is physically valid.
int GetNParameters() const
Get number of params which will be different depending if using Eigen decomposition or not.
bool IsParameterFixed(const int i) const
Is parameter fixed or not.
double GetLowerBound(const int i) const
Get lower parameter bound in which it is physically valid.
double CalcLikelihood() const _noexcept_
Calc penalty term based on inverted covariance matrix.
bool GetFlatPrior(const int i) const
Get if param has flat prior or not.
double GetError(const int i) const
Get the error for the ith parameter.
double GetParPreFit(const int i) const
Get prior parameter value.
Class responsible for handling of systematic error parameters with different types defined in the con...
SystType GetParamType(const int i) const
Returns enum describing our param type.
EW: As ParameterHandlerBase is an abstract base class we have to do some gymnastics to get it to get ...
Definition: parameters.h:17
double float_t
Definition: Core.h:37