Skip to content

Conversation

@ytl0623
Copy link
Contributor

@ytl0623 ytl0623 commented Jan 19, 2026

Description

This PR addresses a TODO by refactoring the ignore_background utility function.

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.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 19, 2026

📝 Walkthrough

Walkthrough

ignore_background in monai/metrics/utils.py now accepts an optional y: NdarrayTensor | None = None and has typing overloads for both call patterns. It computes a background-removed y_pred_out (only when channel count > 1) and returns y_pred_out when y is None or (y_pred_out, y_out) when y is provided. The function docstring and typing.overload import were added. In monai/metrics/active_learning_metrics.py, compute_variance calls ignore_background with include_background=False and assigns the single returned tensor to y_pred (no longer expecting a tuple or reassigning y).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed Title clearly and concisely summarizes the main change: making the y parameter optional in ignore_background.
Description check ✅ Passed Description covers the main change and includes required template sections, though docstring and documentation update checkboxes remain unchecked.

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

✨ Finishing touches
  • 📝 Generate docstrings

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.

@ytl0623 ytl0623 force-pushed the fix-ignore_background branch 2 times, most recently from 2cc0c16 to 864f902 Compare January 19, 2026 05:47
@ytl0623 ytl0623 changed the title Make 'y' optional in ignore_background and clean up workaround in VarianceMetric Make 'y' optional in ignore_background Jan 19, 2026
@ytl0623 ytl0623 force-pushed the fix-ignore_background branch from 864f902 to fd724f1 Compare January 19, 2026 06:05
Copy link
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.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@monai/metrics/utils.py`:
- Around line 62-80: Update the ignore_background docstring to include a
Google-style Returns section describing the returned tuple (y_pred with
background channel removed when applicable, and y which may be None or have its
background removed) and the shapes/types returned; then add a unit test for the
function ignore_background that calls it with y=None (and representative y_pred
shapes for classification and segmentation) asserting it returns the expected
y_pred (background removed only when channels>1) and that y is still None,
ensuring no exceptions occur.

@ytl0623 ytl0623 force-pushed the fix-ignore_background branch from fd724f1 to 3954f5c Compare January 19, 2026 07:29
Copy link
Member

@ericspod ericspod left a comment

Choose a reason for hiding this comment

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

Hi @ytl0623 thanks for this fix, I think it's fine as a minor change with a couple of comments.

@ytl0623 ytl0623 changed the title Make 'y' optional in ignore_background Make y optional in ignore_background function Jan 20, 2026
@ytl0623 ytl0623 force-pushed the fix-ignore_background branch 2 times, most recently from 9dfa483 to 8d887a5 Compare January 20, 2026 01:25
Copy link
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.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@monai/metrics/utils.py`:
- Around line 80-91: Add unit tests covering both branches of the function that
uses y_pred, y, y_pred_out and y_out: (1) when y is None assert the function
returns a single tensor equal to y_pred (and when y_pred.shape[1] > 1 that it
returns y_pred[:,1:]); (2) when y is provided assert it returns a tuple
(y_pred_out, y_out) and that both are correctly sliced to [:,1:] when their
shape[1] > 1 and unchanged when shape[1]==1. Place tests to call the same
function containing this snippet, use small synthetic NdarrayTensor inputs to
validate shapes and values, and include assertions for returned types (single
tensor vs tuple).

Signed-off-by: ytl0623 <david89062388@gmail.com>
@ytl0623 ytl0623 force-pushed the fix-ignore_background branch from ff4bc92 to 665e2f0 Compare January 20, 2026 02:57
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