class_docstr: Compute the generalized Wasserstein Dice Loss defined in:
  
      Fidon L. et al. (2017) Generalised Wasserstein Dice Score for Imbalanced Multi-class
      Segmentation using Holistic Convolutional Networks. BrainLes 2017.
  
  Or its variant (use the option weighting_mode="GDL") defined in the Appendix of:
  
      Tilborghs, S. et al. (2020) Comparative study of deep learning methods for the automatic
      segmentation of lung, lesion and lesion type in CT scans of COVID-19 patients.
      arXiv preprint arXiv:2007.15546
  
  Adapted from:
      https://github.com/LucasFidon/GeneralizedWassersteinDiceLoss
class_docstr_tok: ["Compute","the","generalized","Wasserstein","Dice","Loss","defined","in",":","Fidon","L.","et","al",".","(","2017",")","Generalised","Wasserstein","Dice","Score","for","Imbalanced","Multi-class","Segmentation","using","Holistic","Convolutional","Networks",".","BrainLes","2017",".","Or","its","variant","(","use","the","option","weighting_mode=","''","GDL","''",")","defined","in","the","Appendix","of",":","Tilborghs",",","S.","et","al",".","(","2020",")","Comparative","study","of","deep","learning","methods","for","the","automatic","segmentation","of","lung",",","lesion","and","lesion","type","in","CT","scans","of","COVID-19","patients",".","arXiv","preprint","arXiv:2007.15546","Adapted","from",":","https",":","\/\/github.com\/LucasFidon\/GeneralizedWassersteinDiceLoss"]
docstr: Args:
      dist_matrix: 2d tensor or 2d numpy array; matrix of distances between the classes.
      It must have dimension C x C where C is the number of classes.
      weighting_mode: {``"default"``, ``"GDL"``}
          Specifies how to weight the class-specific sum of errors.
          Default to ``"default"``.
  
          - ``"default"``: (recommended) use the original weighting method as in:
              Fidon L. et al. (2017) Generalised Wasserstein Dice Score for Imbalanced Multi-class
              Segmentation using Holistic Convolutional Networks. BrainLes 2017.
          - ``"GDL"``: use a GDL-like weighting method as in the Appendix of:
              Tilborghs, S. et al. (2020) Comparative study of deep learning methods for the automatic
              segmentation of lung, lesion and lesion type in CT scans of COVID-19 patients.
              arXiv preprint arXiv:2007.15546
      reduction: {``"none"``, ``"mean"``, ``"sum"``}
          Specifies the reduction to apply to the output. Defaults to ``"mean"``.
  
          - ``"none"``: no reduction will be applied.
          - ``"mean"``: the sum of the output will be divided by the number of elements in the output.
          - ``"sum"``: the output will be summed.
      smooth_nr: a small constant added to the numerator to avoid zero.
      smooth_dr: a small constant added to the denominator to avoid nan.
  
  Raises:
      ValueError: When ``dist_matrix`` is not a square matrix.
  
  Example:
      .. code-block:: python
  
          import torch
          import numpy as np
          from monai.losses import GeneralizedWassersteinDiceLoss
  
          # Example with 3 classes (including the background: label 0).
          # The distance between the background class (label 0) and the other classes is the maximum, equal to 1.
          # The distance between class 1 and class 2 is 0.5.
          dist_mat = np.array([[0.0, 1.0, 1.0], [1.0, 0.0, 0.5], [1.0, 0.5, 0.0]], dtype=np.float32)
          wass_loss = GeneralizedWassersteinDiceLoss(dist_matrix=dist_mat)
  
          pred_score = torch.tensor([[1000, 0, 0], [0, 1000, 0], [0, 0, 1000]], dtype=torch.float32)
          grnd = torch.tensor([0, 1, 2], dtype=torch.int64)
          wass_loss(pred_score, grnd)  # 0
