put axis in extra namespace, put python axis objects in axis submodule

This commit is contained in:
Hans Dembinski
2017-06-04 11:53:15 +02:00
parent 6ec89079e0
commit ef4898ddc2
15 changed files with 486 additions and 463 deletions

View File

@@ -20,7 +20,7 @@ namespace histogram {
namespace {
python::object variable_axis_init(python::tuple args, python::dict kwargs) {
python::object variable_init(python::tuple args, python::dict kwargs) {
using namespace python;
object self = args[0];
@@ -54,10 +54,10 @@ python::object variable_axis_init(python::tuple args, python::dict kwargs) {
}
return self.attr("__init__")(
variable_axis<>(v.begin(), v.end(), label, uoflow));
axis::variable<>(v.begin(), v.end(), label, uoflow));
}
python::object category_axis_init(python::tuple args, python::dict kwargs) {
python::object category_init(python::tuple args, python::dict kwargs) {
using namespace python;
object self = args[0];
@@ -86,7 +86,7 @@ python::object category_axis_init(python::tuple args, python::dict kwargs) {
for (int i = 1, n = len(args); i < n; ++i)
c.push_back(extract<std::string>(args[i]));
return self.attr("__init__")(category_axis(c.begin(), c.end(), label));
return self.attr("__init__")(axis::category(c.begin(), c.end(), label));
}
template <typename T> int axis_len(const T &t) {
@@ -101,12 +101,12 @@ template <typename T> python::object axis_getitem(const T &t, int i) {
return python::object(t[i]);
}
template <> python::object axis_getitem(const category_axis &t, int i) {
if (i == axis_len(t)) {
template <> python::object axis_getitem(const axis::category &a, int i) {
if (i == axis_len(a)) {
PyErr_SetString(PyExc_StopIteration, "no more");
python::throw_error_already_set();
}
return python::object(t[i].data());
return python::object(a[i].data());
}
template <typename T> std::string axis_repr(const T &t) {
@@ -135,9 +135,9 @@ struct axis_suite : public python::def_visitor<axis_suite<T>> {
cl.def("__len__", axis_len<T>, ":returns: number of bins for this axis",
python::arg("self"));
cl.def("__getitem__", axis_getitem<T>,
is_same<T, integer_axis>::value
is_same<T, axis::integer>::value
? ":returns: integer mapped to passed bin index"
: is_same<T, category_axis>::value
: is_same<T, axis::category>::value
? ":returns: category mapped to passed bin index"
: ":returns: low edge of the bin",
python::args("self", "index"));
@@ -154,17 +154,17 @@ void register_axis_types() {
using python::arg;
docstring_options dopt(true, true, false);
class_<regular_axis<>>("regular_axis",
class_<axis::regular<>>("regular",
"An axis for real-valued data and bins of equal width."
"\nBinning is a O(1) operation.",
no_init)
.def(init<unsigned, double, double, const std::string &, bool>(
(arg("self"), arg("bin"), arg("min"), arg("max"),
arg("label") = std::string(), arg("uoflow") = true)))
.def(axis_suite<regular_axis<>>());
.def(axis_suite<axis::regular<>>());
class_<circular_axis<>>(
"circular_axis",
class_<axis::circular<>>(
"circular",
"An axis for real-valued angles."
"\nThere are no overflow/underflow bins for this axis,"
"\nsince the axis is circular and wraps around after reaching"
@@ -174,19 +174,19 @@ void register_axis_types() {
(arg("self"), arg("bin"), arg("phase") = 0.0,
arg("perimeter") = math::double_constants::two_pi,
arg("label") = std::string())))
.def(axis_suite<circular_axis<>>());
.def(axis_suite<axis::circular<>>());
class_<variable_axis<>>(
"variable_axis",
class_<axis::variable<>>(
"variable",
"An axis for real-valued data and bins of varying width."
"\nBinning is a O(log(N)) operation. If speed matters and"
"\nthe problem domain allows it, prefer a regular_axis<>.",
"\nthe problem domain allows it, prefer a regular.",
no_init)
.def("__init__", raw_function(variable_axis_init))
.def(init<const variable_axis<> &>())
.def(axis_suite<variable_axis<>>());
.def("__init__", raw_function(variable_init))
.def(init<const axis::variable<> &>())
.def(axis_suite<axis::variable<>>());
class_<integer_axis>("integer_axis",
class_<axis::integer>("integer",
"An axis for a contiguous range of integers."
"\nThere are no underflow/overflow bins for this axis."
"\nBinning is a O(1) operation.",
@@ -194,18 +194,18 @@ void register_axis_types() {
.def(init<int, int, const std::string &, bool>(
(arg("self"), arg("min"), arg("max"), arg("label") = std::string(),
arg("uoflow") = true)))
.def(axis_suite<integer_axis>());
.def(axis_suite<axis::integer>());
class_<category_axis>("category_axis",
class_<axis::category>("category",
"An axis for enumerated categories. The axis stores the"
"\ncategory labels, and expects that they are addressed"
"\nusing an integer from 0 to n-1. There are no"
"\nunderflow/overflow bins for this axis."
"\nBinning is a O(1) operation.",
no_init)
.def("__init__", raw_function(category_axis_init))
.def(init<const category_axis &>())
.def(axis_suite<category_axis>());
.def("__init__", raw_function(category_init))
.def(init<const axis::category &>())
.def(axis_suite<axis::category>());
}
}
}