forbid float weights for int storage for #289#346
forbid float weights for int storage for #289#346LovelyBuggies wants to merge 1 commit intoscikit-hep:developfrom LovelyBuggies:develop
Conversation
|
@henryiii I have fixed the bugs in #289 . I saw your work #290 and found you were focusing on the pybind11 part. However, I focused on the Python part -- Now, the target has been achieved: import boost_histogram as bh
import numpy as np
h = bh.Histogram(bh.axis.Regular(10, 0, 1), storage=bh.storage.Int64())
h.fill([.2, .3], weight=[1, 2]) # this will work
h.fill([.2, .3], weight=[1., 2]) # this will not work -- TypeError: "Weight type must match storage type."
h.fill([.2, .3], weight=[1, 2.]) # this will not work -- TypeError: "Weight type must match storage type."
h.fill([.2, .3], weight=[1., 2.]) # this will not work -- TypeError: "Weight type must match storage type."
w, data = h.to_numpy()This works in my notebooks. |
|
By the way, CI failed again. I wonder why this always happens. 😢 |
henryiii
left a comment
There was a problem hiding this comment.
I think we should fix the underlying problem (as done in the old PR), rather than trying to add a check here. However, maybe a check like you have here might help that PR.
| # Keyword only trick (change when Python2 is dropped) | ||
| with KWArgs(kwargs) as k: | ||
| storage = k.optional("storage", Double()) | ||
| self._storage = k.optional("storage", Double()) |
There was a problem hiding this comment.
You can access the storage type from the C++ object, which is better than trying to hang on to it in Python.
| # should not allow float weight for int storage | ||
| if "weight" in kwargs: | ||
| for w in kwargs["weight"]: | ||
| if not isinstance(self._storage._PyType, type(w)): |
There was a problem hiding this comment.
This is an incorrect check; weight could be a single value or an array of values, this is only checking for one int rather than an array (I think).
There was a problem hiding this comment.
Interesting, it does work on arrays of ints? Something like this might fix the other PR.
There was a problem hiding this comment.
You might misunderstand. As you can see, for w in kwargs["weight"]: will iterate every value in weight list. And you can see the demo above in my comments. Every possible broken case is considered.
There was a problem hiding this comment.
Ah, okay. I see the loop. Does this work when this is not an array? And this will be horribly slow, since it's a Python loop.
There was a problem hiding this comment.
You get the point! Only weight=[1] is supported now. weight=1 is not supported. But it would not be a hard job if you really want!
And this will be horribly slow, since it's a Python loop
Yep, I have seen the importance to deep into the underlying C++ parts.
| @set_module("boost_histogram.storage") | ||
| class Int64(store.int64, Storage): | ||
| def __init__(self): | ||
| self._PyType = int() |
There was a problem hiding this comment.
I think this is causing the segfault in the CI, as Int64 is not properly initializing store.int64 (but could be wrong). The fact the tests are segfaulting is slightly bothering me.
|
This PR doesn't fix the Unlimited storage, since internally they still are always converted to floats, while the the other PR does fix that. |
@henryiii Do you expect a build-in exception rather than I agree that we should modify the underlying parts. The bad thing is, in storage.hpp, a histogram even stores data in double naturally, that's why I think your modification, using C++ templates, is a good direction. So, do you have any plans? Close this PR and stick to your draft PR, |
No description provided.