# Copyright 2020 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Callable, Optional, Sequence
import torch
from monai.metrics import HausdorffDistanceMetric
from monai.utils import MetricReduction, exact_version, optional_import
NotComputableError, _ = optional_import("ignite.exceptions", "0.4.2", exact_version, "NotComputableError")
Metric, _ = optional_import("ignite.metrics", "0.4.2", exact_version, "Metric")
reinit__is_reduced, _ = optional_import("ignite.metrics.metric", "0.4.2", exact_version, "reinit__is_reduced")
sync_all_reduce, _ = optional_import("ignite.metrics.metric", "0.4.2", exact_version, "sync_all_reduce")
[docs]class HausdorffDistance(Metric): # type: ignore[valid-type, misc] # due to optional_import
"""
Computes Hausdorff distance from full size Tensor and collects average over batch, class-channels, iterations.
"""
def __init__(
self,
include_background: bool = False,
distance_metric: str = "euclidean",
percentile: Optional[float] = None,
directed: bool = False,
output_transform: Callable = lambda x: x,
device: Optional[torch.device] = None,
) -> None:
"""
Args:
include_background: whether to include distance computation on the first channel of the predicted output.
Defaults to ``False``.
distance_metric: : [``"euclidean"``, ``"chessboard"``, ``"taxicab"``]
the metric used to compute surface distance. Defaults to ``"euclidean"``.
percentile: an optional float number between 0 and 100. If specified, the corresponding
percentile of the Hausdorff Distance rather than the maximum result will be achieved.
Defaults to ``None``.
directed: whether to calculate directed Hausdorff distance. Defaults to ``False``.
output_transform: transform the ignite.engine.state.output into [y_pred, y] pair.
device: device specification in case of distributed computation usage.
"""
super().__init__(output_transform, device=device)
self.hd = HausdorffDistanceMetric(
include_background=include_background,
distance_metric=distance_metric,
percentile=percentile,
directed=directed,
reduction=MetricReduction.MEAN,
)
self._sum = 0.0
self._num_examples = 0
[docs] @reinit__is_reduced
def reset(self) -> None:
self._sum = 0.0
self._num_examples = 0
[docs] @reinit__is_reduced
def update(self, output: Sequence[torch.Tensor]) -> None:
"""
Args:
output: sequence with contents [y_pred, y].
Raises:
ValueError: When ``output`` length is not 2. The metric can only support y_pred and y.
"""
if len(output) != 2:
raise ValueError(f"output must have length 2, got {len(output)}.")
y_pred, y = output
score, not_nans = self.hd(y_pred, y)
not_nans = int(not_nans.item())
# add all items in current batch
self._sum += score.item() * not_nans
self._num_examples += not_nans
[docs] @sync_all_reduce("_sum", "_num_examples")
def compute(self) -> float:
"""
Raises:
NotComputableError: When ``compute`` is called before an ``update`` occurs.
"""
if self._num_examples == 0:
raise NotComputableError("HausdorffDistance must have at least one example before it can be computed.")
return self._sum / self._num_examples