code: def test_tolerance_all_distances(self):
      batch_size = 1
      n_class = 2
      predictions = torch.zeros((batch_size, 10, 10), dtype=torch.int64)
      labels = torch.zeros((batch_size, 10, 10), dtype=torch.int64)
      predictions[0, 1:4, 1] = 1
      """
          [[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]]
          """
      labels[0, 5:8, 6] = 1
      """
          [[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]]
          """
      predictions_hot = F.one_hot(
          predictions, num_classes=n_class
      ).permute(0, 3, 1, 2)
      labels_hot = F.one_hot(labels, num_classes=n_class).permute(
          0, 3, 1, 2
      )
  
      # Euclidean distance:
      # background:
      # 36 boundary pixels have 0 distances; non-zero distances:
      # distances gt_pred: [3, np.sqrt(9+4), 2, 3, 2, 2, 2, 1]
      # distances pred_gt: [1, 2, 2, 1]
      # class 1:
      # distances gt_pred: [sqrt(25+4), sqrt(25+9), sqrt(25+16)] = [5.38516481, 5.83095189, 6.40312424]
      # distances pred_gt: [sqrt(25+16), sqrt(25+9), sqrt(25+4)] = [6.40312424, 5.83095189, 5.38516481]
  
      res = SurfaceDiceMetric(
          class_thresholds=[0, 0], include_background=True
      )(predictions_hot, labels_hot)
      expected_res = [[1 - (8 + 4) / (36 * 2 + 8 + 4), 0]]
      np.testing.assert_array_almost_equal(res, expected_res)
  
      res = SurfaceDiceMetric(
          class_thresholds=[2.8, 5.5], include_background=True
      )(predictions_hot, labels_hot)
      expected_res = [[1 - 3 / (36 * 2 + 8 + 4), 1 - (2 + 2) / (3 + 3)]]
      np.testing.assert_array_almost_equal(res, expected_res)
  
      res = SurfaceDiceMetric(
          class_thresholds=[3, 6], include_background=True
      )(predictions_hot, labels_hot)
      expected_res = [[1 - 1 / (36 * 2 + 8 + 4), 1 - 2 / (3 + 3)]]
      np.testing.assert_array_almost_equal(res, expected_res)
  
      # Chessboard distance:
      # background:
      # 36 boundary pixels have 0 distances; non-zero distances:
      # distances gt_pred: [max(3,0), max(3,2), max(2,0), max(3,3), max(2,0), max(0,2), max(2,0), max(0,1)] =
      # [3, 3, 2, 3, 2, 2, 2, 1]
      # distances pred_gt: [max(0,1), max(2,0), max(2,0), max(1,0)] = [1, 2, 2, 1]
      # class 1:
      # distances gt_pred: [max(5,2), max(5,3), max(5,4)] = [5, 5, 5]
      # distances pred_gt: [max(5,4), max(5,3), max(5,2)] = [5, 5, 5]
  
      res = SurfaceDiceMetric(
          class_thresholds=[0, 0],
          include_background=True,
          distance_metric="chessboard",
      )(predictions_hot, labels_hot)
      expected_res = [[1 - (8 + 4) / (36 * 2 + 8 + 4), 0]]
      np.testing.assert_array_almost_equal(res, expected_res)
  
      res = SurfaceDiceMetric(
          class_thresholds=[1, 4.999],
          include_background=True,
          distance_metric="chessboard",
      )(predictions_hot, labels_hot)
      expected_res = [[1 - (7 + 2) / (36 * 2 + 8 + 4), 0]]
      np.testing.assert_array_almost_equal(res, expected_res)
  
      res = SurfaceDiceMetric(
          class_thresholds=[2, 5],
          include_background=True,
          distance_metric="chessboard",
      )(predictions_hot, labels_hot)
      expected_res = [[1 - 3 / (36 * 2 + 8 + 4), 1]]
      np.testing.assert_array_almost_equal(res, expected_res)
  
      # Taxicab distance (= Manhattan distance):
      # background:
      # 36 boundary pixels have 0 distances; non-zero distances:
      # distances gt_pred: [3+0, 4+0, 2+0, 0+3, 2+0, 0+2, 2+0, 0+1] = [3, 4, 2, 3, 2, 2, 2, 1]
      # distances pred_gt: [0+1, 2+0, 2+0, 1+0] = [1, 2, 2, 1]
      # class 1:
      # distances gt_pred: [5+2, 5+3, 5+4] = [7, 8, 9]
      # distances pred_gt: [5+4, 5+3, 5+2] = [9, 8, 7]
  
      res = SurfaceDiceMetric(
          class_thresholds=[0, 0],
          include_background=True,
          distance_metric="taxicab",
      )(predictions_hot, labels_hot)
      expected_res = [[1 - (8 + 4) / (36 * 2 + 8 + 4), 0]]
      np.testing.assert_array_almost_equal(res, expected_res)
  
      res = SurfaceDiceMetric(
          class_thresholds=[1, 7],
          include_background=True,
          distance_metric="taxicab",
      )(predictions_hot, labels_hot)
      expected_res = [
          [1 - (7 + 2) / (36 * 2 + 8 + 4), 1 - (2 + 2) / (3 + 3)]
      ]
      np.testing.assert_array_almost_equal(res, expected_res)
  
      res = SurfaceDiceMetric(
          class_thresholds=[3, 9],
          include_background=True,
          distance_metric="taxicab",
      )(predictions_hot, labels_hot)
      expected_res = [[1 - 1 / (36 * 2 + 8 + 4), 1]]
      np.testing.assert_array_almost_equal(res, expected_res)
  
file_path: MONAI/tests/test_surface_dice.py
imports: from __future__ import annotations
  import unittest
  import numpy
  import torch
  import torch.nn.functional
  from monai.metrics.surface_dice import SurfaceDiceMetric, compute_surface_dice
  from tests.utils import assert_allclose
label_desc: Use image node_id 4 for calling the TestAllSurfaceDiceMetrics obj's underlying member method code with example usage: obj.test_tolerance_all_distances() without return types