Skip to content

Fix serialization round-trip for 3D histograms with empty axes#1094

Draft
Copilot wants to merge 2 commits intodevelopfrom
copilot/fix-serialization-of-3d-hists
Draft

Fix serialization round-trip for 3D histograms with empty axes#1094
Copilot wants to merge 2 commits intodevelopfrom
copilot/fix-serialization-of-3d-hists

Conversation

Copy link
Contributor

Copilot AI commented Mar 20, 2026

JSON serialization collapses empty numpy dimensions — a (5, 0, 0) array becomes [[], [], [], [], []] and deserializes as (5, 0), causing from_uhi to assign data with the wrong number of dimensions into the reconstructed histogram.

h = bh.Histogram(
  bh.axis.Variable([0, 1, 2, 3]),
  bh.axis.StrCategory([], growth=True),
  bh.axis.StrCategory([], growth=True),
  storage=bh.storage.Weight(),
)
# Previously raised: ValueError: Setting a 3D histogram with a 2D array
bh.Histogram(json.loads(json.dumps(h, default=uhi.io.json.default), object_hook=uhi.io.json.object_hook))

Changes

  • src/boost_histogram/serialization/__init__.py: In from_uhi, reshape the raw storage data to the correct shape derived from the histogram's view after reconstruction:

    • Simple storages (int, double): reshape to view_shape
    • Compound storages (weighted, mean, weighted_mean): reshape to view_shape + (accumulator_dim,)

    This is a no-op for non-empty histograms and restores lost dimensions for empty ones.

  • tests/test_serialization_uhi.py: Adds regression tests simulating a JSON round-trip (the mechanism that triggers dimension collapse) across all 5 storage types, plus a test reproducing the exact constructor path from the bug report.

Original prompt

This section details on the original issue you should resolve

<issue_title>[BUG] serialization of 3D hists</issue_title>
<issue_description>Hi @henryiii,

the following serialization roundtrip is failing, do you have an idea why this happens?

import json
import uhi.io.json

import boost_histogram as bh


h = bh.Histogram(
  bh.axis.Variable([0, 1, 2, 3]),
  bh.axis.StrCategory([], growth=True),
  bh.axis.StrCategory([], growth=True),
  storage=bh.storage.Weight(),
)

json_obj = json.dumps(h, default=uhi.io.json.default)
roundtripped = json.loads(json_obj, object_hook=uhi.io.json.object_hook)

# this fails:
bh.Histogram(roundtripped)
# ValueError: Setting a 3D histogram with a 2D array must have a matching number of dimensions

</issue_description>

<agent_instructions>Set this example up in our test suite.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Copilot AI changed the title [WIP] Fix serialization issue for 3D histograms Fix serialization round-trip for 3D histograms with empty axes Mar 20, 2026
Copilot AI requested a review from henryiii March 20, 2026 15:14
@github-actions github-actions bot added the needs changelog Might need a changelog entry label Mar 20, 2026
@henryiii
Copy link
Member

henryiii commented Mar 20, 2026

I kind of told it to just start with the test, but I guess the fix is welcome too...

I love that it did a variety of storages, and handles the special case. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs changelog Might need a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] serialization of 3D hists

2 participants