|
| 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 | +from ignite.engine import Engine, Events |
| 18 | +from parameterized import parameterized |
| 19 | + |
| 20 | +from monai.handlers import MetricsReloadedBinaryHandler, MetricsReloadedCategoricalHandler, from_engine |
| 21 | +from monai.utils import optional_import |
| 22 | +from tests.utils import assert_allclose |
| 23 | + |
| 24 | +_, has_metrics = optional_import("MetricsReloaded") |
| 25 | + |
| 26 | +TEST_CASE_BIN_1 = [ |
| 27 | + {"metric_name": "Volume Difference"}, |
| 28 | + [torch.tensor([[[1.0, 0.0], [0.0, 1.0]]]), torch.tensor([[[1.0, 0.0], [0.0, 1.0]]])], |
| 29 | + [torch.tensor([[[1.0, 0.0], [1.0, 1.0]]]), torch.tensor([[[1.0, 0.0], [1.0, 1.0]]])], |
| 30 | + 0.3333, |
| 31 | +] |
| 32 | + |
| 33 | +TEST_CASE_BIN_2 = [ |
| 34 | + {"metric_name": "Boundary IoU"}, |
| 35 | + [torch.tensor([[[1.0, 0.0], [0.0, 1.0]]]), torch.tensor([[[1.0, 0.0], [0.0, 1.0]]])], |
| 36 | + [torch.tensor([[[1.0, 0.0], [1.0, 1.0]]]), torch.tensor([[[1.0, 0.0], [1.0, 1.0]]])], |
| 37 | + 0.6667, |
| 38 | +] |
| 39 | + |
| 40 | +TEST_CASE_BIN_3 = [ |
| 41 | + {"metric_name": "xTh Percentile Hausdorff Distance"}, |
| 42 | + [torch.tensor([[[1.0, 0.0], [0.0, 1.0]]]), torch.tensor([[[1.0, 0.0], [0.0, 1.0]]])], |
| 43 | + [torch.tensor([[[1.0, 0.0], [1.0, 1.0]]]), torch.tensor([[[1.0, 0.0], [1.0, 1.0]]])], |
| 44 | + 0.9, |
| 45 | +] |
| 46 | + |
| 47 | +TEST_CASE_CAT_1 = [ |
| 48 | + {"metric_name": "Weighted Cohens Kappa"}, |
| 49 | + [ |
| 50 | + torch.tensor([[[0, 0], [0, 1]], [[0, 0], [0, 0]], [[1, 1], [1, 0]]]), |
| 51 | + torch.tensor([[[0, 0], [0, 1]], [[0, 0], [0, 0]], [[1, 1], [1, 0]]]), |
| 52 | + ], |
| 53 | + [ |
| 54 | + torch.tensor([[[1, 0], [0, 1]], [[0, 1], [0, 0]], [[0, 0], [1, 0]]]), |
| 55 | + torch.tensor([[[1, 0], [0, 1]], [[0, 1], [0, 0]], [[0, 0], [1, 0]]]), |
| 56 | + ], |
| 57 | + 0.272727, |
| 58 | +] |
| 59 | + |
| 60 | +TEST_CASE_CAT_2 = [ |
| 61 | + {"metric_name": "Matthews Correlation Coefficient"}, |
| 62 | + [ |
| 63 | + torch.tensor([[[0, 0], [0, 1]], [[0, 0], [0, 0]], [[1, 1], [1, 0]]]), |
| 64 | + torch.tensor([[[0, 0], [0, 1]], [[0, 0], [0, 0]], [[1, 1], [1, 0]]]), |
| 65 | + ], |
| 66 | + [ |
| 67 | + torch.tensor([[[1, 0], [0, 1]], [[0, 1], [0, 0]], [[0, 0], [1, 0]]]), |
| 68 | + torch.tensor([[[1, 0], [0, 1]], [[0, 1], [0, 0]], [[0, 0], [1, 0]]]), |
| 69 | + ], |
| 70 | + 0.387298, |
| 71 | +] |
| 72 | + |
| 73 | + |
| 74 | +@unittest.skipIf(not has_metrics, "MetricsReloaded not available.") |
| 75 | +class TestHandlerMetricsReloadedBinary(unittest.TestCase): |
| 76 | + @parameterized.expand([TEST_CASE_BIN_1, TEST_CASE_BIN_2, TEST_CASE_BIN_3]) |
| 77 | + def test_compute(self, input_params, y_pred, y, expected_value): |
| 78 | + input_params["output_transform"] = from_engine(["pred", "label"]) |
| 79 | + metric = MetricsReloadedBinaryHandler(**input_params) |
| 80 | + |
| 81 | + # set up engine |
| 82 | + |
| 83 | + def _val_func(engine, batch): |
| 84 | + pass |
| 85 | + |
| 86 | + engine = Engine(_val_func) |
| 87 | + metric.attach(engine=engine, name=input_params["metric_name"]) |
| 88 | + engine.state.output = {"pred": y_pred, "label": y} |
| 89 | + engine.fire_event(Events.ITERATION_COMPLETED) |
| 90 | + |
| 91 | + engine.state.output = {"pred": y_pred, "label": y} |
| 92 | + engine.fire_event(Events.ITERATION_COMPLETED) |
| 93 | + |
| 94 | + engine.fire_event(Events.EPOCH_COMPLETED) |
| 95 | + assert_allclose( |
| 96 | + engine.state.metrics[input_params["metric_name"]], expected_value, atol=1e-4, rtol=1e-4, type_test=False |
| 97 | + ) |
| 98 | + |
| 99 | + @parameterized.expand([TEST_CASE_BIN_1, TEST_CASE_BIN_2, TEST_CASE_BIN_3]) |
| 100 | + def test_shape_mismatch(self, input_params, _y_pred, _y, _expected_value): |
| 101 | + input_params["output_transform"] = from_engine(["pred", "label"]) |
| 102 | + metric = MetricsReloadedBinaryHandler(**input_params) |
| 103 | + with self.assertRaises((AssertionError, ValueError)): |
| 104 | + y_pred = torch.Tensor([[0, 1], [1, 0]]) |
| 105 | + y = torch.ones((2, 3)) |
| 106 | + metric.update([y_pred, y]) |
| 107 | + |
| 108 | + with self.assertRaises((AssertionError, ValueError)): |
| 109 | + y_pred = [torch.ones((2, 1, 1)), torch.ones((1, 1, 1))] |
| 110 | + y = [torch.ones((2, 1, 1)), torch.ones((1, 1, 1))] |
| 111 | + metric.update([y_pred, y]) |
| 112 | + |
| 113 | + |
| 114 | +@unittest.skipIf(not has_metrics, "MetricsReloaded not available.") |
| 115 | +class TestMetricsReloadedCategorical(unittest.TestCase): |
| 116 | + @parameterized.expand([TEST_CASE_CAT_1, TEST_CASE_CAT_2]) |
| 117 | + def test_compute(self, input_params, y_pred, y, expected_value): |
| 118 | + input_params["output_transform"] = from_engine(["pred", "label"]) |
| 119 | + metric = MetricsReloadedCategoricalHandler(**input_params) |
| 120 | + |
| 121 | + # set up engine |
| 122 | + |
| 123 | + def _val_func(engine, batch): |
| 124 | + pass |
| 125 | + |
| 126 | + engine = Engine(_val_func) |
| 127 | + metric.attach(engine=engine, name=input_params["metric_name"]) |
| 128 | + engine.state.output = {"pred": y_pred, "label": y} |
| 129 | + engine.fire_event(Events.ITERATION_COMPLETED) |
| 130 | + |
| 131 | + engine.state.output = {"pred": y_pred, "label": y} |
| 132 | + engine.fire_event(Events.ITERATION_COMPLETED) |
| 133 | + |
| 134 | + engine.fire_event(Events.EPOCH_COMPLETED) |
| 135 | + assert_allclose( |
| 136 | + engine.state.metrics[input_params["metric_name"]], expected_value, atol=1e-4, rtol=1e-4, type_test=False |
| 137 | + ) |
| 138 | + |
| 139 | + @parameterized.expand([TEST_CASE_CAT_1, TEST_CASE_CAT_2]) |
| 140 | + def test_shape_mismatch(self, input_params, y_pred, y, _expected_value): |
| 141 | + input_params["output_transform"] = from_engine(["pred", "label"]) |
| 142 | + metric = MetricsReloadedCategoricalHandler(**input_params) |
| 143 | + with self.assertRaises((AssertionError, ValueError)): |
| 144 | + y_pred[0] = torch.zeros([3, 2, 1]) |
| 145 | + metric.update([y_pred, y]) |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + unittest.main() |
0 commit comments