quala 0.0.1a0
Quasi-Newton and other accelerators
kwargs-to-struct.hpp
Go to the documentation of this file.
1/**
2 * @file
3 * This file defines mappings from Python dicts (kwargs) to simple parameter
4 * structs.
5 */
6
7#pragma once
8
9#include <functional>
10#include <map>
11#include <variant>
12
13#include <pybind11/detail/typeid.h>
14#include <pybind11/pybind11.h>
15namespace py = pybind11;
16
17struct cast_error_with_types : py::cast_error {
18 cast_error_with_types(const py::cast_error &e, std::string from,
19 std::string to)
20 : py::cast_error(e), from(std::move(from)), to(std::move(to)) {}
21 std::string from;
22 std::string to;
23};
24
25template <class T, class A>
26auto attr_setter(A T::*attr) {
27 return [attr](T &t, const py::handle &h) {
28 try {
29 t.*attr = h.cast<A>();
30 } catch (const py::cast_error &e) {
31 throw cast_error_with_types(e, py::str(py::type::handle_of(h)),
32 py::type_id<A>());
33 }
34 };
35}
36template <class T, class A>
37auto attr_getter(A T::*attr) {
38 return [attr](const T &t) { return py::cast(t.*attr); };
39}
40
41template <class T>
43 public:
44 template <class A>
46 : set(attr_setter(attr)), get(attr_getter(attr)) {}
47
48 std::function<void(T &, const py::handle &)> set;
49 std::function<py::object(const T &)> get;
50};
51
52template <class T>
53using kwargs_to_struct_table_t = std::map<std::string, attr_setter_fun_t<T>>;
54
55template <class T>
57
58template <class T>
59void kwargs_to_struct_helper(T &t, const py::kwargs &kwargs) {
60 const auto &m = kwargs_to_struct_table<T>;
61 for (auto &&[key, val] : kwargs) {
62 auto skey = key.template cast<std::string>();
63 auto it = m.find(skey);
64 if (it == m.end())
65 throw py::key_error("Unknown parameter " + skey);
66 try {
67 it->second.set(t, val);
68 } catch (const cast_error_with_types &e) {
69 throw std::runtime_error("Error converting parameter '" + skey +
70 "' from " + e.from + " to '" + e.to +
71 "': " + e.what());
72 } catch (const std::runtime_error &e) {
73 throw std::runtime_error("Error setting parameter '" + skey +
74 "': " + e.what());
75 }
76 }
77}
78
79template <class T>
80py::dict struct_to_dict_helper(const T &t) {
81 const auto &m = kwargs_to_struct_table<T>;
82 py::dict d;
83 for (auto &&[key, val] : m) {
84 py::object o = val.get(t);
85 if (py::hasattr(o, "to_dict"))
86 o = o.attr("to_dict")();
87 d[key.c_str()] = std::move(o);
88 }
89 return d;
90}
91
92template <class T>
93T kwargs_to_struct(const py::kwargs &kwargs) {
94 T t{};
95 kwargs_to_struct_helper(t, kwargs);
96 return t;
97}
98
99template <class T>
100py::dict struct_to_dict(const T &t) {
101 return struct_to_dict_helper<T>(t);
102}
103
104template <class T>
105T var_kwargs_to_struct(const std::variant<T, py::dict> &p) {
106 return std::holds_alternative<T>(p)
107 ? std::get<T>(p)
108 : kwargs_to_struct<T>(std::get<py::dict>(p));
109}
110
111#include <quala/lbfgs.hpp>
112
113template <>
115 kwargs_to_struct_table<quala::LBFGSParams>{
116 {"memory", &quala::LBFGSParams::memory},
117 {"min_div_fac", &quala::LBFGSParams::min_div_fac},
118 {"min_abs_s", &quala::LBFGSParams::min_abs_s},
119 {"force_pos_def", &quala::LBFGSParams::force_pos_def},
120 {"cbfgs", &quala::LBFGSParams::cbfgs},
121 };
122
123template <>
126 {"α", &decltype(quala::LBFGSParams::cbfgs)::α},
127 {"ϵ", &decltype(quala::LBFGSParams::cbfgs)::ϵ},
128 };
129
131
132template <>
134 kwargs_to_struct_table<quala::AndersonAccelParams>{
136 };
137
138#include <quala/broyden-good.hpp>
139
140template <>
142 kwargs_to_struct_table<quala::BroydenGoodParams>{
145 {"force_pos_def", &quala::BroydenGoodParams::force_pos_def},
147 };
std::function< py::object(const T &)> get
attr_setter_fun_t(A T::*attr)
std::function< void(T &, const py::handle &)> set
py::dict struct_to_dict(const T &t)
void kwargs_to_struct_helper(T &t, const py::kwargs &kwargs)
std::map< std::string, attr_setter_fun_t< T > > kwargs_to_struct_table_t
kwargs_to_struct_table_t< T > kwargs_to_struct_table
auto attr_setter(A T::*attr)
T kwargs_to_struct(const py::kwargs &kwargs)
T var_kwargs_to_struct(const std::variant< T, py::dict > &p)
auto attr_getter(A T::*attr)
py::dict struct_to_dict_helper(const T &t)
real_t min_abs_s
Reject update if .
Definition: decl/lbfgs.hpp:15
real_t min_div_abs
Reject update if .
CBFGSParams cbfgs
Parameters in the cautious BFGS update condition.
Definition: decl/lbfgs.hpp:26
bool restarted
If set to true, the buffer is cleared after memory iterations.
length_t memory
Length of the history to keep.
Definition: decl/lbfgs.hpp:11
bool force_pos_def
If set to true, the inverse Hessian estimate should remain definite, i.e.
Definition: decl/lbfgs.hpp:33
real_t min_div_fac
Reject update if .
Definition: decl/lbfgs.hpp:13
cast_error_with_types(const py::cast_error &e, std::string from, std::string to)