MaCh3  2.5.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 
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 *, M3::float_t, 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  "get_internal_par_name",
77  [](ParameterHandlerBase &self, int index)
78  {
79  // do this to disambiguate between the std::string and const char* version of this fn
80  std::string ret;
81  ret = self.GetParName(index);
82  return ret;
83  },
84  "Get the internally used name of this parameter. \n\
85  :param index: The global index of the parameter",
86  py::arg("index")
87  )
88 
89  .def(
90  "get_fancy_par_name",
91  [](ParameterHandlerBase &self, int index)
92  {
93  // do this to disambiguate between the std::string and const char* version of this fn
94  std::string ret;
95  ret = self.GetParFancyName(index);
96  return ret;
97  },
98  "Get the name of this parameter. \n\
99  :param index: The global index of the parameter",
100  py::arg("index")
101  )
102 
103  .def(
104  "get_n_pars",
106  "Get the number of parameters that this ParameterHandler object knows about."
107  )
108 
109  .def(
110  "propose_step",
112  "Propose a step based on the covariances. Also feel free to overwrite if you want something more funky."
113  )
114 
115  .def(
116  "get_proposal_array",
117  [](ParameterHandlerBase &self)
118  {
119  // Get the number of parameters
120  size_t n_pars = self.GetNParameters();
121 
122  // Get pointer to the data
123  const M3::float_t* data_ptr = self.GetParPropVec().data();
124 
125  // Create a numpy array that copies the data
126  // This ensures the numpy array owns its data and won't have lifetime issues
127  py::array_t<M3::float_t> result(n_pars);
128  auto buf = result.request();
129  M3::float_t* result_ptr = static_cast<M3::float_t*>(buf.ptr);
130 
131  // Copy the data
132  std::memcpy(result_ptr, data_ptr, n_pars * sizeof(M3::float_t));
133 
134  return result;
135  },
136  "Get the parameter proposal values as a numpy array. \n\
137  This returns a copy of the current proposal values. \n\
138  :return: A numpy array containing the proposal values for all parameters."
139  )
140 
141  .def("set_parameters",
142  [](ParameterHandlerBase& self, py::object pars_obj = py::none()) {
143  if (pars_obj.is_none()) {
144  self.SetParameters();
145  } else {
146  // This handles both numpy arrays and Python lists
147  std::vector<double> pars_vec = pars_obj.cast<std::vector<double>>();
148  self.SetParameters(pars_vec);
149  }
150  },
151  py::arg("pars") = py::none(),
152  R"pbdoc(
153  Set parameter values using array.
154 
155  Parameters
156  ----------
157  pars : numpy.ndarray or list of float, optional
158  Array holding new values for every parameter.
159  Must have same size as the number of parameters in the covariance class.
160  If not provided, parameters are set to their pre-fit values.
161 
162  Examples
163  --------
164  >>> import numpy as np
165  >>> handler.set_parameters(np.array([1.0, 2.0, 3.0]))
166  >>> handler.set_parameters([1.0, 2.0, 3.0])
167  >>> handler.set_parameters()
168  )pbdoc")
169 
170  ; // End of ParameterHandlerBase binding
171 
172 
173  py::class_<ParameterHandlerGeneric, ParameterHandlerBase /* <--- trampoline*/>(m_parameters, "ParameterHandlerGeneric")
174  .def(
175  py::init<const std::vector<std::string>&, const char *, M3::float_t, int, int>(),
176  "Construct a systematic ParameterHandler object from a set of yaml files that define the systematic parameters \n\
177  :param yaml_files: The name of the yaml file to initialise from. \n\
178  :param name: the name of this ParameterHandler object. \n\
179  :param threshold: threshold PCA threshold from 0 to 1. Default is -1 and means no PCA. \n\
180  :param first_PCA_par: FirstPCAdpar First PCA parameter that will be decomposed. \n\
181  :param last_PCA_par: LastPCAdpar First PCA parameter that will be decomposed.",
182  py::arg("yaml_files"),
183  py::arg("name") = "xsec_cov",
184  py::arg("threshold") = -1.0,
185  py::arg("firs_PCA_par") = -999,
186  py::arg("last_PCA_par") = -999
187  )
188 
189  .def(
190  "get_par_type",
192  "Get what type of systematic this parameters is (see :py:class:`pyMaCh3.m_parameters.SystematicType` for possible types). \n\
193  :param index: The global index of the parameter",
194  py::arg("index")
195  )
196 
197  .def(
198  "get_par_spline_type",
@ 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.
int GetNParameters() const
Get number of params which will be different depending if using Eigen decomposition or not.
double CalcLikelihood() const _noexcept_
Calc penalty term based on inverted covariance matrix.
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