added serialization to python/histogram

This commit is contained in:
klemens-morgenstern
2016-06-20 19:42:27 +02:00
parent fc86a1c465
commit 88456d9daf

View File

@@ -7,6 +7,7 @@
#include "serialization_suite.hpp"
#include <boost/histogram/axis.hpp>
#include <boost/histogram/histogram.hpp>
#include <boost/histogram/serialization.hpp>
#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>
#include <boost/foreach.hpp>
@@ -74,8 +75,8 @@ histogram_fill(python::tuple args, python::dict kwargs) {
if (nargs == 2) {
object o = args[1];
if (PySequence_Check(o.ptr())) {
PyArrayObject* a = (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");
throw_error_already_set();
@@ -103,8 +104,8 @@ histogram_fill(python::tuple args, python::dict kwargs) {
if (!ow.is_none()) {
if (PySequence_Check(ow.ptr())) {
PyArrayObject* aw = (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");
throw_error_already_set();
@@ -121,9 +122,9 @@ histogram_fill(python::tuple args, python::dict kwargs) {
}
for (unsigned i = 0; i < dims[0]; ++i) {
double* v = (double*)PyArray_GETPTR1(a, i);
double* w = (double*)PyArray_GETPTR1(aw, i);
self.wfill_c(self.dim(), v, *w);
double* v = reinterpret_cast<double*>(PyArray_GETPTR1(a, i) );
double* w = reinterpret_cast<double*>(PyArray_GETPTR1(aw, i));
self.wfill(boost::make_iterator_range(v, v+self.dim()), *w);
}
Py_DECREF(aw);
@@ -133,8 +134,8 @@ histogram_fill(python::tuple args, python::dict kwargs) {
}
} else {
for (unsigned i = 0; i < dims[0]; ++i) {
double* v = (double*)PyArray_GETPTR1(a, i);
self.fill_c(self.dim(), v);
double* v = reinterpret_cast<double*>(PyArray_GETPTR1(a, i));
self.fill(boost::make_iterator_range(v, v+self.dim()));
}
}
@@ -155,10 +156,11 @@ histogram_fill(python::tuple args, python::dict kwargs) {
v[i] = extract<double>(args[1 + i]);
if (ow.is_none()) {
self.fill_c(self.dim(), v);
self.fill(boost::make_iterator_range(v, v+self.dim()));
} else {
const double w = extract<double>(ow);
self.wfill_c(self.dim(), v, w);
self.wfill(boost::make_iterator_range(v, v+self.dim()), w);
}
return object();
@@ -183,7 +185,7 @@ histogram_value(python::tuple args, python::dict kwargs) {
for (unsigned i = 0; i < self.dim(); ++i)
idx[i] = extract<int>(args[1 + i]);
return object(self.value_c(self.dim(), idx));
return object(self.value(boost::make_iterator_range(idx, idx + self.dim())));
}
python::object
@@ -205,7 +207,7 @@ histogram_variance(python::tuple args, python::dict kwargs) {
for (unsigned i = 0; i < self.dim(); ++i)
idx[i] = extract<int>(args[1 + i]);
return object(self.variance_c(self.dim(), idx));
return object(self.variance(boost::make_iterator_range(idx, idx + self.dim())));
}
class histogram_access {
@@ -217,14 +219,14 @@ public:
python::list shape;
for (unsigned i = 0; i < self.dim(); ++i)
shape.append(self.shape(i));
if (self.data_.depth() == sizeof(detail::wtype)) {
if (self.depth() == sizeof(detail::wtype)) {
shape.append(2);
d["typestr"] = python::str("<f") + python::str(sizeof(double));
} else {
d["typestr"] = python::str("<u") + python::str(self.data_.depth());
d["typestr"] = python::str("<u") + python::str(self.depth());
}
d["shape"] = python::tuple(shape);
d["data"] = python::make_tuple((long long)(self.data_.buffer()), false);
d["data"] = python::make_tuple(reinterpret_cast<boost::uintptr_t>(self.buffer()), false);
return d;
}
};