Source code for monai.handlers.hausdorff_distance

# Copyright 2020 - 2021 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
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Callable, Optional, Union

import torch

from monai.handlers.iteration_metric import IterationMetric
from monai.metrics import HausdorffDistanceMetric
from monai.utils import MetricReduction

[docs]class HausdorffDistance(IterationMetric): """ 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: Union[str, torch.device] = "cpu", save_details: bool = True, ) -> 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. save_details: whether to save metric computation details per image, for example: hausdorff distance of every image. default to True, will save to `engine.state.metric_details` dict with the metric name as key. """ super().__init__(output_transform, device=device) metric_fn = HausdorffDistanceMetric( include_background=include_background, distance_metric=distance_metric, percentile=percentile, directed=directed, reduction=MetricReduction.NONE, ) super().__init__( metric_fn=metric_fn, output_transform=output_transform, device=device, save_details=save_details, )