code: def test_bin_seg_3d(self):
      for use_softmax in [True, False]:
          num_classes = 2  # labels 0, 1
          # define 3d examples
          target = torch.tensor(
              [
                  # raw 0
                  [
                      [0, 0, 0, 0],
                      [0, 1, 1, 0],
                      [0, 1, 1, 0],
                      [0, 0, 0, 0],
                  ],
                  # raw 1
                  [
                      [0, 0, 0, 0],
                      [0, 1, 1, 0],
                      [0, 1, 1, 0],
                      [0, 0, 0, 0],
                  ],
                  # raw 2
                  [
                      [0, 0, 0, 0],
                      [0, 1, 1, 0],
                      [0, 1, 1, 0],
                      [0, 0, 0, 0],
                  ],
              ]
          )
          # add another dimension corresponding to the batch (batch size = 1 here)
          target = target.unsqueeze(0)  # shape (1, H, W, D)
          target_one_hot = F.one_hot(
              target, num_classes=num_classes
          ).permute(
              0, 4, 1, 2, 3
          )  # test one hot
          pred_very_good = (
              1000
              * F.one_hot(target, num_classes=num_classes)
              .permute(0, 4, 1, 2, 3)
              .float()
              - 500.0
          )
  
          # initialize the mean dice loss
          loss = FocalLoss(to_onehot_y=True, use_softmax=use_softmax)
          loss_onehot = FocalLoss(
              to_onehot_y=False, use_softmax=use_softmax
          )
  
          # focal loss for pred_very_good should be close to 0
          target = target.unsqueeze(1)  # shape (1, 1, H, W)
          focal_loss_good = float(loss(pred_very_good, target).cpu())
          self.assertAlmostEqual(focal_loss_good, 0.0, places=3)
  
          focal_loss_good = float(
              loss_onehot(pred_very_good, target_one_hot).cpu()
          )
          self.assertAlmostEqual(focal_loss_good, 0.0, places=3)
  
          # with alpha
          loss = FocalLoss(
              to_onehot_y=True, alpha=0.5, use_softmax=use_softmax
          )
          focal_loss_good = float(loss(pred_very_good, target).cpu())
          self.assertAlmostEqual(focal_loss_good, 0.0, places=3)
          loss_onehot = FocalLoss(
              to_onehot_y=False, alpha=0.5, use_softmax=use_softmax
          )
          focal_loss_good = float(
              loss_onehot(pred_very_good, target_one_hot).cpu()
          )
          self.assertAlmostEqual(focal_loss_good, 0.0, places=3)
  
imports: from __future__ import annotations
  import unittest
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from parameterized import parameterized
  from monai.losses import FocalLoss
  from monai.networks import one_hot
  from tests.utils import test_script_save
label_desc: Use image node_id 10 for calling the TestFocalLoss obj's underlying member method code with example usage: obj.test_bin_seg_3d() without return types