class_docstr: Test class for testing groups computation.
class_docstr_tok: ["Test","class","for","testing","groups","computation","."]
docstr: Check that compute groups are formed after initialization and that metrics are correctly computed.
docstr_tok: ["Check","that","compute","groups","are","formed","after","initialization","and","that","metrics","are","correctly","computed","."]
code: def test_check_compute_groups_correctness(
      self,
      metrics,
      expected,
      preds,
      target,
      prefix,
      postfix,
      with_reset,
  ):
      """Check that compute groups are formed after initialization and that metrics are correctly computed."""
      if isinstance(metrics, MetricCollection):
          prefix, postfix = None, None  # disable for nested collections
      m = MetricCollection(
          deepcopy(metrics),
          prefix=prefix,
          postfix=postfix,
          compute_groups=True,
      )
      # Construct without for comparison
      m2 = MetricCollection(
          deepcopy(metrics),
          prefix=prefix,
          postfix=postfix,
          compute_groups=False,
      )
  
      assert len(m.compute_groups) == len(m)
      assert m2.compute_groups == {}
  
      for _ in range(2):  # repeat to emulate effect of multiple epochs
          m.update(preds, target)
          m2.update(preds, target)
  
          for member in m.values():
              assert member.update_called
  
          assert m.compute_groups == expected
          assert m2.compute_groups == {}
  
          # compute groups should kick in here
          m.update(preds, target)
          m2.update(preds, target)
  
          for member in m.values():
              assert member.update_called
  
          # compare results for correctness
          res_cg = m.compute()
          res_without_cg = m2.compute()
          for key in res_cg:
              assert torch.allclose(res_cg[key], res_without_cg[key])
  
          if with_reset:
              m.reset()
              m2.reset()
  
imports: import pickle
  from copy import deepcopy
  from typing import Any
  import pytest
  import torch
  from torchmetrics import Metric, MetricCollection
  from torchmetrics.classification import BinaryAccuracy, MulticlassAccuracy, MulticlassAUROC, MulticlassAveragePrecision, MulticlassCohenKappa, MulticlassConfusionMatrix, MulticlassF1Score, MulticlassMatthewsCorrCoef, MulticlassPrecision, MulticlassRecall, MultilabelAUROC, MultilabelAveragePrecision
  from torchmetrics.utilities.checks import _allclose_recursive
  from unittests.helpers import seed_all
  from unittests.helpers.testers import DummyMetricDiff, DummyMetricMultiOutputDict, DummyMetricSum
label_desc: Use image node_id 1 for calling the TestComputeGroups obj's underlying member method code with example usage: obj.test_check_compute_groups_correctness(metrics, expected, preds, target, prefix, postfix, with_reset) without return types