-
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
Initializing histograms from arrays of bin values #421
Comments
Two ways: h = bh.Histogram(bh.axis.Regular(10,0,1), storage=bh.storage.Weight())
values = np.ones(10)
variances = np.ones(10) * .1 Using the shortcut for Weight Histograms: h[...] = np.stack([values, variances], axis=-1) For a AxBxC histogram, you need a AxBxCx2 array. You can optionally include the flow bins, and those will get set too. Using the view method: h.view().value = values
h.view().variance = variances You can pass flow=True to |
@alexander-held I use this feature myself. I fit invariant-mass distributions in eta and pt of some particle. Then I store the fitted particle yield and its uncertainty squared as "values" and "variances" in a histogram with |
Thanks for the quick reply! There is no way to instantiate a
This might be convenient to have (maybe something for hist?). |
No, but long constructors with many keywords are not a good design. It is not significantly more efficient to do this instead of assigning. |
Comment from the 🥜 gallery: if you are going to have many end-users all extending the class to add a sub-set of the keyword arguments you can end up with a mess of slightly different sub-classes floating around the world. It could be better to standardize on how to manage that bit of complexity. You could do something like def __init__(self, ..., **kwargs):
# current init
for k, v in kwargs.items():
setattr(self, k, v) It is not much code and makes the API a bit for ergonomic for your users. |
I was surprised that the default |
h.view()[...] = np.array(...) This is actually good, because it emphases that you are changing the contents, rather than changing the object. You can do the same thing with .value, as well, I believe Note: I am horribly mixing meanings above. The first For a general way to do this regardless of the backend storage, see #423 - but that won't work very well nor is it designed for setting values on Profiles. Maybe we should expose and provide nice constructors or shortcuts for the AccumulatorViews? |
PS: of course, you can use |
Thank you @tacaswell for chiming in and making a valid point. My background is in C++ and there constructors with many arguments are frowned upon, hence my reaction... |
If we ever add the ability to use existing memory allocated in Python as the Storage's memory, this would suddenly make histograms initialized this way more efficient (and would have a side effect that the array passed in would start being the one that changes). |
Note: I've proposed an API for accessing the |
I'm intending to implement this in Hist first (it's already available for pandas data frame storages, actually - but that one can't be upstreamed to boost-histogram because it depends on named axes). I'd like a classmethod, like used for that feature, but in this case, having the other args (axes, storage, etc) are useful, so it is probably better as a keyword only argument instead of forwarding a lot of stuff. |
Unless I'm mistaken, both |
Update: this is supported in Hist (as you can see from the linked merged PR above). I'd be fine to upstream it (as with any non-dependency addition to Hist) if @HDembinski would like a |
Is it possible to set the bin contents (and variances, for
storage=bh.storage.Weight()
) of abh.Histogram()
directly without having to.fill()
in events?I assume that aghast could be used to create a histogram from this information and then convert it. Is there a more direct way?
For context: I am considering using
bh.Histogram
or possibly the hist version as a container for histograms in another library. For this I would like to have the flexibility to create histograms in multiple ways.The text was updated successfully, but these errors were encountered: