code: def compute_variance(
      y_pred: torch.Tensor,
      include_background: bool = True,
      spatial_map: bool = False,
      scalar_reduction: str = "mean",
      threshold: float = 0.0005,
  ) -> torch.Tensor | None:
      """
      Args:
          y_pred: [N, C, H, W, D] or [N, C, H, W] or [N, C, H] where N is repeats, C is channels and H, W, D stand for
              Height, Width & Depth
          include_background: Whether to include the background of the spatial image or channel 0 of the 1-D vector
          spatial_map: Boolean, if set to True, spatial map of variance will be returned corresponding to i/p image
              dimensions
          scalar_reduction: reduction type of the metric, either 'sum' or 'mean' can be used
          threshold: To avoid NaN's a threshold is used to replace zero's
      Returns:
          A single scalar uncertainty/variance value or the spatial map of uncertainty/variance
      """
  
      # The background utils is only applicable here because instead of Batch-dimension we have repeats here
      y_pred = y_pred.float()
  
      if not include_background:
          y = y_pred
          # TODO If this utils is made to be optional for 'y' it would be nice
          y_pred, y = ignore_background(y_pred=y_pred, y=y)
  
      # Set any values below 0 to threshold
      y_pred[y_pred <= 0] = threshold
  
      n_len = len(y_pred.shape)
  
      if n_len < 4 and spatial_map:
          warnings.warn(
              "Spatial map requires a 2D/3D image with N-repeats and C-channels"
          )
          return None
  
      # Create new shape list
      # The N-repeats are multiplied by channels
      n_shape = y_pred.shape
      new_shape = [n_shape[0] * n_shape[1]]
      for each_dim_idx in range(2, n_len):
          new_shape.append(n_shape[each_dim_idx])
  
      y_reshaped = torch.reshape(y_pred, new_shape)
      variance = torch.var(y_reshaped, dim=0, unbiased=False)
  
      if spatial_map:
          return variance
  
      if scalar_reduction == MetricReduction.MEAN:
          return torch.mean(variance)
      if scalar_reduction == MetricReduction.SUM:
          return torch.sum(variance)
      raise ValueError(
          f"scalar_reduction={scalar_reduction} not supported."
      )
  
file_path: MONAI/monai/metrics/active_learning_metrics.py
imports: from __future__ import annotations
  import warnings
  from typing import Any
  import torch
  from monai.metrics.utils import ignore_background
  from monai.utils import MetricReduction
  from .metric import Metric
label_desc: Use image node_id 1 for calling a global function with example usage: compute_variance(y_pred, include_background, spatial_map, scalar_reduction, threshold) and returns: None, variance, torch, torch