mirror of
https://github.com/boostorg/histogram.git
synced 2026-01-30 20:02:13 +00:00
clang-formatted code
This commit is contained in:
@@ -7,38 +7,37 @@
|
||||
#include "serialization_suite.hpp"
|
||||
#include <boost/histogram/axis.hpp>
|
||||
#include <boost/histogram/dynamic_histogram.hpp>
|
||||
#include <boost/histogram/utility.hpp>
|
||||
#include <boost/histogram/serialization.hpp>
|
||||
#include <boost/histogram/histogram_ostream_operators.hpp>
|
||||
#include <boost/histogram/serialization.hpp>
|
||||
#include <boost/histogram/utility.hpp>
|
||||
#include <boost/python.hpp>
|
||||
#include <boost/python/raw_function.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/variant/static_visitor.hpp>
|
||||
#include <boost/variant/apply_visitor.hpp>
|
||||
#include <boost/variant/static_visitor.hpp>
|
||||
#ifdef HAVE_NUMPY
|
||||
# define NO_IMPORT_ARRAY
|
||||
# define PY_ARRAY_UNIQUE_SYMBOL boost_histogram_ARRAY_API
|
||||
# define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
||||
# include <numpy/arrayobject.h>
|
||||
#define NO_IMPORT_ARRAY
|
||||
#define PY_ARRAY_UNIQUE_SYMBOL boost_histogram_ARRAY_API
|
||||
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
||||
#include <numpy/arrayobject.h>
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_HISTOGRAM_AXIS_LIMIT
|
||||
#define BOOST_HISTOGRAM_AXIS_LIMIT 32
|
||||
#define BOOST_HISTOGRAM_AXIS_LIMIT 32
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace histogram {
|
||||
|
||||
struct axis_visitor : public static_visitor<python::object>
|
||||
{
|
||||
template <typename T>
|
||||
python::object operator()(const T& t) const { return python::object(t); }
|
||||
struct axis_visitor : public static_visitor<python::object> {
|
||||
template <typename T> python::object operator()(const T &t) const {
|
||||
return python::object(t);
|
||||
}
|
||||
};
|
||||
|
||||
python::object
|
||||
histogram_axis(const dynamic_histogram<>& self, int i)
|
||||
{
|
||||
if (i < 0) i += self.dim();
|
||||
python::object histogram_axis(const dynamic_histogram<> &self, int i) {
|
||||
if (i < 0)
|
||||
i += self.dim();
|
||||
if (i < 0 || i >= int(self.dim())) {
|
||||
PyErr_SetString(PyExc_IndexError, "axis index out of range");
|
||||
python::throw_error_already_set();
|
||||
@@ -46,8 +45,7 @@ histogram_axis(const dynamic_histogram<>& self, int i)
|
||||
return apply_visitor(axis_visitor(), self.axis(i));
|
||||
}
|
||||
|
||||
python::object
|
||||
histogram_init(python::tuple args, python::dict kwargs) {
|
||||
python::object histogram_init(python::tuple args, python::dict kwargs) {
|
||||
using namespace python;
|
||||
using python::tuple;
|
||||
|
||||
@@ -66,15 +64,30 @@ histogram_init(python::tuple args, python::dict kwargs) {
|
||||
for (unsigned i = 0; i < dim; ++i) {
|
||||
object pa = args[i + 1];
|
||||
extract<regular_axis<>> er(pa);
|
||||
if (er.check()) { axes.push_back(er()); continue; }
|
||||
if (er.check()) {
|
||||
axes.push_back(er());
|
||||
continue;
|
||||
}
|
||||
extract<circular_axis<>> ep(pa);
|
||||
if (ep.check()) { axes.push_back(ep()); continue; }
|
||||
if (ep.check()) {
|
||||
axes.push_back(ep());
|
||||
continue;
|
||||
}
|
||||
extract<variable_axis<>> ev(pa);
|
||||
if (ev.check()) { axes.push_back(ev()); continue; }
|
||||
if (ev.check()) {
|
||||
axes.push_back(ev());
|
||||
continue;
|
||||
}
|
||||
extract<integer_axis> ei(pa);
|
||||
if (ei.check()) { axes.push_back(ei()); continue; }
|
||||
if (ei.check()) {
|
||||
axes.push_back(ei());
|
||||
continue;
|
||||
}
|
||||
extract<category_axis> ec(pa);
|
||||
if (ec.check()) { axes.push_back(ec()); continue; }
|
||||
if (ec.check()) {
|
||||
axes.push_back(ec());
|
||||
continue;
|
||||
}
|
||||
std::string msg = "require an axis object, got ";
|
||||
msg += extract<std::string>(pa.attr("__class__").attr("__name__"))();
|
||||
PyErr_SetString(PyExc_TypeError, msg.c_str());
|
||||
@@ -84,12 +97,11 @@ histogram_init(python::tuple args, python::dict kwargs) {
|
||||
return pyinit(h);
|
||||
}
|
||||
|
||||
python::object
|
||||
histogram_fill(python::tuple args, python::dict kwargs) {
|
||||
python::object histogram_fill(python::tuple args, python::dict kwargs) {
|
||||
using namespace python;
|
||||
|
||||
const unsigned nargs = len(args);
|
||||
dynamic_histogram<>& self = extract<dynamic_histogram<>&>(args[0]);
|
||||
dynamic_histogram<> &self = extract<dynamic_histogram<> &>(args[0]);
|
||||
|
||||
object ow;
|
||||
if (kwargs) {
|
||||
@@ -104,44 +116,47 @@ histogram_fill(python::tuple args, python::dict kwargs) {
|
||||
if (nargs == 2) {
|
||||
object o = args[1];
|
||||
if (PySequence_Check(o.ptr())) {
|
||||
PyArrayObject* a = reinterpret_cast<PyArrayObject*>
|
||||
(PyArray_FROM_OTF(o.ptr(), NPY_DOUBLE, NPY_ARRAY_IN_ARRAY));
|
||||
PyArrayObject *a = reinterpret_cast<PyArrayObject *>(
|
||||
PyArray_FROM_OTF(o.ptr(), NPY_DOUBLE, NPY_ARRAY_IN_ARRAY));
|
||||
if (!a) {
|
||||
PyErr_SetString(PyExc_ValueError, "could not convert sequence into array");
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"could not convert sequence into array");
|
||||
throw_error_already_set();
|
||||
}
|
||||
|
||||
npy_intp* dims = PyArray_DIMS(a);
|
||||
npy_intp *dims = PyArray_DIMS(a);
|
||||
switch (PyArray_NDIM(a)) {
|
||||
case 1:
|
||||
case 1:
|
||||
if (self.dim() > 1) {
|
||||
PyErr_SetString(PyExc_ValueError, "array has to be two-dimensional");
|
||||
throw_error_already_set();
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (self.dim() != dims[1])
|
||||
{
|
||||
PyErr_SetString(PyExc_ValueError, "size of second dimension does not match");
|
||||
case 2:
|
||||
if (self.dim() != dims[1]) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"size of second dimension does not match");
|
||||
throw_error_already_set();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
PyErr_SetString(PyExc_ValueError, "array has wrong dimension");
|
||||
throw_error_already_set();
|
||||
default:
|
||||
PyErr_SetString(PyExc_ValueError, "array has wrong dimension");
|
||||
throw_error_already_set();
|
||||
}
|
||||
|
||||
if (!ow.is_none()) {
|
||||
if (PySequence_Check(ow.ptr())) {
|
||||
PyArrayObject* aw = reinterpret_cast<PyArrayObject*>
|
||||
(PyArray_FROM_OTF(ow.ptr(), NPY_DOUBLE, NPY_ARRAY_IN_ARRAY));
|
||||
PyArrayObject *aw = reinterpret_cast<PyArrayObject *>(
|
||||
PyArray_FROM_OTF(ow.ptr(), NPY_DOUBLE, NPY_ARRAY_IN_ARRAY));
|
||||
if (!aw) {
|
||||
PyErr_SetString(PyExc_ValueError, "could not convert sequence into array");
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"could not convert sequence into array");
|
||||
throw_error_already_set();
|
||||
}
|
||||
|
||||
if (PyArray_NDIM(aw) != 1) {
|
||||
PyErr_SetString(PyExc_ValueError, "array has to be one-dimensional");
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"array has to be one-dimensional");
|
||||
throw_error_already_set();
|
||||
}
|
||||
|
||||
@@ -151,9 +166,9 @@ histogram_fill(python::tuple args, python::dict kwargs) {
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < dims[0]; ++i) {
|
||||
double* v = reinterpret_cast<double*>(PyArray_GETPTR1(a, i) );
|
||||
double* w = reinterpret_cast<double*>(PyArray_GETPTR1(aw, i));
|
||||
self.wfill(*w, v, v+self.dim());
|
||||
double *v = reinterpret_cast<double *>(PyArray_GETPTR1(a, i));
|
||||
double *w = reinterpret_cast<double *>(PyArray_GETPTR1(aw, i));
|
||||
self.wfill(*w, v, v + self.dim());
|
||||
}
|
||||
|
||||
Py_DECREF(aw);
|
||||
@@ -163,8 +178,8 @@ histogram_fill(python::tuple args, python::dict kwargs) {
|
||||
}
|
||||
} else {
|
||||
for (unsigned i = 0; i < dims[0]; ++i) {
|
||||
double* v = reinterpret_cast<double*>(PyArray_GETPTR1(a, i));
|
||||
self.fill(v, v+self.dim());
|
||||
double *v = reinterpret_cast<double *>(PyArray_GETPTR1(a, i));
|
||||
self.fill(v, v + self.dim());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,19 +207,19 @@ histogram_fill(python::tuple args, python::dict kwargs) {
|
||||
v[i] = extract<double>(args[1 + i]);
|
||||
|
||||
if (ow.is_none()) {
|
||||
self.fill(v, v+self.dim());
|
||||
self.fill(v, v + self.dim());
|
||||
} else {
|
||||
const double w = extract<double>(ow);
|
||||
self.wfill(w, v, v+self.dim());
|
||||
self.wfill(w, v, v + self.dim());
|
||||
}
|
||||
|
||||
return object();
|
||||
}
|
||||
|
||||
python::object
|
||||
histogram_value(python::tuple args, python::dict kwargs) {
|
||||
python::object histogram_value(python::tuple args, python::dict kwargs) {
|
||||
using namespace python;
|
||||
const dynamic_histogram<>& self = extract<const dynamic_histogram<>&>(args[0]);
|
||||
const dynamic_histogram<> &self =
|
||||
extract<const dynamic_histogram<> &>(args[0]);
|
||||
|
||||
const unsigned dim = len(args) - 1;
|
||||
if (self.dim() != dim) {
|
||||
@@ -231,10 +246,10 @@ histogram_value(python::tuple args, python::dict kwargs) {
|
||||
return object(self.value(idx + 0, idx + self.dim()));
|
||||
}
|
||||
|
||||
python::object
|
||||
histogram_variance(python::tuple args, python::dict kwargs) {
|
||||
python::object histogram_variance(python::tuple args, python::dict kwargs) {
|
||||
using namespace python;
|
||||
const dynamic_histogram<>& self = extract<const dynamic_histogram<>&>(args[0]);
|
||||
const dynamic_histogram<> &self =
|
||||
extract<const dynamic_histogram<> &>(args[0]);
|
||||
|
||||
const unsigned dim = len(args) - 1;
|
||||
if (self.dim() != dim) {
|
||||
@@ -261,19 +276,16 @@ histogram_variance(python::tuple args, python::dict kwargs) {
|
||||
return object(self.variance(idx + 0, idx + self.dim()));
|
||||
}
|
||||
|
||||
std::string
|
||||
histogram_repr(const dynamic_histogram<>& h) {
|
||||
std::ostringstream os;
|
||||
os << h;
|
||||
return os.str();
|
||||
std::string histogram_repr(const dynamic_histogram<> &h) {
|
||||
std::ostringstream os;
|
||||
os << h;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
struct storage_access {
|
||||
#ifdef HAVE_NUMPY
|
||||
static
|
||||
python::object
|
||||
array_interface(dynamic_histogram<>& self) {
|
||||
auto& b = self.storage_.buffer_;
|
||||
static python::object array_interface(dynamic_histogram<> &self) {
|
||||
auto &b = self.storage_.buffer_;
|
||||
|
||||
if (b.type_.id_ == 0) {
|
||||
// buffer not created yet, do that now
|
||||
@@ -297,14 +309,14 @@ struct storage_access {
|
||||
strides.append(stride);
|
||||
stride *= s;
|
||||
}
|
||||
PyObject* ptr = PyArray_SimpleNew(self.dim(), shapes2, NPY_DOUBLE);
|
||||
PyObject *ptr = PyArray_SimpleNew(self.dim(), shapes2, NPY_DOUBLE);
|
||||
stride = sizeof(double);
|
||||
for (unsigned i = 0; i < self.dim(); ++i) {
|
||||
const auto s = shape(self.axis(i));
|
||||
PyArray_STRIDES((PyArrayObject*)ptr)[i] = stride;
|
||||
PyArray_STRIDES((PyArrayObject *)ptr)[i] = stride;
|
||||
stride *= s;
|
||||
}
|
||||
double* buf = (double*)PyArray_DATA((PyArrayObject*)ptr);
|
||||
double *buf = (double *)PyArray_DATA((PyArrayObject *)ptr);
|
||||
for (unsigned i = 0; i < b.size_; ++i)
|
||||
buf[i] = static_cast<double>(b.template at<detail::mp_int>(i));
|
||||
d["shape"] = python::tuple(shapes);
|
||||
@@ -337,54 +349,49 @@ struct storage_access {
|
||||
#endif
|
||||
};
|
||||
|
||||
void register_histogram()
|
||||
{
|
||||
void register_histogram() {
|
||||
using namespace python;
|
||||
using python::arg;
|
||||
docstring_options dopt(true, true, false);
|
||||
|
||||
class_<dynamic_histogram<>, boost::shared_ptr<dynamic_histogram<>>>("histogram",
|
||||
"N-dimensional histogram for real-valued data.",
|
||||
no_init)
|
||||
.def("__init__", raw_function(histogram_init),
|
||||
":param axis args: axis objects"
|
||||
"\nPass one or more axis objects to define"
|
||||
"\nthe dimensions of the dynamic_histogram<>.")
|
||||
// shadowed C++ ctors
|
||||
.def(init<const dynamic_histogram<>&>())
|
||||
class_<dynamic_histogram<>, boost::shared_ptr<dynamic_histogram<>>>(
|
||||
"histogram", "N-dimensional histogram for real-valued data.", no_init)
|
||||
.def("__init__", raw_function(histogram_init),
|
||||
":param axis args: axis objects"
|
||||
"\nPass one or more axis objects to define"
|
||||
"\nthe dimensions of the dynamic_histogram<>.")
|
||||
// shadowed C++ ctors
|
||||
.def(init<const dynamic_histogram<> &>())
|
||||
#ifdef HAVE_NUMPY
|
||||
.add_property("__array_interface__",
|
||||
&storage_access::array_interface)
|
||||
.add_property("__array_interface__", &storage_access::array_interface)
|
||||
#endif
|
||||
.def("__len__", &dynamic_histogram<>::dim)
|
||||
.def("__getitem__", histogram_axis)
|
||||
.def("axis", histogram_axis,
|
||||
":param int i: index of the axis\n"
|
||||
":returns: axis object for axis i",
|
||||
(arg("self"), arg("i") = 0))
|
||||
.def("fill", raw_function(histogram_fill),
|
||||
"Pass a sequence of values with a length n is"
|
||||
"\nequal to the dimensions of the histogram,"
|
||||
"\nand optionally a weight w for this fill"
|
||||
"\n(*int* or *float*)."
|
||||
"\n"
|
||||
"\nIf Numpy support is enabled, values may also"
|
||||
"\nbe a 2d-array of shape (m, n), where m is"
|
||||
"\nthe number of tuples, and optionally"
|
||||
"\nanother a second 1d-array w of shape (n,).")
|
||||
.add_property("sum", &dynamic_histogram<>::sum)
|
||||
.def("value", raw_function(histogram_value),
|
||||
":param int args: indices of the bin"
|
||||
"\n:return: count for the bin")
|
||||
.def("variance", raw_function(histogram_variance),
|
||||
":param int args: indices of the bin"
|
||||
"\n:return: variance estimate for the bin")
|
||||
.def("__repr__", histogram_repr,
|
||||
":returns: string representation of the histogram")
|
||||
.def(self == self)
|
||||
.def(self += self)
|
||||
.def_pickle(serialization_suite<dynamic_histogram<>>())
|
||||
;
|
||||
.def("__len__", &dynamic_histogram<>::dim)
|
||||
.def("__getitem__", histogram_axis)
|
||||
.def("axis", histogram_axis, ":param int i: index of the axis\n"
|
||||
":returns: axis object for axis i",
|
||||
(arg("self"), arg("i") = 0))
|
||||
.def("fill", raw_function(histogram_fill),
|
||||
"Pass a sequence of values with a length n is"
|
||||
"\nequal to the dimensions of the histogram,"
|
||||
"\nand optionally a weight w for this fill"
|
||||
"\n(*int* or *float*)."
|
||||
"\n"
|
||||
"\nIf Numpy support is enabled, values may also"
|
||||
"\nbe a 2d-array of shape (m, n), where m is"
|
||||
"\nthe number of tuples, and optionally"
|
||||
"\nanother a second 1d-array w of shape (n,).")
|
||||
.add_property("sum", &dynamic_histogram<>::sum)
|
||||
.def("value", raw_function(histogram_value),
|
||||
":param int args: indices of the bin"
|
||||
"\n:return: count for the bin")
|
||||
.def("variance", raw_function(histogram_variance),
|
||||
":param int args: indices of the bin"
|
||||
"\n:return: variance estimate for the bin")
|
||||
.def("__repr__", histogram_repr,
|
||||
":returns: string representation of the histogram")
|
||||
.def(self == self)
|
||||
.def(self += self)
|
||||
.def_pickle(serialization_suite<dynamic_histogram<>>());
|
||||
}
|
||||
|
||||
} // NS histogram
|
||||
|
||||
Reference in New Issue
Block a user