MyPyModule.cc
Go to the documentation of this file.
1 #include "MyPyModule.h"
2 
3 #include <fwk/CentralConfig.h>
4 #include <utl/ErrorLogger.h>
5 #include <utl/Reader.h>
6 #include <evt/Event.h>
7 
8 // everything needed for embedding
9 #include <Eigen/Core>
10 #include <pybind11/embed.h> // everything needed for embedding
11 #include <pybind11/eigen.h> // required for auto-conversion of eigen matrix
12 
13 #include <iostream>
14 
15 using namespace std;
16 using namespace utl;
17 using namespace fwk;
18 
19 using Mat = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>;
20 
21 
22 namespace MyPyModuleNS {
23 
26  {
27  INFO("Loading XML-config");
28  CentralConfig& cc = *CentralConfig::GetInstance();
29 
30  Branch topB = cc.GetTopBranch("MyPyModule");
31 
32  topB.GetChild("infoLevel").GetData(fInfoLevel);
33  topB.GetChild("filename").GetData(fFilename);
34 
35  INFO("Initialize the python interpreter.");
36  pybind11::initialize_interpreter();
37 
38  INFO("Initialize main module and main class.");
39  fModule = pybind11::module_::import(fFilename.c_str());
40  fMainClass = fModule.attr("Dog")();
41 
42  return eSuccess;
43  }
44 
45 
47  MyPyModule::Run(evt::Event& event)
48  {
49  // (0) prepare data
50  typedef Eigen::Matrix<float, 10, 10> M;
51  auto m1 = M::Zero();
52  auto m2 = M::Zero();
53 
54  // (1) prepare data
55  fMainClass.attr("bark")("It works!");
56  fMainClass.attr("woof")();
57 
58  pybind11::tuple args = pybind11::make_tuple(m1, m2);
59  pybind11::object result_py = fMainClass.attr("fetch_neural_network_result")(args);
60 
61  const auto result = result_py.cast<Mat>();
62 
63  cout << "result has the values " << result << "\n"
64  "result is of size " << result.rows() << 'x' << result.cols() << std::endl;
65 
66  return eSuccess;
67  }
68 
69 
71  MyPyModule::Finish()
72  {
73  // Put any termination or cleanup code here.
74  // This method is called once at the end of the run.
75 
76  INFO("Finalize the python interpreter.");
77 
78  /* We have to do this here to not run into memory leaks due to
79  still living (py-)objects before the interpreter shuts down. */
80 
81  fMainClass.dec_ref();
82  fModule.dec_ref();
83 
84  pybind11::finalize_interpreter();
85 
86  return eSuccess;
87  }
88 
89 }
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Mat
Definition: MyPyModule.cc:19
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
void Init()
Initialise the registry.
constexpr double m2
Definition: AugerUnits.h:122
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
Class representing a document branch.
Definition: Branch.h:107
const Data result[]
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
Main configuration utility.
Definition: CentralConfig.h:51
utl::Branch GetTopBranch(const std::string &id)
Get top branch for moduleConfigLink with given id (XML files)

, generated on Tue Sep 26 2023.