Skip to content
Closed
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
4 changes: 4 additions & 0 deletions src/hflow/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
from dataclasses import dataclass
from itertools import pairwise

import numpy as np

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Isolated utility import fails

The existing utility test runs Python without site-packages and imports hflow.statistics. This new numpy import raises ModuleNotFoundError, so the test fails before its checks run. Keep that import working without site-packages, or change the test and the documented standard-library-only promise together.



def _finite_number(value: float, name: str) -> float:
if isinstance(value, np.generic):
value = value.item()
if isinstance(value, bool) or not isinstance(value, (int, float)):
Comment on lines +13 to 15

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Finite longdouble values fail

np.longdouble(1).item() does not become a Python int or float. The next check therefore makes WeightedValue(np.longdouble(1), 1) raise ValueError, even though the measurement is finite. Callers using that NumPy scalar type still cannot use these summaries; accept finite real NumPy scalars without relying on .item() to return a built-in number.

raise ValueError(f"{name} must be a finite number")
try:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import itertools
import math

import numpy as np
import pytest

from hflow import (
Expand Down Expand Up @@ -165,6 +166,19 @@ def test_mean_combines_tiny_contributions_before_rounding_to_smallest_float() ->
assert distribution.mean == smallest_float


def test_measurements_accept_finite_numpy_numeric_scalars() -> None:
observation = WeightedValue(np.float32(0.5), np.int64(30))
assert observation == WeightedValue(0.5, 30)


@pytest.mark.parametrize("invalid_numpy_bool", [np.bool_(True), np.bool_(False)])
def test_measurements_reject_numpy_booleans(invalid_numpy_bool: np.bool_) -> None:
with pytest.raises(ValueError, match="value must be a finite number"):
WeightedValue(invalid_numpy_bool, 1) # ty: ignore[invalid-argument-type]
with pytest.raises(ValueError, match="weight must be a finite number"):
WeightedValue(1, invalid_numpy_bool) # ty: ignore[invalid-argument-type]


@pytest.mark.parametrize("invalid_value", [True, "1", None, math.inf, -math.inf, math.nan])
def test_measurements_reject_nonfinite_or_nonnumeric_values(invalid_value: object) -> None:
with pytest.raises(ValueError, match="value must be a finite number"):
Expand Down
Loading