29#include <boost/python/dict.hpp>
30#include <boost/python/exec.hpp>
31#include <boost/python/extract.hpp>
32#include <boost/python/import.hpp>
33#include <boost/python/object.hpp>
36#include <system_error>
56 struct sigaction sigint_handler;
57 sigaction(SIGINT,
nullptr, &sigint_handler);
61#if PY_VERSION_HEX < 3090000
66 sigaction(SIGINT, &sigint_handler,
nullptr);
79#if PY_MAJOR_VERSION == 2
80 using py_argv_char_t = char;
81#define py_argv_assign(d, s, l) d = strndup(s, l)
83 using py_argv_char_t = wchar_t;
84#define py_argv_assign(d, s, l) d = Py_DecodeLocale(s, &(l))
87 py_argv_char_t** py_argv =
static_cast<py_argv_char_t**
>(PyMem_MALLOC((argv.
size() + 1) *
sizeof(py_argv_char_t*)));
88 size_t wlen = filename.size();
90 for (
size_t i = 0; i < argv.
size(); ++i) {
91 wlen = argv[i].
size();
94 PySys_SetArgv(argv.
size() + 1, py_argv);
97 py::import(
"_SourceXtractorPy");
100 PySys_SetObject(
"stdout", py::object(boost::ref(
m_out_wrapper)).ptr());
101 PySys_SetObject(
"stderr", py::object(boost::ref(
m_err_wrapper)).ptr());
104 py::object main_module = py::import(
"__main__");
105 py::setattr(main_module,
"__file__", py::object(filename));
106 py::object main_namespace = main_module.attr(
"__dict__");
116 py::exec(pycode.
c_str(), main_namespace);
117 }
catch (
const py::error_already_set&
e) {
127 m_measurement_config = py::import(
"sourcextractor.config.measurement_config").attr(
"global_measurement_config");
128 }
catch (
const py::error_already_set&
e) {
138 py::list ids = images.keys();
140 for (
int i = 0; i < py::len(ids); ++i) {
141 int id = py::extract<int>(ids[i]);
146 }
catch (
const py::error_already_set&
e) {
156 py::list ids = apertures.keys();
158 for (
int i = 0; i < py::len(ids); ++i) {
159 int id = py::extract<int>(ids[i]);
160 PyAperture ap = py::extract<PyAperture>(apertures[ids[i]]);
164 }
catch (
const py::error_already_set&
e) {
173 py::list output = py::extract<py::list>(
m_measurement_config.attr(
"model_fitting_parameter_columns"));
175 for (
int i = 0; i < py::len(output); ++i) {
176 py::tuple t = py::extract<py::tuple>(output[i]);
178 auto extract_list = py::extract<py::list>(t[1]);
181 if (extract_list.check()) {
182 py::list cs = extract_list;
183 for (
int j = 0; j < py::len(cs); ++j) {
184 int c = py::extract<int>(cs[j].attr(
"id"));
188 int c = py::extract<int>(t[1]);
194 }
catch (
const py::error_already_set&
e) {
205 for (
int i = 0; i < py::len(output); ++i) {
206 py::tuple t = py::extract<py::tuple>(output[i]);
208 auto extract_list = py::extract<py::list>(t[1]);
210 if (extract_list.check()) {
211 py::list cs = extract_list;
212 for (
int j = 0; j < py::len(cs); ++j) {
213 int c = py::extract<int>(cs[j].attr(
"id"));
214 result[name].push_back(c);
217 int c = py::extract<int>(t[1]);
218 result[name].push_back(c);
222 }
catch (
const py::error_already_set&
e) {
228 const char* dict_name) {
233 py::dict parameters = py::extract<py::dict>(obj.attr(dict_name));
234 py::list ids = parameters.keys();
237 for (
int i = 0; i < py::len(ids); ++i) {
238 int id = py::extract<int>(ids[i]);
239 auto par = parameters[ids[i]];
243 }
catch (
const py::error_already_set&
e) {
249 return getMapFromDict(
"model_fitting",
"constant_parameter_dict");
257 return getMapFromDict(
"model_fitting",
"dependent_parameter_dict");
269 return getMapFromDict(
"model_fitting",
"point_source_model_dict");
281 return getMapFromDict(
"model_fitting",
"de_vaucouleurs_model_dict");
293 py::dict frame_dict = py::extract<py::dict>(model_fitting.attr(
"frame_models_dict"));
294 py::list frame_ids = frame_dict.keys();
295 for (
int i = 0; i < py::len(frame_ids); ++i) {
296 int frame_id = py::extract<int>(frame_ids[i]);
297 py::list model_ids = py::extract<py::list>(frame_dict[frame_ids[i]]);
298 for (
int j = 0; j < py::len(model_ids); ++j) {
299 int model_id = py::extract<int>(model_ids[j]);
300 result[frame_id].push_back(model_id);
304 }
catch (
const py::error_already_set&
e) {
313 py::dict parameters = py::extract<py::dict>(model_fitting.attr(
"params_dict"));
314 py::list ids = parameters.keys();
317 for (
int i = 0; i < py::len(ids); ++i) {
319 auto par = parameters[ids[i]];
#define PYSTON_MODULE_INIT
static Elements::Logging stdout_logger
#define py_argv_assign(d, s, l)
static Elements::Logging logger
static Elements::Logging stderr_logger
static Logging getLogger(const std::string &name="")
void info(const std::string &logMessage)
const Exception & log(log4cpp::Priority::Value level, Elements::Logging &logger) const
static size_t getLockCount()
T emplace_back(T... args)
static Elements::Logging logger
T system_category(T... args)