use pyo3::prelude::*;
use pyo3::types::PyDict;
use tempfile::NamedTempFile;
pub fn run_optimization(
custom_engine: PyObject,
params: &Py<PyDict>,
input: Option<&str>,
) -> PyResult<PyObject> {
Python::with_gil(|py| {
let run_optimizer = py.import("geometric.optimize")?.getattr("run_optimizer")?;
let deepcopy = py.import("copy")?.getattr("deepcopy")?;
let kwargs = deepcopy.call1((params,))?.extract::<Bound<PyDict>>()?;
let tmpfile = NamedTempFile::new()?;
let tmp_path = tmpfile.path().to_str().unwrap();
match input {
Some(input) => kwargs.set_item("input", input)?,
None => kwargs.set_item("input", tmp_path)?,
}
kwargs.set_item("customengine", custom_engine)?;
let result = run_optimizer.call((), Some(&kwargs))?;
Ok(result.into())
})
}