Skip to content

fix(data): skip empty foreground when computing DatasetSummary statistics - #9043

Merged
ericspod merged 6 commits into
Project-MONAI:devfrom
aymuos15:fix/dataset-summary-no-foreground-crash
Oct 3, 2026
Merged

ericspod merged 6 commits into
Project-MONAI:devfrom
aymuos15:fix/dataset-summary-no-foreground-crash

Conversation

@aymuos15

@aymuos15 aymuos15 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Description

DatasetSummary.calculate_statistics crashed with a bare RuntimeError on any sample whose label had no foreground voxels at the given foreground_threshold (common with 2D slices or sparse annotations), and if every sample lacked foreground it would divide by zero in the mean/std. calculate_percentiles had the same empty-accumulation hazard. Both methods now skip empty foreground selections and, when no sample contributes any foreground voxels, raise an actionable ValueError that names foreground_threshold and points to setting it to -1 to compute statistics over whole images:

  • monai/data/dataset_summary.py — calculate_statistics continues past empty foreground selections and guards voxel_ct == 0; calculate_percentiles skips empty selections and guards the accumulated list before calling np.percentile.
  • tests/data/test_dataset_summary.py — two new tests: a mixed foreground/background dataset (statistics and percentiles computed from the remaining foreground), and an all-background dataset (ValueError raised by both methods).

Behavior verified end-to-end: a dataset with one normal and one all-background sample now produces statistics from the normal sample (previously it crashed), and an all-background dataset raises the actionable ValueError from both calculate_statistics and calculate_percentiles.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

…tics

Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
@coderabbitai

coderabbitai Bot commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: Project-MONAI/MONAI/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a25b59eb-b756-44d4-ae3f-53382bd607b8
📥 Commits

Reviewing files that changed from the base of the PR and between 33030bf and af9c54d.

📒 Files selected for processing (2)
  • monai/data/dataset_summary.py
  • tests/data/test_dataset_summary.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

calculate_statistics and calculate_percentiles skip empty foreground selections. Both raise ValueError when no qualifying voxels are found. Tests cover mixed inputs and all-background datasets.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to af9c5

DatasetSummary now skips samples with no foreground and gives a clear error when none exist. No merge-blocking risk was found.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: skipping empty foreground when calculating DatasetSummary statistics.
Description check ✅ Passed The description explains the problem, changes, and added tests, and includes the required Description and Types of changes sections. It does not include a linked issue reference, but the description i…
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.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 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.

Inline comments:
In `@monai/data/dataset_summary.py`:
- Around line 171-175: Complete the Google-style docstrings for
calculate_statistics and calculate_percentiles in monai/data/dataset_summary.py,
documenting their Raises behavior and Returns as None. Also add descriptive
docstrings for the mixed foreground/background and all-background error tests in
tests/data/test_dataset_summary.py at lines 103 and 124, respectively.

In `@tests/data/test_dataset_summary.py`:
- Around line 127-130: Update both assertRaisesRegex checks around
calculator.calculate_statistics() and calculator.calculate_percentiles() to
match the complete remediation text, including the exact recommendation
foreground_threshold=-1, rather than only the parameter name.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 31ab911b-369d-4c2e-af71-268749401698

📥 Commits

Reviewing files that changed from the base of the PR and between 8690ae7 and 2f5d4d4.

📒 Files selected for processing (2)
  • monai/data/dataset_summary.py
  • tests/data/test_dataset_summary.py

Comment thread monai/data/dataset_summary.py Resolved
Comment thread tests/data/test_dataset_summary.py Resolved
Comment thread monai/data/dataset_summary.py Resolved
Comment thread monai/data/dataset_summary.py Resolved
Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
@ericspod
ericspod enabled auto-merge (squash) October 3, 2026 20:32
@ericspod
ericspod merged commit 522697c into Project-MONAI:dev Oct 3, 2026
30 checks passed
@aymuos15
aymuos15 deleted the fix/dataset-summary-no-foreground-crash branch October 6, 2026 16:17
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