Skip to content

Commit ca1ca56

Browse files
fix: histogram metadata not being serialized correctly (#1038)
* fix: histogram metadata not being serialized correctly Signed-off-by: Henry Schreiner <[email protected]> * style: pre-commit fixes --------- Signed-off-by: Henry Schreiner <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent eed1b87 commit ca1ca56

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/boost_histogram/serialization/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ def to_uhi(h: histogram.Histogram, /) -> dict[str, Any]:
2525
"axes": [_axis_to_dict(axis) for axis in h.axes],
2626
"storage": _storage_to_dict(h.storage_type(), h.view(flow=True)),
2727
}
28-
if h.metadata is not None:
29-
data["metadata"] = {
30-
k: v for k, v in h.metadata.items() if not k.startswith("@")
31-
}
28+
data["metadata"] = {k: v for k, v in h.__dict__.items() if not k.startswith("@")}
3229

3330
return data
3431

src/boost_histogram/tag.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
def Slicer() -> np.lib._index_tricks_impl.IndexExpression:
2222
"""
23-
It is encouraged to use "np.s_" directly instead of this function:
23+
It is encouraged to use ``np.s_`` directly instead of this function:
2424
2525
h[{0: np.s_[::bh.rebin(2)]}] # rebin axis 0 by two
2626

tests/test_serialization_uhi.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def test_simple_to_dict(storage_type: bh.storage.Storage, expected_type: str) ->
2525
)
2626
data = to_uhi(h)
2727

28-
assert "metadata" not in data
2928
assert data["axes"][0]["type"] == "regular"
3029
assert data["axes"][0]["lower"] == 0
3130
assert data["axes"][0]["upper"] == 1
@@ -61,8 +60,8 @@ def test_mean_to_dict() -> None:
6160
h = bh.Histogram(
6261
bh.axis.StrCategory(["one", "two", "three"]),
6362
storage=bh.storage.Mean(),
64-
metadata={"name": "hi"},
6563
)
64+
h.name = "hi"
6665
data = to_uhi(h)
6766

6867
assert data["metadata"]["name"] == "hi"
@@ -239,9 +238,28 @@ def test_round_trip_clean() -> None:
239238

240239
def test_unserializable_metadata() -> None:
241240
h = bh.Histogram(
242-
bh.axis.Integer(0, 10, metadata={"c": 3, "@d": 4}), metadata={"a": 1, "@b": 2}
241+
bh.axis.Integer(0, 10, metadata={"c": 3, "@d": 4}),
243242
)
243+
h.__dict__["a"] = 1
244+
h.__dict__["@b"] = 2
244245
data = to_uhi(h)
245246

246-
assert data["metadata"] == {"a": 1}
247+
assert data["metadata"] == {"a": 1, "_variance_known": True, "metadata": None}
247248
assert data["axes"][0]["metadata"] == {"c": 3}
249+
250+
251+
def test_histogram_metadata() -> None:
252+
h = bh.Histogram(bh.axis.Integer(0, 10))
253+
h.name = "Hi"
254+
h.label = "hi"
255+
h.other = 3
256+
257+
data = to_uhi(h)
258+
259+
assert data["metadata"] == {
260+
"name": "Hi",
261+
"label": "hi",
262+
"other": 3,
263+
"_variance_known": True,
264+
"metadata": None,
265+
}

0 commit comments

Comments
 (0)