Skip to content

fix(metrics): return the minimum for Hausdorff percentile=0 - #9033

Open
aymuos15 wants to merge 1 commit into
Project-MONAI:devfrom
aymuos15:fix/hausdorff-percentile-zero
Open

fix(metrics): return the minimum for Hausdorff percentile=0#9033
aymuos15 wants to merge 1 commit into
Project-MONAI:devfrom
aymuos15:fix/hausdorff-percentile-zero

Conversation

@aymuos15

Copy link
Copy Markdown
Contributor

Description

HausdorffDistanceMetric(percentile=0) returned the full Hausdorff distance instead of the 0th-percentile (minimum) surface distance. The guard in _compute_percentile_hausdorff_distance was a truthiness test, if not percentile:, so the valid input 0 was treated as "percentile unset" and short-circuited to surface_distance.max().

percentile is documented as "an optional float number between 0 and 100", and the 0th percentile of the surface distances is the minimum — so percentile=0 must not be treated as unset. The fix:

  • percentile=None.max() (unchanged: the default, full Hausdorff distance)
  • percentile == 0.min() (the 0th-percentile minimum)
  • everything else → the existing torch.quantile branch

percentile == 0 is routed through .min() rather than the quantile branch on purpose: torch.quantile returns NaN for an all-inf distance tensor (empty prediction or ground truth), which would have regressed those cases from inf to NaN.

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_distance covering None/0/quantile paths, the all-inf guard, the empty-tensor path, and out-of-range ValueErrors.

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.

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>
@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The Hausdorff distance helper now distinguishes percentile=None from percentile=0. It returns the maximum distance for None and the minimum distance for 0. Tests cover helper boundaries, invalid values, infinite and empty inputs, median results, and metric calculations with isotropic and anisotropic spacing.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the fix for percentile=0 in the Hausdorff metric.
Description check ✅ Passed The description explains the bug, implementation, edge cases, and tests, and includes the required change-type section.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

207-212: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add required Google-style docstrings to the changed definitions.

  • monai/metrics/hausdorff_distance.py#L207-L212: document None as maximum distance, 0 as minimum surface distance, valid quantiles, the return value, and ValueError.
  • 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

📥 Commits

Reviewing files that changed from the base of the PR and between 8690ae7 and 31b8496.

📒 Files selected for processing (2)
  • monai/metrics/hausdorff_distance.py
  • tests/metrics/test_hausdorff_distance.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.

1 participant