Skip to content

Commit 9b168f4

Browse files
asifuddin01claude
andcommitted
docs(metrics): document the Hausdorff nan/inf contract
Adds the Google-style `Returns` and `Raises` sections the repository's path instructions ask for, on `compute_hausdorff_distance` and on `_compute_percentile_hausdorff_distance`, which had a one-line docstring and no `Args`. The sections state the part this change turns on: `nan` when neither mask has a foreground, so there is nothing to measure and the reduction excludes it, and `inf` when exactly one mask is empty, at the maximum and at every percentile alike. That distinction was previously only inferable from the code. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: asifuddin01 <md.asif.uddin@g.bracu.ac.bd>
1 parent 503e979 commit 9b168f4

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

monai/metrics/hausdorff_distance.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ def compute_hausdorff_distance(
166166
If inner sequence has length 1, isotropic spacing with that value is used for all images in the batch,
167167
else the inner sequence length must be equal to the image dimensions. If ``None``, spacing of unity is used
168168
for all images in batch. Defaults to ``None``.
169+
170+
Returns:
171+
A ``float`` tensor of shape ``[batch_size, n_class]`` holding one distance per
172+
image and class. An entry is ``inf`` where exactly one of the two masks is empty,
173+
and ``nan`` where both are, which ``do_metric_reduction`` then excludes.
174+
175+
Raises:
176+
ValueError: if ``y_pred`` and ``y`` have different shapes, or if ``percentile``
177+
is outside ``[0, 100]``.
169178
"""
170179

171180
if not include_background:
@@ -201,7 +210,24 @@ def _compute_percentile_hausdorff_distance(
201210
surface_distance: torch.Tensor, percentile: float | None = None
202211
) -> torch.Tensor:
203212
"""
204-
This function is used to compute the Hausdorff distance.
213+
Reduce a tensor of surface distances to a single Hausdorff distance.
214+
215+
Args:
216+
surface_distance: the surface distances for one image and class, as returned by
217+
``get_surface_distance``. Empty when neither mask has a foreground, and
218+
entirely infinite when exactly one of them does.
219+
percentile: an optional float between 0 and 100. If given, the corresponding
220+
percentile of ``surface_distance`` is returned rather than its maximum.
221+
Defaults to ``None``.
222+
223+
Returns:
224+
A scalar ``float`` tensor. ``nan`` when ``surface_distance`` is empty, meaning
225+
there was no structure on either side to measure; ``inf`` when every distance is
226+
infinite, meaning one mask was empty, at the maximum and at every percentile
227+
alike.
228+
229+
Raises:
230+
ValueError: if ``percentile`` is outside ``[0, 100]``.
205231
"""
206232

207233
# for both pred and gt do not have foreground

0 commit comments

Comments
 (0)