Skip to content

Commit cd74ae2

Browse files
committed
Address review: strict zip, docstrings, Returns section
Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
1 parent e459cff commit cd74ae2

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

monai/metrics/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,9 @@ def remap_instance_id(pred: torch.Tensor, by_size: bool = False) -> torch.Tensor
418418
value of the tensor should be an integer, and represents the prediction of its corresponding instance id.
419419
by_size: if True, largest instance will be assigned a smaller id.
420420
421+
Returns:
422+
tensor of the same shape as ``pred`` with ids remapped to ``1..K`` (``torch.int`` dtype),
423+
or ``pred`` unchanged (original dtype) when it contains no foreground ids.
421424
"""
422425
uniq, inverse = torch.unique(pred, return_inverse=True)
423426
order = torch.nonzero(uniq != 0).flatten()

tests/metrics/test_remap_instance_id.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _reference_remap(pred: torch.Tensor, by_size: bool = False) -> torch.Tensor:
4040
return pred
4141
if by_size:
4242
instance_size = [(pred == instance_id).sum() for instance_id in pred_id]
43-
pair_list = sorted(zip(pred_id, instance_size), key=lambda x: x[1], reverse=True)
43+
pair_list = sorted(zip(pred_id, instance_size, strict=True), key=lambda x: x[1], reverse=True)
4444
pred_id = [p[0] for p in pair_list]
4545
new_pred = torch.zeros_like(pred, dtype=torch.int)
4646
for idx, instance_id in enumerate(pred_id):
@@ -49,20 +49,24 @@ def _reference_remap(pred: torch.Tensor, by_size: bool = False) -> torch.Tensor:
4949

5050

5151
class TestRemapInstanceId(unittest.TestCase):
52+
"""Tests for `remap_instance_id` covering expected values, pass-through cases, and reference equivalence."""
5253

5354
@parameterized.expand(TEST_CASES)
5455
def test_expected_value(self, _, pred, by_size, expected):
56+
"""Remapping produces the hand-computed contiguous ids, including `by_size` ordering and ties."""
5557
result = remap_instance_id(torch.as_tensor(pred, device=_device), by_size=by_size)
5658
torch.testing.assert_close(result.cpu(), torch.as_tensor(expected, dtype=torch.int), check_dtype=False)
5759

5860
@parameterized.expand([["all_background_2d", (4, 4)], ["all_background_3d", (2, 3, 4)], ["empty", (0,)]])
5961
def test_passthrough(self, _, shape):
62+
"""Inputs without foreground ids are returned unchanged, keeping their original dtype."""
6063
pred = torch.zeros(shape, dtype=torch.int64, device=_device)
6164
result = remap_instance_id(pred, by_size=True)
6265
self.assertEqual(result.dtype, pred.dtype)
6366
torch.testing.assert_close(result, pred)
6467

6568
def test_output_dtype(self):
69+
"""Remapped outputs use the `torch.int` dtype regardless of input dtype."""
6670
pred = torch.as_tensor([[0, 9]], dtype=torch.int64, device=_device)
6771
self.assertEqual(remap_instance_id(pred).dtype, torch.int)
6872

@@ -75,8 +79,9 @@ def test_output_dtype(self):
7579
]
7680
)
7781
def test_matches_reference(self, name, shape, n_inst, by_size):
78-
g = torch.Generator().manual_seed(0)
79-
pred = torch.randint(0, n_inst + 1, shape, generator=g).to(_device)
82+
"""Randomized inputs produce output identical to the previous per-instance-loop implementation."""
83+
generator = torch.Generator().manual_seed(0)
84+
pred = torch.randint(0, n_inst + 1, shape, generator=generator).to(_device)
8085
if name.startswith("sparse"):
8186
pred = pred * 1000 + 17 # large, non-contiguous, no-background ids
8287
result = remap_instance_id(pred, by_size=by_size)

0 commit comments

Comments
 (0)