mirror of
https://github.com/boostorg/histogram.git
synced 2025-05-12 05:31:51 +00:00
fix serialization issues
This commit is contained in:
parent
784e482037
commit
ce7046d1a4
@ -27,6 +27,9 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
// forward declaration for serialization
|
||||||
|
namespace boost { namespace serialization { class access; }}
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
namespace histogram {
|
namespace histogram {
|
||||||
|
|
||||||
@ -136,8 +139,9 @@ private:
|
|||||||
int shape_ = 0;
|
int shape_ = 0;
|
||||||
std::string label_;
|
std::string label_;
|
||||||
|
|
||||||
|
friend class ::boost::serialization::access;
|
||||||
template <class Archive>
|
template <class Archive>
|
||||||
friend void serialize(Archive &, axis_base<true> &, unsigned);
|
void serialize(Archive &, unsigned);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> class axis_base<false> {
|
template <> class axis_base<false> {
|
||||||
@ -185,8 +189,9 @@ private:
|
|||||||
int size_ = 0;
|
int size_ = 0;
|
||||||
std::string label_;
|
std::string label_;
|
||||||
|
|
||||||
|
friend class ::boost::serialization::access;
|
||||||
template <class Archive>
|
template <class Archive>
|
||||||
friend void serialize(Archive &, axis_base<false> &, unsigned);
|
void serialize(Archive &, unsigned);
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace transform {
|
namespace transform {
|
||||||
@ -281,14 +286,9 @@ public:
|
|||||||
private:
|
private:
|
||||||
value_type min_ = 0.0, delta_ = 1.0;
|
value_type min_ = 0.0, delta_ = 1.0;
|
||||||
|
|
||||||
template <class Archive, typename RealType1,
|
friend class ::boost::serialization::access;
|
||||||
template <class> class Transform1>
|
template <class Archive>
|
||||||
friend void serialize(Archive &, regular_axis<RealType1, Transform1> &,
|
void serialize(Archive &, unsigned);
|
||||||
unsigned);
|
|
||||||
|
|
||||||
// workaround for gcc-4.8
|
|
||||||
template <class Archive, typename RealType1>
|
|
||||||
friend void serialize(Archive &, regular_axis<RealType1> &, unsigned);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Axis for real-valued angles.
|
/** Axis for real-valued angles.
|
||||||
@ -350,8 +350,9 @@ public:
|
|||||||
private:
|
private:
|
||||||
value_type phase_ = 0.0, perimeter_ = 1.0;
|
value_type phase_ = 0.0, perimeter_ = 1.0;
|
||||||
|
|
||||||
template <class Archive, typename RealType1>
|
friend class ::boost::serialization::access;
|
||||||
friend void serialize(Archive &, circular_axis<RealType1> &, unsigned);
|
template <class Archive>
|
||||||
|
void serialize(Archive &, unsigned);
|
||||||
};
|
};
|
||||||
|
|
||||||
/** An axis for real-valued data and bins of varying width.
|
/** An axis for real-valued data and bins of varying width.
|
||||||
@ -442,8 +443,9 @@ public:
|
|||||||
private:
|
private:
|
||||||
std::unique_ptr<value_type[]> x_; // smaller size compared to std::vector
|
std::unique_ptr<value_type[]> x_; // smaller size compared to std::vector
|
||||||
|
|
||||||
template <class Archive, typename RealType1>
|
friend class ::boost::serialization::access;
|
||||||
friend void serialize(Archive &, variable_axis<RealType1> &, unsigned);
|
template <class Archive>
|
||||||
|
void serialize(Archive &, unsigned);
|
||||||
};
|
};
|
||||||
|
|
||||||
/** An axis for a contiguous range of integers.
|
/** An axis for a contiguous range of integers.
|
||||||
@ -499,8 +501,9 @@ public:
|
|||||||
private:
|
private:
|
||||||
value_type min_ = 0;
|
value_type min_ = 0;
|
||||||
|
|
||||||
|
friend class ::boost::serialization::access;
|
||||||
template <class Archive>
|
template <class Archive>
|
||||||
friend void serialize(Archive &, integer_axis &, unsigned);
|
void serialize(Archive &, unsigned);
|
||||||
};
|
};
|
||||||
|
|
||||||
/** An axis for enumerated categories.
|
/** An axis for enumerated categories.
|
||||||
@ -582,8 +585,9 @@ public:
|
|||||||
private:
|
private:
|
||||||
std::unique_ptr<std::string[]> ptr_;
|
std::unique_ptr<std::string[]> ptr_;
|
||||||
|
|
||||||
|
friend class ::boost::serialization::access;
|
||||||
template <class Archive>
|
template <class Archive>
|
||||||
friend void serialize(Archive &, category_axis &, unsigned);
|
void serialize(Archive &, unsigned);
|
||||||
};
|
};
|
||||||
|
|
||||||
using default_axes = mpl::vector<regular_axis<double>, regular_axis<float>,
|
using default_axes = mpl::vector<regular_axis<double>, regular_axis<float>,
|
||||||
|
@ -24,6 +24,12 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
// forward declaration for serialization
|
||||||
|
namespace boost { namespace serialization { class access; }}
|
||||||
|
|
||||||
|
// forward declaration for python
|
||||||
|
namespace boost { namespace python { class access; }}
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
namespace histogram {
|
namespace histogram {
|
||||||
|
|
||||||
@ -274,12 +280,11 @@ private:
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend struct storage_access;
|
|
||||||
|
|
||||||
template <typename D, typename A, typename S> friend class histogram;
|
template <typename D, typename A, typename S> friend class histogram;
|
||||||
|
|
||||||
template <typename Archiv, typename A, typename S>
|
friend class ::boost::python::access;
|
||||||
friend void serialize(Archiv &, histogram<Dynamic, A, S> &, unsigned);
|
friend class ::boost::serialization::access;
|
||||||
|
template <typename Archive> void serialize(Archive &, unsigned);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename... Axes>
|
template <typename... Axes>
|
||||||
|
@ -29,6 +29,9 @@
|
|||||||
#include <boost/mpl/vector.hpp>
|
#include <boost/mpl/vector.hpp>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
|
// forward declaration for serialization
|
||||||
|
namespace boost { namespace serialization { class access; }}
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
namespace histogram {
|
namespace histogram {
|
||||||
|
|
||||||
@ -204,8 +207,8 @@ private:
|
|||||||
|
|
||||||
template <typename D, typename A, typename S> friend class histogram;
|
template <typename D, typename A, typename S> friend class histogram;
|
||||||
|
|
||||||
template <class Archive, class S, class A>
|
friend class ::boost::serialization::access;
|
||||||
friend void serialize(Archive &, histogram<Static, S, A> &, unsigned);
|
template <typename Archive> void serialize(Archive &, unsigned);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// default static type factory
|
/// default static type factory
|
||||||
|
@ -29,7 +29,7 @@ namespace histogram {
|
|||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
template <class Archive>
|
template <class Archive>
|
||||||
inline void serialize(Archive &ar, weight &wt, unsigned /* version */) {
|
void serialize(Archive &ar, weight &wt, unsigned /* version */) {
|
||||||
ar &wt.w;
|
ar &wt.w;
|
||||||
ar &wt.w2;
|
ar &wt.w2;
|
||||||
}
|
}
|
||||||
@ -43,7 +43,7 @@ template <typename Archive> struct serialize_helper {
|
|||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template <class Archive, typename Container>
|
template <class Archive, typename Container>
|
||||||
inline void serialize(Archive &ar, container_storage<Container> &store,
|
void serialize(Archive &ar, container_storage<Container> &store,
|
||||||
unsigned /* version */) {
|
unsigned /* version */) {
|
||||||
ar &store.c_;
|
ar &store.c_;
|
||||||
}
|
}
|
||||||
@ -118,84 +118,79 @@ void adaptive_storage<Allocator>::serialize(Archive &ar,
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class Archive>
|
template <class Archive>
|
||||||
inline void serialize(Archive &ar, axis_base<false> &base,
|
void axis_base<false>::serialize(Archive &ar,
|
||||||
unsigned /* version */) {
|
unsigned /* version */) {
|
||||||
ar &base.size_;
|
ar &size_;
|
||||||
ar &base.label_;
|
ar &label_;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Archive>
|
template <class Archive>
|
||||||
inline void serialize(Archive &ar, axis_base<true> &base,
|
void axis_base<true>::serialize(Archive &ar,
|
||||||
unsigned /* version */) {
|
unsigned /* version */) {
|
||||||
ar &base.size_;
|
ar &size_;
|
||||||
ar &base.shape_;
|
ar &shape_;
|
||||||
ar &base.label_;
|
ar &label_;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Archive, typename RealType, template <class> class Transform>
|
template <typename RealType, template <class> class Transform>
|
||||||
inline void serialize(Archive &ar, regular_axis<RealType, Transform> &axis,
|
template <class Archive>
|
||||||
unsigned /* version */) {
|
void regular_axis<RealType, Transform>::serialize(Archive &ar,
|
||||||
ar &boost::serialization::base_object<axis_base<true>>(axis);
|
unsigned /* version */) {
|
||||||
ar &axis.min_;
|
ar &boost::serialization::base_object<axis_base<true>>(*this);
|
||||||
ar &axis.delta_;
|
ar &min_;
|
||||||
|
ar &delta_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// workaround for gcc-4.8
|
template <typename RealType>
|
||||||
template <class Archive, typename RealType>
|
template <class Archive>
|
||||||
inline void serialize(Archive &ar, regular_axis<RealType> &axis,
|
void circular_axis<RealType>::serialize(Archive &ar,
|
||||||
unsigned /* version */) {
|
unsigned /* version */) {
|
||||||
ar &boost::serialization::base_object<axis_base<true>>(axis);
|
ar &boost::serialization::base_object<axis_base<false>>(*this);
|
||||||
ar &axis.min_;
|
ar &phase_;
|
||||||
ar &axis.delta_;
|
ar &perimeter_;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Archive, typename RealType>
|
template <typename RealType>
|
||||||
inline void serialize(Archive &ar, circular_axis<RealType> &axis,
|
template <class Archive>
|
||||||
|
void variable_axis<RealType>::serialize(Archive &ar,
|
||||||
unsigned /* version */) {
|
unsigned /* version */) {
|
||||||
ar &boost::serialization::base_object<axis_base<false>>(axis);
|
ar &boost::serialization::base_object<axis_base<true>>(*this);
|
||||||
ar &axis.phase_;
|
|
||||||
ar &axis.perimeter_;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class Archive, typename RealType>
|
|
||||||
inline void serialize(Archive &ar, variable_axis<RealType> &axis,
|
|
||||||
unsigned /* version */) {
|
|
||||||
ar &boost::serialization::base_object<axis_base<true>>(axis);
|
|
||||||
if (Archive::is_loading::value) {
|
if (Archive::is_loading::value) {
|
||||||
axis.x_.reset(new RealType[axis.bins() + 1]);
|
x_.reset(new RealType[bins() + 1]);
|
||||||
}
|
}
|
||||||
ar &boost::serialization::make_array(axis.x_.get(), axis.bins() + 1);
|
ar &boost::serialization::make_array(x_.get(), bins() + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Archive>
|
template <class Archive>
|
||||||
inline void serialize(Archive &ar, integer_axis &axis, unsigned /* version */) {
|
void integer_axis::serialize(Archive &ar, unsigned /* version */) {
|
||||||
ar &boost::serialization::base_object<axis_base<true>>(axis);
|
ar &boost::serialization::base_object<axis_base<true>>(*this);
|
||||||
ar &axis.min_;
|
ar &min_;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Archive>
|
template <class Archive>
|
||||||
inline void serialize(Archive &ar, category_axis &axis,
|
void category_axis::serialize(Archive &ar,
|
||||||
unsigned /* version */) {
|
unsigned /* version */) {
|
||||||
ar &boost::serialization::base_object<axis_base<false>>(axis);
|
ar &boost::serialization::base_object<axis_base<false>>(*this);
|
||||||
if (Archive::is_loading::value) {
|
if (Archive::is_loading::value) {
|
||||||
axis.ptr_.reset(new std::string[axis.bins()]);
|
ptr_.reset(new std::string[bins()]);
|
||||||
}
|
}
|
||||||
ar &boost::serialization::make_array(axis.ptr_.get(), axis.bins());
|
ar &boost::serialization::make_array(ptr_.get(), bins());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Archive, class A, class S>
|
template <class A, class S>
|
||||||
inline void serialize(Archive &ar, histogram<Static, A, S> &h,
|
template <class Archive>
|
||||||
unsigned /* version */) {
|
void histogram<Static, A, S>::serialize(Archive &ar, unsigned /* version */) {
|
||||||
detail::serialize_helper<Archive> sh(ar);
|
detail::serialize_helper<Archive> sh(ar);
|
||||||
fusion::for_each(h.axes_, sh);
|
fusion::for_each(axes_, sh);
|
||||||
ar &h.storage_;
|
ar &storage_;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Archive, class A, class S>
|
template <class A, class S>
|
||||||
inline void serialize(Archive &ar, histogram<Dynamic, A, S> &h,
|
template <class Archive>
|
||||||
|
void histogram<Dynamic, A, S>::serialize(Archive &ar,
|
||||||
unsigned /* version */) {
|
unsigned /* version */) {
|
||||||
ar &h.axes_;
|
ar &axes_;
|
||||||
ar &h.storage_;
|
ar &storage_;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace histogram
|
} // namespace histogram
|
||||||
|
@ -17,12 +17,11 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
// forward declaration for serialization workaround
|
// forward declaration for serialization
|
||||||
namespace boost {
|
namespace boost { namespace serialization { class access; }}
|
||||||
namespace serialization {
|
|
||||||
class access;
|
// forward declaration for python
|
||||||
} // namespace serialization
|
namespace boost { namespace python { class access; }}
|
||||||
} // namespace boost
|
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
namespace histogram {
|
namespace histogram {
|
||||||
@ -450,8 +449,7 @@ private:
|
|||||||
|
|
||||||
buffer_type buffer_;
|
buffer_type buffer_;
|
||||||
|
|
||||||
friend struct storage_access;
|
friend class ::boost::python::access;
|
||||||
// workaround for gcc-4.8
|
|
||||||
friend class ::boost::serialization::access;
|
friend class ::boost::serialization::access;
|
||||||
template <class Archive> void serialize(Archive &, unsigned);
|
template <class Archive> void serialize(Archive &, unsigned);
|
||||||
};
|
};
|
||||||
|
@ -11,6 +11,9 @@
|
|||||||
#include <boost/histogram/detail/meta.hpp>
|
#include <boost/histogram/detail/meta.hpp>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
|
// forward declaration for serialization
|
||||||
|
namespace boost { namespace serialization { class access; }}
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
namespace histogram {
|
namespace histogram {
|
||||||
|
|
||||||
@ -81,8 +84,9 @@ private:
|
|||||||
|
|
||||||
template <typename C> friend class container_storage;
|
template <typename C> friend class container_storage;
|
||||||
|
|
||||||
template <typename Archive, typename C>
|
friend class ::boost::serialization::access;
|
||||||
friend void serialize(Archive &, container_storage<C> &, unsigned);
|
template <typename Archive>
|
||||||
|
void serialize(Archive &, unsigned);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace histogram
|
} // namespace histogram
|
||||||
|
@ -27,16 +27,138 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
namespace histogram {
|
|
||||||
|
|
||||||
|
namespace histogram {
|
||||||
using dynamic_histogram = histogram<Dynamic, default_axes, adaptive_storage<>>;
|
using dynamic_histogram = histogram<Dynamic, default_axes, adaptive_storage<>>;
|
||||||
|
} // namespace histogram
|
||||||
|
|
||||||
|
namespace python {
|
||||||
|
|
||||||
#ifdef HAVE_NUMPY
|
#ifdef HAVE_NUMPY
|
||||||
auto array_cast = [](python::handle<>& h) {
|
auto array_cast = [](handle<>& h) {
|
||||||
return python::downcast<PyArrayObject>(h.get());
|
return downcast<PyArrayObject>(h.get());
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_NUMPY
|
||||||
|
class access {
|
||||||
|
public:
|
||||||
|
using mp_int = histogram::adaptive_storage<>::mp_int;
|
||||||
|
using weight = histogram::adaptive_storage<>::weight;
|
||||||
|
template <typename T>
|
||||||
|
using array = histogram::adaptive_storage<>::array<T>;
|
||||||
|
|
||||||
|
struct dtype_visitor : public static_visitor<std::pair<int, object>> {
|
||||||
|
template <typename Array>
|
||||||
|
std::pair<int, object> operator()(const Array& /*unused*/) const {
|
||||||
|
std::pair<int, object> p;
|
||||||
|
p.first = sizeof(typename Array::value_type);
|
||||||
|
p.second = str("|u") + str(p.first);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
std::pair<int, object> operator()(const array<void>& /*unused*/) const {
|
||||||
|
std::pair<int, object> p;
|
||||||
|
p.first = sizeof(uint8_t);
|
||||||
|
p.second = str("|u") + str(p.first);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
std::pair<int, object> operator()(const array<mp_int>& /*unused*/) const {
|
||||||
|
std::pair<int, object> p;
|
||||||
|
p.first = sizeof(double);
|
||||||
|
p.second = str("|f") + str(p.first);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
std::pair<int, object> operator()(const array<weight>& /*unused*/) const {
|
||||||
|
std::pair<int, object> p;
|
||||||
|
p.first = 0; // communicate that the type was array<weight>
|
||||||
|
p.second = str("|f") + str(sizeof(double));
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct data_visitor : public static_visitor<object> {
|
||||||
|
const list& shapes;
|
||||||
|
const list& strides;
|
||||||
|
data_visitor(const list& sh, const list& st) : shapes(sh), strides(st) {}
|
||||||
|
template <typename Array>
|
||||||
|
object operator()(const Array& b) const {
|
||||||
|
return make_tuple(reinterpret_cast<uintptr_t>(b.begin()), true);
|
||||||
|
}
|
||||||
|
object operator()(const array<void>& b) const {
|
||||||
|
// cannot pass non-existent memory to numpy; make new
|
||||||
|
// zero-initialized uint8 array, and pass it
|
||||||
|
int dim = len(shapes);
|
||||||
|
npy_intp shapes2[BOOST_HISTOGRAM_AXIS_LIMIT];
|
||||||
|
for (int i = 0; i < dim; ++i) {
|
||||||
|
shapes2[i] = extract<npy_intp>(shapes[i]);
|
||||||
|
}
|
||||||
|
handle<> a(PyArray_SimpleNew(dim, shapes2, NPY_UINT8));
|
||||||
|
for (int i = 0; i < dim; ++i) {
|
||||||
|
PyArray_STRIDES(array_cast(a))[i] = extract<npy_intp>(strides[i]);
|
||||||
|
}
|
||||||
|
auto *buf = static_cast<uint8_t *>(PyArray_DATA(array_cast(a)));
|
||||||
|
std::fill(buf, buf + b.size, uint8_t(0));
|
||||||
|
PyArray_CLEARFLAGS(array_cast(a), NPY_ARRAY_WRITEABLE);
|
||||||
|
return object(a);
|
||||||
|
}
|
||||||
|
object operator()(const array<mp_int>& b) const {
|
||||||
|
// cannot pass cpp_int to numpy; make new
|
||||||
|
// double array, fill it and pass it
|
||||||
|
int dim = len(shapes);
|
||||||
|
npy_intp shapes2[BOOST_HISTOGRAM_AXIS_LIMIT];
|
||||||
|
for (int i = 0; i < dim; ++i) {
|
||||||
|
shapes2[i] = extract<npy_intp>(shapes[i]);
|
||||||
|
}
|
||||||
|
handle<> a(PyArray_SimpleNew(dim, shapes2, NPY_DOUBLE));
|
||||||
|
for (int i = 0; i < dim; ++i) {
|
||||||
|
PyArray_STRIDES(array_cast(a))[i] = extract<npy_intp>(strides[i]);
|
||||||
|
}
|
||||||
|
auto *buf = static_cast<double *>(PyArray_DATA(array_cast(a)));
|
||||||
|
for (std::size_t i = 0; i < b.size; ++i) {
|
||||||
|
buf[i] = static_cast<double>(b[i]);
|
||||||
|
}
|
||||||
|
PyArray_CLEARFLAGS(array_cast(a), NPY_ARRAY_WRITEABLE);
|
||||||
|
return object(a);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static object array_interface(const histogram::dynamic_histogram &self) {
|
||||||
|
dict d;
|
||||||
|
|
||||||
|
list shapes;
|
||||||
|
list strides;
|
||||||
|
auto &b = self.storage_.buffer_;
|
||||||
|
auto dtype = apply_visitor(dtype_visitor(), b);
|
||||||
|
auto stride = dtype.first;
|
||||||
|
if (stride == 0) { // buffer is weight, needs special treatment
|
||||||
|
stride = sizeof(double);
|
||||||
|
strides.append(stride);
|
||||||
|
stride *= 2;
|
||||||
|
shapes.append(2);
|
||||||
|
}
|
||||||
|
for (unsigned i = 0; i < self.dim(); ++i) {
|
||||||
|
const auto s = shape(self.axis(i));
|
||||||
|
shapes.append(s);
|
||||||
|
strides.append(stride);
|
||||||
|
stride *= s;
|
||||||
|
}
|
||||||
|
if (self.dim() == 0) {
|
||||||
|
shapes.append(0);
|
||||||
|
strides.append(stride);
|
||||||
|
}
|
||||||
|
d["shape"] = tuple(shapes);
|
||||||
|
d["strides"] = tuple(strides);
|
||||||
|
d["typestr"] = dtype.second;
|
||||||
|
d["data"] = apply_visitor(data_visitor(shapes, strides), b);
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} // namespace python
|
||||||
|
|
||||||
|
namespace histogram {
|
||||||
|
|
||||||
struct axis_visitor : public static_visitor<python::object> {
|
struct axis_visitor : public static_visitor<python::object> {
|
||||||
template <typename T> python::object operator()(const T &t) const {
|
template <typename T> python::object operator()(const T &t) const {
|
||||||
return python::object(t);
|
return python::object(t);
|
||||||
@ -123,8 +245,8 @@ python::object histogram_fill(python::tuple args, python::dict kwargs) {
|
|||||||
// exception is thrown automatically if
|
// exception is thrown automatically if
|
||||||
python::handle<> a(PyArray_FROM_OTF(o.ptr(), NPY_DOUBLE, NPY_ARRAY_IN_ARRAY));
|
python::handle<> a(PyArray_FROM_OTF(o.ptr(), NPY_DOUBLE, NPY_ARRAY_IN_ARRAY));
|
||||||
|
|
||||||
npy_intp *dims = PyArray_DIMS(array_cast(a));
|
npy_intp *dims = PyArray_DIMS(python::array_cast(a));
|
||||||
switch (PyArray_NDIM(array_cast(a))) {
|
switch (PyArray_NDIM(python::array_cast(a))) {
|
||||||
case 1:
|
case 1:
|
||||||
if (self.dim() > 1) {
|
if (self.dim() > 1) {
|
||||||
PyErr_SetString(PyExc_ValueError, "array has to be two-dimensional");
|
PyErr_SetString(PyExc_ValueError, "array has to be two-dimensional");
|
||||||
@ -150,20 +272,20 @@ python::object histogram_fill(python::tuple args, python::dict kwargs) {
|
|||||||
python::handle<> aw(
|
python::handle<> aw(
|
||||||
PyArray_FROM_OTF(ow.ptr(), NPY_DOUBLE, NPY_ARRAY_IN_ARRAY));
|
PyArray_FROM_OTF(ow.ptr(), NPY_DOUBLE, NPY_ARRAY_IN_ARRAY));
|
||||||
|
|
||||||
if (PyArray_NDIM(array_cast(aw)) != 1) {
|
if (PyArray_NDIM(python::array_cast(aw)) != 1) {
|
||||||
PyErr_SetString(PyExc_ValueError,
|
PyErr_SetString(PyExc_ValueError,
|
||||||
"array has to be one-dimensional");
|
"array has to be one-dimensional");
|
||||||
python::throw_error_already_set();
|
python::throw_error_already_set();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PyArray_DIMS(array_cast(aw))[0] != dims[0]) {
|
if (PyArray_DIMS(python::array_cast(aw))[0] != dims[0]) {
|
||||||
PyErr_SetString(PyExc_ValueError, "sizes do not match");
|
PyErr_SetString(PyExc_ValueError, "sizes do not match");
|
||||||
python::throw_error_already_set();
|
python::throw_error_already_set();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned i = 0; i < dims[0]; ++i) {
|
for (unsigned i = 0; i < dims[0]; ++i) {
|
||||||
double *v = reinterpret_cast<double *>(PyArray_GETPTR1(array_cast(a), i));
|
double *v = reinterpret_cast<double *>(PyArray_GETPTR1(python::array_cast(a), i));
|
||||||
double *w = reinterpret_cast<double *>(PyArray_GETPTR1(array_cast(aw), i));
|
double *w = reinterpret_cast<double *>(PyArray_GETPTR1(python::array_cast(aw), i));
|
||||||
self.wfill(*w, v, v + self.dim());
|
self.wfill(*w, v, v + self.dim());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,7 +295,7 @@ python::object histogram_fill(python::tuple args, python::dict kwargs) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < dims[0]; ++i) {
|
for (unsigned i = 0; i < dims[0]; ++i) {
|
||||||
double *v = reinterpret_cast<double *>(PyArray_GETPTR1(array_cast(a), i));
|
double *v = reinterpret_cast<double *>(PyArray_GETPTR1(python::array_cast(a), i));
|
||||||
self.fill(v, v + self.dim());
|
self.fill(v, v + self.dim());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -273,120 +395,6 @@ std::string histogram_repr(const dynamic_histogram &h) {
|
|||||||
return os.str();
|
return os.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_NUMPY
|
|
||||||
struct storage_access {
|
|
||||||
using mp_int = adaptive_storage<>::mp_int;
|
|
||||||
using weight = adaptive_storage<>::weight;
|
|
||||||
template <typename T>
|
|
||||||
using array = adaptive_storage<>::array<T>;
|
|
||||||
|
|
||||||
struct dtype_visitor : public static_visitor<std::pair<int, python::object>> {
|
|
||||||
template <typename Array>
|
|
||||||
std::pair<int, python::object> operator()(const Array& /*unused*/) const {
|
|
||||||
std::pair<int, python::object> p;
|
|
||||||
p.first = sizeof(typename Array::value_type);
|
|
||||||
p.second = python::str("|u") + python::str(p.first);
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
std::pair<int, python::object> operator()(const array<void>& /*unused*/) const {
|
|
||||||
std::pair<int, python::object> p;
|
|
||||||
p.first = sizeof(uint8_t);
|
|
||||||
p.second = python::str("|u") + python::str(p.first);
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
std::pair<int, python::object> operator()(const array<mp_int>& /*unused*/) const {
|
|
||||||
std::pair<int, python::object> p;
|
|
||||||
p.first = sizeof(double);
|
|
||||||
p.second = python::str("|f") + python::str(p.first);
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
std::pair<int, python::object> operator()(const array<weight>& /*unused*/) const {
|
|
||||||
std::pair<int, python::object> p;
|
|
||||||
p.first = 0; // communicate that the type was array<weight>
|
|
||||||
p.second = python::str("|f") + python::str(sizeof(double));
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct data_visitor : public static_visitor<python::object> {
|
|
||||||
const python::list& shapes;
|
|
||||||
const python::list& strides;
|
|
||||||
data_visitor(const python::list& sh, const python::list& st) : shapes(sh), strides(st) {}
|
|
||||||
template <typename Array>
|
|
||||||
python::object operator()(const Array& b) const {
|
|
||||||
return python::make_tuple(reinterpret_cast<uintptr_t>(b.begin()), true);
|
|
||||||
}
|
|
||||||
python::object operator()(const array<void>& b) const {
|
|
||||||
// cannot pass non-existent memory to numpy; make new
|
|
||||||
// zero-initialized uint8 array, and pass it
|
|
||||||
int dim = python::len(shapes);
|
|
||||||
npy_intp shapes2[BOOST_HISTOGRAM_AXIS_LIMIT];
|
|
||||||
for (int i = 0; i < dim; ++i) {
|
|
||||||
shapes2[i] = python::extract<npy_intp>(shapes[i]);
|
|
||||||
}
|
|
||||||
python::handle<> a(PyArray_SimpleNew(dim, shapes2, NPY_UINT8));
|
|
||||||
for (int i = 0; i < dim; ++i) {
|
|
||||||
PyArray_STRIDES(array_cast(a))[i] = python::extract<npy_intp>(strides[i]);
|
|
||||||
}
|
|
||||||
auto *buf = static_cast<uint8_t *>(PyArray_DATA(array_cast(a)));
|
|
||||||
std::fill(buf, buf + b.size, uint8_t(0));
|
|
||||||
PyArray_CLEARFLAGS(array_cast(a), NPY_ARRAY_WRITEABLE);
|
|
||||||
return python::object(a);
|
|
||||||
}
|
|
||||||
python::object operator()(const array<mp_int>& b) const {
|
|
||||||
// cannot pass cpp_int to numpy; make new
|
|
||||||
// double array, fill it and pass it
|
|
||||||
int dim = python::len(shapes);
|
|
||||||
npy_intp shapes2[BOOST_HISTOGRAM_AXIS_LIMIT];
|
|
||||||
for (int i = 0; i < dim; ++i) {
|
|
||||||
shapes2[i] = python::extract<npy_intp>(shapes[i]);
|
|
||||||
}
|
|
||||||
python::handle<> a(PyArray_SimpleNew(dim, shapes2, NPY_DOUBLE));
|
|
||||||
for (int i = 0; i < dim; ++i) {
|
|
||||||
PyArray_STRIDES(array_cast(a))[i] = python::extract<npy_intp>(strides[i]);
|
|
||||||
}
|
|
||||||
auto *buf = static_cast<double *>(PyArray_DATA(array_cast(a)));
|
|
||||||
for (std::size_t i = 0; i < b.size; ++i) {
|
|
||||||
buf[i] = static_cast<double>(b[i]);
|
|
||||||
}
|
|
||||||
PyArray_CLEARFLAGS(array_cast(a), NPY_ARRAY_WRITEABLE);
|
|
||||||
return python::object(a);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static python::object array_interface(const dynamic_histogram &self) {
|
|
||||||
python::dict d;
|
|
||||||
|
|
||||||
python::list shapes;
|
|
||||||
python::list strides;
|
|
||||||
auto &b = self.storage_.buffer_;
|
|
||||||
auto dtype = apply_visitor(dtype_visitor(), b);
|
|
||||||
auto stride = dtype.first;
|
|
||||||
if (stride == 0) { // buffer is weight, needs special treatment
|
|
||||||
stride = sizeof(double);
|
|
||||||
strides.append(stride);
|
|
||||||
stride *= 2;
|
|
||||||
shapes.append(2);
|
|
||||||
}
|
|
||||||
for (unsigned i = 0; i < self.dim(); ++i) {
|
|
||||||
const auto s = shape(self.axis(i));
|
|
||||||
shapes.append(s);
|
|
||||||
strides.append(stride);
|
|
||||||
stride *= s;
|
|
||||||
}
|
|
||||||
if (self.dim() == 0) {
|
|
||||||
shapes.append(0);
|
|
||||||
strides.append(stride);
|
|
||||||
}
|
|
||||||
d["shape"] = python::tuple(shapes);
|
|
||||||
d["strides"] = python::tuple(strides);
|
|
||||||
d["typestr"] = dtype.second;
|
|
||||||
d["data"] = apply_visitor(data_visitor(shapes, strides), b);
|
|
||||||
return d;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void register_histogram() {
|
void register_histogram() {
|
||||||
python::docstring_options dopt(true, true, false);
|
python::docstring_options dopt(true, true, false);
|
||||||
|
|
||||||
@ -399,7 +407,7 @@ void register_histogram() {
|
|||||||
// shadowed C++ ctors
|
// shadowed C++ ctors
|
||||||
.def(python::init<const dynamic_histogram &>())
|
.def(python::init<const dynamic_histogram &>())
|
||||||
#ifdef HAVE_NUMPY
|
#ifdef HAVE_NUMPY
|
||||||
.add_property("__array_interface__", &storage_access::array_interface)
|
.add_property("__array_interface__", &python::access::array_interface)
|
||||||
#endif
|
#endif
|
||||||
.def("__len__", &dynamic_histogram::dim)
|
.def("__len__", &dynamic_histogram::dim)
|
||||||
.def("__getitem__", histogram_axis)
|
.def("__getitem__", histogram_axis)
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
|
|
||||||
namespace histogram {
|
namespace histogram {
|
||||||
|
|
||||||
template <typename T> adaptive_storage<> prepare(unsigned n = 1) {
|
template <typename T> adaptive_storage<> prepare(unsigned n = 1) {
|
||||||
@ -48,13 +49,20 @@ template <> adaptive_storage<> prepare<detail::mp_int>(unsigned n) {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct storage_access {
|
} // namespace prepare
|
||||||
template <typename T> static adaptive_storage<> set_value(unsigned n, T x) {
|
|
||||||
adaptive_storage<> s = prepare<T>(n);
|
namespace python { // cheating to get access
|
||||||
get<adaptive_storage<>::array<T>>(s.buffer_)[0] = x;
|
class access {
|
||||||
|
public:
|
||||||
|
template <typename T> static histogram::adaptive_storage<> set_value(unsigned n, T x) {
|
||||||
|
histogram::adaptive_storage<> s = histogram::prepare<T>(n);
|
||||||
|
get<histogram::adaptive_storage<>::array<T>>(s.buffer_)[0] = x;
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
} // namespace python
|
||||||
|
|
||||||
|
namespace histogram {
|
||||||
|
|
||||||
template <typename T> void copy_impl() {
|
template <typename T> void copy_impl() {
|
||||||
const auto b = prepare<T>(1);
|
const auto b = prepare<T>(1);
|
||||||
@ -74,7 +82,7 @@ template <typename T> void copy_impl() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void serialization_impl() {
|
template <typename T> void serialization_impl() {
|
||||||
const auto a = storage_access::set_value(1, T(1));
|
const auto a = python::access::set_value(1, T(1));
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
std::string buf;
|
std::string buf;
|
||||||
{
|
{
|
||||||
@ -115,7 +123,7 @@ template <> void serialization_impl<void>() {
|
|||||||
|
|
||||||
template <typename T> void equal_impl() {
|
template <typename T> void equal_impl() {
|
||||||
adaptive_storage<> a(1);
|
adaptive_storage<> a(1);
|
||||||
auto b = storage_access::set_value(1, T(0));
|
auto b = python::access::set_value(1, T(0));
|
||||||
BOOST_TEST_EQ(a.value(0), 0.0);
|
BOOST_TEST_EQ(a.value(0), 0.0);
|
||||||
BOOST_TEST_EQ(a.variance(0), 0.0);
|
BOOST_TEST_EQ(a.variance(0), 0.0);
|
||||||
BOOST_TEST(a == b);
|
BOOST_TEST(a == b);
|
||||||
@ -123,7 +131,7 @@ template <typename T> void equal_impl() {
|
|||||||
BOOST_TEST(!(a == b));
|
BOOST_TEST(!(a == b));
|
||||||
|
|
||||||
container_storage<std::vector<unsigned>> c(1);
|
container_storage<std::vector<unsigned>> c(1);
|
||||||
auto d = storage_access::set_value(1, T(0));
|
auto d = python::access::set_value(1, T(0));
|
||||||
BOOST_TEST(c == d);
|
BOOST_TEST(c == d);
|
||||||
c.increase(0);
|
c.increase(0);
|
||||||
BOOST_TEST(!(c == d));
|
BOOST_TEST(!(c == d));
|
||||||
@ -131,8 +139,8 @@ template <typename T> void equal_impl() {
|
|||||||
|
|
||||||
template <> void equal_impl<void>() {
|
template <> void equal_impl<void>() {
|
||||||
adaptive_storage<> a(1);
|
adaptive_storage<> a(1);
|
||||||
auto b = storage_access::set_value(1, uint8_t(0));
|
auto b = python::access::set_value(1, uint8_t(0));
|
||||||
auto c = storage_access::set_value(2, uint8_t(0));
|
auto c = python::access::set_value(2, uint8_t(0));
|
||||||
auto d = container_storage<std::vector<unsigned>>(1);
|
auto d = container_storage<std::vector<unsigned>>(1);
|
||||||
BOOST_TEST_EQ(a.value(0), 0.0);
|
BOOST_TEST_EQ(a.value(0), 0.0);
|
||||||
BOOST_TEST_EQ(a.variance(0), 0.0);
|
BOOST_TEST_EQ(a.variance(0), 0.0);
|
||||||
@ -152,7 +160,7 @@ template <> void equal_impl<void>() {
|
|||||||
|
|
||||||
template <typename T> void increase_and_grow_impl() {
|
template <typename T> void increase_and_grow_impl() {
|
||||||
auto tmax = std::numeric_limits<T>::max();
|
auto tmax = std::numeric_limits<T>::max();
|
||||||
auto s = storage_access::set_value<T>(2, tmax - 1);
|
auto s = python::access::set_value<T>(2, tmax - 1);
|
||||||
auto n = s;
|
auto n = s;
|
||||||
auto n2 = s;
|
auto n2 = s;
|
||||||
|
|
||||||
@ -180,7 +188,7 @@ template <> void increase_and_grow_impl<void>() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void convert_container_storage_impl() {
|
template <typename T> void convert_container_storage_impl() {
|
||||||
const auto aref = storage_access::set_value(1, T(0));
|
const auto aref = python::access::set_value(1, T(0));
|
||||||
container_storage<std::vector<uint8_t>> s(1);
|
container_storage<std::vector<uint8_t>> s(1);
|
||||||
s.increase(0);
|
s.increase(0);
|
||||||
|
|
||||||
@ -306,7 +314,7 @@ int main() {
|
|||||||
increase_and_grow_impl<uint64_t>();
|
increase_and_grow_impl<uint64_t>();
|
||||||
|
|
||||||
// only increase for mp_int
|
// only increase for mp_int
|
||||||
auto a = storage_access::set_value(2, detail::mp_int(1));
|
auto a = boost::python::access::set_value(2, detail::mp_int(1));
|
||||||
BOOST_TEST_EQ(a.value(0), 1.0);
|
BOOST_TEST_EQ(a.value(0), 1.0);
|
||||||
BOOST_TEST_EQ(a.value(1), 0.0);
|
BOOST_TEST_EQ(a.value(1), 0.0);
|
||||||
a.increase(0);
|
a.increase(0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user