code: def test_shape_reduction(self):
      set_determinism(seed=123)
      device = "cuda" if torch.cuda.is_available() else "cpu"
  
      # regression metrics to check
      metrics = [
          MSEMetric,
          MAEMetric,
          RMSEMetric,
          partial(PSNRMetric, max_val=1.0),
      ]
  
      # define variations in batch/base_dims/spatial_dims
      batch_dims = [1, 2, 4, 16]
      base_dims = [16, 32, 64]
      spatial_dims = [2, 3, 4]
  
      # iterate over all variations and check shapes for different reduction functions
      for batch in batch_dims:
          for spatial in spatial_dims:
              for base in base_dims:
                  # create random tensors
                  in_tensor = torch.rand(
                      (batch,) + (base,) * (spatial - 1)
                  ).to(device)
  
                  # iterate over regression metrics, check shape for diff. reduction func
                  for mt_fn in metrics:
                      mt = mt_fn(reduction="mean")
                      mt(in_tensor, in_tensor)
                      out_tensor = mt.aggregate()
                      self.assertTrue(len(out_tensor.shape) == 1)
  
                      mt = mt_fn(reduction="sum")
                      mt(in_tensor, in_tensor)
                      out_tensor = mt.aggregate()
                      self.assertTrue(len(out_tensor.shape) == 0)
  
                      mt = mt_fn(
                          reduction="sum"
                      )  # test reduction arg overriding
                      mt(in_tensor, in_tensor)
                      out_tensor = mt.aggregate(
                          reduction="mean_channel"
                      )
                      self.assertTrue(
                          len(out_tensor.shape) == 1
                          and out_tensor.shape[0] == batch
                      )
  
                      mt = mt_fn(reduction="sum_channel")
                      mt(in_tensor, in_tensor)
                      out_tensor = mt.aggregate()
                      self.assertTrue(
                          len(out_tensor.shape) == 1
                          and out_tensor.shape[0] == batch
                      )
  
file_path: MONAI/tests/test_compute_regression_metrics.py
imports: from __future__ import annotations
  import unittest
  from functools import partial
  import numpy
  import torch
  from monai.metrics import MAEMetric, MSEMetric, PSNRMetric, RMSEMetric
  from monai.utils import set_determinism
label_desc: Use image node_id 1 for calling the TestRegressionMetrics obj's underlying member method code with example usage: obj.test_shape_reduction() without return types