diff --git a/.travis.yml b/.travis.yml index baa63f94..43a28c63 100644 --- a/.travis.yml +++ b/.travis.yml @@ -97,6 +97,7 @@ install: fi script: + - python doc/extract_code.py - cd build - if [ "${CMAKE_BUILD_TYPE}" = "cov" ]; then @@ -120,7 +121,6 @@ script: make -j2 && ctest -V; else - python ../doc/extract_code.py; cmake . -DBOOST_ROOT=${BOOST_DIR} -DBUILD_PYTHON=${BUILD_PYTHON} -DPYTHON_VERSION=${PYTHON_VERSION} diff --git a/src/python/histogram.cpp b/src/python/histogram.cpp index 4ee39106..d27ed3c1 100644 --- a/src/python/histogram.cpp +++ b/src/python/histogram.cpp @@ -43,31 +43,27 @@ public: template using array = histogram::adaptive_storage<>::array; - struct dtype_visitor : public static_visitor> { + struct dtype_visitor : public static_visitor { + list & shapes, & strides; + dtype_visitor(list &sh, list &st) : shapes(sh), strides(st) {} template - std::pair operator()(const Array& /*unused*/) const { - std::pair p; - p.first = sizeof(typename Array::value_type); - p.second = dtype_typestr(); - return p; + str operator()(const Array& /*unused*/) const { + strides.append(sizeof(typename Array::value_type)); + return dtype_typestr(); } - std::pair operator()(const array& /*unused*/) const { - std::pair p; - p.first = sizeof(uint8_t); - p.second = dtype_typestr(); - return p; + str operator()(const array& /*unused*/) const { + strides.append(sizeof(uint8_t)); + return dtype_typestr(); } - std::pair operator()(const array& /*unused*/) const { - std::pair p; - p.first = sizeof(double); - p.second = dtype_typestr(); - return p; + str operator()(const array& /*unused*/) const { + strides.append(sizeof(double)); + return dtype_typestr(); } - std::pair operator()(const array& /*unused*/) const { - std::pair p; - p.first = 0; // communicate that the type was array - p.second = dtype_typestr(); - return p; + str operator()(const array& /*unused*/) const { + strides.append(sizeof(double)); + strides.append(strides[-1] * 2); + shapes.append(2); + return dtype_typestr(); } }; @@ -119,31 +115,18 @@ public: 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); - } + d["typestr"] = apply_visitor(dtype_visitor(shapes, strides), b); for (unsigned i = 0; i < self.dim(); ++i) { - const auto s = histogram::shape(self.axis(i)); - shapes.append(s); - strides.append(stride); - stride *= s; + if (i) strides.append(strides[-1] * shapes[-1]); + shapes.append(histogram::shape(self.axis(i))); } - if (self.dim() == 0) { + 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; } diff --git a/test/python_suite_test.py b/test/python_suite_test.py index cde0b048..3a97b074 100644 --- a/test/python_suite_test.py +++ b/test/python_suite_test.py @@ -645,16 +645,18 @@ class test_histogram(unittest.TestCase): for m in range(i+j+k): a.fill(i, j, k) r[i,j,k] = i+j+k - c = numpy.array(a) # a copy - v = numpy.asarray(a) # a view - c2 = numpy.zeros((2, 3, 4), dtype=numpy.int8) + d = numpy.zeros((2, 3, 4), dtype=numpy.int8) for i in range(len(a.axis(0))): for j in range(len(a.axis(1))): for k in range(len(a.axis(2))): - c2[i,j,k] = a.value(i,j,k) + d[i,j,k] = a.value(i,j,k) + + self.assertTrue(numpy.all(d == r)) + + c = numpy.array(a) # a copy + v = numpy.asarray(a) # a view - self.assertTrue(numpy.all(c == c2)) self.assertTrue(numpy.all(c == r)) self.assertTrue(numpy.all(v == r))