Skip to content

Conversation

@Kirscher
Copy link

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
Contributor

coderabbitai bot commented Nov 30, 2025

Walkthrough

The ignore_background function in monai/metrics/utils.py now accepts an optional y parameter (defaulting to None) with updated return type tuple[NdarrayTensor, NdarrayTensor | None]. The calling code in monai/metrics/active_learning_metrics.py passes y=None to ignore_background when include_background is False, discarding the second return value.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

  • Focused change across 2 files
  • Straightforward parameter signature update with None guard
  • Consistent calling pattern change
  • No complex logic modifications

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed Title clearly and concisely describes the main change: making the y parameter optional in ignore_background function.
Description check ✅ Passed Description covers the core change, rationale, and test coverage. Author marked test addition and quick tests passing, though integration tests and docstring updates are unchecked.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@Kirscher Kirscher force-pushed the fix/ignore-background-optional-y branch from 2a9b490 to 74ce60d Compare November 30, 2025 16:40
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: 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
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)

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.

1 participant