From 484bc7af216b0220dc5b8b8fafff6a9deb14bb49 Mon Sep 17 00:00:00 2001 From: Soumya Snigdha Kundu Date: Fri, 9 Jan 2026 11:32:16 +0000 Subject: [PATCH] Fix incomplete activation validation in HausdorffDTLoss The validation check for mutually exclusive activation options was incomplete - it only checked sigmoid and softmax but not other_act, despite the error message explicitly mentioning other_act. Before: ```python if int(sigmoid) + int(softmax) > 1: raise ValueError("... [sigmoid=True, softmax=True, other_act is not None].") ``` After: ```python if int(sigmoid) + int(softmax) + int(other_act is not None) > 1: raise ValueError("... [sigmoid=True, softmax=True, other_act is not None].") ``` This is consistent with the validation in dice.py and tversky.py which correctly include all three options in the check. Added tests for: - sigmoid=True with other_act - softmax=True with other_act - All three options combined Signed-off-by: Soumya Snigdha Kundu --- monai/losses/hausdorff_loss.py | 2 +- tests/losses/test_hausdorff_loss.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/monai/losses/hausdorff_loss.py b/monai/losses/hausdorff_loss.py index b75433e1da..28c91b5be5 100644 --- a/monai/losses/hausdorff_loss.py +++ b/monai/losses/hausdorff_loss.py @@ -83,7 +83,7 @@ def __init__( super().__init__(reduction=LossReduction(reduction).value) if other_act is not None and not callable(other_act): raise TypeError(f"other_act must be None or callable but is {type(other_act).__name__}.") - if int(sigmoid) + int(softmax) > 1: + if int(sigmoid) + int(softmax) + int(other_act is not None) > 1: raise ValueError("Incompatible values: more than 1 of [sigmoid=True, softmax=True, other_act is not None].") self.alpha = alpha diff --git a/tests/losses/test_hausdorff_loss.py b/tests/losses/test_hausdorff_loss.py index f2211008c2..afc9dbe135 100644 --- a/tests/losses/test_hausdorff_loss.py +++ b/tests/losses/test_hausdorff_loss.py @@ -212,6 +212,12 @@ def test_ill_shape(self): def test_ill_opts(self): with self.assertRaisesRegex(ValueError, ""): HausdorffDTLoss(sigmoid=True, softmax=True) + with self.assertRaisesRegex(ValueError, ""): + HausdorffDTLoss(sigmoid=True, other_act=torch.tanh) + with self.assertRaisesRegex(ValueError, ""): + HausdorffDTLoss(softmax=True, other_act=torch.tanh) + with self.assertRaisesRegex(ValueError, ""): + HausdorffDTLoss(sigmoid=True, softmax=True, other_act=torch.tanh) chn_input = torch.ones((1, 1, 3)) chn_target = torch.ones((1, 1, 3)) with self.assertRaisesRegex(ValueError, ""): @@ -244,6 +250,12 @@ def test_ill_shape(self): def test_ill_opts(self): with self.assertRaisesRegex(ValueError, ""): LogHausdorffDTLoss(sigmoid=True, softmax=True) + with self.assertRaisesRegex(ValueError, ""): + LogHausdorffDTLoss(sigmoid=True, other_act=torch.tanh) + with self.assertRaisesRegex(ValueError, ""): + LogHausdorffDTLoss(softmax=True, other_act=torch.tanh) + with self.assertRaisesRegex(ValueError, ""): + LogHausdorffDTLoss(sigmoid=True, softmax=True, other_act=torch.tanh) chn_input = torch.ones((1, 1, 3)) chn_target = torch.ones((1, 1, 3)) with self.assertRaisesRegex(ValueError, ""):