boost-histogram icon indicating copy to clipboard operation
boost-histogram copied to clipboard

[FEAT] Add operators (mult/div/sub) for (weighted) histograms/accumulators

Open jonas-eschle opened this issue 3 years ago • 7 comments

Describe the bug

Dividing/multiplying/subtracting histograms with weights results in an AttributeError, only the addition works. (as also mention here: https://github.com/scikit-hep/boost-histogram/issues/601#issuecomment-921073303)

Is this not implemented yet or is this ill-defined how to propagate the errors?

If the latter, is it worth to have a different error message than an AttributeError?

Steps to reproduce

Tested on newest release and on dev branch.

import boost_histogram as bh

h = bh.Histogram(bh.axis.Regular(10, -5, 5), storage=bh.storage.Weight())
h.fill([1.5, 2.5], weight=[0.5, 1.5])
h + h  # works
h / h  # this errors
h * h  # this errors
h - h  # this errors

Error message:

AttributeError: 'boost_histogram._core.hist.any_weight' object has no attribute '__imul__'

Full traceback

Details

AttributeError Traceback (most recent call last) in ----> 1 h * h

~/anaconda3/envs/zfit39/lib/python3.9/site-packages/boost_histogram/_internal/hist.py in mul(self, other) 360 ) -> H: 361 result = self.copy(deep=False) --> 362 return result._compute_inplace_op("imul", other) 363 364 def rmul(

~/anaconda3/envs/zfit39/lib/python3.9/site-packages/boost_histogram/_internal/hist.py in _compute_inplace_op(self, name, other) 399 # Also takes CppHistogram, but that confuses mypy because it's hard to pick out 400 if isinstance(other, Histogram): --> 401 getattr(self._hist, name)(other._hist) 402 elif isinstance(other, tuple(_histograms)): 403 getattr(self._hist, name)(other)

AttributeError: 'boost_histogram._core.hist.any_weight' object has no attribute 'imul'

jonas-eschle avatar Feb 02 '22 11:02 jonas-eschle

https://github.com/boostorg/histogram/pull/351 is adding this for division to Boost.Histogram. We try to match Boost.Histogram's support here, so division will be added once that is merged.

henryiii avatar Feb 08 '22 16:02 henryiii

And the tracking issue for the rest of them is here: https://github.com/boostorg/histogram/issues/352

We'll follow those as they go in, and include them here.

henryiii avatar Feb 08 '22 16:02 henryiii

Division is in boostorg/histogram now, so it can get included here, right?

masonproffitt avatar Sep 20 '22 15:09 masonproffitt

Yep, if it's in Boost.Histogram, it can be added to boost-histogram.

henryiii avatar Sep 20 '22 16:09 henryiii

What is the status of this issue? In the Hist project, someone opened an issue regarding adding weighted histograms, and another issue is regarding using the .plot_ratio for weighted histograms.

It is possible to divide an weighted histogram by an non-weighted histogram. I think all these issues are caused by the lack of the following or similar attributes: AttributeError: 'boost_histogram._core.hist.any_weight' object has no attribute '__itruediv__' or AttributeError: 'boost_histogram._core.hist.any_weight' object has no attribute '__isub__'

Tested on version 1.3.2

JMulder99 avatar Jan 20 '23 13:01 JMulder99

Dear boost-histogram team,

I would aslo be interesed in this feature. Especially diving two weighted histograms by each other.

Superharz avatar May 05 '24 14:05 Superharz