fix(view): out= handling, scalar division, and broadcasting in structured views#1146
Merged
Conversation
…ured views Three fixes in the structured-view ufunc machinery (part of #1143): - B2: the reduce branch of __array_ufunc__ forwarded the caller's out= into the per-field reductions, so every field overwrote the same buffer (silently wrong results). out= is now popped before reducing: a matching-dtype out is filled from a freshly computed result, and a mismatched dtype raises TypeError. _MeanArithmetic._reduce now raises on reduction arguments it cannot honor (dtype, where, initial) instead of silently ignoring them. - B11: scalar / WeightedSumView produced a statistically meaningless variance (s**2 / variance); it now raises TypeError, matching the mean views. scalar * view and view / scalar keep working. - B12: binary ops preallocated the result with the view's own shape, so legal broadcasts (e.g. a (3,) view + a (2, 3) array) failed; the result is now allocated with np.broadcast_shapes of all inputs. Also drops the redundant np.true_divide matches (np.divide is the same object). Assisted-by: ClaudeCode:claude-fable-5
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 verified findings in the structured-view (
view.py) ufunc machinery:B2 —
out=silently corrupted reductionsThe
"reduce"branch of__array_ufunc__forwarded the caller'sout=into_reduce, wherenp.add.reduce(raw[field], **kwargs)ran once per field with the same out buffer, so each field's reduction overwrote the previous one (e.g.np.sum(v, axis=0, out=np.empty(2))on a 2-D Weight view returned[(4., 4.), (9., 9.)]instead of[(2., 4.), (3., 9.)]).Now
out=is popped before reducing: if the out dtype matches the view dtype, the result is computed fresh and copied into the out buffer (which is returned); otherwise aTypeErroris raised. This applies uniformly toWeightedSumViewand the mean views._MeanArithmetic._reducenow also raisesTypeErroron reduction arguments it cannot honor (dtype,where,initial) instead of silently ignoring them;axisandkeepdimskeep working.B11 —
scalar / viewproduced meaningless statistics2.0 / weighted_sum_viewwent through the scalar-first_scalebranch and producedvariance = s**2 / variance, which is statistically meaningless. The mean views already raise for exactly this case;WeightedSumViewnow raises a matchingTypeErrorfordivide/floor_dividewith the view as the divisor.scalar * viewandview / scalarare unchanged. (The existing test asserting the old1 / vbehavior was updated to expect the error.)B12 — legal broadcasts failed
Binary ops preallocated
np.empty(self.shape, self.dtype), so a(3,)view plus a(2, 3)array raisedValueError: non-broadcastable output operand. The result is now allocated withnp.broadcast_shapesover all inputs; tested in both operand orders for+and*.Cleanup (from D2)
np.divide is np.true_divide, so the redundantnp.true_divideentries in the match alternation and the mean_scaletuple were dropped.Testing
tests/test_views.pyfor each finding, including the exact issue reproductions.uv run pytest -n auto --benchmark-disable: 1006 passed, 1 xfailed.mypy(strict) clean;prek -aclean.🤖 Generated with Claude Code