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

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

Open
jonas-eschle opened this issue Feb 2, 2022 · 6 comments
Labels
project idea Could be a fellow project

Comments

@jonas-eschle
Copy link
Contributor

jonas-eschle commented Feb 2, 2022

Describe the bug

Dividing/multiplying/subtracting histograms with weights results in an AttributeError, only the addition works. (as also mention here: #601 (comment))

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'

@henryiii
Copy link
Member

henryiii commented Feb 8, 2022

boostorg/histogram#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
Copy link
Member

henryiii commented Feb 8, 2022

And the tracking issue for the rest of them is here: boostorg/histogram#352

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

@jonas-eschle jonas-eschle changed the title [BUG/FEAT] Operators (mult/div/sub) raise AttributeError on weighted histograms [FEAT] Add operators (mult/div/sub) for (weighted) histograms/accumulators Feb 8, 2022
@henryiii henryiii added the project idea Could be a fellow project label Apr 15, 2022
@masonproffitt
Copy link

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

@henryiii
Copy link
Member

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

@JMulder99
Copy link

JMulder99 commented Jan 20, 2023

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

@Superharz
Copy link

Dear boost-histogram team,

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
project idea Could be a fellow project
Projects
None yet
Development

No branches or pull requests

5 participants