Skip to content

Commit b3bcba4

Browse files
committed
Improving the docstring explanation
Signed-off-by: Vijay Vignesh Prasad Rao <vijayvigneshp02@gmail.com>
1 parent a74f2cf commit b3bcba4

2 files changed

Lines changed: 17 additions & 13 deletions

File tree

monai/metrics/meandice.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import torch
1515

16+
from monai.apps.detection import metrics
1617
from monai.metrics.utils import compute_voronoi_regions_fast, do_metric_reduction
1718
from monai.utils import MetricReduction, deprecated_arg
1819
from monai.utils.module import optional_import
@@ -47,8 +48,17 @@ class DiceMetric(CumulativeIterationMetric):
4748
image size they can get overwhelmed by the signal from the background. This assumes the shape of both prediction
4849
and ground truth is BCHW[D].
4950
50-
The ``per_component`` parameter can be set to `True` to compute the Dice metric per connected component in the ground truth
51-
, and then average. This requires binary segmentations with 2 channels (background + foreground) as input.
51+
The `per_component=True` approach computes the Dice metric on a per-connected component basis in the ground truth segmentation,
52+
ensuring equal weighting for each component regardless of its size. This method eliminates biases in traditional metrics,
53+
providing a more balanced evaluation, particularly in scenarios where object size does not correlate with clinical relevance.
54+
This provides a more granular evaluation of segmentation quality, especially useful when dealing with fragmented or
55+
disconnected objects in the foreground.
56+
Note:
57+
- The input prediction (`y_pred`) and ground truth (`y`) must both have 2 channels (foreground/background),
58+
with binary segmentation (0 for background, 1 for foreground). That is, this assumes the shape of both prediction
59+
and ground truth is B2HW[D].
60+
- This method cannot be used with multiclass segmentation.
61+
For more information, refer to the original paper: https://arxiv.org/abs/2410.18684
5262
5363
The typical execution steps of this metric class follows :py:class:`monai.metrics.metric.Cumulative`.
5464

tests/metrics/test_compute_meandice.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -346,17 +346,11 @@ def test_nans_class(self, params, input_data, expected_value):
346346
@unittest.skipUnless(has_ndimage, "Requires scipy.ndimage.")
347347
def test_cc_dice_value_nogpu(self, params, input_data, expected_value):
348348
dice_metric = DiceMetric(**params)
349-
cpu_inputs = {"y": input_data["y"].cpu(), "y_pred": input_data["y_pred"].cpu()}
350-
dice_metric(**cpu_inputs)
351-
result = dice_metric.aggregate(reduction="none")
352-
np.testing.assert_allclose(result.cpu().numpy(), expected_value, atol=1e-4)
353-
354-
@parameterized.expand([TEST_CASE_16, TEST_CASE_17])
355-
@unittest.skipUnless(has_ndimage, "Requires scipy.ndimage.")
356-
@unittest.skipUnless(torch.cuda.is_available() and has_cupy_ndimage, "Requires CUDA and cupyx.scipy.ndimage.")
357-
def test_cc_dice_value_gpu(self, params, input_data, expected_value):
358-
dice_metric = DiceMetric(**params)
359-
dice_metric(**input_data)
349+
if not has_cupy_ndimage:
350+
cpu_inputs = {"y": input_data["y"].cpu(), "y_pred": input_data["y_pred"].cpu()}
351+
dice_metric(**cpu_inputs)
352+
else:
353+
dice_metric(**input_data)
360354
result = dice_metric.aggregate(reduction="none")
361355
np.testing.assert_allclose(result.cpu().numpy(), expected_value, atol=1e-4)
362356

0 commit comments

Comments
 (0)