fix: dtype handling and axis validation in statistical functions#145
Draft
henryiii wants to merge 1 commit into
Draft
fix: dtype handling and axis validation in statistical functions#145henryiii wants to merge 1 commit into
henryiii wants to merge 1 commit into
Conversation
sum and prod ignored their dtype argument due to an inverted condition (cast only when dtypes already matched). They now cast via _ensure_dtype whenever the requested dtype differs. mean, var, and std cast their floating-point result back to x.dtype, truncating integer input (e.g. mean([1, 2]) returned 1). They now keep the natural floating-point dtype for integer/bool input while still preserving float32 input dtype. _regularize_axis built an out-of-bounds error message for tuple axes but never raised it, and used an incorrect 0 < out[-1] bound that rejected axis 0. It now raises ak.errors.AxisError and uses 0 <= out[-1]. Part of #144 Assisted-by: ClaudeCode:claude-opus-4-8
27 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 🤖
Fixes three confirmed bugs in
src/ragged/_spec_statistical_functions.py.1.
sum/prodignored theirdtypeargumentThe cast condition was inverted: it cast only when the dtypes already
matched (a no-op) and skipped the cast when they actually differed.
ragged.sum(ragged.array([[1, 2, 3], [4, 5]]), dtype=np.float32)returnedint64.float32(value15.0). The cast now happens via theexisting
_ensure_dtypehelper, which also handles the 0-d scalar case.2.
mean/var/stdtruncated integer inputThese functions cast their floating-point result back to
x.dtype,truncating integer input.
ragged.mean(ragged.array([1, 2]))returned1;ragged.var(ragged.array([1, 2, 4]))returned1(true ≈ 1.556).(
mean([1, 2]) == 1.5), matching the Array API requirement that thesefunctions return a floating-point data type. Floating input (e.g.
float32) is still cast back to preserve the "same data type asx"promise. Docstrings' "Returns" wording updated accordingly.
stddelegates to
var, so it follows automatically.3.
_regularize_axisbuilt an error message but never raised itFor tuple axes, the out-of-bounds branch assigned
msgbut never raised,and the bound check
0 < out[-1]incorrectly rejected axis0.ragged.sum(x, axis=(0, 5))fell through to a less helpfulawkward error.
ak.errors.AxisErrorwith a clear message; the boundis now
0 <= out[-1] < ndim, so tuple axes including0work.Tests
Added regression tests in
tests/test_spec_statistical_functions.pycovering all three fixes. Full suite passes (
1305 passed, 6 skipped),mypy --strictand all pre-commit hooks clean.Part of #144
🤖 Generated with Claude Code