Skip to content

fix: add missing stacklevel=2 to warnings.warn() in metrics/ (Fixes #8931)#8932

Open
rtmalikian wants to merge 2 commits into
Project-MONAI:devfrom
rtmalikian:fix/issue-8931-stacklevel-metrics
Open

fix: add missing stacklevel=2 to warnings.warn() in metrics/ (Fixes #8931)#8932
rtmalikian wants to merge 2 commits into
Project-MONAI:devfrom
rtmalikian:fix/issue-8931-stacklevel-metrics

Conversation

@rtmalikian

Copy link
Copy Markdown

Fixes #8931

Problem

12 warnings.warn() calls in monai/metrics/ are missing stacklevel=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 the metrics/ module.

Solution

Added stacklevel=2 to all 12 warnings.warn() calls across 6 files in monai/metrics/:

File Warning Line
cumulative_average.py non-finite inputs received ~157
average_precision.py y values all same / invalid ~91, ~96
utils.py ground truth/prediction all zero ~342, ~348
utils.py binarized tensor ~380
utils.py Voronoi CPU ~515
active_learning_metrics.py spatial map / reduction ~140, ~195
confusion_matrix.py compute_sample ~96
rocauc.py y values all same / invalid ~80, ~85

Note: embedding_collapse.py already had stacklevel=3 (intentionally different for its call depth) and was left unchanged.

Verification

  • All 6 modified files pass ast.parse() syntax check
  • All 14 warnings.warn() calls in monai/metrics/ confirmed to have stacklevel parameter
  • No other code changes — purely additive stacklevel=2 parameter additions

Changelog

Date Change Author
2026-06-19 Add missing stacklevel=2 to 12 warnings.warn() calls in monai/metrics/ rtmalikian

Files Changed

  • monai/metrics/cumulative_average.py — Added stacklevel=2 to 1 warning
  • monai/metrics/average_precision.py — Added stacklevel=2 to 2 warnings
  • monai/metrics/utils.py — Added stacklevel=2 to 4 warnings
  • monai/metrics/active_learning_metrics.py — Added stacklevel=2 to 2 warnings
  • monai/metrics/confusion_matrix.py — Added stacklevel=2 to 1 warning
  • monai/metrics/rocauc.py — Added stacklevel=2 to 2 warnings

Verification

  • All 6 files pass Python syntax check (ast.parse)
  • All 14 warnings.warn() calls in monai/metrics/ confirmed to have stacklevel parameter
  • No functional behavior changes — only warning source location improves

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.

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>
@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 488a4bcf-5510-416a-b1bb-73b7c621d491

📥 Commits

Reviewing files that changed from the base of the PR and between be2e601 and a4a54c9.

📒 Files selected for processing (1)
  • monai/metrics/utils.py
✅ Files skipped from review due to trivial changes (1)
  • monai/metrics/utils.py

📝 Walkthrough

Walkthrough

Nine warnings.warn calls across six files in monai/metricsactive_learning_metrics.py, average_precision.py, confusion_matrix.py, cumulative_average.py, rocauc.py, and utils.py — are updated to pass stacklevel=2. No logic, signatures, or return values change. Call sites are reformatted to multi-line style where needed to accommodate the new argument.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 77.78% 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
Title check ✅ Passed Title accurately summarizes the main change: adding stacklevel=2 to warnings.warn() calls across metrics/ files to fix issue #8931.
Description check ✅ Passed Description is comprehensive and complete. It provides context, a detailed solution breakdown, verification steps, and specific file/line mappings.
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

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.

@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: 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

📥 Commits

Reviewing files that changed from the base of the PR and between 15f5073 and be2e601.

📒 Files selected for processing (6)
  • monai/metrics/active_learning_metrics.py
  • monai/metrics/average_precision.py
  • monai/metrics/confusion_matrix.py
  • monai/metrics/cumulative_average.py
  • monai/metrics/rocauc.py
  • monai/metrics/utils.py

Comment thread monai/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>
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.

fix: add missing stacklevel=2 to warnings.warn() in metrics/

1 participant