mirror of
https://github.com/boostorg/histogram.git
synced 2026-01-30 07:52:11 +00:00
put axis in extra namespace, put python axis objects in axis submodule
This commit is contained in:
@@ -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>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ public:
|
||||
shapes.append(2);
|
||||
}
|
||||
for (unsigned i = 0; i < self.dim(); ++i) {
|
||||
const auto s = shape(self.axis(i));
|
||||
const auto s = histogram::shape(self.axis(i));
|
||||
shapes.append(s);
|
||||
strides.append(stride);
|
||||
stride *= s;
|
||||
@@ -191,27 +191,27 @@ python::object histogram_init(python::tuple args, python::dict kwargs) {
|
||||
std::vector<dynamic_histogram::axis_type> axes;
|
||||
for (unsigned i = 0; i < dim; ++i) {
|
||||
python::object pa = args[i + 1];
|
||||
python::extract<regular_axis<>> er(pa);
|
||||
python::extract<axis::regular<>> er(pa);
|
||||
if (er.check()) {
|
||||
axes.push_back(er());
|
||||
continue;
|
||||
}
|
||||
python::extract<circular_axis<>> ep(pa);
|
||||
python::extract<axis::circular<>> ep(pa);
|
||||
if (ep.check()) {
|
||||
axes.push_back(ep());
|
||||
continue;
|
||||
}
|
||||
python::extract<variable_axis<>> ev(pa);
|
||||
python::extract<axis::variable<>> ev(pa);
|
||||
if (ev.check()) {
|
||||
axes.push_back(ev());
|
||||
continue;
|
||||
}
|
||||
python::extract<integer_axis> ei(pa);
|
||||
python::extract<axis::integer> ei(pa);
|
||||
if (ei.check()) {
|
||||
axes.push_back(ei());
|
||||
continue;
|
||||
}
|
||||
python::extract<category_axis> ec(pa);
|
||||
python::extract<axis::category> ec(pa);
|
||||
if (ec.check()) {
|
||||
axes.push_back(ec());
|
||||
continue;
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/python/module.hpp>
|
||||
#include <boost/python/scope.hpp>
|
||||
#include <boost/python/object.hpp>
|
||||
#ifdef HAVE_NUMPY
|
||||
#define PY_ARRAY_UNIQUE_SYMBOL boost_histogram_ARRAY_API
|
||||
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
||||
@@ -32,6 +34,15 @@ BOOST_PYTHON_MODULE(histogram) {
|
||||
#ifdef HAVE_NUMPY
|
||||
init_numpy();
|
||||
#endif
|
||||
boost::histogram::register_axis_types();
|
||||
using namespace boost::python;
|
||||
scope current;
|
||||
object axis_module = object(
|
||||
borrowed(PyImport_AddModule("histogram.axis"))
|
||||
);
|
||||
current.attr("axis") = axis_module;
|
||||
{
|
||||
scope scope = axis_module;
|
||||
boost::histogram::register_axis_types();
|
||||
}
|
||||
boost::histogram::register_histogram();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user