docstr_tok: ["Args",":","dist_matrix",":","2d","tensor","or","2d","numpy","array",";","matrix","of","distances","between","the","classes",".","It","must","have","dimension","C","x","C","where","C","is","the","number","of","classes",".","weighting_mode",":","{","``","``","default","''","``",",","``","``","GDL","''","``","}","Specifies","how","to","weight","the","class-specific","sum","of","errors",".","Default","to","``","``","default","''","``",".","-","``","``","default","''","``",":","(","recommended",")","use","the","original","weighting","method","as","in",":","Fidon","L.","et","al",".","(","2017",")","Generalised","Wasserstein","Dice","Score","for","Imbalanced","Multi-class","Segmentation","using","Holistic","Convolutional","Networks",".","BrainLes","2017",".","-","``","``","GDL","''","``",":","use","a","GDL-like","weighting","method","as","in","the","Appendix","of",":","Tilborghs",",","S.","et","al",".","(","2020",")","Comparative","study","of","deep","learning","methods","for","the","automatic","segmentation","of","lung",",","lesion","and","lesion","type","in","CT","scans","of","COVID-19","patients",".","arXiv","preprint","arXiv:2007.15546","reduction",":","{","``","``","none","''","``",",","``","``","mean","''","``",",","``","``","sum","''","``","}","Specifies","the","reduction","to","apply","to","the","output",".","Defaults","to","``","``","mean","''","``",".","-","``","``","none","''","``",":","no","reduction","will","be","applied",".","-","``","``","mean","''","``",":","the","sum","of","the","output","will","be","divided","by","the","number","of","elements","in","the","output",".","-","``","``","sum","''","``",":","the","output","will","be","summed",".","smooth_nr",":","a","small","constant","added","to","the","numerator","to","avoid","zero",".","smooth_dr",":","a","small","constant","added","to","the","denominator","to","avoid","nan",".","Raises",":","ValueError",":","When","``","dist_matrix","``","is","not","a","square","matrix",".","Example",":","..","code-block",":",":","python","import","torch","import","numpy","as","np","from","monai.losses","import","GeneralizedWassersteinDiceLoss","#","Example","with","3","classes","(","including","the","background",":","label","0",")",".","#","The","distance","between","the","background","class","(","label","0",")","and","the","other","classes","is","the","maximum",",","equal","to","1",".","#","The","distance","between","class","1","and","class","2","is","0.5.","dist_mat","=","np.array","(","[","[","0.0",",","1.0",",","1.0","]",",","[","1.0",",","0.0",",","0.5","]",",","[","1.0",",","0.5",",","0.0","]","]",",","dtype=np.float32",")","wass_loss","=","GeneralizedWassersteinDiceLoss","(","dist_matrix=dist_mat",")","pred_score","=","torch.tensor","(","[","[","1000",",","0",",","0","]",",","[","0",",","1000",",","0","]",",","[","0",",","0",",","1000","]","]",",","dtype=torch.float32",")","grnd","=","torch.tensor","(","[","0",",","1",",","2","]",",","dtype=torch.int64",")","wass_loss","(","pred_score",",","grnd",")","#","0"]
code: def __init__(
      self,
      dist_matrix: np.ndarray | torch.Tensor,
      weighting_mode: str = "default",
      reduction: LossReduction | str = LossReduction.MEAN,
      smooth_nr: float = 1e-5,
      smooth_dr: float = 1e-5,
  ) -> None:
      """
      Args:
          dist_matrix: 2d tensor or 2d numpy array; matrix of distances between the classes.
          It must have dimension C x C where C is the number of classes.
          weighting_mode: {``"default"``, ``"GDL"``}
              Specifies how to weight the class-specific sum of errors.
              Default to ``"default"``.
  
              - ``"default"``: (recommended) use the original weighting method as in:
                  Fidon L. et al. (2017) Generalised Wasserstein Dice Score for Imbalanced Multi-class
                  Segmentation using Holistic Convolutional Networks. BrainLes 2017.
              - ``"GDL"``: use a GDL-like weighting method as in the Appendix of:
                  Tilborghs, S. et al. (2020) Comparative study of deep learning methods for the automatic
                  segmentation of lung, lesion and lesion type in CT scans of COVID-19 patients.
                  arXiv preprint arXiv:2007.15546
          reduction: {``"none"``, ``"mean"``, ``"sum"``}
              Specifies the reduction to apply to the output. Defaults to ``"mean"``.
  
              - ``"none"``: no reduction will be applied.
              - ``"mean"``: the sum of the output will be divided by the number of elements in the output.
              - ``"sum"``: the output will be summed.
          smooth_nr: a small constant added to the numerator to avoid zero.
          smooth_dr: a small constant added to the denominator to avoid nan.
  
      Raises:
          ValueError: When ``dist_matrix`` is not a square matrix.
  
      Example:
          .. code-block:: python
  
              import torch
              import numpy as np
              from monai.losses import GeneralizedWassersteinDiceLoss
  
              # Example with 3 classes (including the background: label 0).
              # The distance between the background class (label 0) and the other classes is the maximum, equal to 1.
              # The distance between class 1 and class 2 is 0.5.
              dist_mat = np.array([[0.0, 1.0, 1.0], [1.0, 0.0, 0.5], [1.0, 0.5, 0.0]], dtype=np.float32)
              wass_loss = GeneralizedWassersteinDiceLoss(dist_matrix=dist_mat)
  
              pred_score = torch.tensor([[1000, 0, 0], [0, 1000, 0], [0, 0, 1000]], dtype=torch.float32)
              grnd = torch.tensor([0, 1, 2], dtype=torch.int64)
              wass_loss(pred_score, grnd)  # 0
  
      """
      super().__init__(reduction=LossReduction(reduction).value)
  
      if dist_matrix.shape[0] != dist_matrix.shape[1]:
          raise ValueError(
              f"dist_matrix must be C x C, got {dist_matrix.shape[0]} x {dist_matrix.shape[1]}."
          )
  
      if weighting_mode not in ["default", "GDL"]:
          raise ValueError(
              "weighting_mode must be either 'default' or 'GDL, got %s."
              % weighting_mode
          )
  
      self.m = dist_matrix
      if isinstance(self.m, np.ndarray):
          self.m = torch.from_numpy(self.m)
      if torch.max(self.m) != 1:
          self.m = self.m / torch.max(self.m)
      self.alpha_mode = weighting_mode
      self.num_classes = self.m.size(0)
      self.smooth_nr = float(smooth_nr)
      self.smooth_dr = float(smooth_dr)
  
imports: from __future__ import annotations
  import warnings
  from collections.abc import Callable, Sequence
  from typing import Any
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from torch.nn.modules.loss import _Loss
  from monai.losses.focal_loss import FocalLoss
  from monai.losses.spatial_mask import MaskedLoss
  from monai.networks import one_hot
  from monai.utils import DiceCEReduction, LossReduction, Weight, deprecated_arg, look_up_option, pytorch_after
label_desc: Use image node_id 1 to create a new GeneralizedWassersteinDiceLoss object from inherited base classes: _Loss with example: obj = GeneralizedWassersteinDiceLoss(dist_matrix, weighting_mode, reduction, smooth_nr, smooth_dr)