Skip to content

Avoid scalar divide warnings raised when generating raw .csv files#533

Merged
dodu94 merged 2 commits into
developingfrom
avoid-division-warning
May 21, 2026
Merged

Avoid scalar divide warnings raised when generating raw .csv files#533
dodu94 merged 2 commits into
developingfrom
avoid-division-warning

Conversation

@mcampos16
Copy link
Copy Markdown
Contributor

@mcampos16 mcampos16 commented May 20, 2026

This pull requests implements a solution for the warning repeatedly raised when generating raw results:
image

The warning was traced back to the group_by function in the manipulate_tally.py file. Accordingly, the following modifications were implemented:

  • If the denominator of the division which causes the warning is 0, instead of doing the error propagation, the value 0 is assigned to the corresponding error.
  • The test function of group_by was modified to check for any raised warnings. If any warnings are raised, the test fails.

No additional dependencies were introduced.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed a division by zero error in grouping operations when calculating error values with zero sums, now properly returning zero instead.
  • Tests

    • Enhanced test coverage with expanded datasets across additional energy groups and added validation to ensure no warnings are emitted during grouping operations.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 20, 2026

Warning

Rate limit exceeded

@mcampos16 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 51 minutes and 23 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a598c376-6d2e-4ec3-b793-a9eb238882e4

📥 Commits

Reviewing files that changed from the base of the PR and between f042b1c and da3e23f.

📒 Files selected for processing (1)
  • src/jade/post/manipulate_tally.py

Walkthrough

This PR adds a division-by-zero guard to the groupby error propagation calculation in manipulate_tally.py. When a grouped subset's summed value is zero, the error is set to zero instead of dividing by zero. The test suite is expanded with additional energy group data and updated expected values, with an assertion confirming no warnings are raised.

Changes

Error Guard and Validation

Layer / File(s) Summary
Division-by-zero guard in error propagation
src/jade/post/manipulate_tally.py
The groupby error calculation now returns 0 when subset_value.sum() equals zero, preventing division-by-zero exceptions.
Test expansion and warning validation
tests/post/test_manipulate_tally.py
Imports are reordered, test data expanded with an additional energy group, expected sum/mean/max/min values updated, and warning assertion added to ensure no warnings during groupby execution.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • JADE-V-V/JADE#421: Prior PR modifying the error propagation formula in groupby; this PR builds on that work by adding a safety check for the zero-sum edge case.

Suggested reviewers

  • dodu94

Poem

A rabbit guards against the void,
Where sums retreat to zero's door,
With tests expanded, peace deployed,
No warnings echo anymore. 🐰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: avoiding scalar divide warnings when generating raw .csv files by guarding against division by zero in error propagation calculations.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch avoid-division-warning

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mcampos16 mcampos16 requested a review from dodu94 May 20, 2026 16:05
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/jade/post/manipulate_tally.py (1)

168-172: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Zero-denominator handling is incomplete in groupby (by=="all" path still divides unguarded).

You added a guard for grouped subsets, but Line 170 can still divide by zero when tally["Value"].sum() == 0, so the warning can still occur for by="all".

Proposed fix
     if by == "all":
         grouped = tally
         # Error propagation considering that tally["Error"] are relative errors
         # Valid both for sum and mean
+        total_value = tally["Value"].sum()
         error = pd.Series(
-            np.sqrt(((tally["Error"] * tally["Value"]) ** 2).sum())
-            / tally["Value"].sum(),
+            (
+                np.sqrt(((tally["Error"] * tally["Value"]) ** 2).sum()) / total_value
+                if total_value != 0
+                else 0
+            ),
             name="Error",
         )

Also applies to: 184-185

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/jade/post/manipulate_tally.py` around lines 168 - 172, The calculation of
error in the "by=='all'" path does an unguarded divide by tally["Value"].sum()
(variable error created from tally) which can be zero; change this to compute a
denom = tally["Value"].sum() and if denom == 0 return a safe pd.Series (e.g.
pd.Series(0.0, name="Error") or pd.Series(np.nan, name="Error")) else perform
the original sqrt(...) / denom calculation; apply the same zero-denominator
guard to the analogous computation referenced at lines 184-185 so both the
top-level error assignment and the grouped-case error use the same denom check.
🧹 Nitpick comments (1)
tests/post/test_manipulate_tally.py (1)

225-234: ⚡ Quick win

Add a zero-total by="all" regression case to actually lock in the warning fix.

len(recwarn) == 0 is good, but this test never hits by="all" with Value.sum()==0, which is the remaining warning-prone path. Add a small all-zero dataset case for groupby(..., "all", "sum") and assert no warnings plus Error == 0.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/post/test_manipulate_tally.py` around lines 225 - 234, Add a small
all-zero dataset case to exercise the by="all" sum path: create a DataFrame
(e.g., df_zero) where the Value column sums to 0 and corresponding Error inputs
are zeros, call groupby(df_zero.copy(), "all", "sum"), assert that the resulting
"Value" is 0, "Error" is 0, and len(recwarn) == 0 to ensure no warning is
emitted; place this directly after the existing groupby(..., "all", "sum")
assertions and reference the same groupby function under test.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/jade/post/manipulate_tally.py`:
- Around line 168-172: The calculation of error in the "by=='all'" path does an
unguarded divide by tally["Value"].sum() (variable error created from tally)
which can be zero; change this to compute a denom = tally["Value"].sum() and if
denom == 0 return a safe pd.Series (e.g. pd.Series(0.0, name="Error") or
pd.Series(np.nan, name="Error")) else perform the original sqrt(...) / denom
calculation; apply the same zero-denominator guard to the analogous computation
referenced at lines 184-185 so both the top-level error assignment and the
grouped-case error use the same denom check.

---

Nitpick comments:
In `@tests/post/test_manipulate_tally.py`:
- Around line 225-234: Add a small all-zero dataset case to exercise the
by="all" sum path: create a DataFrame (e.g., df_zero) where the Value column
sums to 0 and corresponding Error inputs are zeros, call groupby(df_zero.copy(),
"all", "sum"), assert that the resulting "Value" is 0, "Error" is 0, and
len(recwarn) == 0 to ensure no warning is emitted; place this directly after the
existing groupby(..., "all", "sum") assertions and reference the same groupby
function under test.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 866e6eda-139b-4d2a-863c-0677ea6d5e79

📥 Commits

Reviewing files that changed from the base of the PR and between 314efef and f042b1c.

📒 Files selected for processing (2)
  • src/jade/post/manipulate_tally.py
  • tests/post/test_manipulate_tally.py

@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/jade/post/manipulate_tally.py 96.05% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dodu94 dodu94 merged commit ca03791 into developing May 21, 2026
14 checks passed
@dodu94 dodu94 deleted the avoid-division-warning branch May 21, 2026 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants