Skip to content

Fix: make y optional in ignore_background#8647

Open
Kirscher wants to merge 4 commits intoProject-MONAI:devfrom
Kirscher:fix/ignore-background-optional-y
Open

Fix: make y optional in ignore_background#8647
Kirscher wants to merge 4 commits intoProject-MONAI:devfrom
Kirscher:fix/ignore-background-optional-y

Conversation

@Kirscher
Copy link
Copy Markdown
Contributor

Fixes # .

Description

This PR addresses a TODO in monai/metrics/active_learning_metrics.py by making the y argument optional in the ignore_background utility function. This allows for cleaner code when only y_pred needs to be processed, as seen in active_learning_metrics.py.

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
Copy Markdown
Contributor

coderabbitai bot commented Nov 30, 2025

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The ignore_background function in monai/metrics/utils.py now accepts an optional y parameter (y: NdarrayTensor | None = None) and returns tuple[NdarrayTensor, NdarrayTensor | None]. The function only removes the background channel from y when y is not None. In monai/metrics/active_learning_metrics.py, when include_background is False, ignore_background is called with only y_pred and the second return value is ignored; when y is passed, it is returned and handled as before.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: making the y parameter optional in ignore_background.
Description check ✅ Passed Description covers the purpose, change scope, and testing status, matching the template requirements adequately.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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.

Signed-off-by: Tristan Kirscher <85068746+Kirscher@users.noreply.github.com>
@Kirscher Kirscher force-pushed the fix/ignore-background-optional-y branch from 2a9b490 to 74ce60d Compare November 30, 2025 16:40
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.

Actionable comments posted: 0

🧹 Nitpick comments (3)
monai/metrics/utils.py (1)

54-56: Align ignore_background docstring with new optional y behavior

The new signature/guard for optional y looks correct and keeps existing callers safe. The docstring still assumes y is always present and doesn’t mention that the second tuple element can be None, and there’s no Returns section.

Consider tightening the docs, e.g.:

    Args:
        y: optional ground truth; if provided, shape matches `y_pred` and
            the first dim is batch. If None, only `y_pred` is modified.

    Returns:
        Tuple of (`y_pred` without background, `y` without background or None).

Also, ensure you have at least one test hitting ignore_background(..., y=None) (e.g., via the compute_variance(include_background=False, ...) path).

Also applies to: 68-71

monai/data/image_reader.py (2)

25-26: NdarrayOrCupy alias is reasonable; minor style thought

The TYPE_CHECKING-only NdarrayOrCupy = Union[np.ndarray, cupy.ndarray] with a runtime Any fallback is a sensible way to avoid a hard CuPy dependency while keeping type checkers happy.

If you touch this again, you might consider PEP 604 syntax for consistency with the rest of the file, e.g. NdarrayOrCupy = np.ndarray | cupy.ndarray, but it’s purely cosmetic.

Also applies to: 60-66


673-701: Broaden get_data typing/docs to match possible CuPy returns

Switching img_array to list[NdarrayOrCupy] in PydicomReader.get_data and NibabelReader.get_data correctly reflects that elements may be NumPy or CuPy arrays when to_gpu=True. However:

  • Both methods are still annotated as returning tuple[np.ndarray, dict].
  • Their docstrings also describe the first return as “numpy array of image data”.

Given that these paths can now yield CuPy arrays, consider (in a follow-up) widening the public typing/docs, for example:

  • Updating the return types to tuple[NdarrayOrCupy, dict] (and adjusting ImageReader.get_data if you want consistent overrides), and
  • Relaxing the docstrings to “array-like image data (NumPy or CuPy)”.

This will help static checkers and users relying on the type hints, without changing runtime behavior.

Also applies to: 1111-1138

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between e267705 and 2a9b490.

📒 Files selected for processing (3)
  • monai/data/image_reader.py (4 hunks)
  • monai/metrics/active_learning_metrics.py (1 hunks)
  • monai/metrics/utils.py (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

⚙️ CodeRabbit configuration file

Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.

Files:

  • monai/data/image_reader.py
  • monai/metrics/utils.py
  • monai/metrics/active_learning_metrics.py
🧬 Code graph analysis (1)
monai/metrics/active_learning_metrics.py (1)
monai/metrics/utils.py (1)
  • ignore_background (54-71)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
  • GitHub Check: min-dep-pytorch (2.8.0)
  • GitHub Check: min-dep-pytorch (2.7.1)
  • GitHub Check: min-dep-py3 (3.12)
  • GitHub Check: min-dep-pytorch (2.5.1)
  • GitHub Check: min-dep-py3 (3.9)
  • GitHub Check: min-dep-pytorch (2.6.0)
  • GitHub Check: min-dep-py3 (3.10)
  • GitHub Check: build-docs
  • GitHub Check: packaging
  • GitHub Check: flake8-py3 (mypy)
  • GitHub Check: min-dep-py3 (3.11)
  • GitHub Check: min-dep-os (ubuntu-latest)
  • GitHub Check: flake8-py3 (pytype)
  • GitHub Check: flake8-py3 (codeformat)
  • GitHub Check: min-dep-os (macOS-latest)
  • GitHub Check: quick-py3 (macOS-latest)
  • GitHub Check: quick-py3 (ubuntu-latest)
  • GitHub Check: min-dep-os (windows-latest)
  • GitHub Check: quick-py3 (windows-latest)
🔇 Additional comments (1)
monai/metrics/active_learning_metrics.py (1)

128-136: ignore_background usage in compute_variance looks correct

Passing y=None and discarding the second return cleanly uses the new optional parameter while preserving the existing “drop channel 0” behavior on y_pred.

Please just confirm there’s coverage for the include_background=False branch (including multi-channel and single-channel cases) in the variance tests.

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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
monai/metrics/active_learning_metrics.py (1)

131-132: Background removal call is correct; y=None is redundant

The new call correctly matches the updated ignore_background signature and behavior. You can simplify slightly by relying on the default for y:

-    if not include_background:
-        y_pred, _ = ignore_background(y_pred=y_pred, y=None)
+    if not include_background:
+        y_pred, _ = ignore_background(y_pred=y_pred)
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 2a9b490 and 74ce60d.

📒 Files selected for processing (2)
  • monai/metrics/active_learning_metrics.py (1 hunks)
  • monai/metrics/utils.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • monai/metrics/utils.py
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

⚙️ CodeRabbit configuration file

Review the Python code for quality and correctness. Ensure variable names adhere to PEP8 style guides, are sensible and informative in regards to their function, though permitting simple names for loop and comprehension variables. Ensure routine names are meaningful in regards to their function and use verbs, adjectives, and nouns in a semantically appropriate way. Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings. Examine code for logical error or inconsistencies, and suggest what may be changed to addressed these. Suggest any enhancements for code improving efficiency, maintainability, comprehensibility, and correctness. Ensure new or modified definitions will be covered by existing or new unit tests.

Files:

  • monai/metrics/active_learning_metrics.py
🧬 Code graph analysis (1)
monai/metrics/active_learning_metrics.py (1)
monai/metrics/utils.py (1)
  • ignore_background (54-71)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
  • GitHub Check: min-dep-py3 (3.10)
  • GitHub Check: min-dep-pytorch (2.8.0)
  • GitHub Check: min-dep-py3 (3.9)
  • GitHub Check: min-dep-pytorch (2.7.1)
  • GitHub Check: min-dep-py3 (3.11)
  • GitHub Check: min-dep-py3 (3.12)
  • GitHub Check: min-dep-pytorch (2.6.0)
  • GitHub Check: min-dep-os (ubuntu-latest)
  • GitHub Check: min-dep-pytorch (2.5.1)
  • GitHub Check: min-dep-os (windows-latest)
  • GitHub Check: min-dep-os (macOS-latest)
  • GitHub Check: quick-py3 (macOS-latest)
  • GitHub Check: packaging
  • GitHub Check: build-docs
  • GitHub Check: quick-py3 (ubuntu-latest)
  • GitHub Check: quick-py3 (windows-latest)
  • GitHub Check: flake8-py3 (codeformat)
  • GitHub Check: flake8-py3 (mypy)
  • GitHub Check: flake8-py3 (pytype)

Copilot AI review requested due to automatic review settings March 1, 2026 11:16
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Makes ignore_background more flexible by allowing callers to drop the y argument, and updates active learning variance computation to use the new optional-y behavior.

Changes:

  • Updated ignore_background to accept y: ... | None and conditionally strip the background channel from y only when provided.
  • Simplified compute_variance to remove background only from y_pred without constructing a dummy y.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
monai/metrics/utils.py Updates ignore_background signature/behavior to support y=None.
monai/metrics/active_learning_metrics.py Uses the new optional-y behavior when excluding background in compute_variance.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +54 to +56
def ignore_background(
y_pred: NdarrayTensor, y: NdarrayTensor | None = None
) -> tuple[NdarrayTensor, NdarrayTensor | None]:
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

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

The updated type signature makes y optional, but it also changes the return type for all callers to tuple[NdarrayTensor, NdarrayTensor | None]. This will propagate Optional[...] into existing call sites that always pass y (many metrics do), and is likely to fail the repo’s mypy checks. Consider using @overload so that when y is provided the return type remains tuple[NdarrayTensor, NdarrayTensor], and only returns None in the second position when y is omitted/None.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @Kirscher we are seeing this issue in the mypy tests now, please look at what was said here to see how to resolve it. There are other uses of ignore_background you can update to remove the unneeded y=y argument.

Comment on lines 58 to 65
@@ -63,7 +65,8 @@ def ignore_background(y_pred: NdarrayTensor, y: NdarrayTensor) -> tuple[NdarrayT

Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

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

ignore_background now allows y=None, but the docstring still describes y as a required ground truth tensor. Please update the docstring to reflect that y is optional and clarify what is returned when y is None (e.g., second tuple element is None).

Copilot uses AI. Check for mistakes.
y = y_pred
# TODO If this utils is made to be optional for 'y' it would be nice
y_pred, y = ignore_background(y_pred=y_pred, y=y)
y_pred, _ = ignore_background(y_pred=y_pred, y=None)
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

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

PR description indicates new tests were added for this change, but the diff only modifies monai/metrics/utils.py and monai/metrics/active_learning_metrics.py (no test updates shown). If there are no test changes, please update the PR description checkbox accordingly, or include the new/updated test(s) in this PR.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Kirscher We should add a test for this, thanks.

ericspod and others added 2 commits March 1, 2026 11:52
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
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)
monai/metrics/utils.py (1)

54-66: ⚠️ Potential issue | 🟡 Minor

Docstring needs update for optional y and return values.

Per coding guidelines, the docstring should document:

  1. That y is optional (default None)
  2. Return values (what the tuple contains)
  3. Behavior when y is None
📝 Proposed docstring update
 def ignore_background(
     y_pred: NdarrayTensor, y: NdarrayTensor | None = None
 ) -> tuple[NdarrayTensor, NdarrayTensor | None]:
     """
     This function is used to remove background (the first channel) for `y_pred` and `y`.

     Args:
         y_pred: predictions. As for classification tasks,
             `y_pred` should has the shape [BN] where N is larger than 1. As for segmentation tasks,
             the shape should be [BNHW] or [BNHWD].
-        y: ground truth, the first dim is batch.
+        y: ground truth, the first dim is batch. If None, only `y_pred` is processed.
+
+    Returns:
+        A tuple of (y_pred, y) with the background channel removed. If `y` is None,
+        returns (y_pred, None).

     """

As per coding guidelines: "Docstrings should be present for all definition which describe each variable, return value, and raised exception in the appropriate section of the Google-style of docstrings."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@monai/metrics/utils.py` around lines 54 - 66, Update the docstring for
function ignore_background to explicitly state that parameter y is optional and
defaults to None, describe the return value as a tuple (y_pred_no_bg,
y_no_bg_or_None) where the first element is y_pred with the background/first
channel removed and the second is y with background removed or None if no y was
provided, and document the function behavior when y is None (i.e., only y_pred
is processed and the second tuple element is None); include these descriptions
in the Args and Returns sections of the Google-style docstring for
ignore_background.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@monai/metrics/utils.py`:
- Around line 54-66: Update the docstring for function ignore_background to
explicitly state that parameter y is optional and defaults to None, describe the
return value as a tuple (y_pred_no_bg, y_no_bg_or_None) where the first element
is y_pred with the background/first channel removed and the second is y with
background removed or None if no y was provided, and document the function
behavior when y is None (i.e., only y_pred is processed and the second tuple
element is None); include these descriptions in the Args and Returns sections of
the Google-style docstring for ignore_background.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d0815cec-5bab-444a-8bc3-f2da55312bf9

📥 Commits

Reviewing files that changed from the base of the PR and between 5c3f448 and e674ec1.

📒 Files selected for processing (1)
  • monai/metrics/utils.py

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.

3 participants