-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython.cpp
More file actions
138 lines (111 loc) · 3.87 KB
/
Copy pathpython.cpp
File metadata and controls
138 lines (111 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// GAMBIT: Global and Modular BSM Inference Tool
// *********************************************
/// \file
///
/// Make an instance of objective scanner and
/// define method to run it
///
/// *********************************************
///
/// Authors (add name and date if you modify):
///
/// \author Gregory Martinez
/// (gregory.david.martinez@gmail.com)
/// \date 2023 Dec
///
/// *********************************************
#include "gambit/cmake/cmake_variables.hpp"
#ifdef HAVE_PYBIND11
#ifdef WITH_MPI
#include "gambit/Utils/begin_ignore_warnings_mpi.hpp"
#include "mpi.h"
#include "gambit/Utils/end_ignore_warnings.hpp"
#endif
#include <vector>
#include <string>
#include <cmath>
#include <iostream>
#include <sstream>
#include "gambit/Utils/begin_ignore_warnings_pybind11.hpp"
#include <pybind11/embed.h>
#include "gambit/Utils/end_ignore_warnings.hpp"
#include "gambit/Utils/python_interpreter.hpp"
#include "gambit/ScannerBit/objective_plugin.hpp"
#include "gambit/ScannerBit/python_utils.hpp"
namespace py = pybind11;
namespace Gambit
{
namespace Scanner
{
namespace Plugins
{
namespace ObjPyPlugin
{
pluginData *&pythonPluginData();
double run(py::object &, std::unordered_map<std::string,double> &);
}
}
}
}
objective_plugin(python, version(1, 0, 0))
{
reqd_headers("Python3");
reqd_headers("pybind11");
Gambit::Utils::python_interpreter_guard g;
/*!
* Instance of python scanner
*/
py::object instance;
py::object run_func;
plugin_constructor
{
// export plugin data to objective plugin
Gambit::Scanner::Plugins::ObjPyPlugin::pythonPluginData() = &__gambit_plugin_namespace__::myData;
// get yaml as dict
py::kwargs options = yaml_to_dict(get_inifile_node());
// make instance of plugin
py::module file;
std::string pkg = get_inifile_value<std::string>("pkg", "");
std::string plugin_name = get_inifile_value<std::string>("plugin");
try
{
if (pkg == "")
{
Gambit::Scanner::Plugins::plugin_info.load_python_plugins();
decltype(auto) details = Gambit::Scanner::Plugins::plugin_info.load_python_plugin("objective", plugin_name);
py::list(py::module::import("sys").attr("path")).append(py::cast(details.loc));
file = py::module::import(details.package.c_str());
}
else
{
std::string::size_type pos = pkg.rfind("/");
std::string pkg_name;
if (pos != std::string::npos)
{
pkg_name = pkg.substr(pos+1);
while(pos != 0 && pkg[pos-1] == '/') --pos;
std::string path = pkg.substr(0, pos);
py::list(py::module::import("sys").attr("path")).append(py::cast(path));
}
else
pkg_name = pkg;
py::list(py::module::import("sys").attr("path")).append(py::cast(GAMBIT_DIR "/ScannerBit/src/objectives/python/plugins"));
file = py::module::import(pkg_name.c_str());
}
instance = py::dict(file.attr("__plugins__"))[plugin_name.c_str()](**options);
run_func = instance.attr("run");
}
catch (std::exception &ex)
{
scan_err << "Error loading plugin \"" << plugin_name << "\": " << ex.what() << scan_end;
}
}
double plugin_main(std::unordered_map<std::string, double> &map)
{
return Gambit::Scanner::Plugins::ObjPyPlugin::run(run_func, map);
}
plugin_deconstructor
{
}
}
#endif