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