class_docstr: Compute average Dice score for a set of pairs of prediction-groundtruth segmentations.
  
  It supports both multi-classes and multi-labels tasks.
  Input `y_pred` is compared with ground truth `y`.
  `y_pred` is expected to have binarized predictions and `y` can be single-channel class indices or in the
  one-hot format. The `include_background` parameter can be set to ``False`` to exclude
  the first category (channel index 0) which is by convention assumed to be background. If the non-background
  segmentations are small compared to the total image size they can get overwhelmed by the signal from the
  background. `y_preds` and `y` can be a list of channel-first Tensor (CHW[D]) or a batch-first Tensor (BCHW[D]),
  `y` can also be in the format of `B1HW[D]`.
  
  Example of the typical execution steps of this metric class follows :py:class:`monai.metrics.metric.Cumulative`.
  
  Args:
      include_background: whether to include Dice computation on the first channel of
          the predicted output. Defaults to ``True``.
      reduction: define mode of reduction to the metrics, will only apply reduction on `not-nan` values,
          available reduction modes: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``,
          ``"mean_channel"``, ``"sum_channel"``}, default to ``"mean"``. if "none", will not do reduction.
      get_not_nans: whether to return the `not_nans` count, if True, aggregate() returns (metric, not_nans).
          Here `not_nans` count the number of not nans for the metric, thus its shape equals to the shape of the metric.
      ignore_empty: whether to ignore empty ground truth cases during calculation.
          If `True`, NaN value will be set for empty ground truth cases.
          If `False`, 1 will be set if the predictions of empty ground truth cases are also empty.
      num_classes: number of input channels (always including the background). When this is None,
          ``y_pred.shape[1]`` will be used. This option is useful when both ``y_pred`` and ``y`` are
          single-channel class indices and the number of classes is not automatically inferred from data.
      return_with_label: whether to return the metrics with label, only works when reduction is "mean_batch".
          If `True`, use "label_{index}" as the key corresponding to C channels; if 'include_background' is True,
          the index begins at "0", otherwise at "1". It can also take a list of label names.
          The outcome will then be returned as a dictionary.
class_docstr_tok: ["Compute","average","Dice","score","for","a","set","of","pairs","of","prediction-groundtruth","segmentations",".","It","supports","both","multi-classes","and","multi-labels","tasks",".","Input","`","y_pred","`","is","compared","with","ground","truth","`","y","`",".","`","y_pred","`","is","expected","to","have","binarized","predictions","and","`","y","`","can","be","single-channel","class","indices","or","in","the","one-hot","format",".","The","`","include_background","`","parameter","can","be","set","to","``","False","``","to","exclude","the","first","category","(","channel","index","0",")","which","is","by","convention","assumed","to","be","background",".","If","the","non-background","segmentations","are","small","compared","to","the","total","image","size","they","can","get","overwhelmed","by","the","signal","from","the","background",".","`","y_preds","`","and","`","y","`","can","be","a","list","of","channel-first","Tensor","(","CHW","[","D","]",")","or","a","batch-first","Tensor","(","BCHW","[","D","]",")",",","`","y","`","can","also","be","in","the","format","of","`","B1HW","[","D","]","`",".","Example","of","the","typical","execution","steps","of","this","metric","class","follows",":","py",":","class",":","`","monai.metrics.metric.Cumulative","`",".","Args",":","include_background",":","whether","to","include","Dice","computation","on","the","first","channel","of","the","predicted","output",".","Defaults","to","``","True","``",".","reduction",":","define","mode","of","reduction","to","the","metrics",",","will","only","apply","reduction","on","`","not-nan","`","values",",","available","reduction","modes",":","{","``","``","none","''","``",",","``","``","mean","''","``",",","``","``","sum","''","``",",","``","``","mean_batch","''","``",",","``","``","sum_batch","''","``",",","``","``","mean_channel","''","``",",","``","``","sum_channel","''","``","}",",","default","to","``","``","mean","''","``",".","if","``","none","''",",","will","not","do","reduction",".","get_not_nans",":","whether","to","return","the","`","not_nans","`","count",",","if","True",",","aggregate","(",")","returns","(","metric",",","not_nans",")",".","Here","`","not_nans","`","count","the","number","of","not","nans","for","the","metric",",","thus","its","shape","equals","to","the","shape","of","the","metric",".","ignore_empty",":","whether","to","ignore","empty","ground","truth","cases","during","calculation",".","If","`","True","`",",","NaN","value","will","be","set","for","empty","ground","truth","cases",".","If","`","False","`",",","1","will","be","set","if","the","predictions","of","empty","ground","truth","cases","are","also","empty",".","num_classes",":","number","of","input","channels","(","always","including","the","background",")",".","When","this","is","None",",","``","y_pred.shape","[","1","]","``","will","be","used",".","This","option","is","useful","when","both","``","y_pred","``","and","``","y","``","are","single-channel","class","indices","and","the","number","of","classes","is","not","automatically","inferred","from","data",".","return_with_label",":","whether","to","return","the","metrics","with","label",",","only","works","when","reduction","is","``","mean_batch","''",".","If","`","True","`",",","use","``","label_","{","index","}","''","as","the","key","corresponding","to","C","channels",";","if","'include_background","'","is","True",",","the","index","begins","at","``","0","''",",","otherwise","at","``","1","''",".","It","can","also","take","a","list","of","label","names",".","The","outcome","will","then","be","returned","as","a","dictionary","."]
docstr: Execute reduction and aggregation logic for the output of `compute_dice`.
  
  Args:
      reduction: define mode of reduction to the metrics, will only apply reduction on `not-nan` values,
          available reduction modes: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``,
          ``"mean_channel"``, ``"sum_channel"``}, default to `self.reduction`. if "none", will not do reduction.
