Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Use integer weights for integer storages #290

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions include/bh_python/fill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ using arg_t = variant::variant<c_array_t<double>,
int,
c_array_t<std::string>,
std::string>;

using weight_t = variant::variant<variant::monostate, double, c_array_t<double>>;
template <class T>
using weight_t = variant::variant<variant::monostate, T, c_array_t<T>>;

inline auto get_vargs(const vector_axis_variant& axes, const py::args& args) {
if(args.size() != axes.size())
Expand Down Expand Up @@ -131,25 +131,26 @@ inline auto get_vargs(const vector_axis_variant& axes, const py::args& args) {
return vargs;
}

inline auto get_weight(py::kwargs& kwargs) {
template <class T>
auto get_weight(py::kwargs& kwargs) {
// default constructed as monostate to indicate absence of weight
variant::variant<variant::monostate, double, c_array_t<double>> weight;
weight_t<T> weight;
auto w = optional_arg(kwargs, "weight");
if(!w.is_none()) {
if(is_value<double>(w))
weight = py::cast<double>(w);
if(is_value<T>(w))
weight = py::cast<T>(w);
else
weight = py::cast<c_array_t<double>>(w);
weight = py::cast<c_array_t<T>>(w);
}
return weight;
}

// for accumulators that accept a weight
template <class Histogram, class VArgs>
template <class weight_type, class Histogram, class VArgs>
void fill_impl(bh::detail::accumulator_traits_holder<true>,
Histogram& h,
const VArgs& vargs,
const weight_t& weight,
const weight_t<weight_type>& weight,
py::kwargs& kwargs) {
none_only_arg(kwargs, "sample");
finalize_args(kwargs);
Expand All @@ -163,11 +164,11 @@ void fill_impl(bh::detail::accumulator_traits_holder<true>,
}

// for accumulators that accept a weight and a double
template <class Histogram, class VArgs>
template <class weight_type, class Histogram, class VArgs>
void fill_impl(bh::detail::accumulator_traits_holder<true, const double&>,
Histogram& h,
const VArgs& vargs,
const weight_t& weight,
const weight_t<weight_type>& weight,
py::kwargs& kwargs) {
auto s = required_arg(kwargs, "sample");
finalize_args(kwargs);
Expand All @@ -192,10 +193,14 @@ void fill_impl(bh::detail::accumulator_traits_holder<true, const double&>,
template <class Histogram>
Histogram& fill(Histogram& self, py::args args, py::kwargs kwargs) {
using value_type = typename Histogram::value_type;
detail::fill_impl(bh::detail::accumulator_traits<value_type>{},
self,
detail::get_vargs(bh::unsafe_access::axes(self), args),
detail::get_weight(kwargs),
kwargs);
using weight_type
= boost::mp11::mp_if_c<std::is_integral<value_type>::value, int, double>;

detail::fill_impl<weight_type>(
bh::detail::accumulator_traits<value_type>{},
self,
detail::get_vargs(bh::unsafe_access::axes(self), args),
detail::get_weight<weight_type>(kwargs),
kwargs);
return self;
}
8 changes: 4 additions & 4 deletions src/boost_histogram/_internal/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

def _fill_cast(value, inner=False):
"""
Convert to NumPy arrays. Some buffer objects do not get converted by forcecast.
Convert to NumPy arrays; must be c-order dense arrays to work.
If not called by itself (inner=False), then will work through one level of tuple/list.
"""
if value is None or isinstance(value, string_types + (bytes,)):
Expand Down Expand Up @@ -340,9 +340,9 @@ def fill(self, *args, **kwargs): # noqa: C901
----------
*args : Union[Array[float], Array[int], Array[str], float, int, str]
Provide one value or array per dimension.
weight : List[Union[Array[float], Array[int], Array[str], float, int, str]]]
Provide weights (only if the histogram storage supports it)
sample : List[Union[Array[float], Array[int], Array[str], float, int, str]]]
weight : List[Union[Array[float], Array[int], float, int]]]
Provide weights (float only if the histogram storage supports it)
sample : List[Union[Array[float], Array[int], float, int]]]
Provide samples (only if the histogram storage supports it)
threads : Optional[int]
Fill with threads. Defaults to None, which does not activate
Expand Down
1 change: 1 addition & 0 deletions src/boost_histogram/_internal/sig_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def inject_signature(sig, locals=None):
# type: (str, Optional[Dict[str, Any]]) -> Any
def wrap(f):
return f

return wrap


Expand Down
6 changes: 6 additions & 0 deletions tests/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def test_copy():
assert id(b) != id(c)


def test_fill_int_storage_with_floats():
h = bh.Histogram(bh.axis.Regular(10,-1,1), storage=bh.storage.Int64())
h.fill([.3,.4,.5], weight=[1, 3, 2])
h.fill([.3,.4,.5], weight=[.1, .3, .2])


def test_fill_int_1d():

h = bh.Histogram(bh.axis.Integer(-1, 2))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_numpy_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
{"bins": 10},
{"bins": "auto" if np113 else 20},
{"range": (0, 5), "bins": 30},
{"range": np.array((0, 5), dtype=np.float), "bins": np.int32(30)},
{"range": np.array((0, 5), dtype=float), "bins": np.int32(30)},
{"range": np.array((0, 3), dtype=np.double), "bins": np.uint32(10)},
{"range": np.array((0, 10), dtype=np.int), "bins": np.int8(30)},
{"range": np.array((0, 10), dtype=int), "bins": np.int8(30)},
{"bins": [0, 1, 1.2, 1.3, 4, 21]},
)

Expand Down