fix(numpy): do not mutate input edges; validate histogramdd dimensions#1144
Draft
henryiii wants to merge 1 commit into
Draft
fix(numpy): do not mutate input edges; validate histogramdd dimensions#1144henryiii wants to merge 1 commit into
henryiii wants to merge 1 commit into
Conversation
- histogram/histogramdd no longer mutate a caller-provided float64 edges array in place when applying the nextafter upper-edge adjustment; the edges are now always copied (B3 in #1143). - histogramdd now validates that the lengths of bins and range match the sample rank up front, raising the same ValueError messages as np.histogramdd instead of silently truncating via zip (B13 in #1143). - Remove a dead numpy<1.13 version check and the obsolete NumPy 1.20 typing workaround comment in the string-bins branch of histogram. Part of #1143 Assisted-by: ClaudeCode:claude-fable-5
38 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 AI text below 🤖
Part of #1143.
Fixes three findings in
src/boost_histogram/numpy.py:B3 (caller data mutation):
histogramdd(and thereforehistogram/histogram2d) usednp.asarray(b, dtype=np.double)on user-supplied bin edges, which aliases an already-float64 input. The subsequent in-placenp.nextafteradjustment of the last edge then mutated the caller's array (e.g.edges = np.array([0., 1., 2.]); bh.numpy.histogram([0.5, 1.5], bins=edges)leftedges[-1] == 2.0000000000000004).np.histogramnever mutates its input. The edges are now always copied before adjustment.B13 (silent truncation):
histogramddzippedbinsandrangewithstrict=False, silently truncating on length mismatch:bh.numpy.histogramdd((x, y), bins=(2, 2, 2))returned a 2-D result wherenp.histogramddraises, and a too-shortrangefailed later with an unhelpful "Wrong number of args". Lengths are now validated up front, raisingValueErrorwith the same messages asnp.histogramdd("The dimension of bins must be equal to the dimension of the sample x." / "range argument must have one entry per dimension").Dead version check (D2, numpy part): removed the unreachable
numpy < 1.13check ("Upgrade numpy to 1.13+") in the string-bins branch ofhistogram—pyproject.tomlrequiresnumpy>=1.21.3— along with the obsolete "Bug in NumPy 1.20 typing" comment;np.histogram_bin_edgesis now called directly (mypy strict remains clean).Regression tests added in
tests/test_numpy_interface.py: input-edges non-mutation forhistogramandhistogramdd, and mismatchedbins/rangeraisingValueErrorin lockstep withnp.histogramdd.🤖 Generated with Claude Code