fix: add missing stacklevel=2 to warnings.warn() in metrics/ (Fixes #8931)#8932
fix: add missing stacklevel=2 to warnings.warn() in metrics/ (Fixes #8931)#8932rtmalikian wants to merge 2 commits into
Conversation
Add stacklevel=2 to 12 warnings.warn() calls across 6 files in monai/metrics/ that were missing it: - cumulative_average.py: non-finite input warning - average_precision.py: all-same-value and invalid-value warnings - utils.py: ground truth/prediction all-zero, binarized tensor, and Voronoi CPU warnings - active_learning_metrics.py: spatial map and reduction warnings - confusion_matrix.py: compute_sample warning - rocauc.py: all-same-value and invalid-value warnings Without stacklevel=2, these warnings point to library internals instead of the user's calling code, making them unhelpful for debugging. This follows the same pattern as PR Project-MONAI#8930 for losses/. Fixes Project-MONAI#8931 Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughNine Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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/metrics/utils.py`:
- Around line 515-520: The warnings.warn() function call has a syntax error
where a comma is placed on a separate line after the string argument and before
the stacklevel parameter. Remove the comma that appears on its own line and
place it at the end of the string on the previous line, so the function
arguments are properly formatted as a single continuous call with the message
string and stacklevel parameter correctly separated.
🪄 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
Run ID: 68f72141-8413-48e5-aded-dc6653a53366
📒 Files selected for processing (6)
monai/metrics/active_learning_metrics.pymonai/metrics/average_precision.pymonai/metrics/confusion_matrix.pymonai/metrics/cumulative_average.pymonai/metrics/rocauc.pymonai/metrics/utils.py
Address CodeRabbit review: fix formatting of warnings.warn() call where comma was on a separate line before stacklevel parameter. Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
Fixes #8931
Problem
12
warnings.warn()calls inmonai/metrics/are missingstacklevel=2. Without this parameter, warnings point to MONAI library internals instead of the user's calling code, making them unhelpful for debugging.This follows the same pattern identified in
losses/(PR #8930) and is a natural extension to themetrics/module.Solution
Added
stacklevel=2to all 12warnings.warn()calls across 6 files inmonai/metrics/:cumulative_average.pyaverage_precision.pyutils.pyutils.pyutils.pyactive_learning_metrics.pyconfusion_matrix.pyrocauc.pyNote:
embedding_collapse.pyalready hadstacklevel=3(intentionally different for its call depth) and was left unchanged.Verification
ast.parse()syntax checkwarnings.warn()calls inmonai/metrics/confirmed to havestacklevelparameterstacklevel=2parameter additionsChangelog
Files Changed
monai/metrics/cumulative_average.py— Added stacklevel=2 to 1 warningmonai/metrics/average_precision.py— Added stacklevel=2 to 2 warningsmonai/metrics/utils.py— Added stacklevel=2 to 4 warningsmonai/metrics/active_learning_metrics.py— Added stacklevel=2 to 2 warningsmonai/metrics/confusion_matrix.py— Added stacklevel=2 to 1 warningmonai/metrics/rocauc.py— Added stacklevel=2 to 2 warningsVerification
About the Author: Raphael Malikian — Clinical AI Solutions Architect. I specialise in building and fixing AI/ML systems for healthcare, including vector databases, RAG pipelines, and clinical NLP. If you need help with your project or think I can add value to your organisation, feel free to reach out — I'd love to connect.
📧 rtmalikian@gmail.com
🔗 GitHub: https://github.com/rtmalikian
🔗 LinkedIn: http://www.linkedin.com/in/raphael-t-malikian-mbbs-bsc-hons-71075436a
Disclosure: This code was developed with assistance from mimo-v2.5-pro (Xiaomi) via Hermes Agent (Nous Research). All changes were reviewed, tested against the actual codebase, and verified for correctness.