init from private repository

This commit is contained in:
Hans Dembinski
2016-04-06 09:23:37 -04:00
parent e7622c74a3
commit 19a740fa68
27 changed files with 3174 additions and 0 deletions

View File

@@ -0,0 +1,325 @@
#include <boost/histogram/axis.hpp>
#include <boost/histogram/histogram_base.hpp>
#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>
#include <boost/python/def_visitor.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp>
#include <sstream>
#include <string>
namespace boost {
namespace histogram {
namespace {
python::object
variable_axis_init(python::tuple args, python::dict kwargs) {
using namespace python;
using python::tuple;
object self = args[0];
object pyinit = self.attr("__init__");
if (len(args) < 2) {
PyErr_SetString(PyExc_TypeError, "require at least two arguments");
throw_error_already_set();
}
std::vector<double> v;
for (int i = 1, n = len(args); i < n; ++i) {
v.push_back(extract<double>(args[i]));
}
std::string label;
bool uoflow = true;
while (len(kwargs) > 0) {
tuple kv = kwargs.popitem();
std::string k = extract<std::string>(kv[0]);
object v = kv[1];
if (k == "label")
label = extract<std::string>(v);
else if (k == "uoflow")
uoflow = extract<bool>(v);
else {
std::stringstream s;
s << "keyword " << k << " not recognized";
PyErr_SetString(PyExc_KeyError, s.str().c_str());
throw_error_already_set();
}
}
return pyinit(v, label, uoflow);
}
python::object
category_axis_init(python::tuple args, python::dict kwargs) {
using namespace python;
object self = args[0];
object pyinit = self.attr("__init__");
if (len(args) == 1) {
PyErr_SetString(PyExc_TypeError, "require at least one argument");
throw_error_already_set();
}
if (len(kwargs) > 0) {
PyErr_SetString(PyExc_TypeError, "unknown keyword argument");
throw_error_already_set();
}
if (len(args) == 2) {
extract<std::string> es(args[1]);
if (es.check())
pyinit(es);
else {
PyErr_SetString(PyExc_TypeError, "require one or several string arguments");
throw_error_already_set();
}
}
std::vector<std::string> c;
for (int i = 1, n = len(args); i < n; ++i)
c.push_back(extract<std::string>(args[i]));
return pyinit(c);
}
// python::object
// regular_axis_data(const regular_axis& self) {
// #if HAVE_NUMPY
// py_intp dims[1] = { self.size() + 1 };
// PyArrayObject* a = (PyArrayObject*)PyArray_SimpleNew(1, dims, NPY_DOUBLE);
// for (unsigned i = 0; i < self.size() + 1; ++i) {
// double* pv = (double*)PyArray_GETPTR1(a, i);
// *pv = self.left(i);
// }
// return python::object(handle<>((PyObject*)a));
// #else
// list v;
// for (int i = 0; i < self.size() + 1; ++i)
// v.append(self.left(i));
// return v;
// #endif
// }
struct axis_visitor : public static_visitor<python::object>
{
template <typename T>
python::object operator()(const T& t) const { return python::object(T(t)); }
};
template <typename T>
unsigned
axis_len(const T& t) {
return t.bins() + 1;
}
template <>
unsigned
axis_len(const category_axis& t) {
return t.bins();
}
template <>
unsigned
axis_len(const integer_axis& t) {
return t.bins();
}
template <typename T>
typename T::value_type
axis_getitem(const T& t, int i) {
if (i == axis_len(t)) {
PyErr_SetString(PyExc_StopIteration, "no more");
python::throw_error_already_set();
}
return t[i];
}
template<typename T>
struct has_index_method
{
struct yes { char x[1]; };
struct no { char x[2]; };
template<typename U, int (U::*)(double) const> struct SFINAE {};
template<typename U> static yes test( SFINAE<U, &U::index>* );
template<typename U> static no test( ... );
enum { value = sizeof(test<T>(0)) == sizeof(yes) };
};
std::string escape(const std::string& s) {
std::ostringstream os;
os << '\'';
for (unsigned i = 0; i < s.size(); ++i) {
const char c = s[i];
if (c == '\'' && (i == 0 || s[i - 1] != '\\'))
os << "\\\'";
else
os << c;
}
os << '\'';
return os.str();
}
std::string axis_repr(const regular_axis& a) {
std::stringstream s;
s << "regular_axis(" << a.bins() << ", " << a[0] << ", " << a[a.bins()];
if (!a.label().empty())
s << ", label=" << escape(a.label());
if (!a.uoflow())
s << ", uoflow=False";
s << ")";
return s.str();
}
std::string axis_repr(const polar_axis& a) {
std::stringstream s;
s << "polar_axis(" << a.bins();
if (a[0] != 0.0)
s << ", " << a[0];
if (!a.label().empty())
s << ", label=" << escape(a.label());
s << ")";
return s.str();
}
std::string axis_repr(const variable_axis& a) {
std::stringstream s;
s << "variable_axis(" << a[0];
for (int i = 1; i <= a.bins(); ++i)
s << ", " << a.left(i);
if (!a.label().empty())
s << ", label=" << escape(a.label());
if (!a.uoflow())
s << ", uoflow=False";
s << ")";
return s.str();
}
std::string axis_repr(const category_axis& a) {
std::stringstream s;
s << "category_axis(";
for (int i = 0; i < a.bins(); ++i)
s << escape(a[i]) << (i == (a.bins() - 1)? ")" : ", ");
return s.str();
}
std::string axis_repr(const integer_axis& a) {
std::stringstream s;
s << "integer_axis(" << a[0] << ", " << a[a.bins() - 1];
if (!a.label().empty())
s << ", label=" << escape(a.label());
if (!a.uoflow())
s << ", uoflow=False";
s << ")";
return s.str();
}
template <class T>
struct axis_suite : public python::def_visitor<axis_suite<T> > {
template <typename Class, typename U>
static
typename enable_if_c<has_index_method<U>::value, void>::type
add_axis_index(Class& cl) {
cl.def("index", &U::index);
}
template <typename Class, typename U>
static
typename disable_if_c<has_index_method<U>::value, void>::type
add_axis_index(Class& cl) {}
template <typename Class, typename U>
static
typename enable_if<is_base_of<axis_base, U>, void>::type
add_axis_label(Class& cl) {
cl.add_property("label",
python::make_function((const std::string&(U::*)() const) &U::label,
python::return_value_policy<python::copy_const_reference>()),
(void(U::*)(const std::string&)) &U::label);
}
template <typename Class, typename U>
static
typename disable_if<is_base_of<axis_base, U>, void>::type
add_axis_label(Class& cl) {}
template <class Class>
static void
visit(Class& cl)
{
cl.add_property("bins", &T::bins);
add_axis_index<Class, T>(cl);
add_axis_label<Class, T>(cl);
cl.def("__len__", axis_len<T>);
cl.def("__getitem__", axis_getitem<T>);
cl.def("__repr__", (std::string (*)(const T&)) &axis_repr);
cl.def(python::self == python::self);
}
};
python::object
histogram_base_axis(const histogram_base& self, unsigned i)
{
return apply_visitor(axis_visitor(), self.axis<axis_type>(i));
}
} // namespace
void register_histogram_base() {
using namespace python;
using python::arg;
// used to pass arguments from raw python init to specialized C++ constructors
class_<std::vector<double> >("vector_double", no_init);
class_<std::vector<std::string> >("vector_string", no_init);
class_<axes_type>("axes", no_init);
class_<regular_axis>("regular_axis", no_init)
.def(init<unsigned, double, double, std::string, bool>(
(arg("bin"), arg("min"), arg("max"),
arg("label") = std::string(),
arg("uoflow") = true)))
.def(axis_suite<regular_axis>())
;
class_<polar_axis>("polar_axis", no_init)
.def(init<unsigned, double, std::string>(
(arg("bin"), arg("start") = 0.0,
arg("label") = std::string())))
.def(axis_suite<polar_axis>())
;
class_<variable_axis>("variable_axis", no_init)
.def("__init__", raw_function(variable_axis_init))
.def(init<std::vector<double>, std::string, bool>())
.def(axis_suite<variable_axis>())
;
class_<category_axis>("category_axis", no_init)
.def("__init__", raw_function(category_axis_init))
.def(init<std::string>())
.def(init<std::vector<std::string> >())
.def(axis_suite<category_axis>())
;
class_<integer_axis>("integer_axis", no_init)
.def(init<int, int, std::string, bool>(
(arg("min"), arg("max"),
arg("label") = std::string(),
arg("uoflow") = true)))
.def(axis_suite<integer_axis>())
;
class_<histogram_base>("histogram_base", no_init)
.add_property("dim", &histogram_base::dim)
.def("bins", &histogram_base::bins)
.def("shape", &histogram_base::shape)
.def("axis", histogram_base_axis)
;
}
}
}

22
src/python/module.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include <boost/python/module.hpp>
#ifdef USE_NUMPY
# define PY_ARRAY_UNIQUE_SYMBOL boost_histogram_ARRAY_API
# define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
# include <numpy/arrayobject.h>
#endif
namespace boost {
namespace histogram {
void register_histogram_base();
void register_nhistogram();
}
}
BOOST_PYTHON_MODULE(histogram)
{
#ifdef USE_NUMPY
import_array();
#endif
boost::histogram::register_histogram_base();
boost::histogram::register_nhistogram();
}

183
src/python/nhistogram.cpp Normal file
View File

@@ -0,0 +1,183 @@
#include <boost/histogram/axis.hpp>
#include <boost/histogram/nhistogram.hpp>
#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>
#include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp>
#include "serialization_suite.hpp"
#ifdef USE_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>
#endif
namespace boost {
namespace histogram {
python::object
nhistogram_init(python::tuple args, python::dict kwargs) {
using namespace python;
using python::tuple;
object self = args[0];
object pyinit = self.attr("__init__");
if (kwargs) {
PyErr_SetString(PyExc_TypeError, "no keyword arguments allowed");
throw_error_already_set();
}
// normal constructor
axes_type axes;
for (unsigned i = 1, n = len(args); i < n; ++i) {
object pa = args[i];
extract<regular_axis> er(pa);
if (er.check()) { axes.push_back(er()); continue; }
extract<polar_axis> ep(pa);
if (ep.check()) { axes.push_back(ep()); continue; }
extract<variable_axis> ev(pa);
if (ev.check()) { axes.push_back(ev()); continue; }
extract<category_axis> ec(pa);
if (ec.check()) { axes.push_back(ec()); continue; }
extract<integer_axis> ei(pa);
if (ei.check()) { axes.push_back(ei()); continue; }
PyErr_SetString(PyExc_TypeError, "require an axis object");
throw_error_already_set();
}
return pyinit(axes);
}
python::dict
nhistogram_array_interface(nhistogram& self) {
using namespace python;
using python::tuple;
using python::make_tuple;
list shape;
for (unsigned i = 0; i < self.dim(); ++i)
shape.append(self.shape(i));
dict d;
d["shape"] = tuple(shape);
d["typestr"] = str("<u") + str(self.data().depth());
d["data"] = make_tuple((long long)(self.data().buffer()), false);
return d;
}
python::object
nhistogram_fill(python::tuple args, python::dict kwargs) {
using namespace python;
const unsigned nargs = len(args);
nhistogram& self = extract<nhistogram&>(args[0]);
#ifdef USE_NUMPY
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);
if (!a) {
PyErr_SetString(PyExc_ValueError, "could not convert sequence into array");
throw_error_already_set();
}
npy_intp* dims = PyArray_DIMS(a);
switch (PyArray_NDIM(a)) {
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");
throw_error_already_set();
}
break;
default:
PyErr_SetString(PyExc_ValueError, "array has wrong dimension");
throw_error_already_set();
}
for (unsigned i = 0; i < dims[0]; ++i) {
double* v = (double*)PyArray_GETPTR1(a, i);
self.fill(v);
}
Py_DECREF(a);
return object();
}
}
#endif
const unsigned dim = nargs - 1;
if (dim != self.dim()) {
PyErr_SetString(PyExc_TypeError, "wrong number of arguments");
throw_error_already_set();
}
if (kwargs) {
PyErr_SetString(PyExc_TypeError, "no keyword arguments allowed");
throw_error_already_set();
}
double v[BOOST_HISTOGRAM_AXIS_LIMIT];
for (unsigned i = 0; i < dim; ++i)
v[i] = extract<double>(args[1 + i]);
self.fill(v);
return object();
}
unsigned
nhistogram_depth(const nhistogram& self) {
return self.data().depth();
}
uint64_t
nhistogram_getitem(const nhistogram& self, python::object oidx) {
using namespace python;
if (self.dim() == 1)
return self(extract<int>(oidx)());
const unsigned dim = len(oidx);
if (dim != self.dim()) {
PyErr_SetString(PyExc_RuntimeError, "wrong number of arguments");
throw_error_already_set();
}
int idx[BOOST_HISTOGRAM_AXIS_LIMIT];
for (unsigned i = 0; i < dim; ++i)
idx[i] = extract<int>(oidx[i]);
return self(idx);
}
void register_nhistogram()
{
using namespace python;
class_<
nhistogram, bases<histogram_base>,
shared_ptr<nhistogram>
>("nhistogram", no_init)
.def("__init__", raw_function(nhistogram_init))
// shadowed C++ ctors
.def(init<const axes_type&>())
.add_property("__array_interface__", nhistogram_array_interface)
.def("fill", raw_function(nhistogram_fill))
.add_property("depth", nhistogram_depth)
.add_property("sum", &nhistogram::sum)
.def("__getitem__", nhistogram_getitem)
.def(self == self)
.def(self += self)
.def(self + self)
.def_pickle(serialization_suite<nhistogram>())
;
}
}
}

View File

@@ -0,0 +1,93 @@
#ifndef _BOOST_PYTHON_SERIALIZATION_SUITE_HPP_
#define _BOOST_PYTHON_SERIALIZATION_SUITE_HPP_
#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/python.hpp>
#include <iosfwd>
#include <algorithm>
namespace boost {
class str_sink : public iostreams::sink {
public:
str_sink(PyObject** pstr) :
pstr_(pstr),
len_(0),
pos_(0)
{ assert(*pstr == 0); }
std::streamsize write(const char* s, std::streamsize n)
{
if (len_ == 0) {
*pstr_ = PyString_FromStringAndSize(s, n);
if (*pstr_ == 0) {
PyErr_SetString(PyExc_RuntimeError, "cannot allocate memory");
python::throw_error_already_set();
}
} else {
if (pos_ + n > len_) {
len_ = pos_ + n;
if (_PyString_Resize(pstr_, len_) == -1)
python::throw_error_already_set();
}
char* b = PyString_AS_STRING(*pstr_);
std::copy(s, s + n, b + pos_);
}
pos_ += n;
return n;
}
private:
PyObject** pstr_;
std::streamsize len_;
std::streamsize pos_;
};
template<class T>
struct serialization_suite : python::pickle_suite
{
static
python::tuple getstate(python::object obj)
{
PyObject* pobj = 0;
iostreams::stream<str_sink> os(&pobj);
archive::text_oarchive oa(os);
oa << python::extract<const T&>(obj)();
os.flush();
return python::make_tuple(obj.attr("__dict__"),
python::object(python::handle<>(pobj)));
}
static
void setstate(python::object obj, python::tuple state)
{
if (python::len(state) != 2) {
PyErr_SetObject(PyExc_ValueError,
("expected 2-item tuple in call to __setstate__; got %s"
% state).ptr());
python::throw_error_already_set();
}
// restore the object's __dict__
python::dict d = python::extract<python::dict>(obj.attr("__dict__"));
d.update(state[0]);
// restore the C++ object
python::object o = state[1];
iostreams::stream<iostreams::array_source>
is(python::extract<const char*>(o)(), python::len(o));
archive::text_iarchive ia(is);
ia >> python::extract<T&>(obj)();
}
static
bool getstate_manages_dict() { return true; }
};
}
#endif