Skip to content

Commit

Permalink
Fix bug in Sum repr
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Dec 30, 2019
1 parent cd54c82 commit 4ef60b8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
38 changes: 22 additions & 16 deletions include/accumulators/ostream.hpp
Expand Up @@ -38,44 +38,50 @@ handle_nonzero_width(std::basic_ostream<CharT, Traits>& os, const T& x) {
return os;
}

namespace accumulators {

// Note that the names are *not* included here, so they can be added in Pybind11.

template <class CharT, class Traits, class W>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os,
const ::boost::histogram::accumulators::sum<W>& x) {
if(os.width() == 0)
return os << "sum(" << x.large() << " + " << x.small() << ")";
return handle_nonzero_width(os, x);
}
// namespace boost {
// namespace histogram {
// namespace accumulators {
// template <class CharT, class Traits, class W>
// std::basic_ostream<CharT, Traits>&
// operator<<(std::basic_ostream<CharT, Traits>& os,
// const sum<W>& x) {
// if(os.width() == 0)
// return os << x.large() << " + " << x.small();
// return handle_nonzero_width(os, x);
//}
//
//} // namespace accumulators
//} // namespace histogram
//} // namespace boost

namespace accumulators {

template <class CharT, class Traits, class W>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
const weighted_sum<W>& x) {
if(os.width() == 0)
return os << "weighted_sum(value=" << x.value << ", variance=" << x.variance
<< ")";
return os << "value=" << x.value << ", variance=" << x.variance;
return handle_nonzero_width(os, x);
}

template <class CharT, class Traits, class W>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
const mean<W>& x) {
if(os.width() == 0)
return os << "mean(count=" << x.count << ", value=" << x.value
<< ", variance=" << x.variance() << ")";
return os << "count=" << x.count << ", value=" << x.value
<< ", variance=" << x.variance();
return handle_nonzero_width(os, x);
}

template <class CharT, class Traits, class W>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
const weighted_mean<W>& x) {
if(os.width() == 0)
return os << "weighted_mean(sum_of_weights=" << x.sum_of_weights
return os << "sum_of_weights=" << x.sum_of_weights
<< ", sum_of_weights_squared=" << x.sum_of_weights_squared
<< ", value=" << x.value << ", variance=" << x.variance() << ")";
<< ", value=" << x.value << ", variance=" << x.variance();
return handle_nonzero_width(os, x);
}

Expand Down
3 changes: 1 addition & 2 deletions include/register_accumulator.hpp
Expand Up @@ -28,8 +28,7 @@ py::class_<A> register_accumulator(py::module acc, Args&&... args) {
[](py::object self) {
const A& item = py::cast<const A&>(self);
py::str str = shift_to_string(item);
str = str.attr("split")("(", 2).attr("__getitem__")(1);
return py::str("{0.__class__.__name__}({1}").format(self, str);
return py::str("{0.__class__.__name__}({1})").format(self, str);
})

.def("__copy__", [](const A& self) { return A(self); })
Expand Down
10 changes: 10 additions & 0 deletions tests/test_accumulators.py
Expand Up @@ -8,6 +8,8 @@
def test_weighted_sum():
a = bh.accumulators.WeightedSum(1.5, 2.5)

assert repr(a) == "WeightedSum(value=1.5, variance=2.5)"

assert a == bh.accumulators.WeightedSum(1.5, 2.5)

assert a.value == 1.5
Expand Down Expand Up @@ -47,6 +49,8 @@ def test_sum():

assert a == bh.accumulators.Sum(6)

assert repr(a) == "Sum(6)"


def test_weighted_mean():
vals = [4, 1]
Expand All @@ -63,6 +67,10 @@ def test_weighted_mean():
assert a == a2

assert a == bh.accumulators.WeightedMean(3, 5, 2, 4.5)
assert (
repr(a)
== "WeightedMean(sum_of_weights=3, sum_of_weights_squared=5, value=2, variance=4.5)"
)


def test_mean():
Expand All @@ -79,3 +87,5 @@ def test_mean():
assert a == a2

assert a == bh.accumulators.Mean(3, 2, 1)

assert repr(a) == "Mean(count=3, value=2, variance=1)"

0 comments on commit 4ef60b8

Please sign in to comment.