Skip to content
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
2 changes: 1 addition & 1 deletion src/boost_histogram/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ def __setitem__(self, index: IndexingExpr, value: ArrayLike | Accumulator) -> No
pass

else:
msg = f"Mismatched shapes in dimension {n}"
msg = f"Mismatched shapes {value_shape} in dimension {n}"
msg += f", {value_shape[n]} != {request_len}"
if use_underflow or use_overflow:
msg += f" or {request_len + use_underflow + use_overflow}"
Expand Down
9 changes: 4 additions & 5 deletions src/boost_histogram/serialization/_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,9 @@ def _data_from_dict(data: dict[str, Any], /) -> np.typing.NDArray[Any]:
if storage_type in {"int", "double"}:
return data["values"]
if storage_type == "weighted":
return np.stack([data["values"], data["variances"]]).T
return np.stack([data["values"], data["variances"]], axis=-1)
if storage_type == "mean":
return np.stack(
[data["counts"], data["values"], data["variances"]],
).T
return np.stack([data["counts"], data["values"], data["variances"]], axis=-1)
if storage_type == "weighted_mean":
return np.stack(
[
Expand All @@ -121,6 +119,7 @@ def _data_from_dict(data: dict[str, Any], /) -> np.typing.NDArray[Any]:
data["values"],
data["variances"],
],
).T
axis=-1,
)

raise TypeError(f"Unsupported storage type: {storage_type}")
29 changes: 26 additions & 3 deletions tests/test_serialization_uhi.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ def test_round_trip_weighted() -> None:
data = to_uhi(h)
h2 = from_uhi(data)

print(h.view())
print(h2.view())

assert pytest.approx(np.array(h.axes[0])) == np.array(h2.axes[0])
assert np.asarray(h) == pytest.approx(h2)

Expand Down Expand Up @@ -285,3 +282,29 @@ def test_remove_writer_info() -> None:
"writer_info": {"boost-histogram": {"foo": "bar"}},
}
assert remove_writer_info(d, library="c") == d


def test_convert_weight() -> None:
h = bh.Histogram(
bh.axis.Regular(3, 13, 10, __dict__={"name": "x"}),
bh.axis.StrCategory(["one", "two"]),
storage=bh.storage.Weight(),
)
data = h._to_uhi_()
h2 = bh.Histogram(data)

assert h == h2


def test_convert_weightmean() -> None:
h = bh.Histogram(
bh.axis.Regular(12, 0, 1),
bh.axis.StrCategory(["a", "b", "c", "d", "e", "f", "g"]),
bh.axis.Boolean(),
bh.axis.Integer(1, 18),
storage=bh.storage.WeightedMean(),
)
data = h._to_uhi_()
h2 = bh.Histogram(data)

assert h.axes == h2.axes
Loading