Skip to content

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

Open
@jonas-eschle

Description

@jonas-eschle

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'

Metadata

Metadata

Assignees

No one assigned

    Labels

    project ideaCould be a fellow project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions