class_docstr: Test class for testing groups computation.
class_docstr_tok: ["Test","class","for","testing","groups","computation","."]
docstr: Check states are copied instead of passed by ref when a single metric in the collection is access.
docstr_tok: ["Check","states","are","copied","instead","of","passed","by","ref","when","a","single","metric","in","the","collection","is","access","."]
code: def test_check_compute_groups_items_and_values(
      self, metrics, expected, preds, target, method
  ):
      """Check states are copied instead of passed by ref when a single metric in the collection is access."""
      m = MetricCollection(deepcopy(metrics), compute_groups=True)
      m2 = MetricCollection(deepcopy(metrics), compute_groups=False)
  
      for _ in range(2):  # repeat to emulate effect of multiple epochs
          for _ in range(
              2
          ):  # repeat to emulate effect of multiple batches
              m.update(preds, target)
              m2.update(preds, target)
  
          def _compare(m1, m2):
              for state in m1._defaults:
                  assert _allclose_recursive(
                      getattr(m1, state), getattr(m2, state)
                  )
              # if states are still by reference the reset will make following metrics fail
              m1.reset()
              m2.reset()
  
          if method == "items":
              for (name_cg, metric_cg), (
                  name_no_cg,
                  metric_no_cg,
              ) in zip(m.items(), m2.items()):
                  assert name_cg == name_no_cg
                  _compare(metric_cg, metric_no_cg)
          if method == "values":
              for metric_cg, metric_no_cg in zip(
                  m.values(), m2.values()
              ):
                  _compare(metric_cg, metric_no_cg)
          if method == "keys":
              for key in m:
                  metric_cg, metric_no_cg = m[key], m2[key]
                  _compare(metric_cg, metric_no_cg)
  
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 2 for calling the TestComputeGroups obj's underlying member method code with example usage: obj.test_check_compute_groups_items_and_values(metrics, expected, preds, target, method) without return types