docstr_tok: ["Execute","reduction","and","aggregation","logic","for","the","output","of","`","compute_dice","`",".","Args",":","reduction",":","define","mode","of","reduction","to","the","metrics",",","will","only","apply","reduction","on","`","not-nan","`","values",",","available","reduction","modes",":","{","``","``","none","''","``",",","``","``","mean","''","``",",","``","``","sum","''","``",",","``","``","mean_batch","''","``",",","``","``","sum_batch","''","``",",","``","``","mean_channel","''","``",",","``","``","sum_channel","''","``","}",",","default","to","`","self.reduction","`",".","if","``","none","''",",","will","not","do","reduction","."]
code: def aggregate(
      self, reduction: MetricReduction | str | None = None
  ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]:
      """
      Execute reduction and aggregation logic for the output of `compute_dice`.
  
      Args:
          reduction: define mode of reduction to the metrics, will only apply reduction on `not-nan` values,
              available reduction modes: {``"none"``, ``"mean"``, ``"sum"``, ``"mean_batch"``, ``"sum_batch"``,
              ``"mean_channel"``, ``"sum_channel"``}, default to `self.reduction`. if "none", will not do reduction.
  
      """
      data = self.get_buffer()
      if not isinstance(data, torch.Tensor):
          raise ValueError(
              f"the data to aggregate must be PyTorch Tensor, got {type(data)}."
          )
  
      # do metric reduction
      f, not_nans = do_metric_reduction(
          data, reduction or self.reduction
      )
      if (
          self.reduction == MetricReduction.MEAN_BATCH
          and self.return_with_label
      ):
          _f = {}
          if isinstance(self.return_with_label, bool):
              for i, v in enumerate(f):
                  _label_key = (
                      f"label_{i+1}"
                      if not self.include_background
                      else f"label_{i}"
                  )
                  _f[_label_key] = round(v.item(), 4)
          else:
              for key, v in zip(self.return_with_label, f):
                  _f[key] = round(v.item(), 4)
          f = _f
      return (f, not_nans) if self.get_not_nans else f
  
file_path: MONAI/monai/metrics/meandice.py
imports: from __future__ import annotations
  import torch
  from monai.metrics.utils import do_metric_reduction
  from monai.utils import MetricReduction
  from .metric import CumulativeIterationMetric
label_desc: Use image node_id 3 for calling the DiceMetric obj's underlying member method code with example usage: obj.aggregate(reduction) and returns: unknown