407 {
408
409 std::string custom_weightfile = weightfile.generateFileName();
410 weightfile.getFile("Python_Weightfile", custom_weightfile);
411 weightfile.getOptions(m_general_options);
412 weightfile.getOptions(m_specific_options);
413
414 if (m_specific_options.m_normalize) {
415 m_means = weightfile.getVector<float>("Python_Means");
416 m_stds = weightfile.getVector<float>("Python_Stds");
417 }
418
419 try {
420 auto pickle = boost::python::import("pickle");
421 auto builtins = boost::python::import("builtins");
422
423
424
425
426 boost::uuids::random_generator uuid_gen;
427 std::string unique_mva_module_name = "custom_framework_" + boost::uuids::to_string(uuid_gen());
428 boost::python::object type = boost::python::import("types");
429 m_unique_mva_module = type.attr("ModuleType")(unique_mva_module_name.c_str());
430
431
432 auto framework = boost::python::import((std::string("basf2_mva_python_interface.") + m_specific_options.m_framework).c_str());
433 auto framework_file = framework.attr("__file__");
434 auto framework_file_source_code = loadPythonFileAsString(boost::python::extract<std::string>(boost::python::object(
435 framework_file)));
436 builtins.attr("exec")(framework_file_source_code.c_str(), boost::python::object(m_unique_mva_module.attr("__dict__")));
437
438
439 if (weightfile.containsElement("Python_Steeringfile")) {
440 std::string custom_steeringfile = weightfile.generateFileName();
441 weightfile.getFile("Python_Steeringfile", custom_steeringfile);
442 auto steeringfile = builtins.attr("open")(custom_steeringfile.c_str(), "rb");
443 auto source_code = pickle.attr("load")(steeringfile);
444 builtins.attr("exec")(boost::python::object(source_code), boost::python::object(m_unique_mva_module.attr("__dict__")));
445 }
446
447 auto file = builtins.attr("open")(custom_weightfile.c_str(), "rb");
448 auto unpickled_fit_object = pickle.attr("load")(file);
449 m_state = m_unique_mva_module.attr("load")(unpickled_fit_object);
450 } catch (...) {
451 PyErr_Print();
452 PyErr_Clear();
453 B2ERROR("Failed calling load in PythonExpert");
454 throw std::runtime_error("Failed calling load in PythonExpert");
455 }
456
457 }