HOPS
HOPS class reference
MHO_PyGenericOperator.hh
Go to the documentation of this file.
1 #ifndef MHO_PyGenericOperator_HH__
2 #define MHO_PyGenericOperator_HH__
3 
4 #include "MHO_Operator.hh"
5 #include "MHO_OperatorToolbox.hh"
6 
8 #include "MHO_FringeData.hh"
12 
13 #include "MHO_PyTableContainer.hh"
14 
15 #include <pybind11/numpy.h> //this is important to have for std::complex<T> support!
16 #include <pybind11/pybind11.h>
17 namespace py = pybind11;
18 
19 namespace hops
20 {
21 
35 {
36  public:
38  : fInitialized(false), fFringeData(nullptr), fFringeDataInterface(nullptr), fOperatorToolbox(nullptr)
39  {
40  fModulePath = "";
41  fFunctionName = "";
42  };
43 
44  virtual ~MHO_PyGenericOperator() { delete fFringeDataInterface; };
45 
46  void SetFringeData(MHO_FringeData* fdata) { fFringeData = fdata; }
47 
48  void SetOperatorToolbox(MHO_OperatorToolbox* toolbox) { fOperatorToolbox = toolbox; }
49 
50  // void SetParameterStore(MHO_ParameterStore* pstore) { fParameterStore = pstore; };
51  //
52  // void SetContainerStore(MHO_ContainerStore* cstore) { fContainerStore = cstore; };
53 
54  void SetModulePath(std::string module_path) { fModulePath = module_path; }
55 
56  void SetFunctionName(std::string function_name) { fFunctionName = function_name; }
57 
58  virtual bool Initialize() override
59  {
60  fInitialized = false;
61  if(fModulePath == "")
62  {
63  return false;
64  }
65  if(fFunctionName == "")
66  {
67  return false;
68  }
69 
70  //construct the python interface exposing the parameter and container store
71  if(fFringeData != nullptr)
72  {
73  if(fFringeDataInterface == nullptr)
74  {
75  fFringeDataInterface = new MHO_PyFringeDataInterface(fFringeData);
76  }
77  if(fOperatorToolbox != nullptr)
78  {
79  fFringeDataInterface->SetOperatorToolbox(fOperatorToolbox);
80  }
81  fInitialized = true;
82  }
83 
84  // //construct the python interface exposing the parameter and container store
85  // if(fContainerStore != nullptr && fParameterStore != nullptr)
86  // {
87  // if(fContainerInterface == nullptr){fContainerInterface = new MHO_PyContainerStoreInterface(fContainerStore); }
88  // if(fParameterInterface == nullptr){fParameterInterface = new MHO_PyParameterStoreInterface(fParameterStore); }
89  // fInitialized = true;
90  // }
91  return fInitialized;
92  };
93 
94  virtual bool Execute() override
95  {
96  if(fInitialized)
97  {
98  bool success = false;
99  if(Py_IsInitialized() == 0)
100  {
101  //the internal python interpreter has not been started, bail out
102  msg_error("python_bindings", "python interpreter not running/initialized, "
103  << "cannot call python subroutine (" << fModulePath << "," << fFunctionName
104  << ")." << eom);
105  return success;
106  }
107 
108  //calling user code, so use try-catch in case there are errors
109  try
110  {
111  auto mod = py::module::import(fModulePath.c_str());
112  mod.attr(fFunctionName.c_str())(*fFringeDataInterface);
113  success = true;
114  }
115  catch(py::error_already_set& excep)
116  {
117  success = false;
118  msg_error("python_bindings", "python exception when calling subroutine (" << fModulePath << ","
119  << fFunctionName << ")" << eom);
120  msg_error("python_bindings", "python error message: " << excep.what() << eom);
121  msg_warn("python_bindings", "attempting to continue, but in-memory data may be in unknown state." << eom);
122  PyErr_Clear(); //clear the error and attempt to continue
123  }
124 
125  return success;
126  }
127  return false;
128  }
129 
130  private:
131  bool fInitialized;
132  std::string fModulePath;
133  std::string fFunctionName;
134 
135  MHO_FringeData* fFringeData;
136  MHO_OperatorToolbox* fOperatorToolbox;
137  MHO_PyFringeDataInterface* fFringeDataInterface;
138 
139  // MHO_ContainerStore* fContainerStore;
140  // MHO_ParameterStore* fParameterStore;
141  // MHO_PyParameterStoreInterface* fParameterInterface;
142  // MHO_PyContainerStoreInterface* fContainerInterface;
143 };
144 
145 } // namespace hops
146 
147 #endif
#define msg_warn(xKEY, xCONTENT)
Definition: MHO_Message.hh:248
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:238
Class MHO_FringeData.
Definition: MHO_FringeData.hh:30
Class MHO_OperatorToolbox.
Definition: MHO_OperatorToolbox.hh:28
Class MHO_Operator.
Definition: MHO_Operator.hh:21
python bindings for the MHO_FringeData class
Definition: MHO_PyFringeDataInterface.hh:37
void SetOperatorToolbox(MHO_OperatorToolbox *toolbox)
Definition: MHO_PyFringeDataInterface.hh:47
this class allows a user to inject a python function of the form: func(fringe_data_interface) into th...
Definition: MHO_PyGenericOperator.hh:35
virtual bool Initialize() override
Function Initialize.
Definition: MHO_PyGenericOperator.hh:58
void SetOperatorToolbox(MHO_OperatorToolbox *toolbox)
Definition: MHO_PyGenericOperator.hh:48
virtual ~MHO_PyGenericOperator()
Definition: MHO_PyGenericOperator.hh:44
void SetModulePath(std::string module_path)
Definition: MHO_PyGenericOperator.hh:54
void SetFunctionName(std::string function_name)
Definition: MHO_PyGenericOperator.hh:56
void SetFringeData(MHO_FringeData *fdata)
Definition: MHO_PyGenericOperator.hh:46
virtual bool Execute() override
Function Execute.
Definition: MHO_PyGenericOperator.hh:94
MHO_PyGenericOperator()
Definition: MHO_PyGenericOperator.hh:37
Definition: MHO_AdhocFlagging.hh:18