-
Notifications
You must be signed in to change notification settings - Fork 22
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
Allow adding constants #261
Comments
This is useful, though you should be able to do (assuming this is all you are doing): import functools, operator
h_tot = functools.reduce(operator.add, generate_histograms()) Allowing scalar addition might even enable (there's a caveat in the docs about expecting numeric types here, but I've used it for things like this before, and histograms are numeric of sorts): h_tot = sum(generate_histograms()) |
You are right, I could write it in more clever ways and then it would already work, but it is also nice to support the naive way (but I think we agree on that). This is again about muscle memory, I know that this trick works with numpy arrays, and I kinda assume it should work with bh. |
Note that adding may trigger a storage conversion:
This should convert the Histogram storage to When you add two histograms with different storage in Boost.Histogram, the C++ code selects the common type for the result. So int + double yields double, for instance. |
The same thing happens when you scale. |
Turns out the Python code currently does not support adding histograms with different storages.
|
I think this issue (adding const & fix add func) could be solved together with #289 , as they are all related to storage type. Besides, I think this snippet should work, but currently NOT. >>> h = bh.Histogram(bh.axis.Regular(10, 0, 1), storage=bh.storage.Int64())
>>> h2 = bh.Histogram(bh.axis.Regular(10, 0, 1), storage=bh.storage.Double())
>>> assert h == h2 It looks like maybe --- Update --- |
@HDembinski, is std::vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
auto h = bh::make_histogram(bh::axis::variable<>(v));
h += 1.0; Should it be? Or should the be Python only? |
And what does |
It is not supported currently, because the operation seemed to make little sense for a real histogram. But it can easily be added. |
I'm thinking it is probably best in Python to allow False-like (0, 0.0, None, False) to do nothing when added to a histogram, and disallow adding non-False values, since they are ambiguous. For a real histogram, I don't think adding a constant to all bins makes physical sense, especially if bins are not equal size or you have flow bins; if you want to add values using |
Consider the following code (from my analysis), which sums up histograms generated in different threads:
It would be nicer to be able to write it like this (or something similar)
We currently don't allow adding numbers to histograms. So to get this feature, we should allow this. Adding a number to a histogram would only be allowed if that is supported by the underlying accumulator.
The text was updated successfully, but these errors were encountered: