tabula rasa

This commit is contained in:
Hans Dembinski
2016-07-16 12:45:16 -04:00
parent ab91774765
commit 6f24a50fdd
70 changed files with 545 additions and 6631 deletions

View File

@@ -1,270 +0,0 @@
// Copyright 2015-2016 Hans Dembinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/histogram/axis.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);
}
template <typename T>
int
axis_len(const T& t) {
return t.bins() + 1;
}
template <>
int
axis_len(const category_axis& t) {
return t.bins();
}
template <>
int
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>
std::string
axis_repr(const T& t) {
std::ostringstream os;
os << t;
return os.str();
}
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) };
};
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,
":param float x: value"
"\n:returns: bin index for the passed value",
python::args("self", "x"));
}
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,
"Name or description for the axis.");
}
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>,
":returns: number of bins for this axis",
python::arg("self"));
cl.def("__getitem__", axis_getitem<T>,
is_same<T, integer_axis>::value ?
":returns: integer mapped to passed bin index" :
is_same<T, category_axis>::value ?
":returns: category mapped to passed bin index" :
":returns: low edge of the bin",
python::args("self", "index"));
cl.def("__repr__", axis_repr<T>,
":returns: string representation of this axis",
python::arg("self"));
cl.def(python::self == python::self);
}
};
} // namespace
void register_axis_types()
{
using namespace python;
using python::arg;
docstring_options dopt(true, true, false);
// 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_<regular_axis>("regular_axis",
"An axis for real-valued data and bins of equal width."
"\nBinning is a O(1) operation.",
no_init)
.def(init<unsigned, double, double, std::string, bool>(
(arg("self"), arg("bin"), arg("min"), arg("max"),
arg("label") = std::string(),
arg("uoflow") = true)))
.def(axis_suite<regular_axis>())
;
class_<polar_axis>("polar_axis",
"An axis for real-valued angles."
"\nThere are no overflow/underflow bins for this axis,"
"\nsince the axis is circular and wraps around after 2pi."
"\nBinning is a O(1) operation.",
no_init)
.def(init<unsigned, double, std::string>(
(arg("self"), arg("bin"), arg("start") = 0.0,
arg("label") = std::string())))
.def(axis_suite<polar_axis>())
;
class_<variable_axis>("variable_axis",
"An axis for real-valued data and bins of varying width."
"\nBinning is a O(log(N)) operation. If speed matters and"
"\nthe problem domain allows it, prefer a regular_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",
"An axis for enumerated categories. The axis stores the"
"\ncategory labels, and expects that they are addressed"
"\nusing an integer from 0 to n-1. There are no"
"\nunderflow/overflow bins for this axis."
"\nBinning is a O(1) operation.",
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",
"An axis for a contiguous range of integers."
"\nThere are no underflow/overflow bins for this axis."
"\nBinning is a O(1) operation.",
no_init)
.def(init<int, int, std::string, bool>(
(arg("self"), arg("min"), arg("max"),
arg("label") = std::string(),
arg("uoflow") = true)))
.def(axis_suite<integer_axis>())
;
}
}
}

View File

@@ -1,53 +0,0 @@
// Copyright 2015-2016 Hans Dembinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/histogram/axis.hpp>
#include <boost/histogram/basic_histogram.hpp>
#include <boost/python.hpp>
#include <boost/python/def_visitor.hpp>
namespace boost {
namespace histogram {
namespace {
struct axis_visitor : public static_visitor<python::object>
{
template <typename T>
python::object operator()(const T& t) const { return python::object(T(t)); }
};
python::object
basic_histogram_axis(const basic_histogram& self, unsigned i)
{
return apply_visitor(axis_visitor(), self.axis<axis_type>(i));
}
} // namespace
void register_basic_histogram() {
using namespace python;
using python::arg;
docstring_options dopt(true, true, false);
class_<basic_histogram>("basic_histogram", no_init)
.add_property("dim", &basic_histogram::dim,
"dimensions of the histogram")
.def("shape", &basic_histogram::shape,
":param int i: index of the axis\n"
":returns: number of count fields for axis i\n"
" (bins + 2 if underflow and overflow"
" bins are enabled, otherwise equal to bins",
args("self", "i"))
.def("axis", basic_histogram_axis,
":param int i: index of the axis\n"
":returns: axis object for axis i",
args("self", "i"))
;
}
}
}

View File

@@ -1,282 +0,0 @@
// Copyright 2015-2016 Hans Dembinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#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>
#include <boost/shared_ptr.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>
#endif
namespace boost {
namespace histogram {
python::object
histogram_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_RuntimeError, "no keyword arguments allowed");
throw_error_already_set();
}
// normal constructor
basic_histogram::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::object
histogram_fill(python::tuple args, python::dict kwargs) {
using namespace python;
const unsigned nargs = len(args);
histogram& self = extract<histogram&>(args[0]);
object ow;
if (kwargs) {
if (len(kwargs) > 1 || !kwargs.has_key("w")) {
PyErr_SetString(PyExc_RuntimeError, "only keyword w allowed");
throw_error_already_set();
}
ow = kwargs.get("w");
}
#ifdef HAVE_NUMPY
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));
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();
}
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));
if (!aw) {
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");
throw_error_already_set();
}
if (PyArray_DIMS(aw)[0] != dims[0]) {
PyErr_SetString(PyExc_ValueError, "sizes do not match");
throw_error_already_set();
}
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(v, v+self.dim(), *w);
}
Py_DECREF(aw);
} else {
PyErr_SetString(PyExc_ValueError, "w is not a sequence");
throw_error_already_set();
}
} else {
for (unsigned i = 0; i < dims[0]; ++i) {
double* v = reinterpret_cast<double*>(PyArray_GETPTR1(a, i));
self.fill(v, v+self.dim());
}
}
Py_DECREF(a);
return object();
}
}
#endif
const unsigned dim = nargs - 1;
if (dim != self.dim()) {
PyErr_SetString(PyExc_RuntimeError, "wrong number of arguments");
throw_error_already_set();
}
double v[BOOST_HISTOGRAM_AXIS_LIMIT];
for (unsigned i = 0; i < dim; ++i)
v[i] = extract<double>(args[1 + i]);
if (ow.is_none()) {
self.fill(v, v+self.dim());
} else {
const double w = extract<double>(ow);
self.wfill(v, v+self.dim(), w);
}
return object();
}
python::object
histogram_value(python::tuple args, python::dict kwargs) {
using namespace python;
const histogram& self = extract<const histogram&>(args[0]);
if (self.dim() != (len(args) - 1)) {
PyErr_SetString(PyExc_RuntimeError, "wrong number of arguments");
throw_error_already_set();
}
if (kwargs) {
PyErr_SetString(PyExc_ValueError, "no keyword arguments allowed");
throw_error_already_set();
}
int idx[BOOST_HISTOGRAM_AXIS_LIMIT];
for (unsigned i = 0; i < self.dim(); ++i)
idx[i] = extract<int>(args[1 + i]);
return object(self.value(idx, idx + self.dim()));
}
python::object
histogram_variance(python::tuple args, python::dict kwargs) {
using namespace python;
const histogram& self = extract<const histogram&>(args[0]);
if (self.dim() != (len(args) - 1)) {
PyErr_SetString(PyExc_RuntimeError, "wrong number of arguments");
throw_error_already_set();
}
if (kwargs) {
PyErr_SetString(PyExc_RuntimeError, "no keyword arguments allowed");
throw_error_already_set();
}
int idx[BOOST_HISTOGRAM_AXIS_LIMIT];
for (unsigned i = 0; i < self.dim(); ++i)
idx[i] = extract<int>(args[1 + i]);
return object(self.variance(idx, idx + self.dim()));
}
class histogram_access {
public:
static
python::dict
histogram_array_interface(histogram& self) {
python::dict d;
python::list shape;
for (unsigned i = 0; i < self.dim(); ++i)
shape.append(self.shape(i));
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.depth());
}
d["shape"] = python::tuple(shape);
d["data"] = python::make_tuple(reinterpret_cast<boost::uintptr_t>(self.buffer()), false);
return d;
}
};
void register_histogram()
{
using namespace python;
docstring_options dopt(true, true, false);
// used to pass arguments from raw python init to specialized C++ constructor
class_<basic_histogram::axes_type>("axes", no_init);
class_<
histogram, bases<basic_histogram>,
shared_ptr<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 histogram.")
// shadowed C++ ctors
.def(init<const basic_histogram::axes_type&>())
.add_property("__array_interface__",
&histogram_access::histogram_array_interface)
.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("depth", &histogram::depth)
.add_property("sum", &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(self == self)
.def(self += self)
.def(self + self)
.def_pickle(serialization_suite<histogram>())
;
}
}
}

View File

@@ -1,35 +0,0 @@
// Copyright 2015-2016 Hans Dembinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/python/module.hpp>
#ifdef HAVE_NUMPY
# define PY_ARRAY_UNIQUE_SYMBOL boost_histogram_ARRAY_API
# define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
# include <numpy/arrayobject.h>
# if PY_MAJOR_VERSION >= 3
static void* init_numpy() { import_array(); return NULL; }
# else
static void init_numpy() { import_array(); }
# endif
#endif
namespace boost {
namespace histogram {
void register_axis_types();
void register_basic_histogram();
void register_histogram();
}
}
BOOST_PYTHON_MODULE(histogram)
{
#ifdef HAVE_NUMPY
init_numpy();
#endif
boost::histogram::register_axis_types();
boost::histogram::register_basic_histogram();
boost::histogram::register_histogram();
}

View File

@@ -1,117 +0,0 @@
// Copyright 2015-2016 Hans Dembinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#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 <boost/assert.hpp>
#include <iosfwd>
#include <algorithm>
namespace boost {
namespace histogram {
namespace detail {
#if PY_MAJOR_VERSION < 3
# define PyBytes_FromStringAndSize PyString_FromStringAndSize
# define PyBytes_AS_STRING PyString_AS_STRING
# define PyBytes_Size PyString_Size
# define _PyBytes_Resize _PyString_Resize
#endif
class python_bytes_sink : public iostreams::sink {
public:
python_bytes_sink(PyObject** pstr) :
pstr_(pstr),
len_(0),
pos_(0)
{ BOOST_ASSERT(*pstr == 0); }
std::streamsize write(const char* s, std::streamsize n)
{
if (len_ == 0) {
*pstr_ = PyBytes_FromStringAndSize(s, n);
if (*pstr_ == 0) {
PyErr_SetString(PyExc_RuntimeError, "cannot allocate memory");
python::throw_error_already_set();
}
len_ = n;
} else {
if (pos_ + n > len_) {
len_ = pos_ + n;
if (_PyBytes_Resize(pstr_, len_) == -1)
python::throw_error_already_set();
}
char* b = PyBytes_AS_STRING(*pstr_);
std::copy(s, s + n, b + pos_);
}
pos_ += n;
return n;
}
private:
PyObject** pstr_;
std::streamsize len_, pos_;
};
}
template<class T>
struct serialization_suite : python::pickle_suite
{
static
python::tuple getstate(python::object obj)
{
PyObject* pobj = 0;
iostreams::stream<detail::python_bytes_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(PyBytes_AS_STRING(o.ptr()), PyBytes_Size(o.ptr()));
archive::text_iarchive ia(is);
ia >> python::extract<T&>(obj)();
}
static
bool getstate_manages_dict() { return true; }
};
#undef PyBytes_FromStringAndSize
#undef PyBytes_AS_STRING
#undef PyBytes_Size
#undef _PyBytes_Resize
}
}
#endif