Skip to content

Commit

Permalink
fix: restore regular_numpy, without a way to create it
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Aug 14, 2023
1 parent f02997f commit c7a9a7a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
8 changes: 6 additions & 2 deletions include/bh_python/axis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <bh_python/pybind11.hpp>

#include <bh_python/metadata.hpp>
#include <bh_python/regular_numpy.hpp>
#include <bh_python/transform.hpp>

#include <boost/histogram/axis.hpp>
Expand Down Expand Up @@ -130,6 +130,9 @@ class boolean : public bh::axis::integer<int, metadata_t, option::none_t> {
// using boolean = bh::axis::boolean<metadata_t>;
BHP_SPECIALIZE_NAME(boolean)

// Axis defined elsewhere
BHP_SPECIALIZE_NAME(regular_numpy)

#undef BHP_SPECIALIZE_NAME

// How edges, centers, and widths are handled
Expand Down Expand Up @@ -216,7 +219,7 @@ py::array_t<double> edges(const A& ax, bool flow = false, bool numpy_upper = fal
for(index_type i = -underflow; i <= ax.size() + overflow; ++i)
edges.mutable_at(i + underflow) = ax.value(i);

if(numpy_upper && !std::is_same<A, axis::regular_none>::value) {
if(numpy_upper && !(std::is_same<A, axis::regular_none>::value || std::is_same<A, axis::regular_numpy>::value) ) {
edges.mutable_at(ax.size() + underflow) = std::nextafter(
edges.at(ax.size() + underflow), std::numeric_limits<double>::min());
}
Expand Down Expand Up @@ -283,6 +286,7 @@ using axis_variant = bh::axis::variant<axis::regular_uoflow,
axis::regular_circular,
axis::regular_pow,
axis::regular_trans,
axis::regular_numpy,
axis::variable_uoflow,
axis::variable_uflow,
axis::variable_oflow,
Expand Down
47 changes: 47 additions & 0 deletions include/bh_python/regular_numpy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2018-2019 Henry Schreiner and Hans Dembinski
//
// Distributed under the 3-Clause BSD License. See accompanying
// file LICENSE or https://github.com/scikit-hep/boost-histogram for details.

#pragma once

#include <bh_python/metadata.hpp>

#include <boost/core/nvp.hpp>
#include <boost/histogram/axis/regular.hpp>

namespace bh = boost::histogram;

namespace axis {

using index_type = boost::histogram::axis::index_type;

/// Mimics the numpy behavior for the upper bin; required for auto range.
class regular_numpy : public bh::axis::regular<double, bh::use_default, metadata_t> {
using value_type = double;
double stop_{0};

public:
regular_numpy(unsigned n, value_type start, value_type stop, metadata_t meta = {})
: regular(n, start, stop, meta)
, stop_(stop) {}

regular_numpy(const regular_numpy& a, index_type i, index_type j, unsigned n)
: regular(a, i, j, n)
, stop_(a.stop_) {}

regular_numpy()
: regular() {}

bh::axis::index_type index(value_type v) const {
return v <= stop_ ? std::min(regular::index(v), size() - 1) : regular::index(v);
}

template <class Archive>
void serialize(Archive& ar, unsigned version) {
regular::serialize(ar, version);
ar& boost::make_nvp("stop", stop_);
}
};

} // namespace axis
4 changes: 3 additions & 1 deletion src/register_axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <bh_python/axis.hpp>
#include <bh_python/kwargs.hpp>
#include <bh_python/register_axis.hpp>
#include <bh_python/regular_numpy.hpp>
#include <boost/mp11.hpp>
#include <vector>

Expand All @@ -29,7 +30,8 @@ void register_axes(py::module& mod) {
axis::regular_oflow,
axis::regular_uoflow,
axis::regular_uoflow_growth,
axis::regular_circular>(mod, [](auto ax) {
axis::regular_circular,
axis::regular_numpy>(mod, [](auto ax) {
ax.def(py::init<unsigned, double, double>(), "bins"_a, "start"_a, "stop"_a);
});

Expand Down

0 comments on commit c7a9a7a

Please sign in to comment.