fixing most of the python errors

This commit is contained in:
Hans Dembinski
2018-07-02 01:01:04 +02:00
parent 5fc67137e4
commit cfd47c4c11
8 changed files with 260 additions and 202 deletions

View File

@@ -212,6 +212,14 @@ struct fetcher {
}
};
template <typename T>
struct span {
T* data;
unsigned size;
const T* begin() const { return data; }
const T* end() const { return data + size; }
};
bp::object histogram_fill(bp::tuple args, bp::dict kwargs) {
const auto nargs = bp::len(args);
pyhistogram &self = bp::extract<pyhistogram &>(args[0]);
@@ -263,21 +271,37 @@ bp::object histogram_fill(bp::tuple args, bp::dict kwargs) {
}
}
double v[BOOST_HISTOGRAM_AXIS_LIMIT];
if (!n) ++n;
for (auto i = 0l; i < n; ++i) {
for (auto d = 0u; d < dim; ++d)
v[d] = fetch[d][i];
if (fetch_weight.n >= 0)
self.fill(v, v + dim, bh::weight(fetch_weight[i]));
else
self.fill(v, v + dim);
if (dim == 1) {
if (fetch_weight.n >= 0) {
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]);
}
} 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];
self(bh::weight(fetch_weight[i]), span<double>{v, dim});
}
}
else {
for (auto i = 0l; i < n; ++i) {
for (auto d = 0u; d < dim; ++d)
v[d] = fetch[d][i];
self(span<double>{v, dim});
}
}
}
return bp::object();
}
bp::object histogram_call(bp::tuple args, bp::dict kwargs) {
bp::object histogram_bin(bp::tuple args, bp::dict kwargs) {
const pyhistogram & self = bp::extract<const pyhistogram &>(args[0]);
const unsigned dim = bp::len(args) - 1;
@@ -299,10 +323,13 @@ bp::object histogram_call(bp::tuple args, bp::dict kwargs) {
}
int idx[BOOST_HISTOGRAM_AXIS_LIMIT];
for (unsigned i = 0; i < self.dim(); ++i)
for (unsigned i = 0; i < dim; ++i)
idx[i] = bp::extract<int>(args[1 + i]);
return bp::object(self(idx, idx + self.dim()));
if (dim == 1)
return bp::object(self.bin(idx[0]));
else
return bp::object(self.bin(span<int>{idx, self.dim()}));
}
bp::object histogram_reduce_to(bp::tuple args, bp::dict kwargs) {
@@ -349,7 +376,10 @@ void register_histogram() {
bp::class_<pyhistogram::element_type>(
"element", "Holds value and variance of bin count.", bp::no_init)
.add_property("value", element_value)
.add_property("variance", element_variance);
.add_property("variance", element_variance)
.def(bp::self + bp::self)
.def(bp::self += bp::self)
;
bp::class_<pyhistogram, boost::shared_ptr<pyhistogram>>(
"histogram", "N-dimensional histogram for real-valued data.", bp::no_init)
@@ -366,7 +396,7 @@ void register_histogram() {
.def("axis", histogram_axis, bp::arg("i") = 0,
":param int i: axis index"
"\n:return: corresponding axis object")
.def("fill", bp::raw_function(histogram_fill),
.def("__call__", bp::raw_function(histogram_fill),
":param double args: values (number must match dimension)"
"\n:keyword double weight: optional weight"
"\n"
@@ -375,9 +405,7 @@ void register_histogram() {
"\nbe mixed arbitrarily in the same call.")
.add_property("bincount", &pyhistogram::bincount,
":return: total number of bins, including under- and overflow")
.add_property("sum", &pyhistogram::sum,
":return: sum of all entries, including under- and overflow bins")
.def("__call__", bp::raw_function(histogram_call),
.def("bin", bp::raw_function(histogram_bin),
":param int args: indices of the bin (number must match dimension)"
"\n:return: bin content")
.def("reduce_to", bp::raw_function(histogram_reduce_to),
@@ -391,5 +419,6 @@ void register_histogram() {
.def(bp::self * double())
.def(double() * bp::self)
.def(bp::self + bp::self)
.def_pickle(bh::serialization_suite<pyhistogram>());
.def_pickle(bh::serialization_suite<pyhistogram>())
;
}