fix(metrics): return the minimum for Hausdorff percentile=0 - #9033
fix(metrics): return the minimum for Hausdorff percentile=0#9033aymuos15 wants to merge 1 commit into
Conversation
percentile=0 is the 0th-percentile surface distance (the minimum), but the falsy guard 'if not percentile' treated it as unset and returned surface_distance.max(). Route None to .max(), 0 to .min() (torch.quantile returns NaN for all-inf input, which would regress empty-mask cases from inf to NaN), and everything else through the existing quantile branch. Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
📝 WalkthroughWalkthroughThe Hausdorff distance helper now distinguishes Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
monai/metrics/hausdorff_distance.py (1)
207-212: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd required Google-style docstrings to the changed definitions.
monai/metrics/hausdorff_distance.py#L207-L212: documentNoneas maximum distance,0as minimum surface distance, valid quantiles, the return value, andValueError.tests/metrics/test_hausdorff_distance.py#L236-L247: document test parameters, the return value, and the expected out-of-range error behavior.As per path instructions, Python definitions must document variables, return values, and raised exceptions in Google-style docstrings.
🤖 Prompt for 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. In `@monai/metrics/hausdorff_distance.py` around lines 207 - 212, Add Google-style docstrings to the function containing the percentile-handling logic in monai/metrics/hausdorff_distance.py (lines 207-212) that document the percentile parameter behavior (None returns maximum distance, 0 returns minimum surface distance, valid quantile range), the return value (surface distance scalar), and the ValueError exception for out-of-range percentiles. Similarly, add a Google-style docstring to the test function in tests/metrics/test_hausdorff_distance.py (lines 236-247) that documents the test parameters, describes what is being validated (the return value and out-of-range error behavior), and documents the expected exception behavior when percentile values are invalid.Source: Path instructions
🤖 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.
Nitpick comments:
In `@monai/metrics/hausdorff_distance.py`:
- Around line 207-212: Add Google-style docstrings to the function containing
the percentile-handling logic in monai/metrics/hausdorff_distance.py (lines
207-212) that document the percentile parameter behavior (None returns maximum
distance, 0 returns minimum surface distance, valid quantile range), the return
value (surface distance scalar), and the ValueError exception for out-of-range
percentiles. Similarly, add a Google-style docstring to the test function in
tests/metrics/test_hausdorff_distance.py (lines 236-247) that documents the test
parameters, describes what is being validated (the return value and out-of-range
error behavior), and documents the expected exception behavior when percentile
values are invalid.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: ca73296d-7cf1-4dc0-8a19-d120f0a4fce6
📒 Files selected for processing (2)
monai/metrics/hausdorff_distance.pytests/metrics/test_hausdorff_distance.py
Description
HausdorffDistanceMetric(percentile=0)returned the full Hausdorff distance instead of the 0th-percentile (minimum) surface distance. The guard in_compute_percentile_hausdorff_distancewas a truthiness test,if not percentile:, so the valid input0was treated as "percentile unset" and short-circuited tosurface_distance.max().percentileis documented as "an optional float number between 0 and 100", and the 0th percentile of the surface distances is the minimum — sopercentile=0must not be treated as unset. The fix:percentile=None→.max()(unchanged: the default, full Hausdorff distance)percentile == 0→.min()(the 0th-percentile minimum)torch.quantilebranchpercentile == 0is routed through.min()rather than the quantile branch on purpose:torch.quantilereturnsNaNfor an all-inf distance tensor (empty prediction or ground truth), which would have regressed those cases frominftoNaN.Tests: two new percentile-0 cases in the spherical-segmentation test matrix (plain and with spacing, red on the old code), plus a parameterized unit test of
_compute_percentile_hausdorff_distancecoveringNone/0/quantile paths, the all-inf guard, the empty-tensor path, and out-of-rangeValueErrors.Types of changes
./runtests.sh -f -u --net --coverage../runtests.sh --quick --unittests --disttests.make htmlcommand in thedocs/folder.