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

[BUG] Hist.fill does not accept scalars for sample argument #646

Open
nsmith- opened this issue Sep 7, 2021 · 1 comment
Open

[BUG] Hist.fill does not accept scalars for sample argument #646

nsmith- opened this issue Sep 7, 2021 · 1 comment
Labels
project idea Could be a fellow project

Comments

@nsmith-
Copy link

nsmith- commented Sep 7, 2021

Take for example:

import hist
import numpy as np
h = hist.Hist.new.Regular(10, 0, 1, name="x").WeightedMean()

I can fill with

h.fill(x=np.array([0.1, 0.1, 0.2]), sample=np.array([1, 2, 3]), weight=np.array([1, 2, 3]))
h.fill(x=0.3, sample=np.array([4]), weight=1.)

etc. but not

>>> h.fill(x=0.3, sample=4, weight=1.)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-0e663158df79> in <module>
----> 1 h.fill(x=0.3, sample=4, weight=1.)

/usr/local/lib/python3.9/site-packages/hist/basehist.py in fill(self, weight, sample, threads, *args, **kwargs)
    233
    234         total_data = tuple(args) + tuple(data)  # Python 2 can't unpack twice
--> 235         return super().fill(*total_data, weight=weight, sample=sample, threads=threads)
    236
    237     def sort(

/usr/local/lib/python3.9/site-packages/boost_histogram/_internal/hist.py in fill(self, weight, sample, threads, *args)
    431
    432         if threads is None or threads == 1:
--> 433             self._hist.fill(*args_ars, weight=weight_ars, sample=sample_ars)
    434             return self
    435

ValueError: Sample array must be 1D
@henryiii henryiii transferred this issue from scikit-hep/hist Sep 15, 2021
@henryiii henryiii added the project idea Could be a fellow project label Apr 15, 2022
@JMulder99
Copy link

When using the Mean or WeightedMean accumulator, it will also raise this error when no sample argument is passed.
For Mean: TypeError: Sample key-argument (sample=) needs to be provided.
For WeightedMean: ValueError: Sample array must be 1D

In the documentation it states "you can add a sample argument".

N = 10000
zenith = np.random.random(size=N) - 1
energy = np.random.exponential(scale=1000, size=N)
weights = 1/ np.random.randint(1, 5, size=N)

h_area = hist.Hist(
    hist.axis.Regular(20, -1, 0, name="zenith", label="Zenith"),
    hist.axis.Regular(20, 0, 10000, name="energy", label="Energy")
    name="Area",
    label="h_area",
    storage=hist.storage.WeightedMean()
)
h_AREA_DETFLUX_mean.fill(
    zenith=zenith,
    energy=energy, 
    weight=weights,
)

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

3 participants