-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix: make y optional in ignore_background #8647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Fix: make y optional in ignore_background #8647
Conversation
WalkthroughThe Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ 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 |
Signed-off-by: Tristan Kirscher <[email protected]>
2a9b490 to
74ce60d
Compare
There was a problem hiding this 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 optionalybehaviorThe new signature/guard for optional
ylooks correct and keeps existing callers safe. The docstring still assumesyis always present and doesn’t mention that the second tuple element can beNone, 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 thecompute_variance(include_background=False, ...)path).Also applies to: 68-71
monai/data/image_reader.py (2)
25-26: NdarrayOrCupy alias is reasonable; minor style thoughtThe TYPE_CHECKING-only
NdarrayOrCupy = Union[np.ndarray, cupy.ndarray]with a runtimeAnyfallback 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 returnsSwitching
img_arraytolist[NdarrayOrCupy]inPydicomReader.get_dataandNibabelReader.get_datacorrectly reflects that elements may be NumPy or CuPy arrays whento_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 adjustingImageReader.get_dataif 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
📒 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.pymonai/metrics/utils.pymonai/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 incompute_variancelooks correctPassing
y=Noneand discarding the second return cleanly uses the new optional parameter while preserving the existing “drop channel 0” behavior ony_pred.Please just confirm there’s coverage for the
include_background=Falsebranch (including multi-channel and single-channel cases) in the variance tests.
There was a problem hiding this 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=Noneis redundantThe new call correctly matches the updated
ignore_backgroundsignature and behavior. You can simplify slightly by relying on the default fory:- 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
📒 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)
Fixes # .
Description
This PR addresses a TODO in
monai/metrics/active_learning_metrics.pyby making theyargument optional in theignore_backgroundutility function. This allows for cleaner code when onlyy_predneeds to be processed, as seen inactive_learning_metrics.py.Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.