mirror of
https://github.com/boostorg/histogram.git
synced 2026-01-30 07:52:11 +00:00
clang-format everything
This commit is contained in:
@@ -4,18 +4,18 @@
|
||||
// (See accompanying file LICENSE_1_0.txt
|
||||
// or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include "serialization_suite.hpp"
|
||||
#include "utility.hpp"
|
||||
#include <boost/histogram/detail/cat.hpp>
|
||||
#include <boost/histogram/dynamic_histogram.hpp>
|
||||
#include <boost/histogram/storage/adaptive_storage.hpp>
|
||||
#include <boost/histogram/ostream_operators.hpp>
|
||||
#include <boost/histogram/serialization.hpp>
|
||||
#include <boost/histogram/detail/cat.hpp>
|
||||
#include <boost/histogram/storage/adaptive_storage.hpp>
|
||||
#include <boost/python.hpp>
|
||||
#include <boost/python/raw_function.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/variant/apply_visitor.hpp>
|
||||
#include <boost/variant/static_visitor.hpp>
|
||||
#include "serialization_suite.hpp"
|
||||
#include "utility.hpp"
|
||||
#ifdef HAVE_NUMPY
|
||||
#include <boost/python/numpy.hpp>
|
||||
namespace np = boost::python::numpy;
|
||||
@@ -44,8 +44,8 @@ public:
|
||||
using array = bh::detail::array<T>;
|
||||
|
||||
struct dtype_visitor : public boost::static_visitor<str> {
|
||||
list & shapes, & strides;
|
||||
dtype_visitor(list &sh, list &st) : shapes(sh), strides(st) {}
|
||||
list &shapes, &strides;
|
||||
dtype_visitor(list& sh, list& st) : shapes(sh), strides(st) {}
|
||||
template <typename T>
|
||||
str operator()(const array<T>& /*unused*/) const {
|
||||
strides.append(sizeof(T));
|
||||
@@ -85,26 +85,25 @@ public:
|
||||
// double array, fill it and pass it
|
||||
auto a = np::empty(tuple(shapes), np::dtype::get_builtin<double>());
|
||||
for (auto i = 0l, n = bp::len(shapes); i < n; ++i)
|
||||
const_cast<Py_intptr_t*>(a.get_strides())[i] = bp::extract<int>(strides[i]);
|
||||
auto *buf = (double *)a.get_data();
|
||||
for (auto i = 0ul; i < b.size; ++i)
|
||||
buf[i] = static_cast<double>(b[i]);
|
||||
const_cast<Py_intptr_t*>(a.get_strides())[i] =
|
||||
bp::extract<int>(strides[i]);
|
||||
auto* buf = (double*)a.get_data();
|
||||
for (auto i = 0ul; i < b.size; ++i) buf[i] = static_cast<double>(b[i]);
|
||||
return a;
|
||||
}
|
||||
};
|
||||
|
||||
static object array_interface(const pyhistogram &self) {
|
||||
static object array_interface(const pyhistogram& self) {
|
||||
dict d;
|
||||
list shapes;
|
||||
list strides;
|
||||
auto &b = self.storage_.buffer_;
|
||||
auto& b = self.storage_.buffer_;
|
||||
d["typestr"] = boost::apply_visitor(dtype_visitor(shapes, strides), b);
|
||||
for (auto i = 0u; i < self.dim(); ++i) {
|
||||
if (i) strides.append(strides[-1] * shapes[-1]);
|
||||
shapes.append(self.axis(i).shape());
|
||||
}
|
||||
if (self.dim() == 0)
|
||||
shapes.append(0);
|
||||
if (self.dim() == 0) shapes.append(0);
|
||||
d["shape"] = tuple(shapes);
|
||||
d["strides"] = tuple(strides);
|
||||
d["data"] = boost::apply_visitor(data_visitor(shapes, strides), b);
|
||||
@@ -117,7 +116,8 @@ public:
|
||||
} // namespace boost
|
||||
|
||||
struct axis_visitor : public boost::static_visitor<bp::object> {
|
||||
template <typename T> bp::object operator()(const T &t) const {
|
||||
template <typename T>
|
||||
bp::object operator()(const T& t) const {
|
||||
return bp::object(t);
|
||||
}
|
||||
};
|
||||
@@ -126,9 +126,10 @@ struct axes_appender {
|
||||
bp::object obj;
|
||||
pyhistogram::axes_type& axes;
|
||||
bool& success;
|
||||
axes_appender(bp::object o, pyhistogram::axes_type& a,
|
||||
bool& s) : obj(o), axes(a), success(s) {}
|
||||
template <typename A> void operator()(const A&) const {
|
||||
axes_appender(bp::object o, pyhistogram::axes_type& a, bool& s)
|
||||
: obj(o), axes(a), success(s) {}
|
||||
template <typename A>
|
||||
void operator()(const A&) const {
|
||||
if (success) return;
|
||||
bp::extract<const A&> get_axis(obj);
|
||||
if (get_axis.check()) {
|
||||
@@ -138,9 +139,8 @@ struct axes_appender {
|
||||
}
|
||||
};
|
||||
|
||||
bp::object histogram_axis(const pyhistogram &self, int i) {
|
||||
if (i < 0)
|
||||
i += self.dim();
|
||||
bp::object histogram_axis(const pyhistogram& self, int i) {
|
||||
if (i < 0) i += self.dim();
|
||||
if (i < 0 || i >= int(self.dim()))
|
||||
throw std::out_of_range("axis index out of range");
|
||||
return boost::apply_visitor(axis_visitor(), self.axis(i));
|
||||
@@ -163,8 +163,7 @@ bp::object histogram_init(bp::tuple args, bp::dict kwargs) {
|
||||
bp::object pa = args[i + 1];
|
||||
bool success = false;
|
||||
boost::mp11::mp_for_each<pyhistogram::any_axis_type::types>(
|
||||
axes_appender(pa, axes, success)
|
||||
);
|
||||
axes_appender(pa, axes, success));
|
||||
if (!success) {
|
||||
std::string msg = "require an axis object, got ";
|
||||
msg += bp::extract<std::string>(pa.attr("__class__"))();
|
||||
@@ -204,8 +203,7 @@ struct fetcher {
|
||||
}
|
||||
|
||||
const T& operator[](long i) const noexcept {
|
||||
if (n > 0)
|
||||
return carray[i];
|
||||
if (n > 0) return carray[i];
|
||||
return value;
|
||||
}
|
||||
};
|
||||
@@ -220,18 +218,20 @@ struct span {
|
||||
|
||||
bp::object histogram_fill(bp::tuple args, bp::dict kwargs) {
|
||||
const auto nargs = bp::len(args);
|
||||
pyhistogram &self = bp::extract<pyhistogram &>(args[0]);
|
||||
pyhistogram& self = bp::extract<pyhistogram&>(args[0]);
|
||||
|
||||
const unsigned dim = nargs - 1;
|
||||
if (dim != self.dim()) {
|
||||
PyErr_SetString(PyExc_ValueError, "number of arguments and dimension do not match");
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"number of arguments and dimension do not match");
|
||||
bp::throw_error_already_set();
|
||||
}
|
||||
|
||||
if (dim > BOOST_HISTOGRAM_AXIS_LIMIT) {
|
||||
PyErr_SetString(PyExc_RuntimeError,
|
||||
bh::detail::cat("too many arguments, maximum is ",
|
||||
BOOST_HISTOGRAM_AXIS_LIMIT).c_str());
|
||||
bh::detail::cat("too many arguments, maximum is ",
|
||||
BOOST_HISTOGRAM_AXIS_LIMIT)
|
||||
.c_str());
|
||||
bp::throw_error_already_set();
|
||||
}
|
||||
|
||||
@@ -241,7 +241,8 @@ bp::object histogram_fill(bp::tuple args, bp::dict kwargs) {
|
||||
fetch[d].assign(args[1 + d]);
|
||||
if (fetch[d].n > 0) {
|
||||
if (n > 0 && fetch[d].n != n) {
|
||||
PyErr_SetString(PyExc_ValueError, "lengths of sequences do not match");
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"lengths of sequences do not match");
|
||||
bp::throw_error_already_set();
|
||||
}
|
||||
n = fetch[d].n;
|
||||
@@ -261,7 +262,8 @@ bp::object histogram_fill(bp::tuple args, bp::dict kwargs) {
|
||||
fetch_weight.assign(kwargs.get("weight"));
|
||||
if (fetch_weight.n > 0) {
|
||||
if (n > 0 && fetch_weight.n != n) {
|
||||
PyErr_SetString(PyExc_ValueError, "length of weight sequence does not match");
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"length of weight sequence does not match");
|
||||
bp::throw_error_already_set();
|
||||
}
|
||||
n = fetch_weight.n;
|
||||
@@ -275,22 +277,18 @@ bp::object histogram_fill(bp::tuple args, bp::dict kwargs) {
|
||||
for (auto i = 0l; i < n; ++i)
|
||||
self(bh::weight(fetch_weight[i]), fetch[0][i]);
|
||||
} else {
|
||||
for (auto i = 0l; i < n; ++i)
|
||||
self(fetch[0][i]);
|
||||
for (auto i = 0l; i < n; ++i) self(fetch[0][i]);
|
||||
}
|
||||
} else {
|
||||
double v[BOOST_HISTOGRAM_AXIS_LIMIT];
|
||||
if (fetch_weight.n >= 0) {
|
||||
for (auto i = 0l; i < n; ++i) {
|
||||
for (auto d = 0u; d < dim; ++d)
|
||||
v[d] = fetch[d][i];
|
||||
for (auto d = 0u; d < dim; ++d) v[d] = fetch[d][i];
|
||||
self(bh::weight(fetch_weight[i]), span<double>{v, dim});
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
for (auto i = 0l; i < n; ++i) {
|
||||
for (auto d = 0u; d < dim; ++d)
|
||||
v[d] = fetch[d][i];
|
||||
for (auto d = 0u; d < dim; ++d) v[d] = fetch[d][i];
|
||||
self(span<double>{v, dim});
|
||||
}
|
||||
}
|
||||
@@ -318,20 +316,20 @@ bp::object histogram_getitem(const pyhistogram& self, bp::object args) {
|
||||
|
||||
if (dim > BOOST_HISTOGRAM_AXIS_LIMIT) {
|
||||
PyErr_SetString(PyExc_RuntimeError,
|
||||
bh::detail::cat("too many arguments, maximum is ",
|
||||
BOOST_HISTOGRAM_AXIS_LIMIT).c_str());
|
||||
bh::detail::cat("too many arguments, maximum is ",
|
||||
BOOST_HISTOGRAM_AXIS_LIMIT)
|
||||
.c_str());
|
||||
bp::throw_error_already_set();
|
||||
}
|
||||
|
||||
int idx[BOOST_HISTOGRAM_AXIS_LIMIT];
|
||||
for (unsigned i = 0; i < dim; ++i)
|
||||
idx[i] = bp::extract<int>(args[i]);
|
||||
for (unsigned i = 0; i < dim; ++i) idx[i] = bp::extract<int>(args[i]);
|
||||
|
||||
return bp::object(self.at(span<int>{idx, self.dim()}));
|
||||
}
|
||||
|
||||
bp::object histogram_at(bp::tuple args, bp::dict kwargs) {
|
||||
const pyhistogram & self = bp::extract<const pyhistogram &>(args[0]);
|
||||
const pyhistogram& self = bp::extract<const pyhistogram&>(args[0]);
|
||||
|
||||
if (kwargs) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "no keyword arguments allowed");
|
||||
@@ -343,14 +341,15 @@ bp::object histogram_at(bp::tuple args, bp::dict kwargs) {
|
||||
}
|
||||
|
||||
bp::object histogram_reduce_to(bp::tuple args, bp::dict kwargs) {
|
||||
const pyhistogram &self = bp::extract<const pyhistogram &>(args[0]);
|
||||
const pyhistogram& self = bp::extract<const pyhistogram&>(args[0]);
|
||||
|
||||
const unsigned nargs = bp::len(args) - 1;
|
||||
|
||||
if (nargs > BOOST_HISTOGRAM_AXIS_LIMIT) {
|
||||
PyErr_SetString(PyExc_RuntimeError,
|
||||
bh::detail::cat("too many arguments, maximum is ",
|
||||
BOOST_HISTOGRAM_AXIS_LIMIT).c_str());
|
||||
bh::detail::cat("too many arguments, maximum is ",
|
||||
BOOST_HISTOGRAM_AXIS_LIMIT)
|
||||
.c_str());
|
||||
bp::throw_error_already_set();
|
||||
}
|
||||
|
||||
@@ -360,84 +359,86 @@ bp::object histogram_reduce_to(bp::tuple args, bp::dict kwargs) {
|
||||
}
|
||||
|
||||
int idx[BOOST_HISTOGRAM_AXIS_LIMIT];
|
||||
for (auto i = 0u; i < nargs; ++i)
|
||||
idx[i] = bp::extract<int>(args[1 + i]);
|
||||
for (auto i = 0u; i < nargs; ++i) idx[i] = bp::extract<int>(args[1 + i]);
|
||||
|
||||
return bp::object(self.reduce_to(idx, idx + nargs));
|
||||
}
|
||||
|
||||
std::string histogram_repr(const pyhistogram &h) {
|
||||
std::string histogram_repr(const pyhistogram& h) {
|
||||
return bh::detail::cat(h);
|
||||
}
|
||||
|
||||
double element_value(const pyhistogram::element_type& b) {
|
||||
return b.value();
|
||||
}
|
||||
double element_value(const pyhistogram::element_type& b) { return b.value(); }
|
||||
|
||||
double element_variance(const pyhistogram::element_type& b) {
|
||||
return b.variance();
|
||||
}
|
||||
|
||||
double element_getitem(const pyhistogram::element_type& e, int i) {
|
||||
if (i < 0 || i > 1)
|
||||
throw std::out_of_range("element_getitem");
|
||||
if (i < 0 || i > 1) throw std::out_of_range("element_getitem");
|
||||
return i == 0 ? e.value() : e.variance();
|
||||
}
|
||||
|
||||
int element_len(const pyhistogram::element_type&) { return 2; }
|
||||
|
||||
std::string element_repr(const pyhistogram::element_type& e) {
|
||||
return bh::detail::cat("histogram.element(", e.value(), ", ", e.variance(), ")");
|
||||
return bh::detail::cat("histogram.element(", e.value(), ", ", e.variance(),
|
||||
")");
|
||||
}
|
||||
|
||||
void register_histogram() {
|
||||
bp::docstring_options dopt(true, true, false);
|
||||
|
||||
bp::scope s = bp::class_<pyhistogram, boost::shared_ptr<pyhistogram>>(
|
||||
"histogram", "N-dimensional histogram for real-valued data.", bp::no_init)
|
||||
.def("__init__", bp::raw_function(histogram_init),
|
||||
":param axis args: axis objects"
|
||||
"\nPass one or more axis objects to configure the histogram.")
|
||||
// shadowed C++ ctors
|
||||
.def(bp::init<const pyhistogram &>())
|
||||
// .def(bp::init<pyhistogram &&>())
|
||||
bp::scope s =
|
||||
bp::class_<pyhistogram, boost::shared_ptr<pyhistogram>>(
|
||||
"histogram", "N-dimensional histogram for real-valued data.",
|
||||
bp::no_init)
|
||||
.def("__init__", bp::raw_function(histogram_init),
|
||||
":param axis args: axis objects"
|
||||
"\nPass one or more axis objects to configure the histogram.")
|
||||
// shadowed C++ ctors
|
||||
.def(bp::init<const pyhistogram&>())
|
||||
// .def(bp::init<pyhistogram &&>())
|
||||
#ifdef HAVE_NUMPY
|
||||
.add_property("__array_interface__", &bp::access::array_interface)
|
||||
.add_property("__array_interface__", &bp::access::array_interface)
|
||||
#endif
|
||||
.add_property("dim", &pyhistogram::dim)
|
||||
.def("axis", histogram_axis, bp::arg("i") = 0,
|
||||
":param int i: axis index"
|
||||
"\n:return: corresponding axis object")
|
||||
.def("__call__", bp::raw_function(histogram_fill),
|
||||
":param double args: values (number must match dimension)"
|
||||
"\n:keyword double weight: optional weight"
|
||||
"\n"
|
||||
"\nIf Numpy support is enabled, 1d-arrays can be passed instead of"
|
||||
"\nvalues, which must be equal in lenght. Arrays and values can"
|
||||
"\nbe mixed arbitrarily in the same call.")
|
||||
.def("__len__", &pyhistogram::size,
|
||||
":return: total number of bins, including under- and overflow")
|
||||
.def("at", bp::raw_function(histogram_at),
|
||||
":param int args: indices of the bin (number must match dimension)"
|
||||
"\n:return: bin content")
|
||||
.def("__getitem__", histogram_getitem,
|
||||
":param int args: indices of the bin (number must match dimension)"
|
||||
"\n:return: bin content")
|
||||
.def("reduce_to", bp::raw_function(histogram_reduce_to),
|
||||
":param int args: indices of the axes in the reduced histogram"
|
||||
"\n:return: reduced histogram with subset of axes")
|
||||
.def("__iter__", bp::iterator<pyhistogram>())
|
||||
.def("__repr__", histogram_repr,
|
||||
":return: string representation of the histogram")
|
||||
.def(bp::self == bp::self)
|
||||
.def(bp::self != bp::self)
|
||||
.def(bp::self += bp::self)
|
||||
.def(bp::self *= double())
|
||||
.def(bp::self * double())
|
||||
.def(double() * bp::self)
|
||||
.def(bp::self + bp::self)
|
||||
.def_pickle(bh::serialization_suite<pyhistogram>())
|
||||
;
|
||||
.add_property("dim", &pyhistogram::dim)
|
||||
.def("axis", histogram_axis, bp::arg("i") = 0,
|
||||
":param int i: axis index"
|
||||
"\n:return: corresponding axis object")
|
||||
.def(
|
||||
"__call__", bp::raw_function(histogram_fill),
|
||||
":param double args: values (number must match dimension)"
|
||||
"\n:keyword double weight: optional weight"
|
||||
"\n"
|
||||
"\nIf Numpy support is enabled, 1d-arrays can be passed "
|
||||
"instead of"
|
||||
"\nvalues, which must be equal in lenght. Arrays and values can"
|
||||
"\nbe mixed arbitrarily in the same call.")
|
||||
.def("__len__", &pyhistogram::size,
|
||||
":return: total number of bins, including under- and overflow")
|
||||
.def("at", bp::raw_function(histogram_at),
|
||||
":param int args: indices of the bin (number must match "
|
||||
"dimension)"
|
||||
"\n:return: bin content")
|
||||
.def("__getitem__", histogram_getitem,
|
||||
":param int args: indices of the bin (number must match "
|
||||
"dimension)"
|
||||
"\n:return: bin content")
|
||||
.def("reduce_to", bp::raw_function(histogram_reduce_to),
|
||||
":param int args: indices of the axes in the reduced histogram"
|
||||
"\n:return: reduced histogram with subset of axes")
|
||||
.def("__iter__", bp::iterator<pyhistogram>())
|
||||
.def("__repr__", histogram_repr,
|
||||
":return: string representation of the histogram")
|
||||
.def(bp::self == bp::self)
|
||||
.def(bp::self != bp::self)
|
||||
.def(bp::self += bp::self)
|
||||
.def(bp::self *= double())
|
||||
.def(bp::self * double())
|
||||
.def(double() * bp::self)
|
||||
.def(bp::self + bp::self)
|
||||
.def_pickle(bh::serialization_suite<pyhistogram>());
|
||||
|
||||
bp::class_<pyhistogram::element_type>(
|
||||
"element", "Holds value and variance of bin count.",
|
||||
@@ -453,6 +454,5 @@ void register_histogram() {
|
||||
.def(bp::self + bp::self)
|
||||
.def(bp::self + double())
|
||||
.def(double() + bp::self)
|
||||
.def("__repr__", element_repr)
|
||||
;
|
||||
.def("__repr__", element_repr);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user