|
| 1 | +# Copyright (c) MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import unittest |
| 15 | + |
| 16 | +import torch |
| 17 | + |
| 18 | +from monai.metrics import AbsoluteVolumeDifferenceMetric, compute_absolute_volume_difference |
| 19 | + |
| 20 | + |
| 21 | +class TestComputeAbsoluteVolumeDifference(unittest.TestCase): |
| 22 | + """Tests for the standalone compute_absolute_volume_difference function.""" |
| 23 | + |
| 24 | + def test_perfect_prediction_returns_zero(self): |
| 25 | + """Identical prediction and ground truth should yield AVD of zero for all classes.""" |
| 26 | + # identical masks → AVD = 0 for every class |
| 27 | + y = torch.zeros(2, 3, 4, 4) |
| 28 | + y[:, 1, :2, :2] = 1.0 |
| 29 | + y[:, 2, 2:, 2:] = 1.0 |
| 30 | + result = compute_absolute_volume_difference(y_pred=y, y=y, ignore_empty=False) |
| 31 | + self.assertEqual(result.shape, torch.Size([2, 3])) |
| 32 | + self.assertTrue(torch.all(result == 0.0)) |
| 33 | + |
| 34 | + def test_known_volume_difference(self): |
| 35 | + """AVD should equal the absolute difference in foreground voxel counts between prediction and GT.""" |
| 36 | + # batch=1, 2 classes (background + foreground), 1D spatial of length 10 |
| 37 | + y_pred = torch.zeros(1, 2, 10) |
| 38 | + y_true = torch.zeros(1, 2, 10) |
| 39 | + y_pred[0, 1, :7] = 1.0 # 7 foreground voxels predicted |
| 40 | + y_true[0, 1, :4] = 1.0 # 4 foreground voxels in GT |
| 41 | + result = compute_absolute_volume_difference(y_pred=y_pred, y=y_true, ignore_empty=False) |
| 42 | + # channel 0: both all-zeros → AVD = 0 |
| 43 | + # channel 1: |7 - 4| = 3 |
| 44 | + self.assertAlmostEqual(result[0, 0].item(), 0.0) |
| 45 | + self.assertAlmostEqual(result[0, 1].item(), 3.0) |
| 46 | + |
| 47 | + def test_ignore_background(self): |
| 48 | + """Setting include_background=False should strip the first channel and reduce output shape accordingly.""" |
| 49 | + y_pred = torch.zeros(2, 3, 8, 8) |
| 50 | + y_true = torch.zeros(2, 3, 8, 8) |
| 51 | + y_pred[:, 1, :3, :3] = 1.0 |
| 52 | + y_true[:, 1, :4, :4] = 1.0 |
| 53 | + result = compute_absolute_volume_difference(y_pred=y_pred, y=y_true, include_background=False) |
| 54 | + # background channel stripped → shape [2, 2] |
| 55 | + self.assertEqual(result.shape, torch.Size([2, 2])) |
| 56 | + |
| 57 | + def test_ignore_empty_sets_nan(self): |
| 58 | + """Channels with no ground-truth foreground voxels should be NaN when ignore_empty=True.""" |
| 59 | + # channel 1 has no GT voxels → should be NaN when ignore_empty=True |
| 60 | + y_pred = torch.zeros(1, 2, 6) |
| 61 | + y_true = torch.zeros(1, 2, 6) |
| 62 | + y_pred[0, 0, :3] = 1.0 |
| 63 | + result = compute_absolute_volume_difference(y_pred=y_pred, y=y_true, ignore_empty=True) |
| 64 | + # channel 0: GT is empty → NaN |
| 65 | + self.assertTrue(torch.isnan(result[0, 0])) |
| 66 | + # channel 1: GT is empty → NaN |
| 67 | + self.assertTrue(torch.isnan(result[0, 1])) |
| 68 | + |
| 69 | + def test_ignore_empty_false_returns_pred_volume(self): |
| 70 | + """With ignore_empty=False and empty GT, AVD should equal the predicted volume.""" |
| 71 | + # when GT is all zero and ignore_empty=False, AVD = |V_pred - 0| = V_pred |
| 72 | + y_pred = torch.zeros(1, 2, 6) |
| 73 | + y_true = torch.zeros(1, 2, 6) |
| 74 | + y_pred[0, 1, :5] = 1.0 |
| 75 | + result = compute_absolute_volume_difference(y_pred=y_pred, y=y_true, ignore_empty=False) |
| 76 | + self.assertAlmostEqual(result[0, 1].item(), 5.0) |
| 77 | + |
| 78 | + def test_shape_mismatch_raises(self): |
| 79 | + """Mismatched y_pred and y shapes should raise a ValueError.""" |
| 80 | + with self.assertRaises(ValueError): |
| 81 | + compute_absolute_volume_difference(y_pred=torch.zeros(2, 3, 8, 8), y=torch.zeros(2, 3, 4, 4)) |
| 82 | + |
| 83 | + def test_too_few_dims_raises(self): |
| 84 | + """Input tensors with fewer than 3 dimensions should raise a ValueError.""" |
| 85 | + with self.assertRaises(ValueError): |
| 86 | + compute_absolute_volume_difference(y_pred=torch.zeros(2, 3), y=torch.zeros(2, 3)) |
| 87 | + |
| 88 | + def test_3d_volumes(self): |
| 89 | + """AVD should correctly count voxel differences in 3-D spatial inputs.""" |
| 90 | + # 3-D spatial (D, H, W) |
| 91 | + y_pred = torch.zeros(1, 2, 8, 8, 8) |
| 92 | + y_true = torch.zeros(1, 2, 8, 8, 8) |
| 93 | + y_pred[0, 1, :4, :4, :4] = 1.0 # 64 voxels |
| 94 | + y_true[0, 1, :3, :3, :3] = 1.0 # 27 voxels |
| 95 | + result = compute_absolute_volume_difference(y_pred=y_pred, y=y_true, ignore_empty=False) |
| 96 | + self.assertAlmostEqual(result[0, 1].item(), 37.0) |
| 97 | + |
| 98 | + def test_output_shape_multi_class(self): |
| 99 | + """Output shape should be [batch_size, num_classes] for multi-class inputs.""" |
| 100 | + y = torch.randint(0, 2, (4, 5, 16, 16)).float() |
| 101 | + result = compute_absolute_volume_difference(y_pred=y, y=y, ignore_empty=False) |
| 102 | + self.assertEqual(result.shape, torch.Size([4, 5])) |
| 103 | + |
| 104 | + |
| 105 | +class TestAbsoluteVolumeDifferenceMetric(unittest.TestCase): |
| 106 | + """Tests for the AbsoluteVolumeDifferenceMetric class (cumulative interface).""" |
| 107 | + |
| 108 | + def test_aggregate_mean(self): |
| 109 | + """Mean reduction over accumulated batches should return the correct per-class AVD.""" |
| 110 | + y_pred = torch.zeros(2, 2, 8, 8) |
| 111 | + y_true = torch.zeros(2, 2, 8, 8) |
| 112 | + y_pred[:, 1, :6, :6] = 1.0 # 36 voxels per batch item |
| 113 | + y_true[:, 1, :4, :4] = 1.0 # 16 voxels per batch item |
| 114 | + metric = AbsoluteVolumeDifferenceMetric(include_background=False, reduction="mean", ignore_empty=False) |
| 115 | + metric(y_pred, y_true) |
| 116 | + agg = metric.aggregate() |
| 117 | + # single foreground channel, AVD = 20 for both batch items → mean = 20 |
| 118 | + self.assertAlmostEqual(agg.item(), 20.0) |
| 119 | + metric.reset() |
| 120 | + |
| 121 | + def test_aggregate_returns_not_nans_when_requested(self): |
| 122 | + """When get_not_nans=True, aggregate should return a (metric, not_nans) tuple.""" |
| 123 | + y_pred = torch.zeros(2, 2, 4, 4) |
| 124 | + y_true = torch.zeros(2, 2, 4, 4) |
| 125 | + y_pred[:, 1, :2, :2] = 1.0 |
| 126 | + y_true[:, 1, :2, :2] = 1.0 |
| 127 | + metric = AbsoluteVolumeDifferenceMetric(include_background=False, get_not_nans=True) |
| 128 | + metric(y_pred, y_true) |
| 129 | + result, not_nans = metric.aggregate() |
| 130 | + self.assertIsInstance(result, torch.Tensor) |
| 131 | + self.assertIsInstance(not_nans, torch.Tensor) |
| 132 | + metric.reset() |
| 133 | + |
| 134 | + def test_cumulative_accumulation(self): |
| 135 | + """Multiple forward calls before aggregate should use all accumulated data correctly.""" |
| 136 | + # calling the metric twice and aggregating should use all accumulated data |
| 137 | + metric = AbsoluteVolumeDifferenceMetric(include_background=False, reduction="mean", ignore_empty=False) |
| 138 | + for _ in range(3): |
| 139 | + y_pred = torch.zeros(1, 2, 8) |
| 140 | + y_true = torch.zeros(1, 2, 8) |
| 141 | + y_pred[0, 1, :6] = 1.0 |
| 142 | + y_true[0, 1, :4] = 1.0 |
| 143 | + metric(y_pred, y_true) |
| 144 | + agg = metric.aggregate() |
| 145 | + self.assertAlmostEqual(agg.item(), 2.0) |
| 146 | + metric.reset() |
| 147 | + |
| 148 | + def test_reset_clears_buffer(self): |
| 149 | + """Calling reset() should clear the buffer so a subsequent aggregate() raises.""" |
| 150 | + metric = AbsoluteVolumeDifferenceMetric(ignore_empty=False) |
| 151 | + y = torch.zeros(1, 2, 4) |
| 152 | + y[0, 1, :2] = 1.0 |
| 153 | + metric(y, y) |
| 154 | + metric.reset() |
| 155 | + # after reset the buffer should be empty; calling aggregate raises |
| 156 | + with self.assertRaises(ValueError): |
| 157 | + metric.aggregate() |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + unittest.main() |
0 commit comments