code: def test_tolerance_euclidean_distance_with_spacing(self):
      batch_size = 2
      n_class = 2
      test_spacing = (0.85, 1.2)
      predictions = torch.zeros(
          (batch_size, 480, 640), dtype=torch.int64, device=_device
      )
      labels = torch.zeros(
          (batch_size, 480, 640), dtype=torch.int64, device=_device
      )
      predictions[0, :, 50:] = 1
      labels[0, :, 60:] = 1  # 10 px shift
      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
      )
  
      sd0 = SurfaceDiceMetric(
          class_thresholds=[0, 0], include_background=True
      )
      res0 = sd0(predictions_hot, labels_hot, spacing=test_spacing)
      agg0 = (
          sd0.aggregate()
      )  # aggregation: nanmean across image then nanmean across batch
      sd0_nans = SurfaceDiceMetric(
          class_thresholds=[0, 0],
          include_background=True,
          get_not_nans=True,
      )
      res0_nans = sd0_nans(predictions_hot, labels_hot)
      agg0_nans, not_nans = sd0_nans.aggregate()
  
      np.testing.assert_array_equal(res0.cpu(), res0_nans.cpu())
      np.testing.assert_equal(res0.device, predictions.device)
      np.testing.assert_array_equal(agg0.cpu(), agg0_nans.cpu())
      np.testing.assert_equal(agg0.device, predictions.device)
  
      res1 = SurfaceDiceMetric(
          class_thresholds=[1, 1], include_background=True
      )(predictions_hot, labels_hot, spacing=test_spacing)
      res9 = SurfaceDiceMetric(
          class_thresholds=[9, 9], include_background=True
      )(predictions_hot, labels_hot, spacing=test_spacing)
      res10 = SurfaceDiceMetric(
          class_thresholds=[10, 10], include_background=True
      )(predictions_hot, labels_hot, spacing=test_spacing)
      res11 = SurfaceDiceMetric(
          class_thresholds=[11, 11], include_background=True
      )(predictions_hot, labels_hot, spacing=test_spacing)
      # because spacing is (0.85, 1.2) and we moved 10 pixels in the columns direction,
      # everything with tolerance 12 or more should be the same as tolerance 12 (surface dice is 1.0)
      res12 = SurfaceDiceMetric(
          class_thresholds=[12, 12], include_background=True
      )(predictions_hot, labels_hot, spacing=test_spacing)
      res13 = SurfaceDiceMetric(
          class_thresholds=[13, 13], include_background=True
      )(predictions_hot, labels_hot, spacing=test_spacing)
  
      for res in [res0, res9, res10, res11, res12, res13]:
          assert res.shape == torch.Size([2, 2])
  
      assert (
          res0[0, 0]
          < res1[0, 0]
          < res9[0, 0]
          < res10[0, 0]
          < res11[0, 0]
      )
      assert (
          res0[0, 1]
          < res1[0, 1]
          < res9[0, 1]
          < res10[0, 1]
          < res11[0, 1]
      )
      np.testing.assert_array_equal(res12.cpu(), res13.cpu())
  
      expected_res0 = np.zeros((batch_size, n_class))
      expected_res0[0, 1] = 1 - (478 + 480 + 9 * 2) / (
          480 * 4 + 588 * 2 + 578 * 2
      )
      expected_res0[0, 0] = 1 - (478 + 480 + 9 * 2) / (
          480 * 4 + 48 * 2 + 58 * 2
      )
      expected_res0[1, 0] = 1
      expected_res0[1, 1] = np.nan
      for b, c in np.ndindex(batch_size, n_class):
          np.testing.assert_allclose(
              expected_res0[b, c], res0[b, c].cpu()
          )
      np.testing.assert_array_equal(
          agg0.cpu(),
          np.nanmean(np.nanmean(expected_res0, axis=1), axis=0),
      )
      np.testing.assert_equal(not_nans.cpu(), torch.tensor(2))
  
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 1 for calling the TestAllSurfaceDiceMetrics obj's underlying member method code with example usage: obj.test_tolerance_euclidean_distance_with_spacing() without return types