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

fix: sample required error message #538

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/hist/basehist.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ def fill(
a Hist object.
"""

if (
issubclass(self.storage_type, (bh.storage.Mean, bh.storage.WeightedMean))
and sample is None
):
msg = "A Mean-based storage requires a sample= argument with the values to average"
raise TypeError(msg)

data_dict = {
self._name_to_index(k) if isinstance(k, str) else k: v # type: ignore[redundant-expr]
for k, v in kwargs.items()
Expand Down
49 changes: 33 additions & 16 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import hist
from hist import Hist, axis, storage

BHV = tuple(int(x) for x in bh.__version__.split(".")[:2])

# TODO: specify what error is raised


Expand Down Expand Up @@ -111,7 +113,7 @@ def test_duplicated_names_init(named_hist):
)


def test_general_fill():
def test_general_fill_regular():
"""
Test general fill -- whether Hist can be properly filled.
"""
Expand All @@ -137,6 +139,8 @@ def test_general_fill():
else:
assert z_one_only[idx_x, idx_y] == 0


def test_general_fill_bool():
# Boolean
h = Hist(
axis.Boolean(name="x"),
Expand All @@ -154,7 +158,8 @@ def test_general_fill():
assert z_one_only[True, False] == 3
assert z_one_only[True, True] == 1

# Variable

def test_general_fill_variable():
h = Hist(
axis.Variable(range(11), name="x"),
axis.Variable(range(11), name="y"),
Expand All @@ -175,7 +180,8 @@ def test_general_fill():
else:
assert z_one_only[idx_x, idx_y] == 0

# Integer

def test_general_fill_integer():
h = Hist(
axis.Integer(0, 10, name="x"),
axis.Integer(0, 10, name="y"),
Expand All @@ -196,19 +202,24 @@ def test_general_fill():
else:
assert z_one_only[idx_x, idx_y] == 0

# IntCategory

def test_general_fill_int_cat():
h = Hist(
axis.IntCategory(range(10), name="x", flow=False),
axis.IntCategory(range(10), name="y", overflow=False),
axis.IntCategory(range(10), name="x", flow=BHV < (1, 4)),
axis.IntCategory(range(10), name="y", overflow=BHV < (1, 4)),
axis.IntCategory(range(2), name="z"),
).fill(
x=[3, 3, 3, 4, 5, 5, 5],
y=[3, 3, 4, 4, 4, 4, 4],
z=[0, 0, 1, 1, 1, 1, 1],
)

assert not h.axes[0].traits.overflow
assert not h.axes[1].traits.overflow
if BHV < (1, 4):
assert h.axes[0].traits.overflow
assert h.axes[1].traits.overflow
else:
assert not h.axes[0].traits.overflow
assert not h.axes[1].traits.overflow
assert h.axes[2].traits.overflow

z_one_only = h[{2: bh.loc(1)}]
Expand All @@ -221,19 +232,24 @@ def test_general_fill():
else:
assert z_one_only[idx_x, idx_y] == 0

# StrCategory

def test_general_fill_str_cat():
h = Hist(
axis.StrCategory("FT", name="x", flow=False),
axis.StrCategory(list("FT"), name="y", overflow=False),
axis.StrCategory("FT", name="x", flow=BHV < (1, 4)),
axis.StrCategory(list("FT"), name="y", overflow=BHV < (1, 4)),
axis.StrCategory(["F", "T"], name="z"),
).fill(
["T", "T", "T", "T", "T", "F", "T"],
["F", "T", "T", "F", "F", "T", "F"],
["F", "F", "T", "T", "T", "T", "T"],
)

assert not h.axes[0].traits.overflow
assert not h.axes[1].traits.overflow
if BHV < (1, 4):
assert h.axes[0].traits.overflow
assert h.axes[1].traits.overflow
else:
assert not h.axes[0].traits.overflow
assert not h.axes[1].traits.overflow
assert h.axes[2].traits.overflow

z_one_only = h[{2: bh.loc("T")}]
Expand All @@ -242,7 +258,8 @@ def test_general_fill():
assert z_one_only[bh.loc("T"), bh.loc("F")] == 3
assert z_one_only[bh.loc("T"), bh.loc("T")] == 1

# with names

def test_general_fill_names():
assert Hist(
axis.Regular(50, -3, 3, name="x"), axis.Regular(50, -3, 3, name="y")
).fill(x=np.random.randn(10), y=np.random.randn(10))
Expand All @@ -268,7 +285,7 @@ def test_general_fill():
axis.StrCategory(["F", "T"], name="x"), axis.StrCategory("FT", name="y")
).fill(x=["T", "F", "T"], y=["T", "F", "T"])

h = Hist(
Hist(
axis.Regular(
50, -4, 4, name="X", label="s [units]", underflow=False, overflow=False
)
Expand All @@ -290,7 +307,7 @@ def test_general_access():
h = Hist(
axis.Regular(50, -5, 5, name="Norm", label="normal distribution"),
axis.Regular(50, -5, 5, name="Unif", label="uniform distribution"),
axis.StrCategory(["hi", "hello"], name="Greet", flow=False),
axis.StrCategory(["hi", "hello"], name="Greet", flow=BHV < (1, 4)),
axis.Boolean(name="Yes"),
axis.Integer(0, 1000, name="Int"),
).fill(
Expand Down